Hi All,
I'm looking for VBA code that will export the current worksheet to a CSV file, save it in a pre-determind file path, but allow the user to enter a custom file name.
The code that I'm currently using (below), works completely fine for exporting and saving in the correct path, but the user is unable to specify a file name. Any ideas?
Sub CopyToCSV()
Dim MyPath As String
Dim MyFileName As String
Application.DisplayAlerts = False
'The path and file names:
MyPath = "MY PATH HERE"
MyFileName = "CSV_Export_" & Format(Date, "ddmmyy")
'Makes sure the path name ends with "\":
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
'Makes sure the filename ends with ".csv"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
'Copies the sheet to a new workbook:
Sheets("CURRENT WORKSHEET").Copy
'The new workbook becomes Activeworkbook:
With ActiveWorkbook
'Saves the new workbook to given folder / filename:
.SaveAs Filename:= _
MyPath & MyFileName, _
FileFormat:=xlCSV, _
CreateBackup:=False
'Closes the file
.Close False
Bookmarks