Article ID: 148317
Article Last Modified on 10/11/2006
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
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:
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
Additional query words: 97 8.00 XL97 XL98 XL7 XL5 XL
Keywords: kbdtacode kbhowto kbprogramming KB148317