目次
Background
I often encounter situations where I need to print specific Excel files regularly.
Thinking, “It would be convenient to print with a single click without opening the print settings every time,” I wrote a VBA macro to automatically print “Sheet1”.
Specifications After Execution
When this macro is executed, it performs the following:
- Automatically prints “Sheet1” in the Excel workbook.
- Sets the print orientation to Portrait and scales the content to fit on one page.
- Displays a message box upon completion.
VBA Code
Below is the macro I actually use.
Sub PrintOrderList()
Dim ws As Worksheet
' Specify the sheet to print
Set ws = ThisWorkbook.Sheets("Sheet1")
' Print Settings
With ws.PageSetup
.Orientation = xlPortrait ' Print Portrait
.Zoom = False ' Disable manual scaling
.FitToPagesWide = 1 ' Fit to 1 page wide
.FitToPagesTall = 1 ' Fit to 1 page tall
End With
' Execute Print
ws.PrintOut
MsgBox "Sheet1 has been printed."
End Sub
Tips for Customization
- Change Sheet Name: By changing
"Sheet1"to another sheet name, you can set any sheet as the print target. - Page Layout: You can support landscape printing or multi-page printing by modifying the properties within
PageSetup. - Print Preview: If you want to check the preview before printing, use
.PrintPreviewinstead of.PrintOut.
ws.PrintPreview ' Change to this if you want to see the preview first
Summary
- You can create a simple macro in VBA to automatically print Excel sheets.
- It helps improve efficiency by eliminating the need to adjust print settings every time.
- It can be adapted to handle multiple sheets or specific print ranges.
Being able to execute routine printing tasks with a single click is very convenient for daily work. Please give it a try.
