VBA & Macro Problems

Excel VBA: Let Users Choose a CSV File Name and Folder

Anum MushtaqAnum Mushtaq Nov 27, 2026 868 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic, then open the module containing your existing macro.

2
Replace the save logic

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

3
Run and test the macro

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.

Use GetSaveAsFilename to choose the CSV destination
CSV format limitations: SaveAs changes the active workbook to the CSV format. CSV retains only the active sheet's values and does not preserve VBA, formulas, formatting, or other workbook sheets. To preserve the original workbook, use File > Save a Copy before running the macro.
Free Microsoft Office alternative

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.

Save spreadsheet data as CSV through a familiar Save As workflowOpen and edit Microsoft Excel XLSX and XLS filesUse a lightweight interface familiar to Office usersMove compatible spreadsheet files between WPS Office and Microsoft Office
microsoft office alternative - wps office

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.