XL: How to Sum Ranges Using Visual Basic for Applications

Last reviewed: February 3, 1998
Article ID: Q148317

The information in this article applies to:
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a

SUMMARY

In Microsoft Excel, you can use a Microsoft Visual Basic for Applications macro to sum or subtract two ranges of data and to then place the result into a third range without using formulas in the cells. You can do this in the same workbook or from more than one workbook into a 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 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

There are several ways to perform the task of adding or subtracting columns of data together. You can use a For Each...Next loop in the same workbook to subtract values in one column from another column and have the results appear in a third column. For an example of how to do this using Visual Basic code, perform the following steps in Microsoft Excel:

  1. Open a new worksheet, and in A1:A10, enter some random numbers.

  2. In B1:B10, enter some random numbers.

  3. In a Visual Basic module, type the following:

          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
    
    

  4. Run the macro.

The results of the subtraction appear in C1:C10.

You can use the same code to add the columns. To do this, just change the minus sign to a plus sign in the CurCell.Value line, as follows:

   CurCell.Value = CurCell.Offset(0, -2).Value _
       + CurCell.Offset(0, -1).Value

To add or subtract from different workbooks using Visual Basic code, use the following steps:

  1. Open a workbook (Book1), and in range A1:A10, insert random numbers.

  2. In range A1:A10 of a second workbook (Book2), insert random numbers.

  3. On a Visual Basic module sheet, type the following:

          Sub TotalData()
              Dim File1 As Object, File2 As Object, CurCell As Object
    
              ' Where the first column of data is located in Book1 on Sheet1
              ' in range A1:A10.
              Set File1 = Workbooks("Book1").Sheets("Sheet1").Range("A1:A10")
    
              ' We are now dealing with the second column of data.
              Set File2 = Workbooks("Book2").Sheets("Sheet1").Range("A1:A10")
    
              For Each CurCell In Range("A1:A10")
    
                  ' To add instead of subtract, change the minus sign
                  ' to a plus sign.
                  CurCell.Value = File1.Cells(CurCell.Row, 1).Value - _
                      File2.Cells(CurCell.Row, 1).Value
    
              Next
          End Sub
    
    

  4. Run the macro.

REFERENCES

For more information about using Visual Basic for Applications, click the Contents tab in Microsoft Excel Help, and then double-click the "Getting Started with Visual Basic" topic.


Additional query words: 5.00 7.00 97 8.00 XL97 XL98 XL7 XL5
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH 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 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.