Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting Commas in a Selection.

Counting Commas in a Selection

Written by Allen Wyatt (last updated September 11, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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 = Replace(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 (11029) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel 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

Replacing with Plain Text

When using Find and Replace, how your replacements are formatted will depend on how the text being replaced is formatted. ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Condensing Sequential Values to a Single Row

If you have a bunch of ZIP Codes or part numbers in a list, you may want to "condense" the list so that sequential series ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More

Selecting the First Cell In a Row

When creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...

Discover More

Storing a User's Location before Running a Macro

Macros are often used to process information in a workbook. If your macro makes changes in what is selected in the ...

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 6 - 0?

2021-09-11 09:02:08

Willy Vanhaelen

I like to make my macros as compact as possible. Here is my version of this tip's one:

Function CountComma(R As Range)
Dim Cel As Range, X As Integer
For Each Cel In R
X = X + Len(Cel) - Len(Replace(Cel, ",", ""))
Next
CountComma = X
End Function

Here is even a one-liner with the vba implementation of this tip's array formula:

Function CountComma(R As Range)
CountComma=Evaluate("SUM(LEN("&R.Address &"))-SUM(LEN(SUBSTITUTE("&R.Address &","","","""")))")
End Function

Syntax: =CountComma(your_range).
Since this UDF itself is not an array formula, enter it simply with Enter (instead of Ctrl+Shift+Enter).


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.