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/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor 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;CNTACTMSUsing 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:
- 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
- Press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module.
- 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
- Press ALT+F11 to return to Excel 2000.
- Press ALT+F8 to open the Macro dialog box.
- 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:
- 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
- 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
- Press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module.
- 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
- Press ALT+F11 to return to Excel 2000.
- Press ALT+F8 to open the Macro dialog box.
- 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 +