VBA - Insert Sumif formula into cell - formula contains references to different worksheets

MrSourApple

New Member
Joined
Oct 23, 2015
Messages
17
I am trying to have vba enter a sumif formula into cell E8 on the sheet named "EOC"

Based on the sumif formula (below) here is the information I have:
=sumif(range, criteria, sum range)


Range:
The range for the sumif formula is column AF of a particular sheet. The sheet however will change based on the value of the cell E19 on the sheet "Admin".
(i.e. If Admin!E19 is equal to "Passback" it will look at the worksheet "Passback", and if it is Official it will look at the worksheet "Official" --- there are many other options these are just examples)

Criteria:
The criteria for the sumif formula is if it equals cell D8 of the sheet EOC

Sum range:
The sum range is column AM of the same sheet mentioned above (based on cell E19 of the "Admin" tab).

Here is the code I have but I can't seem to get it to work:

Code:
Sheets("EOC").Range("H8").Formula = _
"=Sumif(Sheets(Sheets("Admin").Range("E19").Value).Range("AF:AF"),Sheets("EOC").Range("D8"),Sheets(Sheets("Admin").Range("E19").Value).Range("AM:AM"))"

Any ideas on how to get this to work? The formula is actually a lot longer and will be dragged down a number of cells but if I can get this part to work I will be able to get the rest as well.
 

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.
I figured it out with some trial and error. It was really difficult to figure out how to do an indirect formula using VBA but I got it (ended up using record macro and that worked out). I would still prefer another way but this way works for now.

Code:
Sheets("EOC").Activate


Range("H8").Select
ActiveCell.FormulaR1C1 = _
"=(SUMIF(INDIRECT(""'""&Admin!R1C5&""'!AF:AF""),'EOC'!RC[-4],INDIRECT(""'""&Admin!R1C5&""'!AM:AM""))-SUMIF(INDIRECT(""'""&Admin!R2C5&""'!AF:AF""),'EOC'!RC[-4],INDIRECT(""'""&Admin!R2C5&""'!AM:AM"")))/1000"
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,912
Members
449,132
Latest member
Rosie14

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