Microsoft KB Archive/89794

= How to use Visual Basic for Applications to count the occurrences of a character in a selection in Excel =

Article ID: 89794

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 2004 for Mac
 * Microsoft Excel X for Mac
 * Microsoft Excel 2001 for Mac
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q89794



SUMMARY
In Microsoft Excel, you can use a macro to count the occurrences of a specific character in a cell or range of cells. This article contains a sample macro to count the occurrences of a specific character in a cell or range of cells.

You can also use a formula to accomplish the task. If you prefer not to use a macro, click the following article numbers to view the articles in the Microsoft Knowledge Base:

187667 Formulas to count the occurrences of text, characters, or words in Excel for Mac



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Sample Visual Basic procedure
The following Visual Basic procedure prompts you for a character (or characters), then searches through the currently selected cell, or range of cells, and displays a message box showing the total number of occurrences of that character or character string. This works for all alphanumeric characters. Dim Count As Integer Dim Target As String Dim Cell As Object Dim N As Integer

Sub Target_Count Count = 0 Target = InputBox(&quot;character(s) to find?&quot;) If Target = &quot;&quot; Then GoTo Done For Each Cell In Selection N = InStr(1, cell.Value, target) While N <> 0 Count = count + 1 N = InStr(n + 1, cell.Value, target) Wend Next Cell MsgBox count & &quot; Occurrences of &quot; & target Done: End Sub

