Print in VBA is very similar to the print in excel, when we have important data in excel or spreadsheets then the only way to have them safe is to save them to pdf or print them, for print we need to set up the print command in VBA first before using it, what this command does if prints or writes the data into another file.
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, we can print the workbook, charts, specified range, etc.
After all the hard work we have done to present the report to the manager we usually send emails. But in some cases in the meeting, your manager needs a hard copy of your reports, in those scenarios you need to print the report you have in the spreadsheet. One of the reasons your manager needs a print out of the report could be it is a very large report to read on the computer. In a worksheet, you must have already familiar with printing the reports. In this article, we 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, workbooks. So PrintOut () method is available with all these objectives.
[From]: From which page of the printing has to start. If we don’t supply any value it will treat as from the first page.
[To]: What should be the last page to print? If ignored it will print till the last page.
[Copies]: How many copies you need to print.
[Preview]: Would you like to see the print preview before proceed 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.
For illustration purposes, I have created dummy data as shown in the below picture.
Now we need to print the report from A1 to D14, this is my range. Enter the range in VBA code to access the PrintOut method.
Sub Print_Example1() Range ("A1:D14") End Sub
Now access the PrintOut method.
Sub Print_Example1() Range("A1:D14").PrintOut End Sub
I am not touching any of the parameters, this is enough to print the selected range. If I run this code it will print the range from A1 to D14 cell.
Parameters of Printout Method in VBA Excel
Now I 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 the entire sheet as Active Sheet, this will cover the entire sheet in it.
Code to Print the Entire Worksheet.
Sub Print_Example1() ActiveSheet.UsedRange.PrintOut 'This will print the entire sheet used range. End Sub
Code to Refer the Sheet Name.
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.
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.
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.
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 print out method. As I told I have expanded the data to use other properties.
For sure this is not going to print in the single sheet. Select the range as A1 to S29.
Sub Print_Example2() Range ("A1:S29") End Sub
Now select the print out method.
Sub Print_Example2() Range("A1:S29").PrintOut End Sub
First & second parameters are From & To, what is the starting & ending pages position. By default it will print all the pages, so I don’t touch this part. Now I want to see the print preview so I will choose Preview as TRUE.
Sub Print_Example2() Range("A1:S29").PrintOut Preview:=True End Sub
Now I will run this code, we will see the print preview.
This is coming in 2 pages.
So first I 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.
Sub Print_Example2() With Worksheets("Example 1").PageSetup .Zoom = False .FitToPagesTall = 2 .FitToPagesWide = 1 .Orientation = xlLandscape End With ActiveSheet.PrintOut Preview:=True End Sub
This will set up the page to print in one sheet as well to print in landscape mode. Now the print preview will be like this.
Like this, we can use VBA print out a method to print the things we wanted to print and play around with them.
This has been a Guide to VBA Print Statement. Here we discuss how to use Excel VBA Code for printout along with examples and step by step explanations. You can learn more about Excel VBA from the following articles