Save file in new folder, without changing file name

BrianLearning

New Member
Joined
Jul 1, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I want to create a macro to save the file to the "July" folder in the Z drive, in a CSV format. Without changing the file name to something different.

I have the macro:

VBA Code:
Sub Save_To_Z()
'
' Save_To_Z Macro
'
' Keyboard Shortcut: Ctrl+Shift+Z
'
ChDir "Z:\July"
ActiveWorkbook.SaveAs FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
End Sub
Sub Macro2()
'
' Macro2 Macro
'
'
End Sub

Here's the problem. It doesn't save the file to the Z drive and the July folder. It just saves the files in the folder that the file is currently in.

I have found sites suggesting I should do the following:

VBA Code:
Sub Save_To_Z()
'
' Save_To_Z Macro
'
' Keyboard Shortcut: Ctrl+Shift+Z
'
ChDir "Z:\July"
ActiveWorkbook.SaveAs Filename:= _
"Z:\July\Ledger_Template_final.csv", _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
End Sub
Sub Macro2()
'
' Macro2 Macro
'
'
End Sub

This works, in that it saves the file in CSV format, to the July folder in the Z drive, with the filename "Ledger_Template_final." The problem is, I don't want the macro to specify the filename. The filename should be whatever the existing name of the file is. The reason is, the macro will be used on many different files that will be saved in the July folder. We can't save them all with the same file name.

Can anyone help me please?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try

VBA Code:
Sub Save_To_Z()
'
' Save_To_Z Macro
'
' Keyboard Shortcut: Ctrl+Shift+Z
'
Location = "Z:\July\"
ActiveWorkbook.SaveAs Filename:= _
Location & ActiveWorkbook.Name , _
FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
End Sub
 
Upvote 0
That gets me most of the way there, but not completely. The code that you provided saves the document to the correct folder and with the filename unchanged (YES! THANK YOU!), but it doesn't save it as a .CSV file format. The saved document is still in .XLSM format.

I don't understand why, because our VBA code does contain:

VBA Code:
FileFormat:=xlCSV
 
Upvote 0
Sorry, missed that.

Try:

VBA Code:
Sub Save_To_Z()

Dim fso As New Scripting.FileSystemObject

Location = "Z:\July\"
ActiveWorkbook.SaveAs Filename:= _
Location & fso.GetBaseName(ActiveWorkbook.Name), _
FileFormat:=xlCSV
ActiveWorkbook.Close
End Sub

You'll need to add the "Microsoft Scrip[ting Runtime" object in Tools>References

1593726854780.png
 
Upvote 0
I added "Microsoft Scripting Runtime."

When I ran the macro, I got the message: "Run-time error ‘424’
Object required."

I clicked the "Debug" button. It highlighted the lines:

VBA Code:
ActiveWorkbook.SaveAs Filename:= _
Location & fso.GetBaseName(ActiveWorkbook.Name), _
FileFormat:=xlCSV

And it had a yellow arrow pointing at the last line:

VBA Code:
FileFormat:=xlCSV

Do you have any idea what I might be missing?
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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