Home » excel » excel – Finding multiple counts of phrase in a single cell

excel – Finding multiple counts of phrase in a single cell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need something counts the number of times a certain phrase presents itself in a SINGLE cell.

Cell B5 contains the value “Test 123 Test 1234”

Sub count()
ival = Application.WorksheetFunction.CountIf(Range("B5"), "Test")
MsgBox ival
End Function

This returns “0” because it’s not an exact match. I want something that counts the number of times “Test” is in the cell and properly returns 2 as the value.

How to&Answers:

Combine Split and Ubound

Split the string into zero based 1D array of substrings, then find the upperbound of that array using ubound

Sub count()
 Dim count As Integer
 count = UBound(Split(Range("B5"), "123"))
 MsgBox count
End Sub