Save As, using a string variable in the file name

lsteffen

Board Regular
Joined
May 11, 2006
Messages
111
Hi again,

I am trying to save a txt file created from an excel file with a unique name each time the macro code is run. What I did was to make cell A1 on Sheet 2 = NOW() for a date/time stamp and formatted that cell so that there is no spaces between the date and time: mm/dd/yyyhh:mm:ss. However, my code will not allow me to rename the file created to have this unique filename. Here is that portion of my code.

'//Rename exported file with unique name for LT
Dim LTName As String
Workbooks("WagnerExport.xls").Activate
ActiveWorkbook.Sheets("Sheet2").Activate
Range("A1").Activate
LTName = ActiveCell.Value & "CON_608250_020911_154750.txt"
Workbooks.Open Filename:="C:\" & "CON_608250_020911_154750.txt"
ActiveWorkbook.SaveAs Filename:=LTName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="password", ReadOnlyRecommended:=True _
, CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True

Any help is greatly appreciated.

Thank you,
Liz
:rolleyes:
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You cannot include a ':' in the filename! Excel should be throwing an error but as you are using a macro you may not notice it.

remove the :'s and it should work, though there may be further bugs. That is just the first thing that occurred to me
 
Upvote 0
You don't need a cell. And you can't use colons in a filename. Just do sumpin' like:
Code:
LTName = format(now,"yyyymmdd hhmmss ") & "CON_608250_020911_154750.txt"
 
Upvote 0
I tried this: but it still doesn't rename the file or give me errors...

'//Rename exported file with unique name for LT
Dim LTName As String
LTName = Format(Now, "yyyymmddhhmmss") & "CON_608250_020911_154750.txt"
Workbooks.Open Filename:="C:\" & "CON_608250_020911_154750.txt"
Workbooks("CON_608250_020911_154750.txt").Activate
ActiveWorkbook.SaveAs Filename:=LTName, FileFormat:= _
xlNormal, Password:="", WriteResPassword:="password", ReadOnlyRecommended:=True _
, CreateBackup:=False
'Application.DisplayAlerts = False
ActiveWorkbook.Close
'Application.DisplayAlerts = True

Any other help???

Thanks again,
Liz
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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