VBA: Removing rows from range based on criteria

wigarth

Board Regular
Joined
Apr 16, 2016
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have a sheet with data.
I want to remove the entire row if range B137 to b23368 is "0"

I have the following code (se below)
The code works fine, but if I have two 0's in a row, it will skip the 2nd one for some reason. (I guess thats becuase it moves the row one line up after deleting it)
I tried to just run the macro several times, wich also works, but its time consuming and really annoying.
I need to point out that all data above B137 can not be modified by this macro.

Any solutions here?
Best Reggards:
Wigarth

Code:
For Each cell In Range("b137:b233")
  If Not IsEmpty(cell) Then
      If cell.Value = 0 Then
      cell.EntireRow.Delete
      End If
  End If
Next
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try
Code:
   Dim i As Long
   For i = 233 To 137 Step -1
      If Not IsEmpty(Range("B" & i)) And Range("B" & i).Value = 0 Then Rows(i).Delete
   Next i
 
Upvote 0
And there you go posting 4 lines of code rather than me running my own macro 17 times in loop...

VBA never seems to stop to amaze me.

Thank you so much for the help Fluff! Worked like a dream!
 
Upvote 0
Glad to help & thanks for the feedback.

When deleting/inserting rows it's always better working from the bottom up, for the reasons you have discovered.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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