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 :)
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

DiscoPistol

Active Member
Joined
Jun 6, 2006
Messages
261
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
 

oldbrewer

Well-known Member
Joined
Apr 11, 2010
Messages
11,004
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>
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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:

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,861
Messages
5,627,307
Members
416,239
Latest member
Counselor85027

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
Top