Microsoft KB Archive/213337

= XL2000: How to Programmatically Subtract or Sum Two Ranges of Data =

Article ID: 213337

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



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:

https://partner.microsoft.com/global/30000104

For 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;CNTACTMS

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:   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.</li>  In the Macro name box, click MyTotal, and then click Run. The macro returns the following values: <pre class="fixed_text">  C1:    -10 C2:   -10 C3:   -10 C4:   -10 C5:   -10 C6:   -10 C7:   -10 C8:   -10 C9:   -10 C10:  -10 </li></ol>

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: <ol>  In Book1, create the following worksheet: <pre class="fixed_text">  A1:    1 A2:   2 A3:   3 A4:   4 A5:   5 A6:   6 A7:   7 A8:   8 A9:   9 A10: 10 </li>  Press CTRL+N to open a second workbook (Book2), and then create the following worksheet: <pre class="fixed_text">  A1:    11 A2:   12 A3:   13 A4:   14 A5:   15 A6:   16 A7:   17 A8:   18 A9:   19 A10:  20 </li> Press ALT+F11 to start the Visual Basic Editor.</li> On the Insert menu, click Module.</li>  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 </li> Press ALT+F11 to return to Excel 2000.</li> Press ALT+F8 to open the Macro dialog box.</li>  In the Macro name box, click TotalData, and then click Run. The macro returns the following values: <pre class="fixed_text">  A1:    -10 A2:   -10 A3:   -10 A4:   -10 A5:   -10 A6:   -10 A7:   -10 A8:   -10 A9:   -10 A10:  -10 </li></ol>

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 +

Additional query words: xl2000

Keywords: kbdtacode kbhowto kbinfo kbprogramming KB213337

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.