VBA Save File To New Folder

Gian624

Board Regular
Joined
Jul 23, 2009
Messages
92
Hi Everyone,

I'm working on a project where I need to open a spreadsheet with a macro, run a macro in the spreadsheet and save the spreadsheet to a new folder with a slightly modified name. My string of code in VBA reads:

ActiveWorkbook.SaveAs Filename:=Location & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

Location is defined as the folder path I need to save to which is fine
Left... just takes the last 15 characters off the file name and changes it to the newDate

When I run the macro, everything works find but it saves the spreadsheet in the current folder and adds the path where I want to save it to the file name at the front. I need to modify the code so it saves it to the Location and saves the file name as the Left...

Thanks for any help,
Matt
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Everyone,

I'm working on a project where I need to open a spreadsheet with a macro, run a macro in the spreadsheet and save the spreadsheet to a new folder with a slightly modified name. My string of code in VBA reads:

ActiveWorkbook.SaveAs Filename:=Location & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

Location is defined as the folder path I need to save to which is fine
Left... just takes the last 15 characters off the file name and changes it to the newDate

When I run the macro, everything works find but it saves the spreadsheet in the current folder and adds the path where I want to save it to the file name at the front. I need to modify the code so it saves it to the Location and saves the file name as the Left...

Thanks for any help,
Matt

you need to create the new folder.

Code:
MKdir location
ActiveWorkbook.SaveAs Filename:=Location & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

that should make the folder you have defined in your location variable and then save the file there.

thanks,
 
Upvote 0
Hi RCBricker,

I had previously created the new folder I wanted everything to copy to. I used cell B10 as the reference cell for "Location" where I pasted the folder extension to. I tried adding Mkdir like you showed but that did not do anything. What was that supposed to do?

Thanks,
Matt
 
Upvote 0
nothing if you have not defined a variable named location.

So you have the full path of the where you want the file to go in cell B10?

Code:
ActiveWorkbook.SaveAs Filename:=range("B10").value & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

if you have a path but it does not end in character "\" in cell b10 then use this

Code:
ActiveWorkbook.SaveAs Filename:=range("B10").value & "\" & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

if cell B10 is a named range for the sheet and named "Location" but the cell does not end in "\" then try this

Code:
ActiveWorkbook.SaveAs Filename:=location & "\" & Left(myFile, Len(myFile) - 15) & newDate & ".xlsm"

Just a note going forward. Please remember that none of us can see your spreadsheet. you stated location was defined, but no specific which led me to believe that it was a variable. use code tags and pasting your existing code in its entirety is a great help also.

thanks,
 
Upvote 0
Hi RCBricker,

That was the exact problem I was having... stupid little "\" missing. It works like a charm now.

Thanks again for all your help,
Matt
 
Upvote 0
I'm having a related issue with a Command button to save a file. The entire filename (including Path and Extension) is defined in a cell named FNAME. I get the error shown:

Private Sub CommandButton5_Click()
'
Dim FN As String
'
FN = Range("FNAME") [Error: Method 'Range' of object'_Worksheet' failed]
ActiveWorkbook.SaveAs FileName:=FN
End Sub

Can't seem to figure out what I'm doing wrong with the named range.
 
Upvote 0

Forum statistics

Threads
1,216,765
Messages
6,132,591
Members
449,737
Latest member
naes

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