Before printing worksheets, it’s safe to check the print preview for the worksheets so that no errors remain and how they will look after printing. Excel VBA offers some quick and smart ways to do that. Hope this article will be a quick guide to display a print preview in Excel VBA.
How to Display Print Preview in Excel: 3 VBA Macros
To explore the methods, we’ll use the following dataset which represents some salesperson sales in different regions.
1. VBA Macro to Display Print Preview for the Active Sheet
Firstly, we’ll learn how to display a print preview for the current working active sheet.
- Right-click on the sheet title.
- Then select View Code from the context menu.
Soon after, a VBA window will appear. Or you can press Alt+F11 to open the VBA window directly.
- Later, type the following codes in it-
Sub PrintPreview_ActiveSheet() ActiveSheet.PrintPreview End Sub
- Then press the Run icon to run the codes and consequently you will get a Macros dialog box.
- At this moment, select the Macro Name as mentioned in the above codes.
- Finally, just press Run.
Soon after a Print Preview window will open up.
2. VBA Macro to Display Print Preview for Specific Worksheet in Excel
We can display a print preview for any specific sheet by mentioning its name in the VBA codes. You will learn that in this method. I have set the name ‘Specific Sheet’ for the worksheet used in this method.
- Open the VBA window by selecting View Code from the context menu after right-clicking on the sheet title.
- After that, write the following codes in the VBA window-
Sub PrintPreview_SpecificSheet() Worksheets("Specific Sheet").PrintPreview End Sub
- Press the Run icon and the Macros dialog box will appear.
- Select the specified Macro Name from the dialog box.
- Finally, just press Run.
Then you will get the Print Preview window like the image below.
Related Content: How to Print Specific Sheets Using VBA Macro in Excel
3. VBA Macro to Display Print Preview for the Selected Worksheets
In the previous methods, we learned to display a print preview for a single worksheet. In this method, you will learn to display a print preview for multiple worksheets at a time.
- Open the VBA window for this sheet, like the previous methods.
- Then write the following codes in the VBA window-
Sub PrintPreview_SelectedSheets() ActiveWindow.SelectedSheets.PrintPreview End Sub
- Press the Run icon to run the codes and open the Macros dialog box.
- Select the Macro Name as mentioned in the above codes and then just press Run.
Soon after you will get the print preview for the currently selected sheet only.
- Now to get the print preview for multiple sheets, close the preview and minimize the VBA window then just go back to your workbook and select the multiple sheets using your mouse by pressing and holding the Ctrl key on your keyboard.
- Then again go to that VBA window and now run the codes by clicking the Run icon.
- Select the same Macro Name again.
- Now have a look that the Print Preview window shows the preview of the first worksheet. To see the other previews just press Next Page.
It’s the preview of another worksheet.
Things to Remember
- The VBA operations can be operatable after making Macros for the source data.
- Make sure there are no spaces between the functions.
- VBA function can directly be accessed by Alt+F11.
- Make sure that you have clicked the same worksheet whose Print Preview you want before typing the Activesheet function in the VBA window.
Download Practice Workbook
You can download the free Excel template from here and practice on your own.
I hope the procedures described above will be well enough to display a print preview in Excel VBA. Feel free to ask any question in the comment section and please give me feedback.