VBA & Macro Problems

Excel for Mac: Save Each Worksheet as a Separate PDF with VBA

Olivia MillerOlivia Miller Nov 25, 2026 868 views

Question details

The user needs a Mac-compatible VBA macro that saves each Excel worksheet as a separate PDF.

Save Each Excel Worksheet as a Separate PDF on Mac with VBA
Product
Microsoft Excel for Mac
Device & OS
macOS
Scenario
A Windows Excel macro is being adapted to export every worksheet in a workbook to an individual PDF on a Mac.
Observed behavior
The adapted macro enters an endless loop instead of completing the worksheet exports.
Before you start

Save the workbook containing the worksheets as a macro-enabled workbook (.xlsm) in a local Mac folder, and enable macros if prompted. Close any existing PDFs with the same worksheet names in that folder; the export may overwrite them.

Solution 1Recommended

Export each worksheet once using a fixed worksheet count

Use this Mac-oriented VBA loop when your adapted export macro keeps repeating worksheets or fails to finish.

The macro records the worksheet count before the loop and exports each worksheet by index. It saves PDFs beside the workbook containing the macro, so copy the code into the workbook you want to export.

1
Open the VBA editor

In Excel for Mac, open the workbook and choose Tools > Macro > Visual Basic Editor. In the editor, select the workbook and choose Insert > Module.

2
Add the export macro

Paste this code into the module: Sub ExportWorksheetsToPDF() Dim ws As Worksheet Dim folderPath As String Dim separator As String Dim sheetCount As Long Dim i As Long folderPath = ThisWorkbook.Path If Len(folderPath) = 0 Or LCase(Left(folderPath, 4)) = "http" Then MsgBox "Save this workbook in a local Mac folder first." Exit Sub End If If InStr(folderPath, "/") > 0 Then separator = "/" Else separator = ":" End If If Right(folderPath, 1) <> separator Then folderPath = folderPath & separator End If sheetCount = ThisWorkbook.Worksheets.Count On Error GoTo ExportError For i = 1 To sheetCount Set ws = ThisWorkbook.Worksheets(i) ws.ExportAsFixedFormat Type:=xlTypePDF, _ FileName:=folderPath & ws.Name & ".pdf" Next i MsgBox "Exported " & sheetCount & " worksheets to " & folderPath Exit Sub ExportError: MsgBox "PDF export stopped: " & Err.Description End Sub

3
Run and check the export

Return to Excel, choose Tools > Macro > Macros, select ExportWorksheetsToPDF, and click Run. Open the workbook's local folder in Finder and check the PDFs.

Export each worksheet once using a fixed worksheet count
If an export stops: Check the error message, confirm that Excel can write to the workbook's folder, and unhide any worksheet that cannot be exported. The loop assumes the workbook's worksheet collection does not change while it runs.
Free Microsoft Office alternative

Work with Excel workbooks in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and continue everyday spreadsheet work with an easy transition; it does not repair Excel for Mac VBA macros.

Work with worksheets in a familiar spreadsheet interfaceOpen and edit Microsoft Excel XLSX filesUse a free, lightweight office suiteExport spreadsheet content to PDF using WPS tools
microsoft office alternative - wps office

Frequently Asked Questions

Where does the macro save the PDFs?

It saves them in the local folder containing the workbook with the macro. Choose File > Save As in Excel to save that workbook locally before running it.

Why does the macro record the worksheet count first?

The Worksheets.Count value sets a fixed upper limit for the For loop, so the loop processes the worksheets by index rather than relying on a condition that might never be met.

Can I run the macro from a different workbook?

The code uses ThisWorkbook, which refers to the workbook containing the macro. Insert the module into the workbook you want to export, or modify the code to reference the intended workbook explicitly.

What should I do if Excel cannot save a PDF?

Check the error message, save the workbook in a local folder Excel can write to, and close any existing PDF with the same name. If a hidden worksheet fails to export, unhide it and try again.