XL2000: How to Programmatically Subtract or Sum Two Ranges of Data (213337)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213337

SUMMARY

In Microsoft Excel 2000, you can use a Microsoft Visual Basic for Applications macro to programmatically subtract or sum two ranges of data. You can do this in the same workbook, or from more than one workbook, using defined names.

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

Using One Worksheet

To subtract or add values in one column from or to another column, and have the results appear in a third column, follow these steps:
  1. Start Excel 2000, and then create the following worksheet:
       A1:    1    B1:    11
       A2:    2    B2:    12
       A3:    3    B3:    13
       A4:    4    B4:    14
       A5:    5    B5:    15
       A6:    6    B6:    16
       A7:    7    B7:    17
       A8:    8    B8:    18
       A9:    9    B9:    19
       A10:  10    B10:   20
    					
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. In the module sheet, type the following code:
    Sub MyTotal()
        Dim CurCell As Object
        For Each CurCell In Range("C1:C10")
            CurCell.Value = CurCell.Offset(0, -2).Value _
                - CurCell.Offset(0, -1).Value
        Next
    End Sub
    					
  5. Press ALT+F11 to return to Excel 2000.
  6. Press ALT+F8 to open the Macro dialog box.
  7. In the Macro name box, click MyTotal, and then click Run. The macro returns the following values:
       C1:    -10
       C2:    -10
       C3:    -10
       C4:    -10
       C5:    -10
       C6:    -10
       C7:    -10
       C8:    -10
       C9:    -10
       C10:   -10
    					
NOTE: You can use the same code to add the columns. To do so, change the minus sign to a plus sign in the CurCell.Value line, as follows:
+ CurCell.Offset(0, -1).Value
				

Using Two Workbooks

To subtract or add from different workbooks in Excel, follow these steps:
  1. In Book1, create the following worksheet:
       A1:    1
       A2:    2
       A3:    3
       A4:    4
       A5:    5
       A6:    6
       A7:    7
       A8:    8
       A9:    9
       A10:  10
    					
  2. Press CTRL+N to open a second workbook (Book2), and then create the following worksheet:
       A1:    11
       A2:    12
       A3:    13
       A4:    14
       A5:    15
       A6:    16
       A7:    17
       A8:    18
       A9:    19
       A10:   20
    					
  3. Press ALT+F11 to start the Visual Basic Editor.
  4. On the Insert menu, click Module.
  5. In the module sheet, type or paste the following code:
    Sub TotalData()
        Dim File1 As Object, File2 As Object, CurCell As Object
    
        Set File1 = Workbooks("Book1").Sheets("Sheet1").Range("A1:A10")
    
        Set File2 = Workbooks("Book2").Sheets("Sheet1").Range("A1:A10")
    
        For Each CurCell In Range("A1:A10")
    
            CurCell.Value = File1.Cells(CurCell.Row, 1).Value - _
                File2.Cells(CurCell.Row, 1).Value
    
        Next
    End Sub
    					
  6. Press ALT+F11 to return to Excel 2000.
  7. Press ALT+F8 to open the Macro dialog box.
  8. In the Macro name box, click TotalData, and then click Run. The macro returns the following values:
       A1:    -10
       A2:    -10
       A3:    -10
       A4:    -10
       A5:    -10
       A6:    -10
       A7:    -10
       A8:    -10
       A9:    -10
       A10:   -10
    					
NOTE: To add instead of subtract, change the minus sign to a plus sign in the CurCell.Value line, as follows:
CurCell.Value = File1.Cells(CurCell.Row, 1).Value +
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbinfo kbProgramming KB213337