VBA & Macro Problems

How to Save an Excel Worksheet as a Numbered CSV on Mac Using VBA

Chanuka GeekiyanageChanuka Geekiyanage Nov 10, 2026 868 views

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.

How to Save an Excel Worksheet as a Numbered CSV on Mac Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Copy the Active Sheet

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.

2
Convert Formulas to Values

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.

3
Set the macOS File Path

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/".

4
Implement Sequential Numbering

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'.

Correct the VBA Script for macOS Paths and File Copying
File Format Reminder: Make sure to explicitly state 'FileFormat:=xlCSV' (or xlCSVUTF8 for special characters) in your SaveAs method to ensure macOS Excel correctly encodes the text output.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the macOS version.
  2. 2. Install the Software: Open the downloaded package and drag the WPS Office icon to your Applications folder.
  3. 3. Open Your Spreadsheets: Launch WPS Office and directly open your existing Excel workbooks to effortlessly edit and export them as CSV files.
Completely free to use with a lightweight installationExcellent compatibility with Microsoft Excel formats (.xlsx, .xlsm, .csv)Intuitive, familiar interface ensures a zero-learning-curve transitionReliable and straightforward file management without complex OS barriers
microsoft office alternative - wps office

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.