Ctrl+select in VBA

01's

Board Regular
Joined
Jun 1, 2011
Messages
85
Hi,

I need to select all cells in a range which have 0 or null value and then delete entirerow.
I tried below code but it deletes some rows and eliminates others. Can you pls suggest something.

For Each c In ActiveSheet.Range("E2:E" & Lastrow).Cells
If c.Value = 0 Or c.Value = Null Then
Selection.EntireRow.Delete
c.Select
End If
Next
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
Sub DeleteAllRows()
    For i = lastRow To 2 Step -1
       If Cells(i, "E") = 0 Or IsEmpty(Cells(i, "E")) Then Cells(i, 1).EntireRow.Delete
    Next
End Sub

P.S. When deleting rows, ALWAYS go from down to top. Otherwise you'll loose row/iterator correspondence.
 
Upvote 0
Hi

Code:
Dim i As Long
For i = LastRow to 2 Step -1
  If Range("E" & i).Value = 0 Or IsEmpty(Range("E" & i).Value) Then
      Range("E" & i).EntireRow.Delete
  End If
Next i
 
Upvote 0
You need to loop backwards when deleting rows. Try

Code:
For i = lastrow To 2 Step -1
    If Range("E" & i).Value = 0 Or Range("E" & i).Value = "" Then Rows(i).Delete
Next i
 
Upvote 0
Hi,

You need to loop backwards. Try something like

Code:
Option Explicit
Dim lRow As Long, i As Long
Sub test()
    lRow = Range("E" & Rows.Count).End(xlUp).Row
    For i = lRow To 2 Step -1
        If Cells(i, 5).Value = 0 Or Cells(i, 5).Value = Null Then Cells(i, 5).EntireRow.Delete
    Next i
End Sub

The reason for looping backwards is becasue if you are going from E2 to E100 then when you delete, say, E10 the next cell is going to be E11, when it is actually E10 again, so it'll skip a row. If that makes sense?
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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