UDF to sum corresponding cells on tabs (tabs are range argument)

mountainman88

Board Regular
Joined
Jun 22, 2019
Messages
109
Office Version
  1. 2016
Platform
  1. Windows
I would like some code to create a UDF which works as follows:

The UDF has one argument which is a range, the range can be any size and each cell in the range must contain a valid tab name in the same workbook (tab names may have spaces). The UDF then adds together the cells on each tab in the range with the same cell reference the UDF is entered into.

For example lets call the UDF SUMTAB. The SUMTAB UDF is entered into cell B3 as below, and the range argument contains 4 tab names (tab1, tab2 etc). The output would be the sum of cell b3 on tabs1-4.

Book3
AB
1
2
3=SUMTAB(A5:A8)
4
5tab1
6tab2
7tab3
8tab4
UDF


I would also like a 2nd version of the UDF called SUMTABRNG with an extra argument where the cell range that is summed can be specified:

See below the 1st argument is now a range a1:b3, this range will be what is summed across all tabs in the 2nd argument

Book3
ABC
1
2
3=SUMTABRNG("a1:b3",A5:A8)
4
5tab1
6tab2
7tab3
8tab4
UDF


Thanks in advance!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This is amazing thanks, the reason I wanted both is because I assume the 2nd one is more processor intensive than the first? Also if I want to just drag across and down the first one is easier because I don't have put in a dynamic reference to the cell the formula is in? WHat is the best way to put in a dynamic reference to that cell in the 2nd function?

thanks
You are welcome - thanks for the reply. Here's a dynamic reference to the cell the UDF is in for the first UDF:
VBA Code:
Function SUMTAB(R As Range) As Double
Dim c As Range, whichCell As String
whichCell = Application.ThisCell.Address(0, 0)
For Each c In R
    If Not SheetExists(c.Value) Then
        SUMTAB = CVErr(xlErrValue)
        Exit Function
    Else
        SUMTAB = SUMTAB + Evaluate("'" & c.Value & "'!" & whichCell)
    End If
Next c
End Function
Function SheetExists(shName As String) As Boolean
SheetExists = False
With ThisWorkbook
    For Each sh In .Sheets
        If sh.Name = shName Then
            SheetExists = True
            Exit For
        End If
     Next sh
End With
End Function
You can take the same approach for the 2nd UDF, but what exactly do you want to do with the address of the cell the UDF is in for the case where you have already entered the range to be summed in each tab?
 
Upvote 0
You are welcome - thanks for the reply. Here's a dynamic reference to the cell the UDF is in for the first UDF:
VBA Code:
Function SUMTAB(R As Range) As Double
Dim c As Range, whichCell As String
whichCell = Application.ThisCell.Address(0, 0)
For Each c In R
    If Not SheetExists(c.Value) Then
        SUMTAB = CVErr(xlErrValue)
        Exit Function
    Else
        SUMTAB = SUMTAB + Evaluate("'" & c.Value & "'!" & whichCell)
    End If
Next c
End Function
Function SheetExists(shName As String) As Boolean
SheetExists = False
With ThisWorkbook
    For Each sh In .Sheets
        If sh.Name = shName Then
            SheetExists = True
            Exit For
        End If
     Next sh
End With
End Function
You can take the same approach for the 2nd UDF, but what exactly do you want to do with the address of the cell the UDF is in for the case where you have already entered the range to be summed in each tab?
I meant a relative reference as the first argument when using the UDF. Because the argument is text, when you drag across and down it repeats the same text. Sorry for the confusion!
 
Upvote 0
I meant a relative reference as the first argument when using the UDF. Because the argument is text, when you drag across and down it repeats the same text. Sorry for the confusion!
Maybe my imagination is blunt tonight, but the best I can come up with is something like below. This UDF will sum a range for each tab in the tab list whose upper left-most cell on the tab is the cell that the UDF is entered in (on the UDF tab) and is Rws rows by Cols columns in size. This can be dragged down or across to change the Rws x Cols upper left-most cell. Note that absolute references must be used on the Tab List range (3rd argument for the UDF). As an example, if this UDF is entered in B2 of the UDF tab:
Excel Formula:
=SUMTABRNG1(4,3,$A$5:$A$8)
The sum range for each tab in the tab list ($A$5:$A$8) would be B2:D5 (12 cells on each tab). Dragging down and/or across changes the upper left-most cell of the 12-cell sum range.
VBA Code:
Function SUMTABRNG1(Rws As Long, Cols As Long, R As Range) As Double
Dim c As Range, whichCell As String, whatRange As String
whichCell = Application.ThisCell.Address(0, 0)
whatRange = Range(whichCell).Resize(Rws, Cols).Address(0, 0)
For Each c In R
    If Not SheetExists(c.Value) Then
        SUMTABRNG1 = CVErr(xlErrValue)
        Exit Function
    Else
        SUMTABRNG1 = SUMTABRNG1 + Evaluate("Sum(" & "'" & c.Value & "'!" & whatRange)
    End If
Next c
End Function
Function SheetExists(shName As String) As Boolean
SheetExists = False
With ThisWorkbook
    For Each sh In .Sheets
        If sh.Name = shName Then
            SheetExists = True
            Exit For
        End If
     Next sh
End With
End Function
 
Upvote 0
Solution
Joe could you possibly help me out on this one? Probably even simpler than the above request.


Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top