Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here's a fast way to return the number of substring occurrences. Hope it helps!</p> <pre><code>Option Explicit Option Compare Binary Option Base 0 DefLng A-Z Sub TestSubStringOccurence() Dim GrabRangeArray() As Variant Dim i As Long Dim L As Long Dim RunTime As Double Dim SubStringCounter As Long Dim J As Long Dim InStrPosition As Long Dim Ws As Excel.Worksheet Set Ws = ThisWorkbook.Sheets("Sheet1") RunTime = Timer With Ws For i = 1 To 50000 If i Mod 2 = 0 Then .Cells(i, 1).Value2 = "1 abcdef 2 abcdef 3 abcdef 4 abcdef 5 abcdef" _ Else .Cells(i, 1).Value2 = i Next i GrabRangeArray = .Range("a1:a50000").Value End With RunTime = Timer 'returns number of substring occurrences For i = 1 To UBound(GrabRangeArray, 1) InStrPosition = 1 Do InStrPosition = InStr(InStrPosition, GrabRangeArray(i, 1), "abcdef", vbBinaryCompare) If InStrPosition &lt;&gt; 0 Then SubStringCounter = SubStringCounter + 1 InStrPosition = InStrPosition + 6 End If Loop Until InStrPosition = 0 Next i Debug.Print "Runtime: " &amp; Timer - RunTime &amp; ", ""abcdef"" occurences: " &amp; SubStringCounter End Sub </code></pre> <p>here's a fast way to test if a substring exists, but does not return the number of substring occurrences.</p> <pre><code>Option Explicit Option Compare Binary Option Base 0 DefLng A-Z Sub TestSubStringOccurence() Dim GrabRangeArray() As Variant Dim I As Long Dim L As Long Dim RunTime As Double Dim SubStringCounter As Long Dim J As Long Dim InStrPosition As Long Dim Ws As Excel.Worksheet Const ConstABCDEFString As String = "abcdef" Dim B As Boolean Set Ws = ThisWorkbook.Sheets("Sheet1") RunTime = Timer ReDim GrabRangeArray(0 To 49999) With Ws For I = 1 To 50000 If I Mod 2 = 0 Then GrabRangeArray(I - 1) = "1 abcdef 2 abcdef 3 abcdef 4 abcdef 5 abcdef" _ Else GrabRangeArray(I - 1) = I - 1 Next I .Range("a1:a50000").Value = Application.Transpose(GrabRangeArray) End With RunTime = Timer For I = 1 To UBound(GrabRangeArray, 1) If InStrB(1, GrabRangeArray(I), ConstABCDEFString, vbBinaryCompare) Then _ SubStringCounter = SubStringCounter + 1 Next I Debug.Print "Runtime: " &amp; Timer - RunTime &amp; ", ""abcdef"" occurences: " &amp; SubStringCounter End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload