Big sheet, need a way to delete fast

Spurious

Active Member
Joined
Dec 14, 2010
Messages
439
Hello,

the following:

I got a sheet with ~15000 rows, I sort them first and then I go from the last to the first and delete if the input in column 5 is the same as in the row above, additionally I add the values in column 12.

Now, this takes forever. I takes me roughly 2.5 minutes to do it for 250 rows.

Code:
Do While i > i
    If Sheet.Cells(i, Clm) = Sheet.Cells(i - 1, Clm) Then
        Sheet.Cells(i - 1, 12) = Sheet.Cells(i - 1, 12) + Sheet.Cells(i, 12)
        Sheet.Range(Cells(i, 1), Cells(i, 25)).Delete xlShiftUp
    End If
    
    i = i - 1
Loop

Is there a faster way to do this?

Thanks in advance!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
When I tried your code with 250 rows it took about 2 seconds. Maybe you need to set calculation to manual. By the way, this:

Rich (BB code):
Do While i > i

should be:

Rich (BB code):
Do While i > 1
 
Upvote 0
Since i will never be greater than itself, I do not see how this loop is executing at all??? Can you post your actual code? Also, explain what is going on in Column 12.

Sorry, simple typo in the code, I currently do it for i > 15000, so I had to manually change the code in the OP.

I got articles in column 5 and the amount sold in column 12.

When I tried your code with 250 rows it took about 2 seconds. Maybe you need to set calculation to manual.

I had them at automatic and it took forever. At manual it takes 2 seconds.

Thanks for this. What's the difference between the two and which advantage has automatic compared to manual?

It's for my boss and he is not familiar with VBA, so if it changes his excel experience, I cant really change it.
 
Upvote 0
If calculation is automatic the workbook may be recalculated each time your code changes a cell or deletes a row. At the start of your procedure assign the calculation state to a variable and set calculation to manual. At the end of your procedure reset calculation to its previous state. That way you haven't permanently changed any of the user's settings.
 
Upvote 0
How do I do this?

And, I think my code is pretty much useless if I cant increase the speed. I currently need roughly 7 minutes for going through the data and do it what I wanted it to do. My boss needs 2 minutes if he creates a pivot table, sorts it and calculates it by hand.

My code would be faster if I had a smaller amount of rows, because he needs 2 minutes no matter what.

Is there a way to create a pivot, sort it, take out values in the data column and go from there?

Sorry for this mess.
 
Upvote 0
Example:

Code:
Dim CalcState
CalcState = Application.Calculation
Application.Calculation = xlManual
'Your code
Application.Calculation = CalcState
 
Upvote 0
I got articles in column 5 and the amount sold in column 12.
I'm still unclear about Column 12... if it has values in it, what are you adding and putting in each row during the deletion process? Anyway, the following code will perform your deletions and I think it will do it quite quickly...

Code:
Sub RemoveDupes()
  Dim UnusedColumn As Long, LastRow As Long
  UnusedColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column + 1
  LastRow = Cells(Rows.Count, "E").End(xlUp).Row
  On Error Resume Next
  Application.ScreenUpdating = False
  With Cells(2, UnusedColumn).Resize(LastRow - 1)
    .FormulaR1C1 = "=IF(RC5=R[-1]C5,""x"","""")"
    .Value = .Value
    .SpecialCells(xlCellTypeConstants).EntireRow.Delete
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Example:

Code:
Dim CalcState
CalcState = Application.Calculation
Application.Calculation = xlManual
'Your code
Application.Calculation = CalcState

thanks

I'm still unclear about Column 12... if it has values in it, what are you adding and putting in each row during the deletion process? Anyway, the following code will perform your deletions and I think it will do it quite quickly...

Code:
Sub RemoveDupes()
  Dim UnusedColumn As Long, LastRow As Long
  UnusedColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column + 1
  LastRow = Cells(Rows.Count, "E").End(xlUp).Row
  On Error Resume Next
  Application.ScreenUpdating = False
  With Cells(2, UnusedColumn).Resize(LastRow - 1)
    .FormulaR1C1 = "=IF(RC5=R[-1]C5,""x"","""")"
    .Value = .Value
    .SpecialCells(xlCellTypeConstants).EntireRow.Delete
  End With
  Application.ScreenUpdating = True
End Sub

Not sure if this code does what I need it to do. I dont really understand it, but it seems like it deletes the entire row if there is a duplicate, but doesnt add the values in column 12.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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