Find no. of repetitions of a string in a cell

Hitesh_Banda

New Member
Joined
Jun 8, 2010
Messages
25
Hi,

I have a text in cell H2.

My sheet contains text values from A5 to A233. I need to find out how many times the text in cell H2 is being repeated in the column A. (Apparently the whole sheet excluding H2)

Is there any formula where I can find out the no. of instances of an instring in a cell so that I can sum them all and get the total no. of repetitions?

Thanks.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
if cell values (cell contents) are exactly the same:

=COUNTIF(A5:A233,H2)


edit: i did not see "instring". sorry.
 
Last edited:
Upvote 0
Hi

How about:

In another column (col B perhaps)

=IF(ISNA(MATCH($H$2,A1,0)),"",1) - copy down

Then SUM column B
 
Upvote 0
When you say 'instring' I take it you mean the text may appear within other text...

e.g. Searching for the string 'Text' you could have 'aaaTextaaa', 'Text', 'SomeText', 'dsafa'

You can use wildcards in the COUNTIF function to find all three instances in the example above:
Code:
=COUNTIF(A5:A233,"*" & $H$2 & "*")

This will only find 1 instance per cell though - 'aaaaTEXTaaaaTEXTaaa' will only count as 1.
 
Last edited:
Upvote 0
Sorry I was not clear when posting this thread. The text in cell H2 is going to repeat more than once in some cells. I need to find the no. of those instances.
 
Upvote 0
Possibly a custom function to count them all, but if there's a maximum of 5 then could do a formula I guess... but here's the custom function version:
Code:
Function CountInString(Target As Range, SearchFor As String) As Long

    Dim lAns As Long
    Dim sText As String
    Dim rngCell As Range
    
    For Each rngCell In Target
        sText = sText & rngCell.Value
    Next rngCell
    
    Do While InStr(sText, SearchFor) > 0
        lAns = lAns + 1
        sText = Replace(sText, SearchFor, "", 1, 1)
    Loop

    CountInString = lAns

End Function
 
Upvote 0
=SUM(--(MID(A5,ROW(INDIRECT("1:"&LEN(A5))),LEN(H2))=H2))

Arrayformula - Confirm with CTRL + SHIFT + ENTER (and copy down to A233)


... but I don´t see how to SUM them directly in a resultcell.

/Mile
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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