In this article, we will learn How to Print a Table in Excel.
After you are done with your table, and you need a hard copy of the page. You can just Select a cell in the Table range. And then Click File -> Print -> and select Table option button in Print what section. But Excel will just print that. Here we learn how we can print any table in Excel.
To save a selected range in excel (say a receipt) as PDF in Excel using VBA use below syntax.
The Genetic Code Sub SaveRangeAsPDF() Range.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ "C:\Users\file_name", Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End Sub
The above code is a generic code to save a fixed range as a PDF document. When your record macro while you export excel to PDF, a similar code will appear.
Range: It can be a fixed range, a dynamic range or a selection. You need to define it.
Filename: This is the fully qualified name of pdf. Define it as a string.
The rest of the variables can be left as it is. I have explained them after example.
Example: Print Excel receipt as a PDF using VBA
Let's say you have to print and send receipts in Excel on a regular basis. In that case, you would like to have it automated. You wouldn't want to do the export excel to pdf over and over again. If you can just use a button to do this it would be helpful, isn't it?
Here, I have designed this receipt format. I want to print/save/export it as a pdf using the button "Create PDF".
This receipt covers the range "A2:L21". I have already set the print area.
To save on this excel range as PDF, we will use the above mentioned generic VBA code of exporting excel sheet to pdf. We will adjust the code as per our requirement.
Sub PrintSelectionToPDF() Dim invoiceRng As Range Dim pdfile As String 'Setting range to be printed Set invoiceRng = Range("A1:L21") 'setting file name with a time stamp. pdfile = "invoice" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf" 'setting the fulli qualified name. The resultent pdf will be saved where the main file exists. pdfile = ThisWorkbook.Path & strfile invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:= pdfile, _ Quality:= xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End Sub
Using The Code:
Copy the above code in your receipt file by opening the VBA editor ( use Alt+F11). Adjust the range and the file path (if you want to) in the code that you want to print. You can download the working file below.
Dim invoiceRng As Range Dim pdfile As String
The code is simple. First, we have created two variables. "InvoiceRng as Range" for the range/sheet that we want to save as pdf. strFile for the fully qualified filename of the resultant PDF.
Set invoiceRng = Range("A1:L21")
Since our printing range is fixed, we set the invoice range as Range("A1:L21").
pdfile = "invoice" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf" pdfile = ThisWorkbook.Path & strfile
In the above two lines, we first name the file with a timestamp and then add to the path of the main file. As a result, pdfile contains the fully qualified name of the resultant pdf file.
invoiceRng.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:= pdfile, _ Quality:= xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=True, _ OpenAfterPublish:=False
Finally, we use the ExpoortAsFixedFormat method of Range class to print the defined Excel range as PDF. We define type as xlTypePDF. Another choice is xlTypeXPS, which will save the selected range as XPS file.
We set the Filename as pdfile, which contains the string of the fully qualified name of the pdf file. You can write a hardcoded text here or customize it as per your need.
Next, we set the quality of the pdfile as xlQualityStandard. We have another choice as xlQualityMinimum.
Next, we set the IncludeDocProperties as True. It means that the resultant PDF will have the properties of a Document.
Then we set the IgnorePrintAreas as True. It means it will ignore any print area already set.
Finally, we set OpenAfterPublish as False. It means that the file you create will not open automatically. I set it false because I create 100s of files using a loop, and I don't want them to open. If you want to open the file after creating using excel, set it True.
Usage of printing selected range as pdf
One basic use is to print the selected range by just clicking on one button. But the best use is to use it in a loop where you need to create multiple receipts for different clients. Create a sub for filling data and then use this code to print the selected range in a loop.
Hope this article about How to Print a Table in Excel is explanatory. Find more articles on calculating values and related Excel formulas here. If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook. We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write to us at email@example.com.
Related Articles :
Print multiple selections on one sheet using VBA in Microsoft Excel | Select multiple ranges and print each range in a different sheet using VBA.
Print all workbooks in a folder using VBA in Microsoft Excel | Use this VBA code to print and save each excel workbook in a folder using.
Split Excel Sheet Into Multiple Files Based On Column Using VBA | This VBA code split excel sheet base on unique values in a specified column. Download the working file.
Turn Off Warning Messages Using VBA In Microsoft Excel 2016 | To turn off warning messages that interrupt the running VBA code, we use the Application class.
Add And Save New Workbook Using VBA In Microsoft Excel 2016 | To add and save workbooks using VBA we use Workbooks class. Workbooks.Add adds a new workbook easily, however...
Popular Articles :
How to use the IF Function in Excel : The IF statement in Excel checks the condition and returns a specific value if the condition is TRUE or returns another specific value if FALSE.
How to use the VLOOKUP Function in Excel : This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets.
How to use the SUMIF Function in Excel : This is another dashboard essential function. This helps you sum up values on specific conditions.
How to use the COUNTIF Function in Excel : Count values with conditions using this amazing function. You don't need to filter your data to count specific values. Countif function is essential to prepare your dashboard.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.