Sum a cell value in one sheet with value in another sheet

Amir Wisal

New Member
Joined
Oct 25, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello to the best community i encountered ever,

I am just starting to learn VBA to do some analyses and to make my tasks way much easier.

Does anyone have VBA code that I could use in a module to sum a values from the same cell in multiple sheets (i.e. cell B3 in both sheets SDI and 001-SDI) into a final master sheet?

Both the sheets have the exact same text written in Column A (i am not sure whether this will be helpful) but in each sheet there are seven columns with numeric values. The way i want to sum them is to take B2 value from sheet1 (which is SDI in my case) and add B2 value from sheet2 (001-SDI) but i want sum of each cell (cell number will be same i.e B2, B3.... to last entry of the data) from both sheets and in the same manner i want to sum each cell in the row till last filled cell (i.e B2, C2, D2... to H7). it would be great if i somehow get the consolidated data in a different sheet with the sum of each cell mentioned against the text in column A.

For sample i am sharing two sheets, but in reality i have to consolidate more than 20 sheets of data in pairs (although all sheets are name in the same sequence i.e. ABC and 001-ABC and DEF and 001-DEF) and i want the results of both the sheets for every pair in a separate sheet combined i.e "ABC consolidated".

I would be really helpful if the VBA itself recognises the sheets names written in the same pattern and sum them to a master sheet (which will have the consolidated results of those two sheets i.e. either ABC, SDI or DEF) or if the code require me to enter in the names of the sheets (through a dialog box) I want to sum from and the destination master sheet with the final sum.

I am not sure whether i explained it properly but I am very new to VBA so any help would be greatly appreciated!


Thank you.


VBA Dummy Data.xlsx
ABCDEFGH
1001-SDI
2External26,05144,62545,8918,57544,26039,41910,744
3Interdivisional20,48515,51846,57839,20534,55920,55927,669
4Intergroup11,88721,84111,22820,37433,63642,61217,957
5DIRECT EXPENSES12,11247,47531,81913,09514,20337,30511,772
001-SDI


VBA Dummy Data.xlsx
ABCDEFGH
1SDI
2External3155760302944045989240814758138651
3Interdivisional2247133005492792018577272641119864
4Intergroup40263488541285912784234122900825697
5DIRECT EXPENSES956433914160794055393463199011418
SDI


VBA Dummy Data.xlsx
ABCDEFGH
1SDI Consolidated
2External57608506557533154564683418700049395
3Interdivisional42956485239585759390422864697047533
4Intergroup52150706952408733158570487162043654
5DIRECT EXPENSES21676813894789853648235496929523190
Desired results
Cell Formulas
RangeFormula
B2:H5B2='001-SDI'!B2+SDI!B2


 

Attachments

  • SDI.PNG
    SDI.PNG
    11.3 KB · Views: 20
  • 001-SDI.PNG
    001-SDI.PNG
    11.4 KB · Views: 18
  • Desired Results.PNG
    Desired Results.PNG
    10.5 KB · Views: 16

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Sum a cell value in one sheet with value in another sheet and post the result in a new consolidated sheet​


Hi All,

Some one has shared the below code which is working fine on the dummy data i shared above however in practical i have to use it on the below workbook. Can someone modify this code for me to suit my requirements. This code works fine if Column A data is fixed which is not my case. Usually in my case column A has data around 100 rows and it would be great if you somehow keep this as dynamic in the code.

Private Sub Worksheet_Activate()
Application.ScreenUpdating = False
t_sh = "Desired results"
With Sheets(t_sh)
y = .UsedRange.Rows(.UsedRange.Rows.Count).Row
x = .UsedRange.Columns(.UsedRange.Columns.Count).Column
If x < 2 Then x = 2
.Range(.Cells(1, 2), .Cells(y, x)).ClearContents
End With
For sh = 1 To Sheets.Count
If Sheets(sh).Name <> t_sh Then
Sheets(sh).UsedRange.Copy
Sheets(t_sh).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:=False, Transpose:=False
End If
Next sh
Application.ScreenUpdating = False
MsgBox "Refreshed", vbInformation, "Note"
End Sub

Key differences between the dummy data and the actual data are as follows.

I am attaching the actual sheet in which i would perform this. Key differences from my dummy data are listed as follows.
a. This spreadsheet has a lot of sheets in it. the one i want to consolidate are highlighted as green. The problem here is that the sheets are not named correctly this time for example, there are two sheets which i want to consolidate (i.e. IWT and IWT(2)). I want to perform the same operation for the rest of the sheets as well in pair e.g. IGR and IGR(2).
b. The cell A1 in both sheets have the same name i.e. in case of IWT, IWT is mentioned in cell A1 on both IWT and IWT(2).
c. The text in column A is dynamic and might not match on both sheets in some rows (which i can correct through Power Query) but what i need from your end is to sum the values irrespective of the names.
d. Can we please get rid of the msgbox "refreshed".
e. The sequence of both the sheets might not be adjacent to each other. i mean one might be at number 1 other might be at 8 which i want to consolidate.
f. Ignore all other sheets which does not have the 2nd pair.
g. FYI - There are some other macros in the sample file i am attaching and first sheet from the right (i.e. C1233) is fetching data from Power Query which is the source file.
h. Can we please somehow create a new sheet each time we consolidate two sheets and give them a proper name like in case of IWT and IWT(2) can we get the consolidated result on a new sheet called "IWT consolidated".

File can be accessed from the link below.



Thank you
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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