Microsoft KB Archive/104412

{|
 * width="100%"|

-

The information in this article applies to:


 * Microsoft Excel for Windows, versions 3.0, 4.0, 4.0a, 5.0, 5.0c
 * Microsoft Excel for Windows 95, versions 7.0, 7.0a
 * Microsoft Excel 97 for Windows
 * Microsoft Excel for OS/2, version 3.0
 * Microsoft Excel for the Macintosh, versions 3.0, 4.0, 5.0, 5.0a
 * Microsoft Excel 98 Macintosh Edition

-

SUMMARY
In Microsoft Excel, you can count the number of cells in a worksheet that contain formulas, text, or numbers, by using the Go To Special dialog box (versions 5.0 and later) or the Select Special dialog box (versions 4.0 and earlier) to select the cells and then running a macro that counts the number of selected cells.

MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

"http://www.microsoft.com/support/supportnet/overview/overview.asp"

Selecting the Cells
Follow the appropriate procedure below to select formulas, text, or numbers.

Microsoft Excel Versions 5.0 and Later:
 * 1) On the Edit menu, click Go To. Click the Special button.
 * 2) In the Go To Special dialog box, to select formulas, select the Formulas option and select the desired check boxes. To select text, select the Constants option and select the Text check box. To select numbers, select the Constants option and select the Numbers check box.

Microsoft Excel Versions 4.0 and Earlier:
 * 1) On the Formula menu, click Select Special.
 * 2) In the Select Special dialog box, to select formulas, select the Formulas option and select the desired check boxes. To select text, select the Constants option and select the Text check box. To select numbers, select the Constants option and select the Numbers check box.

Running a Macro to Count the Selected Cells
Visual Basic Procedure for Versions 5.0 and Later:

The following procedure counts the number of cells in the selection and displays the result in a message box:   Sub Count_Selection Dim cell As Object Dim count As Integer count = 0 For Each cell In Selection count = count + 1 Next cell MsgBox count & " item(s) selected" End Sub Microsoft Excel Macro for Versions 4.0 and Earlier:

The following macro counts the number of cells selected and displays the result in an Alert dialog box:   A1: Count_Selection A2: Count=0 A3: =FOR.CELL("Current") A4: Count=Count+1 A5: =NEXT A6: =ALERT(Count&" item(s) selected") A7: =RETURN Following is an explanation of the preceding macro: A1: Contains the name of the macro.

A2: Initializes Count to 0.

A3: The FOR.CELL allows the macro to loop through each selected cell.

A4: Adds 1 to the current count.

A5: Completes the FOR.CELL loop.

A6: Displays an Alert Box with the count results.

A7: Ends the macro.