How to Save an Excel Worksheet as a Numbered CSV on Mac Using VBA
Question details
The user needs a VBA macro on macOS to export a single worksheet from an XLSM workbook and save it as sequentially numbered CSV files.

- Product
- Microsoft Excel
- Device & OS
- Mac / macOS
- Scenario
- Exporting active worksheet data to separate, auto-numbered CSV files (e.g., Test File 1.csv, Test File 2.csv) without modifying or overwriting the original XLSM source file.
- Observed behavior
- The VBA macro fails to save the CSV files due to incorrect Windows-style backslashes in the file path, mistakenly appending the .xlsm extension to the folder path, and macOS sandboxing blocking the application's file save permissions.
Verify that Microsoft Excel has been granted permission to access your target folder in the macOS System Settings, as Apple's strict sandboxing rules can block automated VBA save operations.
Correct the VBA Script for macOS Paths and File Copying
Update the VBA macro to use Mac-compatible forward slashes, isolate the worksheet in a new workbook, and dynamically check for existing files to generate the next sequential number.
Windows uses backslashes (\) for file paths, whereas macOS requires forward slashes (/). Using the wrong delimiter, or improperly including the .xlsm extension in a folder path string, will trigger an error.
Additionally, directly saving an active workbook as a CSV can overwrite the original file. To prevent this, the macro should copy the target worksheet to a temporary workbook before saving.
Use the command 'ActiveWorkbook.ActiveSheet.Copy' followed by 'Set newsheet = ActiveSheet'. This duplicates the worksheet into a brand new, unsaved workbook, protecting your original XLSM file.
To ensure your CSV file contains clean data without broken references, add the line 'newsheet.UsedRange.Value = newsheet.UsedRange.Value' to convert all formulas into static text.
Define your base directory using forward slashes and ensure you remove the .xlsm extension from the folder path. For example: filePath = "/Users/xxxx/Desktop/Testing Save As/".
Use a VBA loop utilizing the 'Dir' function to check if your desired filename (e.g., Test File 1.csv) already exists. Increment the number in your filename variable until 'Dir' returns an empty string, then use 'ActiveWorkbook.SaveAs' with 'FileFormat:=xlCSV'.

Grant Excel Access to Folders in macOS Settings
Resolve access errors caused by macOS sandboxing by manually allowing Microsoft Excel to write files to your Desktop or Documents folder.
A Powerful and Free Alternative for Managing Spreadsheets
If troubleshooting complex VBA syntax errors and navigating macOS sandboxing restrictions becomes tedious, consider switching to WPS Office. It provides a highly compatible, feature-rich, and entirely free alternative for all your spreadsheet needs.
- 1. Download WPS Office: Visit the official WPS website and download the macOS version.
- 2. Install the Software: Open the downloaded package and drag the WPS Office icon to your Applications folder.
- 3. Open Your Spreadsheets: Launch WPS Office and directly open your existing Excel workbooks to effortlessly edit and export them as CSV files.

Frequently Asked Questions
Why do I get a file path error when running my Excel VBA macro on Mac?
Macs use forward slashes (/) for directory paths, whereas Windows uses backward slashes (\). If your VBA macro was written on a PC, you must update the path delimiters to forward slashes. Additionally, ensure you haven't accidentally included a file extension like .xlsm in your directory folder string.
How do I prevent VBA from modifying my original workbook when saving as CSV?
Instead of saving the active workbook directly, use the command 'ActiveWorkbook.ActiveSheet.Copy' to duplicate the worksheet into a new, temporary workbook. You can then safely save this new workbook as a CSV and close it without affecting your original source file.
Why does Excel prompt for folder access when saving a file via VBA on Mac?
macOS utilizes a strict sandboxing security protocol that restricts applications from interacting with the file system without explicit user consent. You must manually grant Excel permission to access your destination folder (e.g., Desktop or Documents) via your Mac's Privacy & Security settings.
How do I generate sequential numbers for my CSV filenames in VBA?
You can append a counter variable to your filename string (e.g., 'Test File ' & i & '.csv'). Use a 'Do While' loop coupled with the VBA 'Dir' function to check if the file currently exists. If it does, increment the counter variable 'i' by 1 until the 'Dir' function returns an empty string, indicating an available filename.




