Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 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.
by Allen Wyatt
(last updated June 7, 2019)
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:
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:
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:
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:
Excel does the replacement and displays a dialog box that shows how many replacements were made.
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 Office 365. You can find a version of this tip for the older menu interface of Excel here: Counting Commas in a Selection.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Creating macros can help extend what you can do in Excel. If you work with macros, you know that creating macros from ...Discover More
When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially ...Discover More
You might want to display a value in a cell as an upward-counting value. This might seem difficult but can be done rather ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.