VBA delete cells based on criteria

Jammydan

Board Regular
Joined
Feb 15, 2010
Messages
141
Hi

I have the following code, however it seems to stop after removing 1 row, it doesn't check the rest of the range. Can anybody help please?

Many thanks

Code:
[SIZE=2]
Private Sub Worksheet_Activate()
Dim RemoveOld As Range
Set RemoveOld = Range("C5:C41")
Application.ScreenUpdating = False
ActiveSheet.Unprotect

'**** DELETE OLD CONTRACTS *****
For Each Cell In RemoveOld
If Cell.Value > 0 And Cell.Offset(0, 10) >= 0 Then
Cell.EntireRow.Delete Shift:=xlUp
Application.Run "InsertRowsAndFillFormulas_caller"
End If
Next Cell
ActiveSheet.Protect
Application.ScreenUpdating = True
Range("E3").End(xlDown).Offset(1, 0).Select
End Sub
[/SIZE]
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I think because of the nature of how you're using your FOR LOOP. Try changing it to:
Rich (BB code):
Private Sub Worksheet_Activate()
Dim RemoveOld As Range
Set RemoveOld = Range("C5:C41")
Dim i as Long
Application.ScreenUpdating = False
ActiveSheet.Unprotect
 
'**** DELETE OLD CONTRACTS *****
For i = 5 to 41 
 If cells(i, 3) > 0 And cells(i, 13) >= 0 Then
  Rows(i).EntireRow.Delete Shift:=xlUp
   Application.Run "InsertRowsAndFillFormulas_caller"
  i = i + 1
  End If
Next Cell
ActiveSheet.Protect
Application.ScreenUpdating = True
Range("E3").End(xlDown).Offset(1, 0).Select
End Sub
Basically, I think you're deleting a row and then running something called InsertRowsAndFillFormulas_caller (a macro?!) which is then placing something into the next position of i so the IF criteria fails and no more rows are deleted. Something like that anyway.

I'd use F8 to step through your code and turn on the locals watch to how the value of i changes which will tell you what row the macro is acting on.

Bits in red are my changes
 
Upvote 0
Thanks, still doing the same thing but I think the problem is with the macro i call to replace the deleted lines, thanks though
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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