VBA Print
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
What is Print in VBA Excel?
VBA Printout is nothing, but as usual, how we print in the regular worksheet. There is no difference in this. Using Excel VBA code, we can print the whole worksheet data. For example, we can print the workbook, charts, specified range, etc.
After all our hard work in presenting the report to the manager, we usually send emails. But sometimes, your manager needs a hard copy of your reports in the meeting. In those scenarios, you need to print the report you have in the spreadsheet. One of the reasons your manager needs a printout of the report could be a very large report to read on the computer. In a worksheet, you must be already familiar with printing the reports. This article will show you how to print using VBA coding. Follow this article for the next 15 minutes to learn how to print reports in VBA.
Syntax of VBA PrintOut in VBA Excel
Before we see the syntax, let me clarify this first. What do we print? We print ranges, charts, worksheets, and workbooks. So, the PrintOut () method is available with all these objectives.
: From which page of the printing has to start? It will be treated as from the first page if we do not supply any value.
: What should be the last page to print? If ignored, it will print till the last page.
: How many copies do you need to print?
: Would you like to see the print preview before proceeding to print? If yes, TRUE is the argument. If not, FALSE is the argument.
Examples of Print in VBA Excel
Below are the examples of Print in VBA Excel.
We have created dummy data for illustration purposes, as shown in the picture below.
Now, we need to print the report from A1 to D14. It is our range. Enter the range in the VBA code to access the PrintOut method.
Code:
Sub Print_Example1() Range ("A1:D14") End Sub
Now, access the PrintOut method.
Code:
Sub Print_Example1() Range("A1:D14").PrintOut End Sub
We are not touching any of the parameters. It is enough to print the selected range. If we run this code, it will print the range from A1 to D14 cell.
Parameters of Printout Method in VBA Excel
Now, we have copied and pasted the same data to use other parameters of the PrintOut method in VBA Excel.
When we want to print the entire sheet, we can refer to the entire sheet as Active Sheet. It is because it will cover the entire sheet in it.
-
Code to Print the Entire Worksheet.
Code:
Sub Print_Example1() ActiveSheet.UsedRange.PrintOut 'This will print the entire sheet used range. End Sub
-
Code to Refer the Sheet Name.
Code:
Sub Print_Example1() Sheets("Ex 1").UsedRange.PrintOut 'This will also print the entire used range of the sheet called Ex 1. End Sub
-
Code to Print all the Worksheets in the Workbook.
Code:
Sub Print_Example1() Worksheets.UsedRange.PrintOut 'This will also print the entire used range of all the sheet in the workbook. End Sub
-
Code to Print the Entire Workbook Data.
Code:
Sub Print_Example1() ThisWorkbook.UsedRange.PrintOut 'This will also print the entire used range of all the sheet in the workbook. End Sub
-
Code to Print Only the Selected Area.
Code:
Sub Print_Example1() Selection.PrintOut 'This will print only the selected range End Sub
How to use the Parameters of Print Out Method in Excel VBA?
Now, we will see how to use the parameters of the PrintOut method. As we told you, we expanded the data to use other properties.
For sure, this is not going to print on a single sheet. Select the range as A1 to S29.
Code:
Sub Print_Example2() Range ("A1:S29") End Sub
Now, select the PrintOut method.
Code:
Sub Print_Example2() Range("A1:S29").PrintOut End Sub
The first and second parameters are From & To, and what is the starting and ending pages position. It will print all the pages by default, so we do not touch this part. Next, we want to see the print preview so that we will choose the preview as TRUE.
Code:
Sub Print_Example2() Range("A1:S29").PrintOut Preview:=True End Sub
Now, we will run this code. First, we will see the print preview.
This is coming in 2 pages.
So first, we want to set up the page to come in a single sheet. Use the below code to set up the page to come in one sheet.
Code:
Sub Print_Example2() With Worksheets("Example 1").PageSetup .Zoom = False .FitToPagesTall = 2 .FitToPagesWide = 1 .Orientation = xlLandscape End With ActiveSheet.PrintOut Preview:=True End Sub
It will set up the page to print in one sheet and landscape mode. So, now the print preview will be like this.
Like this, we can use the VBA PrintOut method to print the things we want to print and play around with them.
Recommended Articles
This article is a guide to VBA Print Statement. Here, we discuss using Excel VBA code for printouts, examples, and step-by-step explanations. You can learn more about Excel VBA from the following articles: -