Thread: Insert rows
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick[_2_] Bernie Deitrick[_2_] is offline
external usenet poster
 
Posts: 176
Default Insert rows

In case anyone is interested in the performance differences, with
20,000 rows, 365 unique values in the date column, and 10 total
columns of data, my routine took 1.43 secs, and Paul's took 72.94
secs - about 50 times longer.

HTH,
Bernie
MS Excel MVP

"Bernie Deitrick" wrote in message
...
Dear Lost,

The code below will insert two blank rows at every change in value

in
column B. To use a different column, change the myCol = 2 to an
appropriate number.

This also assumes you have headers in Row 1.

This code uses the fastest way to insert rows: sorting using Excel's
built-in sorting routines. Other code is possible that would step
cell-wise through your range of data, but inserting mulitple pairs

of
rows can be quite slow on large files.

HTH,
Bernie
MS Excel MVP

Sub Insert2BlankColumns()
Dim myCell1 As Range
Dim myCell2 As Range
Dim myCol As Integer
Dim myRow As Long

'Change these two variables as needed
myCol = 2 'For column B
myRow = 1 'Row with labels

Set myCell1 = Cells(Rows.Count, myCol).End(xlUp)
Range(Cells(myRow, myCol), myCell1).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=myCell1(2), _
Unique:=True
myCell1(2).EntireRow.Delete
Set myCell2 = Cells(Rows.Count, myCol).End(xlUp)
Range(myCell1(2), myCell2).Copy myCell2(2)

Cells(myRow, myCol).CurrentRegion.Sort _
Key1:=Cells(myRow, myCol), _
Order1:=xlAscending, _
Header:=xlYes

For Each myCell1 In Range(Cells(myRow, myCol), _
Cells(Rows.Count, myCol).End(xlUp))
If myCell1(1, 0).Value = "" Then myCell1.ClearContents
Next myCell1
End Sub
"Lost" wrote in message
...
I hope someone can help me with this confusing thing.

I have a large spreadsheet (20,000 rows +) and I have a
date in column R. The sheet has been sorted on column R.

I want the most efficient code I can find to insert two
rows everytime the a different date is found in column R.

Please supply your example code...and thanks millions...