Adding comands to a current Macro

mlbarry

New Member
Joined
Sep 26, 2012
Messages
47
Can I just add "Application.Calculation = xlManual" to the beginning and "Application.Calculation = xlAutomatic" to the end of my macro, below. It is taking for ever to run it because it's getting very large. Thank you.


Sub ProcessCash()
lR = Cells(Rows.Count, "A").End(xlUp).Row
For R = lR To 2 Step -1
If Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 11) = Cells(R - 1, 18) Then
Rows(R).Delete
Rows(R - 1).Delete
ElseIf Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 10) + Cells(R, 11) = Cells(R - 1, 18) Then
Rows(R).Delete
Rows(R - 1).Delete
End If
Next R
End Sub
 
This (Rng.EntireRow.Interior.Color = 1234) did highlight the rows that should have been deleted. I'm a macro novic so I don't understand what this accomplished. Is there a way to remove them automatically?
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
That test was just to check that it was correctly finding the correct rows to delete.
Do you have any merged cells?
 
Upvote 0
Do you have any merged cells?
...or protected cells, like I asked back in post 9?
 
Last edited:
Upvote 0
I did answer if I had any thing protected in post #6 , no to protected and to merged.

I did notice that the very last row was highlighted, it was not in the table, other than that it worked but didn't delete the rows nor did it turn back on the calculating function.
 
Upvote 0
That's why it doesn't work. Try
Code:
Sub ProcessCash()
   Dim LR As Long
   Dim R As Long
   Dim Rng As Range
   
   With Application
      .ScreenUpdating = False
      .Calculation = xlCalculationManual
   End With
   
   LR = Cells(Rows.Count, "A").End(xlUp).Row
   For R = LR To 2 Step -1
      If Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 11) = Cells(R - 1, 18) Then
         If Rng Is Nothing Then
            Set Rng = Range("A" & R - 1).Resize(2)
         Else
            Set Rng = Union(Rng, Range("A" & R - 1).Resize(2))
         End If
      ElseIf Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 10) + Cells(R, 11) = Cells(R - 1, 18) Then
         If Rng Is Nothing Then
            Set Rng = Range("A" & R - 1).Resize(2)
         Else
            Set Rng = Union(Rng, Range("A" & R - 1).Resize(2))
         End If
      End If
   Next R
   Rng.EntireRow.Delete
   
   With Application
      .ScreenUpdating = True
      .Calculation = xlCalculationAutomatic
   End With

End Sub
 
Upvote 0
This one gave me the same error message: Run-Time error '1004': - Delete method of Range class failed - Points to: "Rng.EntireRow.Delete" in the macro.

I just combined mine and yours, see below. This did what I wanted, it turned off calculating when started and back on at the end. Thank you all for your help.

If you have a fix I'll try it, if you don't then I'm okay with what I have.

Sub ProcessCash()
Dim LR As Long
Dim R As Long
Dim Rng As Range

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

LR = Cells(Rows.Count, "A").End(xlUp).Row
For R = LR To 2 Step -1
If Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 11) = Cells(R - 1, 18) Then
Rows(R).Delete
Rows(R - 1).Delete
ElseIf Cells(R, 4) = Cells(R - 1, 4) And Cells(R, 10) + Cells(R, 11) = Cells(R - 1, 18) Then
Rows(R).Delete
Rows(R - 1).Delete
End If
Next R

With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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