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:
Excel does the replacement and displays a dialog box that shows how many replacements were made.
Note:
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as ...
Discover MoreExcel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...
Discover MoreWorkbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-06-20 01:27:16
excelady
this excel tips website is AWESOME!!! thank you for a very cool formula.
2017-04-20 16:49:42
Conrad
Another Great Tip!
Glad I found this website
2017-02-07 18:24:27
Tim
Great tip on the array formula for counting commas. Worked a treat. Thanks!
2015-04-12 15:20:46
John
This was extremely helpful for assessing some survey results. Thanks!
2014-07-14 11:08:37
john
Hi,
If the Range is large, it will take quite some time to run the macro, because Excel will have to sweep every Cell in the Range.
Can Replacement:=... help?
Just a thought.
Thanks.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments