VBA’s "Save As Values" in Excel saves sheets as plain text files, removing formatting and leaving only raw data—useful for compatibility with databases, scripts, or further analysis. It’s the shortest way to export Excel ranges as clean CSV-like data: just call `Workbooks.SaveAs`, specify a .txt file, and set `FileFormat = xlTextWindows` or `xlCSV`. No extra steps are needed beyond opening the file in Excel and using the macro.
What You'll Need
- A Microsoft Excel workbook
- VBA editor (Excel’s built-in editor or Visual Studio Code with Excel add-in)
- Permissions to edit the workbook and create new files
- An output folder or drive where files can be saved
Step-by-Step
-
Open the VBA editor:
Press
Alt + F11to open the Visual Basic for Applications window. If prompted, enable macros for the workbook. Labels may vary slightly by Excel version (e.g., "Project Explorer" in older versions), but the shortcut remainsAlt + F11. - Insert a new module: In the VBA editor, right-click the project name in the Project Explorer panel and select Insert > Module. This opens a clean code window where you’ll paste the macro.
-
Paste this macro code:
Copy the following into the module:
Sub SaveAsValues() Dim wb As Workbook, ws As Worksheet Dim fileName As String Dim filePath As String Set wb = ThisWorkbook Set ws = wb.ActiveSheet 'Change to a specific sheet if needed 'Prompt for save location (optional) fileName = InputBox("Enter filename (without extension):", "Save as Values", "DataExport") If fileName = "" Then Exit Sub 'User cancelled filePath = Application.GetSaveAsFilename( InitialFileName:="C:\Path\to\output\folder\" & fileName & ".txt", _ FileFilter:="Text Files (*.txt),*.txt" ) If filePath = "False" Then Exit Sub 'Cancelled 'Save as plain text (no formatting) ws.Copy ActiveWorkbook.SaveAs FileName:=filePath, FileFormat:=xlTextWindows Application.DisplayAlerts = False 'Skip overwrite warning wb.Save Application.DisplayAlerts = True MsgBox "Data saved as plain values in " & filePath, vbInformationKey parameters:xlTextWindowsensures only raw values (numbers, text) are saved, no formulas or formatting.InputBoxlets you choose the filename dynamically.- Replace
C:\Path\to\output\folderwith your actual folder path.
-
Run the macro:
Press
F5or go to the Run > Run Sub/UserForm menu. If prompted, edit the macro file (not the workbook) to enable macros. - Verify the output: The macro creates a .txt file with all cell data in columns—no headers, no formatting. Open it in a text editor to confirm. Formulas, conditional formatting, and cell styles are stripped.
Common Mistakes
-
Forgetting to set the file format:
Always use
xlTextWindowsorxlCSV—other formats (e.g.,xlWorkbookText) include headers or binary data. This causes garbage characters or missing values. -
ActiveSheet vs. specific sheet:
Using
ws = wb.ActiveSheetexports the active sheet. To target a named sheet, replace it withSet ws = wb.Sheets("Sheet1")and ensure the sheet exists. -
Overwriting existing files:
The macro skips overwrite prompts via
Application.DisplayAlerts = False, but always check the folder for duplicates. Use a timestamp in filenames (e.g.,fileName & Format(Date, "yyyymmdd")) to avoid conflicts. -
Macros disabled:
If the macro runs but creates empty files, Excel may block macros. Enable them in:
- File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros.
- Or sign in with an account that has admin privileges.
Tips
-
Selective export:
Instead of copying the entire sheet, use a range:
ws.Range("A1:D100").Copy. This saves only the specified cells. -
Headers preserved:
To include headers (first row), add
ws.Range("A1").Copybefore the main range. Note: This requiresFileFormat = xlCSVfor proper column alignment. -
Multisheet export:
Loop through all sheets with:
For Each ws In wb.Sheets ws.Copy ws.SaveAs filePath & ws.Name & ".txt", xlTextWindows Next wsThis creates separate files per sheet. -
Encoding issues:
If files appear garbled, specify UTF-8 encoding in Excel’s "Save As" dialog under "Text File" options, or use
xlUTF8in older Excel versions. -
Automate with VBA:
Combine this macro with
Application.OnTimeto run daily backups:Application.OnTime Now + TimeValue("01:00:00"), "SaveAsValues"Schedule it via Task Scheduler (Windows) or Finder Automation (Mac).
Troubleshooting
-
Macro runs but file is empty:
Check the folder for:
- Corrupted permissions (right-click file > Properties > Security).
- Incorrect file extension (save as .txt, not .xlsx).
- ActiveSheet vs. wrong sheet reference.
Ctrl + Shift + Escto open Task Manager and kill Excel processes. -
Garbage characters in output:
The file may be corrupted from:
- Using the wrong format (try
xlCSVorxlTextWindows). - Unicode issues: Save as UTF-8 or ANSI in Excel’s "Save As" dialog.
ws.Range("A1").Value = "Test"—if it exports correctly, the issue is elsewhere. - Using the wrong format (try
-
Error "File already exists":
The macro skips prompts, but check the file exists first:
If Dir(filePath) <> "" Then MsgBox "File already exists. Overwrite?", vbYesNo + vbQuestion Else wb.SaveAs filePath, xlTextWindows End IfOr delete existing files manually before running. -
Macro crashes with "#1004":
This often means Excel can’t write to the folder. Try:
- Running as Administrator.
- Changing the folder to a writable location (e.g., desktop).
- Using `Dir` to verify the folder exists:
If Dir("C:\Path") = "" Then MsgBox "Folder does not exist!".
Frequently Asked Questions
Can I save a specific range instead of the entire sheet?
Yes. Replace the `Copy` line with:
ws.Range("A2:D100").Copy 'Only cells A2–D100
This exports only those values, ignoring empty rows.
How do I convert back to Excel?
Copy the .txt file into Excel’s "From Text" feature:- Go to Data > From Text/CSV.
- Browse the file and select the first column (default).
- Choose Delimited > Tab or Fixed Width.
- Load into a new sheet.
Why do headers disappear?
Plain text exports remove headers by default. To include them:- Use
xlCSVformat instead ofxlTextWindows. - Manually copy the header row to the top of the data range before exporting.
Can I automate this daily?
Yes. Add this to your macro:
Application.OnTime Now + TimeValue("00:00:00"), "SaveAsValues"
Run it once manually to schedule daily backups. Use Task Scheduler (Windows) or Automator (Mac) to trigger it at your preferred time.
What if my data has dates or special characters?
Dates export as numbers (e.g., 123456789). To preserve dates:- Convert dates to text: Format cells as Text before exporting.
- Use
xlCSVand specify Delimited > Comma in Excel’s "From Text" import.
How do I export multiple sheets at once?
Replace the macro with this loop:
For Each ws In ThisWorkbook.Sheets
ws.Copy
ws.SaveAs ThisWorkbook.Path & "\" & ws.Name & ".txt", xlTextWindows
Next ws
This creates a separate .txt file for each sheet in your workbook.