Not detecting all arrays

Mux99

Board Regular
Joined
Apr 15, 2019
Messages
57
I'm using the code below to delete all rows that don't contain a list of words. For some reason it is deleting the cell that contains the word Apple and keeps the other three with Banana, Orange & Apple Tree. Hopefully someone can help me out.

Also is it possible to search for Apple and automatically detect all cells containing that word? Ex. Apple, Apple Tree etc...

Sub DeleteRows() Dim FirstRow As Long
Dim LastRow As Long
Dim Lrow As Long
Dim Testt As Variant
Testt = Array("Banana", "Apple", "Orange", "Apple Tree")
With ActiveSheet
.Select
FirstRow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = LastRow To FirstRow Step -1
With .Cells(Lrow, "D")
If Not UBound(filter(Testt, .Value, True, vbTextCompare)) = 0 Then .EntireRow.Delete
End With
Next Lrow
End With
End Sub
 
Unfortunately, as you have found, that technique doesn't work if the cells contain "words" that start with a number.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this instead
Code:
Sub DeleteRows()
   Dim FirstRow As Long
   Dim LastRow As Long
   Dim i As Long
   Dim Vr As Variant
   Dim Rng As Range
   Vr = Array("First Floor", "Entrance 1A", "Entrance 2D", "Main Office", "John's Office")
   With ActiveSheet
      FirstRow = .UsedRange.Cells(1).Row
      LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
      For i = FirstRow To LastRow
         If InStr(1, "|" & Join(Vr, "|") & "|", "|" & .Cells(i, "D").Value & "|", vbTextCompare) = 0 Then
            If Rng Is Nothing Then Set Rng = Rows(i) Else Set Rng = Union(Rng, Rows(i))
         End If
      Next i
   End With
   If Not Rng Is Nothing Then Rng.Delete
End Sub
 
Upvote 0
Or, slightly simpler, as you're hardcoding the array
Code:
Sub DeleteRows()
   Dim FirstRow As Long
   Dim LastRow As Long
   Dim i As Long
   Dim Vr As String
   Dim Rng As Range
   Vr = "|First Floor|Entrance 1A|Entrance 2D|Main Office|John's Office|"
   With ActiveSheet
      FirstRow = .UsedRange.Cells(1).Row
      LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
      For i = FirstRow To LastRow
         If InStr(1, Vr, "|" & .Cells(i, "D").Value & "|", vbTextCompare) = 0 Then
            If Rng Is Nothing Then Set Rng = Rows(i) Else Set Rng = Union(Rng, Rows(i))
         End If
      Next i
   End With
   If Not Rng Is Nothing Then Rng.Delete
End Sub
 
Upvote 0
Or, slightly simpler, as you're hardcoding the array
Code:
Sub DeleteRows()
   Dim FirstRow As Long
   Dim LastRow As Long
   Dim i As Long
   Dim Vr As String
   Dim Rng As Range
   Vr = "|First Floor|Entrance 1A|Entrance 2D|Main Office|John's Office|"
   With ActiveSheet
      FirstRow = .UsedRange.Cells(1).Row
      LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
      For i = FirstRow To LastRow
         If InStr(1, Vr, "|" & .Cells(i, "D").Value & "|", vbTextCompare) = 0 Then
            If Rng Is Nothing Then Set Rng = Rows(i) Else Set Rng = Union(Rng, Rows(i))
         End If
      Next i
   End With
   If Not Rng Is Nothing Then Rng.Delete
End Sub

This works great. Thanks for all the help
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,044
Members
452,542
Latest member
Bricklin

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