Calculate values across the sheets in a workbook.

llbac

New Member
Joined
Jul 20, 2023
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

Could anyone please tell me how to write a VBA to do this simple task (but not easy for me ^^!, sadly)?
I have a workbook that contains many sheets (maybe 30 sheets or more) which their names are quite random.
Then I will create a sheet named Master and calculate very simple formular like this:
Excel Formula:
=SheetA!A2*SheetA!B2+SheetB!A2*SheetB!B2+SheetC!A2*SheetC!B2..........

That is the sum of all the multiplication of two cells of two column (A & B) on the same row (e.g. A2 * B2), for all of the sheets available on the workbook.
All the sheets I want to calculate have the same structure: column A and B with number, start from row 2. Also, the number of rows is the same in every sheets.
This is the samples in XL2BB:
SheetA:
Fomular-test-3.xlsm
AB
1Title ATitle B
20.5375522780.3560159
30.7280599090.0117653
40.2319460940.8570487
50.3699685550.7705613
60.4512323070.9802114
SheetA

SheetB:
Fomular-test-3.xlsm
AB
1Title ATitle B
20.8014033280.398481
30.6547618920.415879
40.8878485630.7809355
50.0966875590.1760872
60.8971794860.115832
SheetB

SheetC:
Fomular-test-3.xlsm
AB
1Title ATitle B
20.933571770.5453227
30.4280931250.8806354
40.2195009150.6875302
50.3107672140.3542
60.0383726370.9443508
SheetC

The sheet Master I want to calculate from all other sheets:
Fomular-test-3.xlsm
A
1Across
21.019819025
30.657861573
41.043055101
50.41218262
60.582462382
Master
Cell Formulas
RangeFormula
A2:A6A2=SheetA!A2*SheetA!B2+SheetB!A2*SheetB!B2+SheetC!A2*SheetC!B2


The structure is simple but when typing for too many sheets, usually mistakes happened then it will be a huge problem for my final result.
The point here is that:
The code should calculate all available sheets without knowing sheets' names, and paste to the current active sheet (e.g. Master).
Really appreciate your help!
 
@llbac !!! In the above, post, #10, I inadvertently copied the code with which I tested the error handling, by getting lr form an empty column 4 !!!

Please correct by editing the line to reference column A, (1) ....
VBA Code:
 lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Here are my interpretation of your original post.
A variable number of randomly named sheets all contain similarly structured data in columns A & B.
There is one other non-data sheet that is named 'Master' (Edit code if this sheet name differs) .
Sheet 'Master' is to receive results.
If any of the above is not correct then please advise as the code will need to reflect your reality.

I can confirm that all conditions you mentioned are correct.
However, I got message: "Check that your first data sheet has data rows in column A"
I have copied and pasted data to another workbook with only 2 sheets to check, but the result is the same with that message.
Here is my real data in my example workbook (I just copied 7 rows in this example)

Book1
AB
1Column2Column22
293.74030.908611
394.46480.908611
4105.50860.908611
532.71530.908611
638.74660.908611
737.62110.908611
8-1.9290.908611
AhjfaweDb


Book1
AB
1Column2Column22
293.79130.829509
394.41130.829509
4105.46990.829509
532.80980.829509
638.74720.829509
737.58980.829509
8-1.82110.829509
Dxoawe98
 
Upvote 0
@llbac !!! In the above, post, #10, I inadvertently copied the code with which I tested the error handling, by getting lr form an empty column 4 !!!

Please correct by editing the line to reference column A, (1) ....
VBA Code:
 lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable

I have changed the code to 1, but the same message appeared :(

Hear is the code:

VBA Code:
Sub llbac ()
Dim arr As Variant, res As Variant
Dim lr As Long, mlr As Long, c As Long
Dim sht As Worksheet

For Each sht In ThisWorkbook.Sheets
    If Not sht.Name = "Master" Then  '<<< Edit Master if name differs
        If Not lr > 1 Then
        On Error GoTo BailOut  'controled exit sub without failing
            lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable
            If Not lr > 1 Then MsgBox "Check that your first data sheet has data rows in column A"   '<<<<check if lr is being set correctly??????"
            ' if no data or only header row lr <= 1 then the following will error
            ReDim arr(1 To lr - 1, 1 To 1)
            ReDim res(1 To lr - 1)
        End If
    arr = sht.Range("A2:B" & lr).Value
        For c = 1 To lr - 1
            res(c) = res(c) + (arr(c, 1) * arr(c, 2))
        Next c
    End If
Next sht
With Sheets("Master")
    mlr = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A2:A" & mlr).ClearContents
    .Range("A2:A" & lr).Value = Application.Transpose(res)
End With
BailOut:
On Error GoTo 0  'reset default error handling
End Sub
 
Upvote 0
I'm a bit baffled.
Running that code on that data here resets in the below result
Book3
A
1My Result
2162.974184
3164.146768
4183.354493
556.941502
667.3467336
765.3640183
8-3.2633293
9
Master


Can you add a line to message the value you are getting for lr and see what it is?
It should of course equal the last row of your data.

VBA Code:
Sub llbac ()
Dim arr As Variant, res As Variant
Dim lr As Long, mlr As Long, c As Long
Dim sht As Worksheet

For Each sht In ThisWorkbook.Sheets
    If Not sht.Name = "Master" Then  '<<< Edit Master if name differs
        If Not lr > 1 Then
        On Error GoTo BailOut  'controled exit sub without failing
            lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable

MsgBox lr  '**for diagnostics

            If Not lr > 1 Then MsgBox "Check that your first data sheet has data rows in column A"   '<<<<check if lr is being set correctly??????"
            ' if no data or only header row lr <= 1 then the following will error
            ReDim arr(1 To lr - 1, 1 To 1)
            ReDim res(1 To lr - 1)
        End If
    arr = sht.Range("A2:B" & lr).Value
        For c = 1 To lr - 1
            res(c) = res(c) + (arr(c, 1) * arr(c, 2))
        Next c
    End If
Next sht
With Sheets("Master")
    mlr = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A2:A" & mlr).ClearContents
    .Range("A2:A" & lr).Value = Application.Transpose(res)
End With
BailOut:
On Error GoTo 0  'reset default error handling
End Sub
 
Last edited:
Upvote 0
Solution
I'm a bit baffled.
Running that code on that data here resets in the below result
Book3
A
1My Result
2162.974184
3164.146768
4183.354493
556.941502
667.3467336
765.3640183
8-3.2633293
9
Master


Can you add a line to message the value you are getting for lr and see what it is?
It should of course equal the last row of your data.

VBA Code:
Sub llbac ()
Dim arr As Variant, res As Variant
Dim lr As Long, mlr As Long, c As Long
Dim sht As Worksheet

For Each sht In ThisWorkbook.Sheets
    If Not sht.Name = "Master" Then  '<<< Edit Master if name differs
        If Not lr > 1 Then
        On Error GoTo BailOut  'controled exit sub without failing
            lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable

MsgBox lr  '**for diagnostics

            If Not lr > 1 Then MsgBox "Check that your first data sheet has data rows in column A"   '<<<<check if lr is being set correctly??????"
            ' if no data or only header row lr <= 1 then the following will error
            ReDim arr(1 To lr - 1, 1 To 1)
            ReDim res(1 To lr - 1)
        End If
    arr = sht.Range("A2:B" & lr).Value
        For c = 1 To lr - 1
            res(c) = res(c) + (arr(c, 1) * arr(c, 2))
        Next c
    End If
Next sht
With Sheets("Master")
    mlr = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A2:A" & mlr).ClearContents
    .Range("A2:A" & lr).Value = Application.Transpose(res)
End With
BailOut:
On Error GoTo 0  'reset default error handling
End Sub

Really, I am so confused :-(

The message show number 1
Here is the code:
VBA Code:
Sub cal_sum_NMR_Boltz()
Dim arr As Variant, res As Variant
Dim lr As Long, mlr As Long, c As Long
Dim sht As Worksheet

For Each sht In ThisWorkbook.Sheets
    If Not sht.Name = "Master" Then  '<<< Edit Master if name differs
        If Not lr > 1 Then
        On Error GoTo BailOut  'controled exit sub without failing
            lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable
            MsgBox lr '**for diagnostics
            If Not lr > 1 Then MsgBox "Check that your first data sheet has data rows in column A"   '<<<<check if lr is being set correctly??????"
            ' if no data or only header row lr <= 1 then the following will error
            ReDim arr(1 To lr - 1, 1 To 1)
            ReDim res(1 To lr - 1)
        End If
    arr = sht.Range("A2:B" & lr).Value
        For c = 1 To lr - 1
            res(c) = res(c) + (arr(c, 1) * arr(c, 2))
        Next c
    End If
Next sht
With Sheets("Master")
    mlr = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A2:A" & mlr).ClearContents
    .Range("A2:A" & lr).Value = Application.Transpose(res)
End With
BailOut:
On Error GoTo 0  'reset default error handling
End Sub
 
Upvote 0
Again your code works fine here and returns lr as 8 for that trial data.
I am baffled and it is now beyond my bedtime.
Is there something odd about the data you have in column A that is screwing up the return for lr ?
Maybe link me to the file you are testing on and I will take a look tomorrow.
 
Upvote 0
Again your code works fine here and returns lr as 8 for that trial data.
I am baffled and it is now beyond my bedtime.
Is there something odd about the data you have in column A that is screwing up the return for lr ?
Maybe link me to the file you are testing on and I will take a look tomorrow.
Yes, sure, here is the file I uploaded: Book1
Thank you very much and have a good night!
 
Upvote 0
See if setting lr to a value known to be in excess of your max likely data rows works.
It will / should, return 0 in any of the excess result rows.

Goog Night!!!!!!!

VBA Code:
Sub cal_sum_NMR_Boltz()
Dim arr As Variant, res As Variant
Dim lr As Long, mlr As Long, c As Long
Dim sht As Worksheet

For Each sht In ThisWorkbook.Sheets
    If Not sht.Name = "Master" Then  '<<< Edit Master if name differs
        If Not lr > 1 Then
        On Error GoTo BailOut  'controled exit sub without failing
        
        lr = 1000 '*** or edit to a value that is in excess of your max depth of data
        
            ''lr = sht.Cells(Rows.Count, 1).End(xlUp).Row      '<<< 1 = column A  Edit for another column if A not applicable
            ''MsgBox lr '**for diagnostics
            
            
            If Not lr > 1 Then MsgBox "Check that your first data sheet has data rows in column A"   '<<<<check if lr is being set correctly??????"
            ' if no data or only header row lr <= 1 then the following will error
            ReDim arr(1 To lr - 1, 1 To 1)
            ReDim res(1 To lr - 1)
        End If
    arr = sht.Range("A2:B" & lr).Value
        For c = 1 To lr - 1
            res(c) = res(c) + (arr(c, 1) * arr(c, 2))
        Next c
    End If
Next sht
With Sheets("Master")
    mlr = .Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A2:A" & mlr).ClearContents
    .Range("A2:A" & lr).Value = Application.Transpose(res)
End With
BailOut:
On Error GoTo 0  'reset default error handling
End Sub
 
Upvote 0
See if setting lr to a value known to be in excess of your max likely data rows works.
It will / should, return 0 in any of the excess result rows.

It returns zero from the row 2 all the way down to 1000, only the first row is blank.
See you tomorrow!
 
Upvote 0
If you want to write sheet name dynamically, a common approach is used name manager of formula in the menu bar. Create a name which is sheetname, its formula is =GET.WORKBOOK(1)&T(NOW()). You should put Master on the first one, and then you can use the formula of this mini-sheet.

I read again your instruction, then I realized that I missed the step "create a name which is sheetname".
After trying again, it showed error "Blocked".

The file I used as an example was uploaded here: Book1
I attached all related errors images, hope it is clear. (I am sorry, it is really long page)

Error-071500.png
Error-071501.png
Error-071502.png
Error-071503.png
Error-071504.png
Error-071505.png
Error-071506.png
Error-071507.png
Error-071508.png
Error-071509.png
Error-071510.png
Error-071511.png
Error-071512.png
Error-071513.png
Error-071514.png
Error-071515.png
Error-071516.png
Error-071517.png
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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