Here is the equivalent VBA code to count the number of cells containing at least one string
Sub StringCounter()
Dim r As Range, s As String, i As Long
s = "*string*"
For Each sh In Sheets
Set r = sh.Range("C2:C150")
i = i + Application.WorksheetFunction.CountIf(r, s)
Next sh
MsgBox i
End Sub
EDIT#1:
Here is the code to count the number of strings It will count multiple string within a single cell:
Sub StringCounter_II_The_Sequel()
Dim r As Range, s As String, i As Long
Dim rr As Range, L As Long
s = "string"
L = Len(s)
For Each sh In Sheets
Set r = sh.Range("C2:C150")
For Each rr In r
txt = rr.Text
If InStr(txt, s) > 0 Then
i = i + (Len(txt) - Len(Replace(txt, s, ""))) / L
End If
Next rr
Next sh
MsgBox i
End Sub
EDIT#2:
The math works as follows:
- say there are three string in a cell and the number of characters in that cell is 100
- the replace removes all three instances and the new length is 82
- the subtract results in 18
- the division results in 3 (because the word string is 6 characters long)