[VBA] Sumifs where the sheet name is variable

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hi guys, I'm building a tool which opens two different workbooks which I DIM as Workbook called "Rep1" and "Rep2" in VBA.

What I need to do is reference them in a SUMIFS formula, like this:

VBA Code:
Range("C3:C" & Lastrow).FormulaR1C1 = "=SUMIFS('[Rep1.xlsx]1'!R7C7:R6833C7,'[Rep1.xlsx]1'!R7C3:R6833C3,RC[-2])"


Ignoring the fixed sum references for the minute, which I do know how to change, I can't remember how to reference the name. I know it's something like ' & Rep1.name & ' but I can't remember exactly what I replace and obviously the errors I get in VBA are just saying "This formula isn't right" lol.


If anyone can help jog my memory that would be super duper. Thanks.

EDIT:
Actually I've evidently also forgotten how to reference a bloody dynamic range too, it should be something like:
R7C7:R" & LastrowRep1 - 1 & "C7

Right?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:
VBA Code:
    Range("C3:C" & Lastrow).FormulaR1C1 = "=SUMIFS('[" & Rep1.Name & "]1'!R7C7:R6833C7,'[" & Rep1.Name & "]1'!R7C3:R6833C3,RC[-2])"
 
Upvote 1
Solution
Actually I've evidently also forgotten how to reference a bloody dynamic range too, it should be something like:

R7C7:R" & LastrowRep1 - 1 & "C7

Right?
 
Upvote 0
Try this:
VBA Code:
    Range("C3:C" & Lastrow).FormulaR1C1 = "=SUMIFS('[" & Rep1.Name & "]1'!R7C7:R6833C7,'[" & Rep1.Name & "]1'!R7C3:R6833C3,RC[-2])"
Yesss!!

It's the square brackets! The square brackets I was missing!
 
Upvote 0
Yesss!!

It's the square brackets! The square brackets I was missing!
You are welcome. The key is to look at what "Rep1.Name" returns (you can see this with a simple MsgBox command, i.e. "MsgBox Rep1.Name", and then replace that part in your original hard-coded formula with the variable. Since you can see the square brackets in your original hard-coded formula, but not in the "Rep1.Name" command, you know that you have to leave those in with the hard-coded part of the formula.
 
Upvote 0
You are welcome. The key is to look at what "Rep1.Name" returns (you can see this with a simple MsgBox command, i.e. "MsgBox Rep1.Name", and then replace that part in your original hard-coded formula with the variable. Since you can see the square brackets in your original hard-coded formula, but not in the "Rep1.Name" command, you know that you have to leave those in with the hard-coded part of the formula.

Hey

For some reason I am absolutely stumped at this.

Just says Application Defined or Object Defined error. From what I can see this should be working perfectly. The formula above it produces what I need, with the csv file referenced directly. Any help? Thanks.
 

Attachments

  • vba error.png
    vba error.png
    28.9 KB · Views: 10
Upvote 0
Is the "csv" file already open in the same instance of Excel?
What exactly is the value of "bnb.Name" at that point in time?
If you aren't sure, you can simply use:
VBA Code:
MsgBox bnb.Name
in your code.
 
Upvote 0
Is the "csv" file already open in the same instance of Excel?
What exactly is the value of "bnb.Name" at that point in time?
If you aren't sure, you can simply use:
VBA Code:
MsgBox bnb.Name
in your code.
Hi Joe

Yep, it's open. bnb.name when hovered over is exactly, precisely the name of the csv file that's open.
 
Upvote 0
If the line above the one in yellow works, try making it exactly the same format as that one.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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