Excel VBA: Let Users Choose a CSV File Name and Folder
Question details
The user wants an Excel macro to prompt for both a CSV file name and destination folder before saving.
- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro currently uses an InputBox to obtain a name when saving a workbook as a CSV file.
- Observed behavior
- The desired behavior is a Save As dialog where the user can select a folder and file name, or cancel without saving.
Open the workbook in Excel and locate the macro that currently uses InputBox. Check which worksheet is active: CSV saves only one sheet and does not preserve workbook features or VBA code.
Use GetSaveAsFilename to choose the CSV destination
Replace the InputBox with Excel's Save As dialog so the user can select a folder and file name.
GetSaveAsFilename returns the selected path without saving the file. If the user cancels, it returns False. Workbook.SaveAs then saves the active workbook using the CSV format.
In Excel, open the Developer tab and select Visual Basic, then open the module containing your existing macro.
Replace the InputBox and existing save code with the following macro, or adapt its statements to your existing procedure: Sub SaveActiveWorkbookAsCSV() Dim wb As Workbook Dim savePath As Variant Set wb = ActiveWorkbook savePath = Application.GetSaveAsFilename( _ InitialFileName:="export.csv", _ FileFilter:="CSV Files (*.csv), *.csv", _ Title:="Save CSV As") If VarType(savePath) = vbBoolean Then Exit Sub If LCase$(Right$(CStr(savePath), 4)) <> ".csv" Then savePath = CStr(savePath) & ".csv" End If wb.SaveAs Filename:=CStr(savePath), FileFormat:=xlCSV End Sub
Return to Excel, select the worksheet to export, and run the macro from Developer > Macros. Choose a folder and file name in the dialog, then click Save. Run it again and click Cancel to verify that no save occurs.

Save spreadsheet data as CSV with WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use Save As to export CSV data; this does not fix the Excel VBA macro itself, and macro support can vary.

Frequently Asked Questions
Does GetSaveAsFilename save the CSV automatically?
No. Application.GetSaveAsFilename only returns the selected path. Call Workbook.SaveAs with FileFormat:=xlCSV to save the file.
What happens if the user cancels the Save As dialog?
GetSaveAsFilename returns the Boolean value False. Check for that result and exit the procedure before calling SaveAs.
Which worksheet is exported to CSV?
Excel saves the active worksheet to CSV. Select the intended worksheet before running the macro; other sheets are not included in the CSV.
Will the CSV retain my formulas and VBA macros?
No. CSV stores the active sheet's values as text, not formulas, formatting, or VBA code. Use File > Save a Copy before exporting if you need to preserve the original workbook.




