Count occurances of character string in a worksheet

vegasguy

New Member
Joined
Feb 26, 2016
Messages
6
Hi All,

How can I count the number of times a word appears in a worksheet using VBA?

Thanks in advance,

vegasguy :)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi,

This will let you enter the word and then count how many cells it appears it. I've set it using a Range (albeit only 3 cells!)

However if a word appears in a cell twice it will only count is once.

It should also be case insensitive.

Hope that helps

DP


Code:
Sub test()

Dim Search_word As String
Dim rng As Range
Dim Word_Count As Long

Word_Count = 0

Set rng = Sheets("Sheet1").Range("A1:A3")

Search_word = InputBox("Please enter the word that you are looking for")

For Each Cell In rng.Cells
     If InStr(1, LCase(Cell.Value), LCase(Search_word)) > 0 Then Word_Count = Word_Count + 1
Next Cell

MsgBox Word_Count

End Sub
 
Upvote 0
applepearlime
limeorangemango
pineapplelimeapricotassume A1:C4 is your entire worksheet
bananacoconutlime
how many times does "lime" occur ?
this macro puts the 4 into cell A10
4For j = 1 To 4
For k = 1 To 4
If Cells(j, k) = "lime" Then Sum = Sum + 1
Next k
Next j
Cells(10, 1) = Sum
End Sub

<colgroup><col width="64" span="11" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Hi
Welcome to the board

A simple way is to use CountIf().
Another way is to use the Find method of the Range object.
 
Last edited:
Upvote 0
If you click "Find All" in the "Find and Replace" dialog it will report how many different cells contain the text but will not count multiple instances in a single cell.
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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