Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Counting Commas in a Selection.

Counting Commas in a Selection

Written by Allen Wyatt (last updated September 17, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


1

At work, Mark regularly needs to count the number of commas in a range of selected cells. He can't find an Excel function to do this type of task, and is wondering if a macro might be able to do the trick.

While there is no worksheet function that will produce the desired count, there is a formula or two you can use. If you just want to know the number of cells that have at least one comma in them, the following formula will work just fine:

=COUNTIF(A1:A10,"*,*")

If you, instead, need to figure out the number of commas in the range when there could be multiple commas per cell, then you need to use a different formula:

=SUM(LEN(A1:A10))-SUM(LEN(SUBSTITUTE(A1:A10,",","")))

This formula should be entered as an array formula, which means that you should use Ctrl+Shift+Enter to enter the formula. If you need to derive the count for a different range, just change the range in two places in the formula.

If you prefer, you could also create a user-defined function to count the number of commas. There are multiple ways to approach such a task; the following is just one example.

Function CountComma(rng As Range)
    Dim iCount As Integer
    Dim rCell As Range
    Dim sTemp As String

    Application.Volatile
    iCount = 0
    For Each rCell In rng
        sTemp = Application.WorksheetFunction. _
          Substitute(rCell.Value, ",", "")
        iCount = iCount + _
          (Len(rCell.Value) - Len(sTemp))
    Next
    CountComma = iCount
    Set rCell = Nothing
    Set rng = Nothing
End Function

In order to use the function in the worksheet, enter the following into a cell:

=CountComma(A1:A10)

All of these methods described so far will count commas that are actually in the cell. They will not count commas that appear to be in the cell because of formatting. For instance, if a number appears as "1,234" in a cell, chances are good that the comma is there because of the way that the cell is formatted; it is not really in the cell itself. Such commas are not counted.

Of course, if all you need to do is know the number of commas and you don't need the value in your worksheet, you can bypass the use of formulas and macros all together. Follow these general steps:

  1. Select the range of cells in which you want to count commas.
  2. Press Ctrl+H to display the Replace tab of the Find and Replace dialog box.
  3. In the Find What box, enter a comma.
  4. In the Replace With box, enter a comma.
  5. Click Replace All.

Excel does the replacement and displays a dialog box that shows how many replacements were made.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3460) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Counting Commas in a Selection.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Word Freezes When Working with Lots of Clip Art

Don't you hate it when Word freezes? Laurie has that problem when she works with clip art in her documents. There are any ...

Discover More

Losing Data in a Shared Workbook

When you create a shared workbook, you run the risk of losing some of the data in that workbook. Here's a discussion ...

Discover More

Swapping Two Strings

Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Creating a Photo Catalog from a Folder of Photos

Excel is great for collecting all sorts of information. You might even use it to create a catalog of your photos. Working ...

Discover More

Getting User Input in a Dialog Box

Want to get some input from the users of your workbooks? You can do it by using the InputBox function in a macro.

Discover More

Converting Numbers to Strings

When creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2022-09-25 18:51:38

ragahd

I think that the best month in Michigan is June because it's not too hot not too cold and the flowers and trees are in full bloom


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.