Save As CSV

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
333
Office Version
  1. 2010
Platform
  1. Windows
Hi

I have a workbook with a macro, but I don't want the workbook overwritten.
Therefore if I press the save button OR close the workbook I always want it to Save As a CSV file format

Ideally I want to show the Save As dialog screen and prefix it with the my default file name.

I have added the code below to BeforeSave and BeforeClose, but I can't get it to work as mentioned above

Dim newFile As String, fName As String

gName = "FGM_"
fName = Range("A2").Value
newFile = gName & " " & fName & " " & Format$(Date, "dd-mm-yyyy")

Dim fileSaveName As Variant

fileSaveName = Application.GetSaveAsFilename(InitialFileName:=newFile, FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I'm pretty sure if you're using the GetSaveAsFileName command, you don't need a variable that is "equal" to its result. Instead, just write:

Code:
Application.GetSaveAsFilename InitialFileName:=newFile, FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As"
 
Upvote 0
In the end I got it to work using the below code. It creates a new CSV using my fixed naming convention and retains the original workbook. The only thing it doesn't give me is a choice as to where to save it, but as it saves it in the same folder as the original file, I'm happy.

Dim newFile As String, fName As String

gName = "FGM_"
fName = Range("A2").Value
newFile = gName & " " & fName & " " & Format$(Date, "dd-mm-yyyy")

ActiveWorkbook.SaveAs FileName:=newFile, FileFormat:=xlCSV, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
 
Upvote 0
Code:
Sub G()
    Dim newFile As String, fName As String, gName, fileSaveName
    gName = "FGM_"
    fName = Range("A2").Value
    newFile = gName & " " & fName & " " & Format$(Date, "dd-mm-yyyy")
    fileSaveName = Application.GetSaveAsFilename(newFile, "CSV (Comma delimited) (*.csv), *.csv")
    ThisWorkbook.SaveAs fileSaveName, xlCSV
End Sub
 
Upvote 0
You're welcome!
hi.gif
 
Upvote 0
In the end I got it to work using the below code. It creates a new CSV using my fixed naming convention and retains the original workbook. The only thing it doesn't give me is a choice as to where to save it, but as it saves it in the same folder as the original file, I'm happy.
You could use this
Code:
    Dim newFile As String, fName As String
 
    gName = "FGM_"
    fName = Range("A2").Value
    newFile = gName & " " & fName & " " & Format$(Date, "dd-mm-yyyy")

    Application.Dialogs(xlDialogSaveAs).Show newFile, xlCSV
 
Upvote 0

Forum statistics

Threads
1,216,037
Messages
6,128,440
Members
449,453
Latest member
jayeshw

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