Comparing strings using InStr() - vba help required

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi All,

I am using the code below to search for a keyword in a string against a list of keywords located in another tab, if the word exists then the row is copied over to another sheet.

The code works, however it has to be run twice to complete successfully. I am using a nested For Each Loop, the outer one is to loop through each keyword in the list and the inner loop for each cell where the string needs to be checked. I have attached a copy of the workbook if anyone can assist please?

https://1drv.ms/x/s!AilrZgAqVaaqmTEk0Ku7G8iY4uSv

Code:
Sub CopyRows()


    Dim SrcSheet As Worksheet
    Dim DstSheet As Worksheet
    Dim KeywordSheet As Worksheet
    Dim LrSrc As Long
    Dim c As Range
    Dim x As Range
    Dim LookupRange As Range
    Dim LrLookup As Long


    Set SrcSheet = Sheets("Raw Data")
    Set DstSheet = Sheets("Filtered Results")
    Set KeywordSheet = Sheets("Keyword Search")


    SrcSheet.Columns.AutoFit


    LrSrc = SrcSheet.Range("A" & Rows.Count).End(xlUp).Row
    LrLookup = KeywordSheet.Range("A" & Rows.Count).End(xlUp).Row


    Set LookupRange = KeywordSheet.Range("A2:A" & LrLookup)


    For Each x In LookupRange
        For Each c In SrcSheet.Range("A2:A" & LrSrc)
            Debug.Print "Checking for keyword: " & x.Value; " in string " & c.Value
            Application.StatusBar = "Checking for keyword: " & x.Value
            If InStr(UCase(c.Value), UCase(x.Value)) > 0 Then 'convert both values to uppercase to compare
                c.EntireRow.Copy DstSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
                c.EntireRow.Delete Shift:=xlUp
            End If
        Next c
    Next x


    DstSheet.Columns.AutoFit
    Application.StatusBar = False


End Sub
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about
Code:
Sub CopyRows()

    Dim SrcSheet As Worksheet
    Dim DstSheet As Worksheet
    Dim KeywordSheet As Worksheet
    Dim LrSrc As Long
    Dim c As Range
    Dim x As Range
    Dim LookupRange As Range
    Dim LrLookup As Long
    Dim Rng As Range
    
    Set SrcSheet = Sheets("Raw Data")
    Set DstSheet = Sheets("Filtered Results")
    Set KeywordSheet = Sheets("Keyword Search")

    SrcSheet.Columns.AutoFit

    LrSrc = SrcSheet.Range("A" & Rows.Count).End(xlUp).Row
    LrLookup = KeywordSheet.Range("A" & Rows.Count).End(xlUp).Row

    Set LookupRange = KeywordSheet.Range("A2:A" & LrLookup)

    For Each x In LookupRange
        For Each c In SrcSheet.Range("A2:A" & LrSrc)
            Debug.Print "Checking for keyword: " & x.Value; " in string " & c.Value
            Application.StatusBar = "Checking for keyword: " & x.Value
            If InStr(1, c.Value, x.Value, vbTextCompare) > 0 Then 'convert both values to uppercase to compare
               If Rng Is Nothing Then
                  Set Rng = c
               Else
                  Set Rng = Union(Rng, c)
               End If
            End If
        Next c
    Next x
    If Not Rng Is Nothing Then
      Rng.EntireRow.Copy DstSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
      Rng.EntireRow.Delete Shift:=xlUp
    End If


    DstSheet.Columns.AutoFit
    Application.StatusBar = False

End Sub
 
Upvote 0
This works great thanks.

Any idea what was wrong with my original code? I can see you have used an additional range variable and also the instr line has changed.

Cheers
 
Upvote 0
When deleting (or inserting) rows(columns) in the way that you were its best to work backwards (bottomup for rows right to left for columns).
With your code if A2 was deleted then A3 becomes A2. You then hit next c which takes you to A3, which was originally A4 & so the original A3 never gets checked.

So rather than completely re doing your code I created a new variable Rng which stores all the cells that need deleting & then that is used to delete all rows in one hit at the end.
With regards the InStr. There wasn't anything particularly wrong with what you did, but using vbtextcompare makes the comparison case insensitive, therefore no need for the UCase
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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