Search TeachExcel.com
TeachExcel.com
TE
Teach Excel MS Office Tutorials Excel Consulting Services Excel Forum
Excel Video Tutorials Excel Tips Free Excel Macros Excel Help Resources Contact TeachExcel
Video Tutorials
  • Free Macros
  • Excel Help Directory
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Excel Forum
  • Contact/About

Macros
Excel Tutorials For Macros

Count The Number of Words in a Cell or Range of Cells in Excel - UDF


Bookmark and Share

Count words in cells with this user defined function (UDF). This UDF allows you to count the number of words that are within one cell or a range of cells. The word count is determined by the number of spaces in the cell(s) - this means that this function will not correctly count the number of words if your words are separated by something other than a space, such as a dash, underscore, period, etc. Also, multiple consecutive spaces might throw the result of this function off. This is something to be aware of but, in most cases, this function will work just fine right off the bat.

If you use something other than a space to separate or delimit words within a cell, then look for the other count words function on this site; this other udf will allow you to specify your own delimiter / separator.
Where to install the macro:  Module

UDF to Count The Number of Words in a Cell or Range of Cells in Excel

Function COUNTWORDS(rRange As Range) As Long

Dim rCell As Range
Dim Count As Long

For Each rCell In rRange

lCount = lCount + Len(Trim(rCell)) - Len(Replace(Trim(rCell), " ", "")) + 1

Next rCell

COUNTWORDS = lCount

End Function


Bookmark and Share


How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.



Similar Helpful Excel Resources

Is It Possible To Count Specific Words In A Number Of Cells? - Excel

View Content
I am importing data from a survey in which some questions had multiple
options, when this is imported to excel multiple entries are seperated by
commas in the same cell and this continues down the column. Is it possible
to get excel to search cells for specific words within it and total their
occurence?



Count Number Of Blank Cells Between Two Words - Excel

View Content
Hi.. how do I go about counting the number of cells between two words?

I have multiple words (e.g., DM, DG) arranged in a column and in chronological order (as below). How do I count the number of blank/empty only cells between two words (i.e., between the first DM and DG?

Also how do I count all cells (with or without text) between DM and DG? Thanks v much.

11-Nov-10
29-Oct-10 DM
30-Sep-10 RL
31-Aug-10
31-Jul-10 DG
30-Jun-10
31-May-10
30-Apr-10 DM
31-Mar-10

Count Number Of Words In A Cell - Excel

View Content
Is there a formula to count how many words are in a cell? (Like LEN counts the characters)

I have a cell that contains first middle and last name. Sometimes they have more than one middle name and I want to know exactly how many names that person has.

Any help appreciated.

Thanks
DJL

Count The Number Of Words In A Cell. - Excel

View Content
Hi,

I am trying to write a macro that counts the number of words in a text.

For example

input

Column A

one two
one two three
one two three
one
...

output

Column B

2
3
3
1
...

I have the macro button on sheet1 and the columns will be on sheet3.
So when I click the macro button, which is on sheet1, it will get the number of words in column A of sheet3 and output it into column B of sheet3.

Any ideas?

I know the formula Code:

=IF(LEN(TRIM(A2))=0;0;LEN(TRIM(A2))-LEN(SUBSTITUTE(A2;" ";""))+1)


which I use without a macro.

Macro To Count Number Of Words In Cell A1 - Excel

View Content
I need a macro to count the number of words in cell A1 sheet1.

Failed example:
Code:

sub countwordsincell()
msgbox countwords(range("a1"))
end sub


Words in cell a1 = "Only if we had a macro to count words"
results:
msgbox says 9
Thanks!


Nicole

How To Count Specific Words In A Cell Range - Excel

View Content
I don't know if this is a 'count' formula or not, but i want to count the number of times a specific word is displayed in a cell range.

for example the cell range is A4:C10 and the word is 'hello'

cheers,
1.zer0

Counting Number Of Words In A Cell Range - Excel

View Content
I have a worksheet and want to count the number of words, say, ABC, in the cell range, say, A1:B5.

Any function/formulas that can help this count?

Genarating Count Of Unique Words In A Cell Or Cells - Excel

View Content
Hi,

Suppose in cell A1 I have a text --- "I had some bread in morning. I had
some eggs at night."

Is it possible to programmatically get a count of unique words in the above
string.
Like in column B the unique words are listed one word in each row in the
following manner..B1 -- "I"
B2 -- "I
B2 -- "had
B3 -- "some"
B4 -- "bread"
B5 -- "in"
B6 -- "morning"
B7 -- "eggs"
B8 -- "at"
B9 -- "night"

Actually I can use excel's text to columns feature ( using space as a
delimiter) but automating is a problem. Like I have 500 or so rows of data
and out of those rows I want to get count of unique words within those 500
rows ( basically a consolidated unique list..). Now, each row would be
having different number of words so, programmatically how would it be
implemented.

Please guide me if possible.

Regards,
Hari
India





Excel 2007 (count Words Entered In Cell) - Excel

View Content
Hey everyone,

I'm working on a project and need some assistance. Using Excel 2007, how do I add up the total of the word "apples" to a cell.

Example: If Cell A1,A10,A25,A30 all have the word apple entered, how do I add them up in lets say cell B10.


Also, I need to do it where I can enter multiple words in 1 cell and have it organized according to how many times the word is used.




Thanks in advance!

Count Number Of Cells Between Range - Excel

View Content
I receive spreadsheets which tell me where samples are located in 96 well plates. The plates' rows are lettered A-H, and the columns are numbered 1-12. So, for example, the first well is labeled A1, the 12th is A12, the 13th is B1, and the 96th is labeled H12.

When I receive the spreadsheets, there are cells that tell me which wells are occupied. For example a cell will contain "A2 - B7" or "D3-F12" (sometimes with the spaces before and after the "-", and sometimes not." I need a program that will tell me that "A2-B7" (or "A2 - B7") means there are 18 wells occupied.

I've tried a few things, but a lot of problems arise when I try to do anything with names like "A1" because excel already defines "A1" as cell A1. Do you guys have any ideas? Am I being clear?

Thanks,
Joe

Random Tutorials
Goal Seek Feature in Excel
(Intermediate)
Net Present Value / Discounted Cash Flow Calculations
(Intermediate)
Remove #N/A Error Result from Empty VLOOKUP() Formulas
(Intermediate)
Lookups With MATCH() and INDEX() Functions
(Intermediate)
Introduction to Making Formulas in Excel
(Easy)
Absolute and Relative Cell References
   - & INDIRECT() Function Introduction

(Easy)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2012 TeachExcel.com