VBA - Delete Rows in named range if value is False

tttommy2

Board Regular
Joined
Oct 1, 2012
Messages
55
Hi there

I have a named range ("RRR") which consists of 1 column and each cell either is TRUE or FALSE. I want to delete the rows which have FALSE.

I tried the code below, however it doesn't delete all the rows with FALSE.

However a curious thing is that if I run the code several times (in Excel 2010) it will finally delete all the FALSE rows!

I'd like my code to work in 1 swoop. Any ideas please?

Code:
Sheets(1).Range("$E$4:$E1245").Name = "RRR"

Dim Jcell As Range
    
For Each Jcell In Range("RRR")
If Jcell.Value = False Then Jcell.EntireRow.Delete
Next
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
The issue is that as you move down a column and delete rows, it shifts the rows up. So if you have 2 FALSE entries in a row, it will miss the second. So you need to loop through your range backwards to avoid those two things form passing each other in reverse direction.

Here is some code that will dynamically find the last row with data in column E (to avoid unnecessary checks):
Code:
Sub MyDeleteRows()

    Dim lastRow As Long
    Dim myRow As Long

    Application.ScreenUpdating = False

'   Find last row in column E
    lastRow = Sheets(1).Cells(Rows.Count, "E").End(xlUp).Row

'   Loop through all rows in column E backwards deleting FALSE rows
    For myRow = lastRow To 4 Step -1
        If Sheets(1).Cells(myRow, "E") = False Then Sheets(1).Rows(myRow).Delete
    Next

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thank you Joe4. Your code works perfectly for me and good explanation of why mine don't.

The issue is that as you move down a column and delete rows, it shifts the rows up. So if you have 2 FALSE entries in a row, it will miss the second. So you need to loop through your range backwards to avoid those two things form passing each other in reverse direction.

Here is some code that will dynamically find the last row with data in column E (to avoid unnecessary checks):
Code:
Sub MyDeleteRows()

    Dim lastRow As Long
    Dim myRow As Long

    Application.ScreenUpdating = False

'   Find last row in column E
    lastRow = Sheets(1).Cells(Rows.Count, "E").End(xlUp).Row

'   Loop through all rows in column E backwards deleting FALSE rows
    For myRow = lastRow To 4 Step -1
        If Sheets(1).Cells(myRow, "E") = False Then Sheets(1).Rows(myRow).Delete
    Next

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

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