This post will guide you how to count the number of a given character in a cell in Excel. How do I count only numbers within a single cell excluding all letters and other characters with a formula in Excel. How to count the number of letters excluding all numbers in a given string in Excel.
Table of Contents
- 1. Count Total Characters in a Cell
- 2. Count Only Numbers in a Cell using formula
- 3. Count Only Numbers in a Cell using VBA
- 4. Count Only Letters or Other Characters Excluding Numbers
- 5. Count Only Letters or Other Characters Excluding Numbers using VBA
- 6. Video: Count the Number of Letters or Numbers separately in a Cell
- 7. Related Functions
1. Count Total Characters in a Cell
Assuming that you have a list of data in range B1:B4, and you want to count the total number of all characters in one cell, you can use a formula based on the LEN function to get it. Like this:
=LEN(B1)
Type this formula in cell C1 and press Enter key on your keyboard, and drag the AutoFill Handle to copy this formula from Cell C1 to range C2:C4.
You should notice that the number of all characters in each cell is calculated.
2. Count Only Numbers in a Cell using formula
If you want only count the total numbers in cells, excluding letters and other specific characters, you can use a formula based on the LEN function and the SUBSTITUTE function. Like this:
=LEN(B1)-LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B1,0,""),1,""),2,""),3,""),4,""),5,""),6,""),7,""),8,""),9,""))
Type this formula into a blank cell and press Enter key. And then drag the AutoFill Handle down to other cells to apply this formula.
3. Count Only Numbers in a Cell using VBA
Now, let’s explore the second method using VBA code to count only numbers in a cell. This dynamic solution offers automation and customization for more advanced scenarios in your Excel data.”
Press ‘Alt + F11‘ to open the Visual Basic for Applications (VBA) editor.
Right-click on any item in the project explorer, choose ‘Insert,’ and select ‘Module‘ to add a new module.
Copy and paste the following VBA code into the module:
Function CountNumbers(cell As Range) As Long
Dim i As Integer
CountNumbers = 0
For i = 1 To Len(cell.Value)
If IsNumeric(Mid(cell.Value, i, 1)) Then
CountNumbers = CountNumbers + 1
End If
Next i
End Function
Close the VBA editor by clicking the “X” button or pressing ‘Alt + Q.’
Enter the function:
=CountNumbers(B1)
where B1 is the cell you want to count numbers in.
Press Enter, and the result will be the count of numbers in the specified cell.
4. Count Only Letters or Other Characters Excluding Numbers
If you want to count only letters and other specific characters in Cells, you can also use another formula based on the LEN function and the SUBSTITUTE function to achieve the result. Like this:
=LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B1,0,""),1,""),2,""),3,""),4,""),5,""),6,""),7,""),8,""),9,""))
Type this formula into a blank cell and press Enter key. And then drag the AutoFill Handle down to other cells to apply this formula.
5. Count Only Letters or Other Characters Excluding Numbers using VBA
For the second method, we’ll turn to VBA code to count only letters or other characters excluding numbers. This dynamic approach offers enhanced customization for precise character analysis within your Excel data.
Press ‘Alt + F11’ to open the Visual Basic for Applications (VBA) editor.
Right-click on any item in the project explorer, choose ‘Insert,’ and select ‘Module’ to add a new module.
Copy and paste the following VBA code into the module:
Function CountLetters(cell As Range) As Long
Dim i As Integer
CountLetters = 0
For i = 1 To Len(cell.Value)
If Not IsNumeric(Mid(cell.Value, i, 1)) Then
CountLetters = CountLetters + 1
End If
Next i
End Function
Close the VBA editor by clicking the “X” button or pressing ‘Alt + Q.’
Return to your Excel workbook and enter the formula:
=CountLetters(B1)
where B1 is the cell you want to count letters in.
Press Enter, and the result will be the count of letters or other characters excluding numbers in the specified cell.
6. Video: Count the Number of Letters or Numbers separately in a Cell
This Excel video tutorial where we’ll explore versatile methods for counting numbers and letters in cells- employing formulas and delving into VBA code.
7. Related Functions
- Excel Substitute function
The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string.The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE (text, old_text, new_text,[instance_num])…. - Excel LEN function
The Excel LEN function returns the length of a text string (the number of characters in a text string).The syntax of the LEN function is as below:= LEN(text)…
Leave a Reply
You must be logged in to post a comment.