Increase looping speed

davahill

New Member
Joined
Jan 16, 2014
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi,

Is there a faster way to perform the following task instead of looping. I've got anywhere from 10,000 to 50,000 unique records.

HTML:
Sub InsertBlankAtChange()
Dim LastRow As Long
Dim i As Long
LastRow = Cells(Cells.Rows.Count, "G").End(xlUp).Row
For i = LastRow To 2 Step -1
If Cells(i, "G").Value <> Cells(i - 1, "G").Value Then Rows(i).Insert
Next i
End Sub

Thank you in adbvence.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I think that's pretty much as good as you can get for the code.

However, you can improve speed by turning off Calculations / Events / ScreenUpdating

Code:
Dim PrevCalc As Variant

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    PrevCalc = .Calculation
    .Calculation = xlCalculationManual
End With

'rest of your code here

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = PrevCalc
End With
 
Upvote 0
Hi,

Is there a faster way to perform the following task instead of looping. I've got anywhere from 10,000 to 50,000 unique records.

...

Thank you in adbvence.
I believe there's no non-looping approach that does it faster.

But you can use a much faster loop, albeit somewhat more complex code.
Code:
Sub blank_if_change()

Dim r As Long, c As Long, a, u(), i As Long, k as Long
r = Cells.Find("*", , , , xlByRows, xlPrevious).Row
c = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
ReDim u(1 To r, 1 To 1)
a = Cells(1, "g").Resize(r + 1)
Application.ScreenUpdating = False
Cells(c + 1) = 1: Cells(c + 1).Resize(r).DataSeries

For i = 1 To r
    If a(i, 1) <> a(i + 1, 1) Then k = k + 1: u(k, 1) = i
Next i

Cells(r + 1, c + 1).Resize(k) = u
Cells(1).Resize(r + k, c + 1).Sort Cells(c + 1), Header:=xlNo
Cells(c + 1).Resize(r + k).ClearContents
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Thank you both. They are both an improvement from my original code.
 
Upvote 0
mirabeau, would it be possible to increase the size (height) of the blank row by a factor of two or twice as tall as it would normally be?

Thank you in advance for your response.
 
Upvote 0
mirabeau, would it be possible to increase the size (height) of the blank row by a factor of two or twice as tall as it would normally be?

Thank you in advance for your response.
taking "as tall as it would normally be" as the height of the first row on your worksheet, then add the three red lines to the code
Rich (BB code):
Sub blank_if_change()

Dim r As Long, c As Long, a, u(), i As Long, k As Long
r = Cells.Find("*", , , , xlByRows, xlPrevious).Row
c = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
ReDim u(1 To r, 1 To 1)
a = Cells(1, "g").Resize(r + 1)
Application.ScreenUpdating = False
Cells(c + 1) = 1: Cells(c + 1).Resize(r).DataSeries

For i = 1 To r
    If a(i, 1) <> a(i + 1, 1) Then k = k + 1: u(k, 1) = i
Next i

Cells(r + 1, c + 1).Resize(k) = u
Cells(1).Resize(r + k, c + 1).Sort Cells(c + 1), Header:=xlNo
Cells(c + 1).Resize(r + k).ClearContents

For i = 1 To k - 1
    Rows(u(i, 1) + i).RowHeight = 2 * Rows(1).RowHeight
Next i

Application.ScreenUpdating = True

End Sub
If you mean twice the Excel default row height, in my 2007 Excel this is 15. So you could replace the second red line by ... = 30
 
Upvote 0
Just throwing an idea out there which unfortunately I don't currently have the time to explore but maybe one of the other guys on here can. How about you loop the values and build an array for the range or rows that need the insert then do all the inserts in one go, this would save a massive amount of time I believe.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top