Dynamic file path

srdr16

New Member
Joined
Jun 16, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have an issue with dynamic file names/paths within my spreadsheet vba.
I have a master file that does a countif against a file and path and worksheet that changes months and file names
I have set up Dim filepath, which picks up the file path, file name and worksheet from a worksheet on my spreadsheet
The vba runs to a point and gives me A:\mypath\mydata\[myfile.xlsx]MyWorksheet
This shows in the debug.print so I know it works
My original formula (that has a fixed location is)
"=COUNTIFS('A:\mypath\mydata\[my file.xlsx]MyWorksheet'!C7,RC11,''A:\mypath\mydata\[my file.xlsx]MyWorksheet'!C14,R2C)"
This works fine, however I can't get the variable location formula to work.

I've tried this
"=Countifs(""" & filepath & """ & ""!C7,RC11," & """ & filepath & """ & "!C14,R2C)"""""

As I've said, my original formula works fine, the file location appears on the debug.print the same as the original macro so I must just be formatting the new macro incorrectly.
Any help would be appreciated.
 
Try this
VBA Code:
Dim filepath As String
filepath = "A:\mypath\mydata\[myfile.xlsx]MyWorksheet"

Dim formula As String
formula = "=COUNTIFS('" & filepath & "'!C7,RC11,'" & filepath & "'!C14,R2C)"
Range("A1").Formula = formula
 
Upvote 0

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