Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

computing multiple sheets

Status
Not open for further replies.

Xception

Technical User
Jan 8, 2003
2
BE
I would like like to make a macro which computes multiple values spread over sheets kinda like this

Sheet 1 Sheet 2 Sheet 3
1 + 2 = 3

But you don't know how many sheets there are because you can add more sheets so how can i do this with a macro or maybe just a function?
 
Xception,

The following function will do what you want. It can be used as a worksheet function as well as called from VBA. It assumes that the total is computed using the same cell accross all worksheets. It takes two arguments, the cell to use in the calculation, and a boolean (TRUE/FALSE) telling the function whether to exclude the worksheet from which it is called.

Code:
Function SumAllSheets(ByVal InCell As Range, ByVal ExcludeCurrentSheet As Boolean)
Dim wks As Worksheet
Dim Sum As Variant

  SumAllSheets = CVErr(xlErrRef)
  If InCell.Count > 1 Then Exit Function
    
  If ExcludeCurrentSheet Then
    For Each wks In ThisWorkbook.Worksheets
      If wks.Name <> InCell.Parent.Name Then
        Sum = Sum + wks.Range(InCell.Address)
      End If
    Next wks
  Else
    For Each wks In ThisWorkbook.Worksheets
      Sum = Sum + wks.Range(InCell.Address)
    Next wks
  End If
  SumAllSheets = Sum
  
End Function


HTH
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top