How to turn sum(first:last!cell) into VBA?

vmpage

Board Regular
Joined
Mar 6, 2014
Messages
53
I have searched and searched and can't seem to find anything that answers my question.

I have a master workbook that compiles files, then adds a "first" and "last" sheet. This part works great!

I have multiple cells that I want to sum across all the compiled sheets. I can do this manually with =sum(first:last!d11) (D11 i s the first cell that I need to sum).

I would like to set up a VBA that would sum the cells from the compiled sheets.

How would I accomplish this?

Thank you.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Im not really sure what the problem is here?
If you need o sum the same range across your sheets, why not just...
=sum(first:last!d11:J11)
 
Upvote 0
Im not really sure what the problem is here?
If you need o sum the same range across your sheets, why not just...
=sum(first:last!d11:J11)

The problem is because it's a dynamic book. When I compile the files the formula erases the first:last portion and leaves me with #Ref ! errors.
I was hoping that if there is a way to put that sum formula into a code, then I can assign a button and simply recalculate after each compile.

Thank you very much for the response. I hope my explanation makes sense!
 
Upvote 0
I just thought of something...
I think my problem is that my First and Last sheets are being inserted when I compile rather than being constant pages.

Sub Compile()
Path = "C:\Users\vanessa.page\Desktop\Safety\123456789"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop

Worksheets.Add(After:=Worksheets("Master")).Name = "First"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Last"
End Sub


if I have First and Last as constant sheets, would the formula work all the time? If so how would I modify the above so that the compile placed the sheets between first and last?

Thank you again for the help!
 
Upvote 0
Yep, adding First and Last as static sheets and adjusting the compile macro fixed it!

Thank you again for making me think!!
 
Upvote 0
The problem is because it's a dynamic book. When I compile the files the formula erases the first:last portion and leaves me with #Ref ! errors.
I was hoping that if there is a way to put that sum formula into a code, then I can assign a button and simply recalculate after each compile.

Thank you very much for the response. I hope my explanation makes sense!

So are you saying that compile removes sheets, or did I misunderstand you?

I have frequently added a sheet called Start (or First) and another sheet called Last, and pulled any sheets I need to be included, in between them. If this is not what you had/meant, perhaps consider doing that?
 
Upvote 0
So are you saying that compile removes sheets, or did I misunderstand you?

I have frequently added a sheet called Start (or First) and another sheet called Last, and pulled any sheets I need to be included, in between them. If this is not what you had/meant, perhaps consider doing that?

When I read your answer to myself the light bulb in my head went off. Once i redid the macro to insert the sheets between first and last the formula was fine.

Thank you again for the help. I do really appreciate it!
 
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,680
Members
449,328
Latest member
easperhe29

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