Step 1: Build the file name in a cell
Choose a spare cell for the name. It can sit outside the print area, so it doesn’t show on the PDF. In this guide it’s H1. Build the name with a formula from cells you already fill in. For example, if the invoice number is in B3, the customer in B5 and the date in E3:
="Invoice " & B3 & " - " & B5 & " - " & TEXT(E3, "yyyy-mm-dd")
That gives names like Invoice 1042 - Smith Plumbing - 2026-11-08. Writing the date as year-month-day keeps the PDFs in date order in the folder. It also avoids slashes, which can’t be used in file names.
Step 2: Set what goes on the page
Select the area that should appear on the PDF and choose Page Layout → Print Area → Set Print Area. Check File → Print once to make sure it fits on one page. If it doesn’t, set Page Layout → Width to 1 page. The macro uses these same settings.
Step 3: Add the macro
Save the workbook as Excel Macro-Enabled Workbook (.xlsm) first. Then press Alt+F11 (on a Mac, Tools → Macro → Visual Basic Editor), choose Insert → Module and paste this:
Option Explicit
Sub SaveSheetAsPDF()
Const NAME_CELL As String = "H1" ' the cell that holds the file name
Dim ws As Worksheet, baseName As String, folder As String
Dim fullPath As String, badChar As Variant
Set ws = ActiveSheet
If ThisWorkbook.Path = "" Then
MsgBox "Save this workbook first, so the PDF has a folder to go in."
Exit Sub
End If
baseName = Trim(CStr(ws.Range(NAME_CELL).Value))
If baseName = "" Then
MsgBox "Cell " & NAME_CELL & " is empty. Fill in the details first."
Exit Sub
End If
' Characters Windows and Mac don't allow in file names
For Each badChar In Array("\", "/", ":", "*", "?", """", "<", ">", "|")
baseName = Replace(baseName, badChar, "-")
Next badChar
folder = ThisWorkbook.Path & Application.PathSeparator
fullPath = folder & baseName & ".pdf"
If Dir(fullPath) <> "" Then
If MsgBox(baseName & ".pdf already exists. Replace it?", _
vbYesNo + vbQuestion) = vbNo Then Exit Sub
End If
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fullPath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
MsgBox "Saved " & baseName & ".pdf in" & vbNewLine & ThisWorkbook.Path
End Sub
If your name cell isn’t H1, change it on the Const NAME_CELL line.
Step 4: Add a button
- Go back to the sheet and choose Insert → Shapes. Draw a rectangle outside the print area and type
Save PDFon it. - Right-click the shape, choose Assign Macro, pick
SaveSheetAsPDFand click OK. - Click away from the shape, then click it once. The PDF appears in the same folder as the workbook.
Shapes outside the print area don’t show on the PDF. You can also run the macro from View → Macros if you’d rather not have a button.
Optional: save into a PDFs subfolder
To keep the PDFs apart from the workbook, replace the folder = ... line with these lines. They create a PDFs folder the first time:
folder = ThisWorkbook.Path & Application.PathSeparator & "PDFs"
If Dir(folder, vbDirectory) = "" Then MkDir folder
folder = folder & Application.PathSeparator
Common problems
| What you see | Likely cause |
|---|---|
An error about the file path, and the folder name starts with https:// | The workbook is open from OneDrive or SharePoint, so Excel reports a web address instead of a folder. Save a copy in a normal folder on your computer, or type a folder path in the folder = ... line, like "C:\Invoices\". |
| On a Mac, Excel asks to Grant Access to a folder | This is normal for Mac Excel the first time it writes to a folder. Click Select or Grant Access once. |
| The PDF has two or more pages | The print area is too wide or tall. Fix it in Step 2, then check File → Print. |
The name shows a number like 46334 instead of a date | The formula joins a raw date. Wrap the date in TEXT(..., "yyyy-mm-dd") as in Step 1. |
| The button does nothing | Macros are turned off. Close the file, right-click it in File Explorer, choose Properties, tick Unblock, then open it again and click Enable Content. |
What this doesn’t do
- It saves the sheet that’s open. To save a PDF for every row of a list in one go, such as one invoice per customer, the macro needs a loop that fills the template from each row. That’s a bigger macro.
- It doesn’t email the PDF. That depends on whether you use Outlook for Windows, new Outlook or Mac Mail, which all behave differently.
- It doesn’t work in Excel for the web or on iPad, because macros need desktop Excel.
Need one PDF per row, or an emailed invoice?
I build Excel VBA tools for small businesses: invoice and quote generators, a PDF for every row of a list, numbering that never repeats and buttons your team can use without touching the code. Every macro is commented and comes with a short how-to.