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
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,066
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Would a COUNTIF not be better and using wildwards
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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:

jscranton

Well-known Member
Joined
May 30, 2011
Messages
707
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,078
Messages
5,599,632
Members
414,326
Latest member
kfg1287

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
Top