Creating variables for file path names VBA

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
160
I am writing coding for a rather large macro and I am trying to decrease the number of characters I use. One way to do this is to define a variable for the long file path name but it is not liking it.

My coding is:

' Ask user for the period of the projection exercise
PNumber = InputBox("What period is it?" & vbNewLine & vbNewLine & "Please enter the number:")

'Define variables
FNTemplate = "J:\FinanceAdmin\Finance\Resource Management\Regional\2019-20\Templates\RPA Zone Roll Up"
FNProjection = "J:\FinanceAdmin\Finance\Resource Management\Regional\2019-20\Projections\P" & PNumber & "\Variances\RPA Roll Up"


'Open all Branch Rollup templates
Workbooks.Open Filename:="'FNTemplate'\2020.PX.LPRA Zone Roll up.v1.RXM.xlsx"
Workbooks.Open Filename:="'FNTemplate'\2020.PX.ABSB Zone Roll up.v1.RXM.xlsx"
Workbooks.Open Filename:="'FNTemplate'\2020.PX.Appeals Zone Roll up.v1.RXM.xlsx"
Workbooks.Open Filename:="'FNTemplate'\2020.PX.CVB Zone Roll up.v1.RXM.xlsx"
Workbooks.Open Filename:="'FNTemplate'\2020.PX.DCP Zone Roll up.v1.RXM.xlsx"
Workbooks.Open Filename:="'FNTemplate'\2020.PX.ILBI Zone Roll up.v1.RXM.xlsx"

Am I wrong to assume you can define a variable as a file path?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Dim FPath as string

FPath = "FNTemplate"

Workbooks.Open Filename:=FPath & "\2020.PX.LPRA Zone Roll up.v1.RXM.xlsx"
 
Upvote 0
Try
Code:
Workbooks.Open Filename:=FNTemplate & "\2020.PX.LPRA Zone Roll up.v1.RXM.xlsx"
 
Upvote 0
You just need to build a string by concatenating your string variable and your file name together eg:

Code:
FNTemplate = "J:\FinanceAdmin\Finance\Resource Management\Regional\2019-20\Templates\RPA Zone Roll Up"
FN = "2020.PX.LPRA Zone Roll up.v1.RXM.xlsx"

Workbooks.Open Filename:=FNTemplate & "\" & FN
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Now I ran into a different problem but it is similar. I think it may have to do with my brackets as the file name has a data that will change each month:

Coding:

'Ask user for the period of the projection exercise
PNumber = InputBox("What period is it?" & vbNewLine & vbNewLine & "Please enter the number:")

'Define variables

Dim FNProjection As String
FNProjection = "\\S15ACFFP0001\GROUP-D\Finance\Resource Management\Regional\2019-20\Projections\P" & PNumber & "\Variances\RPA Roll Up"

'Save each template in the correct projection period folder

Windows("2020.PX.ILBI Zone Roll up.v1.RXM.xlsx").Activate
ActiveWorkbook.SaveAs Filename:=FNProjection & "\2020.P" & PNumber & ".ILBI Zone Roll up.v1.RXM.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close

Windows("2020.PX.DCP Zone Roll up.v1.RXM.xlsx").Activate
ActiveWorkbook.SaveAs Filename:=FNProjection & "\2020.P" & PNumber & ".DCP Zone Roll up.v1.RXM.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

ActiveWindow.Close
 
Upvote 0
What is the value of PNumber?
 
Upvote 0
Yes, but what is the value when the code fails?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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