How do I make a VBA SUMIFS reference the right sheet?

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
788
Office Version
  1. 2010
Platform
  1. Windows
Hello hello hello,

I'm trying to replace a formula within a Macro to optimise my code a bit.


Previously, this was written and references a workbook called Capacity that was already opened previously in the code:


Code:
Range("Z3:Z" & Lastrow).FormulaR1C1 = "=SUMIFS(Capacity.csv!C3,Capacity.csv!C1,RC1)"

So what I've done in the name of optimisation is I have made a copy of Capacity within the workbook.


The trouble is that this formula is placed in another external workbook that's opened called "ads"


So ads is opened, and this formula is placed in cell Z:

=SUMIFS(C:C,A:A,A2)

It will sum cells C:C of Capacity based on a criteria range of A:A in Capacity, then the criteria is in cell A2 of ads


I need to replace this to show that C:C and A:A are in a tab called "CAP" in the original workbook.

What I've done is re-record the macro with the sumifs now looking in the right place, however it gives me this:

Code:
"=SUMIFS('[Reader Offer Portal - Dev.xlsm]CAP'!C3,'[Reader Offer Portal - Dev.xlsm]CAP'!C1,R[-1]C[-25])"


The trouble here is that this workbook name can change many times, it could be called "Reader Offer Portal - JG" or "Reader Offer Portal - K1" etc

I'd rather not use '[Reader Offer Portal*]' and I was wondering whether I can just get it to look in CAP instead. If I write the following:

Code:
"=SUMIFS('CAP'!C3,'CAP'!C1,R[-1]C[-25])"

It just looks to open a workbook called 'CAP'

Thanks.
 
Always nice when that happens. :biggrin:

Glad to help.

Yeah that was exactly what I was after. Knew it could be done because that's the way Excel works when you 'set' a variable, it will lock in to the sheet. I just had absolutely no idea how to name it. Thanks again.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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