VBA Fastest way to find all occurrences of a string in a range

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
Suppose I have a list of words (a rather large list of words ~55k) and I want to find the number of cells in a given range (a very large range) that contains at least one instance of that word. Is there a faster way than looping through every cell in the range and using the INSTR() function?

Code:
For i = LBOUND(words) to UBOUND(words) ' where words() contains my 55k words

  wordToSearch = words(i)

   For x = 1 to lastRowofRange

    If INSTR(1,CSTR(Range("A" & x).Value,wordToSearch) > 0 then count = count+1

   Next x
Next i
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Would a COUNTIF not be better and using wildwards
 
Upvote 0
You didn't specify the range, but this method might be a little faster than a For...Next loop with InStr

Code:
Sub findNCountStuff()
Dim sh As Worksheet, dsh As Worksheet, fn As Range, fAdr As String, i As Long, words As Variant
Set sh = ActiveSheet
Set dsh = Sheets(2)
words = Array("test1", "test2", "test3")
For i = LBound(words) To UBound(words)
    With sh
        Set fn = sh.UsedRange.Find(words(i), , xlValues, xlPart)
            If Not fn Is Nothing Then
                fAdr = fn.Address
                Do
                    cnt = cnt + 1
                    Set fn = sh.UsedRange.FindNext(fn)
                Loop While fn.Address <> fAdr
                dsh.Cells(Rows.Count, 1).End(xlUp)(2) = words(i)
                dsh.Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = cnt
                cnt = 0
            End If
    End With
Next
End Sub

The falacy of this method is that using the xlPart criteria makes it subject to finding pieces of larger strings.
 
Last edited:
Upvote 0
JLGWhiz,

Thanks. I will give it a go tomorrow on a sample and let you know what I find.

Also, I am not worried about the xlPart as these are always words separate by a space so i can build that into the find.

Thanks,

J
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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