Create Dynamic File Name Using Named Range

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am working on a code and trying to insert a named range into the file name reference but I am getting an error "Constant Expression", not sure what I am doing wrong so maybe someone might spot it.

Code:
Sub CountFiles()


Dim xFolder     As String
Dim xPath       As String
Dim xCount      As Long
Dim xFiDialog   As FileDialog
Dim xFile       As String
Dim Year        As String
Dim Month       As String




Set Year = Sheets("Summary").Range("Cyear").Value
Set Month = Sheets("Lookups2").Range("FolderL").Value


xFolder = "\\Wb\acctg\shared\RPTOHP\FP&A\Budgeting & Forecasting\Monthly Management Reports\Testing\" & Year & "\" & Month & "\"

'Do something

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
xFolder is a legal name IF [year] and [month] both have a value.

if either is null, it will fail. But the error does not reflect this. Is this the error line?
put a breakpoint (f9) on the line and evaluate the 2 variables. (hover the cursor over the word after it stops on it)

if both are good values, press F8 to step thru the code.
 
Upvote 0
Can't see anything that would give that error, although you shouldn't be using Set for Strings.

Is that your entire code?
 
Upvote 0
So I tried your comments and added a (msgbox Year) to see if actual text would show up and I am getting my desired result "2018" without the quotes.

When I run the code I am getting the debug highlighting the reference Year and the compile error: "Constant Expression Required"

Not sure what to do now?

xFolder is a legal name IF [year] and [month] both have a value.

if either is null, it will fail. But the error does not reflect this. Is this the error line?
put a breakpoint (f9) on the line and evaluate the 2 variables. (hover the cursor over the word after it stops on it)

if both are good values, press F8 to step thru the code.
 
Upvote 0
the folder MUST exist beforehand.
if not:

MkDir xFolder

(note , to create the folder (the end name) , all the folders above it must exist too)

Code:
Public Sub MakeDir(ByVal pvDir)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(pvDir) Then FSO.CreateFolder pvDir     'MkDir pvDir

Set FSO = Nothing
End Sub
 
Last edited:
Upvote 0
This update fixed the code! thank you Ranman!

the folder MUST exist beforehand.
if not:

MkDir xFolder

(note , to create the folder (the end name) , all the folders above it must exist too)

Code:
Public Sub MakeDir(ByVal pvDir)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(pvDir) Then FSO.CreateFolder pvDir     'MkDir pvDir

Set FSO = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,191
Messages
6,123,553
Members
449,108
Latest member
rache47

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