VBA Delete rows if cell contains specific text within a string

gray_b

New Member
Joined
Apr 17, 2019
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Expanding on VBA Delete rows if cell contains specific text

1 - How can I make it search for say "Dog" within a string in a cell of say "My Dog plays games"

Changing - If (Cells(i, "B").Value) = "Dog"

to something like

If (Cells(i, "B").Value) = "*Dog*"

That does not work, but any ideas please.

Also
2 - To search for multiple words for say "Dog" "Cat" "Mouse" within a strings of say "My Dog plays games" and "My Cat is sleeping" and "Mouse has 4 legs"

Changing - If (Cells(i, "B").Value) = "Dog" or "Cat" or "Mouse"

That does not work, but any ideas please.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Replace the current If (Cells(I, "B").Value) = "Dog*" Then with
VBA Code:
If InStr(1, Cells(I, "B").Value, "Dog", vbTextCompare) > 0 Then

For multiple searches, use OR:
VBA Code:
If InStr(1, Cells(I, "B").Value, "Dog", vbTextCompare) > 0 Or _
 InStr(1, Cells(I, "B").Value, "Cat", vbTextCompare) > 0 Or _
 InStr(1, Cells(I, "B").Value, "Wolf", vbTextCompare) > 0 Then

Try...
 
Upvote 0
To search for multiple words for say "Dog" "Cat" "Mouse" within a strings of say "My Dog plays games" and "My Cat is sleeping" and "Mouse has 4 legs"
Judging by your examples and your text, you want to look for words not text strings?
That is, you would not want to delete the row if the text was "Tom was dogmatic" or "The birds scattered"?
Please clarify.

Also, about how many rows of data do you expect to have to check?
 
Upvote 0
I also assumed you wanted words. So here are a couple of options.
Note: It won't pick up words immediately preceded or followed by punctuation ie full stop or comma.

VBA Code:
Sub Option1_Like()

    Dim sTxt As String
    Dim i As Long
    
    i = 7
    sTxt = " " & UCase(Cells(i, "B").Value) & " "
    ' search terms must be entered in uppercase
    ' or you need to wrap the right hand side in Ucase as well
    If sTxt Like "* DOG *" Or _
        sTxt Like "* CAT *" Or _
        sTxt Like "* MOUSE *" Then
        
        MsgBox "Found"
        
    End If

End Sub

Sub Option2_InStr()

    Dim sTxt As String
    Dim i As Long
    
    i = 7
    sTxt = " " & Cells(i, "B").Value & " "
    ' Not case sensitive
    If InStr(1, sTxt, " dog ", vbTextCompare) > 0 Or _
        InStr(1, sTxt, " cat ", vbTextCompare) > 0 Or _
        InStr(1, sTxt, " mouse ", vbTextCompare) > 0 Then
        
        MsgBox "Found"
    Else
        MsgBox "Not Found"
        
    End If

End Sub
 
Upvote 0
Judging by your examples and your text, you want to look for words not text strings?
That is, you would not want to delete the row if the text was "Tom was dogmatic" or "The birds scattered"?
Please clarify.

Also, about how many rows of data do you expect to have to check?
Yes you are right, complete words, not parts of words within the each cell text
 
Upvote 0
complete words, not parts of words
Thanks for that clarification. :)

Unfortunately, no clarification to this one though. :(
Also, about how many rows of data do you expect to have to check?


Never-the-less, give this a try with a copy of your data. You should find it very fast, especially if your data is large.
I have assumed that the column to check is column B. Change in the code where indicated for another column.

VBA Code:
Sub Del_Rws()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True    '<- Remove this line if you want the word search to be case-sensitive
  RX.Pattern = "\b(Dog|Cat|Mouse)\b"
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value '<- Change column if required
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If RX.test(a(i, 1)) Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub

My sample data BEFORE:

gray_b.xlsm
ABCD
1Hdr1Hdr2Hdr3Hdr4
2a2abcdatadata
3a3My Dog plays gamesdatadata
4a4Tom was dogmaticdatadata
5a5defdatadata
6a6datadata
7a7ghidatadata
8a8The birds scattereddatadata
9a9My cat likes milkdatadata
10a10datadata
11a11Mouse has 4 legsdatadata
12a12Dogdatadata
Sheet3


AFTER:

gray_b.xlsm
ABCD
1Hdr1Hdr2Hdr3Hdr4
2a2abcdatadata
3a4Tom was dogmaticdatadata
4a5defdatadata
5a6datadata
6a7ghidatadata
7a8The birds scattereddatadata
8a10datadata
9
Sheet3
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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