Every Record Deleted But one from a range...

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I the following block of code which is supposed to delete every cell in the range if a value in the row is blank or 0.

VBA Code:
With FinalRange
       For Each r In FinalRange
            If r = "0" Or r = "" Then
                r.EntireRow.Delete
            End If
       Next
End With

It seems to be working properly - but for some reason there is one row in the data set that doesn't get deleted as expected. The first cell in the row has a value but the following 3 columns do not so it should be deleted.

Every other row in the dataset works fine. I can't figure out a possible reason why it would miss or skip that row??

I've checked and the cells are either empty or contain 0...

Thoughts?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What is happening is that when you delete row 3 (for example) row 4 moves up 1 to become row 3 & therefore never gets checked.
Try
VBA Code:
   Dim Rng As Range
   With FinalRange
      For Each r In FinalRange
         If r = "0" Or r = "" Then
            If Rng Is Nothing Then Set Rng = r Else Set Rng = Union(Rng, r)
         End If
      Next
   End With
   If Not Rng Is nothing Then Rng.EntireRow.Delete
EDIT:
Corrected typo pointed out by Mark.
 
Last edited:
Upvote 0
Oops ?
Thanks for spotting that, I've now corrected it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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