This article was previously published under Q148317
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article also applies to:
Microsoft Excel for Windows 95 7.0|7.0
Microsoft Excel for Windows 95 7.0a|7.0a
Microsoft Excel for Windows 5.0|5.0
Microsoft Excel for Windows 5.0c|5.0c
Microsoft Excel for the Macintosh 5.0|5.0
Microsoft Excel for the Macintosh 5.0a|5.0a
In Microsoft Excel, you can use a Microsoft Visual Basic for Applicationsmacro to sum or subtract two ranges of data and to then place the resultinto a third range without using formulas in the cells. You can do this inthe same workbook or from more than one workbook into a workbook usingdefined names.
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. There are several ways to perform the task of adding or subtractingcolumns of data together. You can use a For Each...Next loop in the same workbookto subtract values in one column from another column and have the resultsappear in a third column. For an example of how to do this using VisualBasic code, perform the following steps in Microsoft Excel:
Open a new worksheet, and in A1:A10, enter some random numbers.
In B1:B10, enter some random numbers.
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
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 changethe minus sign to a plus sign in the CurCell.Value line, as follows:
To add or subtract from different workbooks using Visual Basic code, usethe following steps:
Open a workbook (Book1), and in range A1:A10, insert random numbers.
In range A1:A10 of a second workbook (Book2), insert random numbers.
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