## How to count word frequency in a cell range [UDF]

This user defined function creates a unique distinct list of words and how many times they occur in the selected range.

### User defined Function Syntax

FREQWORDS(*cell_range, position*)

### Arguments

cell_range |
Required. The range you want to use. |

position |
Required. Which column to return. The first column contains the values and the second column contains their corresponding frequency. |

### Example

Array formula in cell E3:E30:

Array formula in cell F3:F30:

**How to create an array formula**

### VBA

'Name function and declare arguments Function FreqWords(tbl_array As Range, pos As Integer) As Variant() 'Declare variables and their data types Dim cell As Variant, wrds As Variant, i As Integer Dim a As Integer, j As Integer Dim tmp() As String, nr() As Integer 'Redimension variable tmp so it can grow using Redim Preserve ReDim tmp(0), nr(0) 'Assign 1 to first value in array variable nr nr(0) = 1 'Iterate through cells in cell range For Each cell In tbl_array 'Split words in cell wrds = Split(cell) 'Iterate thorugh words For i = 0 To UBound(wrds) 'Iterate through arrayvariable tmp For j = 0 To UBound(tmp) 'If variable wrds equal variable tmp then increase value in variable nr by 1 If wrds(i) = tmp(j) Then nr(j) = nr(j) + 1 a = 1 Exit For End If Next j 'Check if a is not equal to 1 If a <> 1 Then 'Copy value from variable wrds to tmp tmp(UBound(tmp)) = wrds(i) 'Add another container to array variable tmp ReDim Preserve tmp(UBound(tmp) + 1) ReDim Preserve nr(UBound(tmp)) nr(UBound(tmp)) = 1 End If a = 0 Next i Next cell 'Return values in column 1 if argument pos is equal to 1 If pos = 1 Then ReDim Preserve tmp(UBound(tmp) - 1) FreqWords = Application.Transpose(tmp) Else 'Return values in column 2 if argument pos is not equal to 1 ReDim Preserve nr(UBound(nr) - 1) FreqWords = Application.Transpose(nr) End If End Function

**How to add the User defined Function to your workbook**

- Press Alt-F11 to open visual basic editor
- Press with left mouse button on Module on the Insert menu
- Copy and paste vba code
- Exit visual basic editor

Extract unique distinct values from a filtered Excel defined Table [UDF and Formula]

This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]

List files in a folder and subfolders [UDF]

This article demonstrates a user defined function that lists files in a ggiven folder and subfolders. A user defined function is […]

Search for a file in folder and subfolders [UDF]

The image above demonstrates a user-defined function in cell range B6:D7 that allows you to search a folder and subfolders […]

Split values equally into groups

Question: How do I divide values equally into groups (3 lists or less)? This post shows you two different approaches, […]

Split words in a cell range into a cell each [UDF]

This post describes how to split words in a cell range into a cell each using a custom function. I […]

Filter unique distinct words from a cell range [UDF]

This blog post describes how to create a list of unique distinct words from a cell range. Unique distinct words […]

Count unique distinct values by cell color

This article demonstrates a User Defined Function (UDF) that counts unique distinct cell values based on a given cell color. […]

Substitute multiple text strings [UDF]

The SUBSTITUTE and REPLACE functions can only handle one string, the following User-Defined Function (UDF) allows you to substitute multiple […]

List permutations without repetition [UDF]

This blog post describes how to create permutations, repetition is NOT allowed. Permutations are items arranged in a given order meaning […]

### 26 Responses to “How to count word frequency in a cell range [UDF]”

### Leave a Reply to Rahul Jadhav

### How to comment

**How to add a formula to your comment**

<code>Insert your formula here.</code>

**Convert less than and larger than signs**

Use html character entities instead of less than and larger than signs.

< becomes < and > becomes >

**How to add VBA code to your comment**

[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

**How to add a picture to your comment:**

Upload picture to postimage.org or imgur

Paste image link to your comment.

**Contact Oscar**

You can contact me through this contact form

While executing this function,I am facing #value error in the entire column. Please help.

Sam,

This udf is created in excel 2007, what excel version do you have?

Did you remember to enter the second argument?

=FreqWords(B2:C11,

1)Did you create an array formula?

How to create an array formulaSelect cell range E3:E30.

Copy (Ctrl + c) and paste (Ctrl + v) array formula into formula bar.

Press and hold Ctrl + Shift.

Press Enter once.

Release all keys.

Hello Oscar,

first of all MERRY CHRISTMAS AND HAPPY NEW YEAR 2012- also Thank you very much for Word Frequency function.

I would like to ask if it is possible to convert it to a VBA Macro subroutine . I mean is it possible to use Freqwords function with in a sub(). I am novice to programming . please help.

Thank you very much sir.

Srinivas

Hi Oscar

Happy New Year 2012. Thank you very much for Word Frequency UDF. Forget about above request for sub(). I have found alternative.

Thank you

Srinivas

[...] Does this help?.... Excel udf: Word frequency | Get Digital Help - Microsoft Excel resource Example results from the above solution...... [...]

Hi Oscar,

I need your help to find the macro

I have many excel files with multiple sheets and each excel sheet

has many formula which are starting from perticular word e.g. FDS, FDSB, etc some formula has FDS, FDSB occur in the middle of the formula.

i need to find out how many times FDS,FDSB has been appear in the sheet(total count)

below is the formulae for your referance

=FISERROR(FDSB($D21, "IC_ESTIMATE_DATE(ALL, EXP_RPT, QTR, 3, 0, , , 'MM/DD/YYYY')@RC_ESTIMATE_DATE(ALL, EXP_RPT, QTR, +3, 0 , , , 'MM/DD/YYYY')"),"na)

in such a way there are many formulas in all the cells

i just need to count the how many times FDSB occur in one perticular sheet

please help

Best Regards

Rahul Jadhav

Pune

India

Rahul Jadhav,

Have you read this post:

Count multiple text strings in a cell range

Thank you

Rahul Jadhav,

Read this post:

Count text strings in formulas (vba)

Hi Oscar

Thanks for the reply this is what i was looking for thanks once again now i will apply it in all the worksheets.

One more thing i would like to ask is that

How can we identify any protected excel file before opening using macro VBA (e.g. i have a folder with multiple excel files i need to find out using macro how many files are password protected before opening and it should give result on separate workbook.)

is it possible using excel vba macro

Please reply

Best regards

Rahul Jadhav

rahul jadhav,

read this post:

Find out if excel files in a folder are password protected

[…] Rahul Jadhav asks: […]

[…] rahul jadhav asks: […]

Hi Oscar,

Thank you very much for the macro just to infrom you that it is working correctly

Thanks again

Best regards

Rahul Jadhav

Hi Oscar,

Hope you are doing good!!!

My requirement is to copy data from ms excel and paste into ms word on specific position

>Both the files should get open automatically specially ms word

>macro should identify the user define cursor position in ms word file

>then paste the data where user wants to

>In this way i want to copy tables data, text, from excel into ms word on user define posion.

please check and let me know the suitable solution for the same

Have a nice time ahead

Thanking you in advance

Best Regards

Rahul Jdhav

Awesome function!!! Thanks so much for sharing this!

[…] Excel udf: Word frequency […]

hello oscar,

why u'r formula cannot use in new file?

thanks

boboy,

You need to copy and paste the VBA code to a module in your new file (workbook).

Thank you for that.

The only issue i'm facing is that only the first word of each cell is displaying after entering the formula. Not the full list of each single keyword.

For example, in your spreadsheet, the word "Air" appears 6 times in my list, and the frequency is correct (9).

What am I doing wrong?

Valentino,

you need to enter the formula as an array formula. There are instructions in this article on how to create an array formula.

How do you get the word frequency results in the array to sort by largest to smallest?

Hi great formula!

Quick question - my apologies in advance if it is dumb I typically do not work in Excel.

When I define the array for the results return I am assuming I know the number of unique words I have (in your example you knew there are 27 unique values?). But what happens if I don't know the number of unique words? I selected as an array the whole column but I get many #N/A and then it very inconvenient because I cannot delete these (it tells me I cannot change the array).

Is there a way for it automatically to decide the size of the array?

*I realize supposedly it is a small issue but I am working with a very large file more than 5000 companies that each have many tags and trying to sort the frequency of the tags and it gets messy.

Thanks in advance!

Julie

Is there a way for it automatically to decide the size of the array?Yes, Excel 365 subscribers don't need to enter this as an array formula. Excel takes care of the output and returns spilled values.

This UDF is for previous Excel versions. It will return blank values if the entered cell range is larger than the number of returned values.

Hi Oscar.

Much like Julie, I mod'ed the formula for a single column, removed the array because I am using Excel 365 and am getting the #NAME? error. I double checked the formula call to the function name and it reads the same. Not sue what else to do...

Michael

Hello again.

Nevermind... just found a thread that suggested adding in the module name infant of the function call, so I renamed the call to Module1.FreqWords and it worked.

Michael