Save a text file using a variable as the file name

KiwiDave

New Member
Joined
Oct 15, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi. Total newbie. I can extract some data from my database, but I need to save it as a .csv file using a variable to make the filename unique (based on some of the content). I can do everything except insert the variable as teh file name. Can anyone assist please?

Here's the code in question: As you can see, the code always saves the file as "\AllData Test.CSV. I need the file name to be the variable called "NameFileAllData" & .csv

Application.DefaultFilePath = "C:\Users\owner\blah\blah\blah"

myFile = Application.DefaultFilePath & "\AllData Test.CSV"


Any help would be appreciated.

Many thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the Board!

Note that in VBA, everything between double-quotes is treated as literal text. So you want all variables to be outside of double-quotes/
So if your variable is named "NameFileAllData", then the your code should look something like this:
VBA Code:
myFile = Application.DefaultFilePath & "\" & NameFileAllData & ".CSV"
 
Upvote 0
Welcome to the Board!

Note that in VBA, everything between double-quotes is treated as literal text. So you want all variables to be outside of double-quotes/
So if your variable is named "NameFileAllData", then the your code should look something like this:
VBA Code:
myFile = Application.DefaultFilePath & "\" & NameFileAllData & ".CSV"
Thanks Joe. Now it just returns a file named .CSV
It didnt pick up the named range. ?
 
Upvote 0
It didnt pick up the named range. ?
That is new information. No where in your original post did you say that NameFileAllData was a named range. You said it was a variable.
Named ranges and variables are two totally different things altogether!

So, is NameFillAllData really a named range on your Excel sheet?
If so, is it EXACTLY a single cell named range (and not covering multiple cells)?

If it is, then the code should look like:
VBA Code:
myFile = Application.DefaultFilePath & "\" & Range("NameFileAllData").Value & ".CSV"
 
Upvote 0
Solution
Thank you very much Joe. That works perfectly. Sorry about not understanding the difference between named ranges and variables. I suspect it will be a long learning curve. I appreciate the help.
Regards
David
 
Upvote 0
No worries.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

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