Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Sum a Range of Cells Based on a Number Format


View products that this article applies to.

Summary

This article includes a sample Microsoft Visual Basic for Applications custom function that sums the values in a range of cells that are formatted with a specific custom number format.

↑ Back to the top


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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS The following example creates a user-defined function that sums values based 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. Press and hold down CTRL, and then click cells A1, A3, and A5 to select them.
  3. On the Format menu, click Cells and, in the Format Cells dialog box, click the Number tab.
  4. In the Category list, click Custom. In the Type box, type "F"0 (include the quotation marks), and then click OK.
NOTE: You can use different custom number formats with this example.

Creating the Function

  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. On the File menu, click Close and Return to Microsoft Excel.
  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.

↑ Back to the top


References

For more information about number formats, click Microsoft Excel Help on the Help menu, type about number formats in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB213728, kbprogramming, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213728
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 381