You are currently offline, waiting for your internet to reconnect

How to sum a range of cells based on a number format in Excel

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q164317
SUMMARY
This article includes a sample Microsoft Visual Basic for Applicationscustom function that sums the values in a range of cells that areformatted with a specific custom number format.
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. The following example creates a user-defined function that sums valuesbased on a custom number format.

Preparing sample data

  1. Create a new workbook and type the following data:
    A1: 100
    A2: 5
    A3: 100
    A4: 5
    A5: 100
  2. Select cells A1, A3, and A5. To do this, press and hold down CTRL (or COMMAND on a Macintosh), and then click cells A1, A3, and A5.
  3. On the Format menu, click Cells. Click the Number tab.
  4. In the Category list, click Custom. In the Type box, type F0, and then click OK.
Note You can use different custom number formats with this example.

Creating the function in the versions of Excel that are listed in the "Applies To" section

  1. Press ALT+F11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. In the module sheet, type the following code:
          Function SumFormat(CellRange)         ' Loop through each cell in the range that is passed to this         ' function.         For Each Item In CellRange            ' Check to see if the cell is formatted as "F"0.            ' The additional quotation marks are necessary to look for            ' actual quotation marks in the format string.            If Item.NumberFormat = """F""0" Then               ' Add the cell value to the variable total.               total = total + Item.Value            End If         Next Item         ' Set the results of total equal to the function name.         SumFormat = total      End Function						
  4. Use one of the following methods:
    • In Excel X for Mac and later versions, click Close and Return to Microsoft Excel on the Excel menu.
    • In all other versions of Excel, click Close and Return to Microsoft Excel on the File menu.
  5. Select cell A7.
  6. Type =SumFormat(A1:A5), and then press ENTER.

    Cell A7 contains the value 300 because cells A2 and A4 are not formatted with the custom number format of "F"0.

Creating the function in Microsoft Excel 5.0 and 7.0

  1. On the Insert menu, point to Macro, and then click Module.
  2. In the module sheet, type the following code:
          Function SumFormat(CellRange)         ' Loop through each cell in the range that is passed to this         ' function.         For Each Item In CellRange            ' Check to see if the cell is formatted as "F"0.            ' The additional quotation marks are necessary to look for            ' actual quotation marks in the format string.            If Item.NumberFormat = """F""0" Then               ' Add the cell value to the variable total.               total = total + Item.Value            End If         Next Item         ' Set the results of total equal to the function name.         SumFormat = total      End Function						
  3. Click Sheet1.
  4. Select cell A7.
  5. Type =SumFormat(A1:A5), and then press ENTER.

    Cell A7 contains the value 300 because cells A2 and A4 are not formatted with the custom number format of "F"0.
REFERENCES

Excel 2002 for Windows and later versions

For more information about custom number formats, click Microsoft Excel Help on the Help menu, type custom format codes, click Search, and then click a topic to view it.

Excel X for Mac and later versions

For more information about custom number formats, click Excel Help on the Help menu, type number format codes, click Search, and then click a topic to view it.
XL XL2000 XL2002 XL2003 XL98 XL2001 XLX XL2004
Properties

Article ID: 164317 - Last Review: 01/19/2007 23:01:52 - Revision: 4.2

  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 2004 for Mac
  • Microsoft Excel 2001 for Mac
  • Microsoft Excel X for Mac
  • Microsoft Excel 98 for Macintosh
  • kbdtacode kbhowto kbprogramming KB164317
Feedback