1

I'm pretty sure I'm nearly there. At the moment this deletes all rows. I just want it to delete the rows identified as having the word FALSE in them.

Dim R As Range
Dim myRange As Range

LastRow = Range("A" & Rows.Count).End(xlUp).Row

Set myRange = Range("U2:U" & LastRow)

For Each R In myRange
    If R.Value = "FALSE" Then
        Rows.Delete
    End If
Next

I've also tried using the below code but it did not help.

Dim R As Range
Dim myRange As Range

LastRow = Range("A" & Rows.Count).End(xlUp).Row

Set myRange = Range("U2:U" & LastRow)

For Each R In myRange
    If R = "False" Then
        R.EntireRow.Delete
    End If
Next```

Here's a screenshot of how the value is stored. It's was a field that was stripped from a Vlookup and converted to a value. enter image description here

Campbell
  • 67
  • 6

3 Answers3

1

Working with False can be tricky so I created an example for you as shown below.

enter image description here

As I recomendded in the comments, do not use a loop. Use Autofilter as mentioned in Delete row based on partial text

Try this

Code:

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim SearchValue As Boolean
    Dim lRow As Long

    SearchValue = False
    
    '~~> Change this to the relevant sheet
    Set ws = Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Remove any filters
        .AutoFilterMode = False

        '~~> Filter, offset(to exclude headers) and delete visible rows
        With .Range("U1:U" & lRow)
          .AutoFilter Field:=1, Criteria1:=SearchValue
          .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        '~~> Remove any filters
        .AutoFilterMode = False
    End With
End Sub

Final result:

enter image description here

Note:

If this works for you, I will close this question as a duplicate but retain this answer as it shows how to deal with Boolean and Boolean (Stored as string) values.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
0

Try This:

Sub DeleteRow()

    Dim LastRow As Long, cell As Range
    LastRow = ThisWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    For Each cell In ThisWorkbook.ActiveSheet.Range("U2:U" & LastRow)
        If cell.Value = "False" Then
        cell.EntireRow.Delete
        End If
    Next

End Sub

Note: ThisWorkbook.ActiveSheet is not necessary it is better to give proper reference.

Amit
  • 36
  • 4
0

Have a try on below sub.

Sub RemoveWithFalse()
Dim rng As Range, rws As Long, i As Long
Dim LastRow As Long

  LastRow = Cells(Rows.Count, "U").End(xlUp).Row
  Set rng = ActiveSheet.Range("U1:U" & LastRow)
  rws = rng.Rows.Count
  
  For i = rws To 1 Step (-1)
    If rng.Rows(i) = False Then rng.Rows(i).EntireRow.Delete
  Next
  
  Set rng = Nothing
  
End Sub

If Column U is formatted as Text then use below line

If rng.Rows(i) = "FALSE" Then rng.Rows(i).EntireRow.Delete
Harun24hr
  • 30,391
  • 4
  • 21
  • 36