» Add cells in a range from all worksheets in the workbook using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Custom Functions , Printing in VBA
VERSION - All Microsoft Excel Versions
but that can be a big task if the workbook contains many worksheets,
or if you frequently add new worksheets to the workbook.
To add a range from several worksheets you can use the SUM-function like this:
=SUM(Sheet1:Sheet10!A1:A100)
The formula above will add the range A1:A100 in the worksheets Sheet1 and Sheet10,
including all worksheets between the two sheets in the workbook.
With the custom function below it's easy to add a range from all the worksheets in a workbook:
Function SumAllWorksheets(InputRange As Range, InclAWS As Boolean) As Double
' adds the content of InputRange in all worksheets in the workbook
Dim ws As Worksheet, TempSum As Double
Application.Volatile True ' calculates every time a cell changes
TempSum = 0
For Each ws In ActiveWorkbook.Worksheets
If InclAWS Then ' include the active worksheet
TempSum = TempSum + _
Application.WorksheetFunction.Sum(ws.Range(InputRange.Address))
Else ' exclude the active worksheet
If ws.Name <> ActiveSheet.Name Then ' not the active worksheet
TempSum = TempSum + _
Application.WorksheetFunction.Sum(ws.Range(InputRange.Address))
End If
End If
Next ws
Set ws = Nothing
SumAllWorksheets = TempSum
End Function
The function is used in the same way as the built-in worksheetfunctions.
In Excel 5/95 the variable InputRange must be defined as Variant instead of Range.
The function can easily be adapted for use with other worksheetfunctions that you
want to use on all the worksheets in a workbook.
Book Store:
Recommended Books:
- Definitive Guide to Excel VBA
- Microsoft PowerPoint Version 2002 Step by Step
- Retire Young, Retire Rich
- The Basics of Finance: Financial Tools for Non Financial Managers
- H&R Block's Just Plain Smart(tm) Tax Planning Advisor: A year-round approach to lowering your taxes this year, next year and beyond
- A Mathematician Plays the Stock Market
No comments have been submitted.

