XL: Counting Cells That Contain Formulas, Text, or Numbers

Last reviewed: February 2, 1998
Article ID: Q104412

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 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 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/refguide/default.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.

REFERENCES

"User's Guide 2," version 4.0, pages 203-204 "User's Guide," version 3.0, pages 79-80


Additional query words: 4.00 4.00a 5.00 7.00 8.00 XL98 XL97 XL7 XL5 XL4 XL3
Keywords : kbcode kbprg kbhowto PgmHowto
Version : WINDOWS:3.0,4.0,4.0a,5.0,5.0c,7.0,7.0a,97; MACINTOSH:3.0,4.0,5.0,5.0a,98; OS/2:3.0
Platform : MACINTOSH OS/2 WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.