Create a Print button – macro
This article describes how to create a button and place it on an Excel worksheet, then assign a macro to that button allowing the user to print the worksheet when the button is pressed on.
Table of Contents
1. Create a Print button on a worksheet - macro
A print button located on a worksheet is a great feature that allows the user to quickly print a worksheet. You need a form control button and a macro to build a print button on a worksheet.
What is the biggest downside with a macro assigned to a button?
Most companies have macros disabled because of security reasons.
What is a macro?
A subroutine that contains lines of code made of VBA which is an abbreviation of visual basic of application.
Is it complicated to set up?
No, it is easy and you will learn something new that will be interesting.
1.1 Create a button and place it on the worksheet
The button is found in the Form Controls group, here are the steps:
- Press with left mouse button on "Developer tab" on the ribbon.
Developer tab missing? - Press with left mouse button on the "Insert" button. A popup menu appears containing multiple controls.
- Press the "Button (Form Control)" shown on the image above next to the blue arrow.
- Press and hold with mouse button on the worksheet where you want it located.
- Drag with mouse while holding the mouse button to adjust the button size.
- Release the mouse button.
- A dialog box shows up telling you to assign a macro. Press with left mouse button on "Cancel", we need to assign the macro after putting the code in a module explained below.
Change the button name to Print.
1.2 Create macro
You need the following code to be able to assign the macro to the button.
Sub Macro1() Application.Dialogs(xlDialogPrint).Show End Sub
1.3 Where to put the code?
(The macro shown in the image above is not used here in this article, it only demonstrates where to paste the code.)
- Press Alt-F11 to open the visual basic editor
- Press with mouse on "Module" on the "Insert" menu, see the image above.
- Copy and paste "Sub Macro1" code below to code module.
- Go back to Excel.
1.4 Assign the macro the button
- Press with right mouse button on with the mouse on the button. A popup menu appears.
- Press on "Assign Macro..". A dialog box appears containing available macros to assign.
- Select the macro you want to assign, in this case Macro1.
- Press the "OK" button.
Save the Excel file as a *.xlsm in order to keep the macro attached to the workbook. The reason is that a *.xlsx file can't contain macros.
1.5 Watch a video where I explain the steps to insert a print button
2. Create a Print button in a cell
This section describes how to align and resize a print button to a specific cell, the image above shows a print button with the same size as cells H6 and I6.
The instructions here are for those that have already created a print button. If you have not go to section 1 above and create one first.
How to select a form control without running the assigned macro?
- Press and hold the CTRL button on the keyboard.
- Select the format control "button".
- Release the CTRL key.
How to align the button border to a cell border?
- Make sure the button is already selected, you can tell it is selected if the "handles" around the format control button are shown.
- Press and hold the "Alt" key.
- Press and hold with mouse button on one of the "handles".
- Drag with mouse to the cell border you want to align with. It snaps automatically in place if you get close enough.
- Repeat with the remaining "handles".
Go back to section 1.2 above and follow the instructions there to complete the setup.
3. Create a Print button on a worksheet without macro
To my knowledge it is not possible to run the "Print" command by pressing a button located on a worksheet without using a macro. However, you have a few other options:
- Create instructions on the worksheet to use the shortcut keys CTRL + P to quickly print a spreadsheet.
- You can easily put a print button on the Quick Access Toolbar.
- Enable a print button on the ribbon.
I will now show you how all three examples above are created.
3.1 Use a text box to inform the user about the shortcut keys CTRL + P
The image above demonstrates a basic text box containing instructions on how to use the shortcut keys to command a print. A text box requires no macro or VBA code. Here is how to create it:
- Go to tab "Insert" on the ribbon.
- Press on "Text box" button.
- Press and hold with mouse button on the worksheet where you want it located. Don't worry, you can move it later on if a mistake is done.
- Drag with mouse to resize the text box.
- Release the mouse button.
Press with the mouse on the "text box" to select it.
Press with the mouse on the "text box" text to show the prompt. You can now change the text inside the "text box".
The handles around the "text box" allow you to resize the "text box".
A new tab on the ribbon is shown when the "text box" is selected, it is named "Shape Format". It has settings that allow you to change the background color, the shape outline and so on.
3.2 Print button on the Quick Access toolbar
The image above shows how to create a print button on the Quick Access Toolbar. The downside with this approach is that it is only a customization to your Excel application only, it is not transferred to other users if you send them a copy of your workbook.
- Press with mouse on the "arrow" button on the top menu bar. A popup menu appears.
- Enable the "Quick Print" button or the "Print Preview and Print". The "Quick Print" button prints the active worksheet without any preview at all.
The "Print" button appears on the Quick Access Toolbar.
The "Print Preview and Print" button lets you preview the print before sending it to the printer.
3.3 Enable a print button on the ribbon
The downside with this approach as well is that it is only a customization to your Excel application only. It is not transferred to other users if you send them a copy of your workbook.
- Press with right mouse button on an empty space on the ribbon. A popup menu appears.
- Press with mouse on "Customize the Ribbon...", a dialog box appears.
- Create a new group in the "Home" tab, see the image above.
- Rename the group if you like.
- Go to "Choose commands from:" and select the File Tab. See the image above step 1.
- Scroll down to "Print Preview and Print" and select it.
- Press the "Add >>" button to move it to the new custom group you just created.
- Press the OK button to apply changes.
The "Print Preview and Print" button appears on the ribbon.
4. Create a Print button - multiple sheets
The following vba macro is assigned to a print button shown in the image above. It selects the specified worksheets and then shows the print dialog box.
Section 1 above demonstrates how to:
- Create a button on the worksheet.
- Where to put the vba macro.
- How to assign the macro to a print button.
- Save the workbook as a *.xlsm file to keep the macro attached.
I recommend you follow those steps. Only the macro below is needed.
Sub Print_multiple_ws() 'Dimesnion variables and declare data types Dim ws_list As Variant 'Specify worksheet names and save array to variable ws_list = Array("Invoice", "Data") 'Select worksheets Worksheets(ws_list).Select 'Show print dialog box Application.Dialogs(xlDialogPrint).Show End Sub
5. Create a Print button - pdf
This example demonstrates a button on the worksheet that asks for the file name of the PDF to be saved, then saves the PDF and opens it using the default PDF software.
Here is what happens when you press the "Print to PDF" button:
- A dialog box appears asking for the file name.
- A PDF is generated, saved, and opened with the default PDF software.
Here is how to build the Print to PDF button:
5.1 Copy vba code
Copy the VBA code below.
Sub Print_pdf() 'Dimesnion variables and declare data types Dim ws_name As Variant 'Ask for the file name ws_name = InputBox("PDF file name?") 'Check if file name is not empty If StrPtr(ws_name) <> 0 Then 'Export the worksheet to a PDF ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=ws_name, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=False, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True End If End Sub
5.2 Put the vba code in a module
(The vba code shown in the image above is not used here in this example, it only demonstrates where to paste the code.)
- Press Alt-F11 to open the visual basic editor.
- Press with left mouse button on Module on the Insert menu.
- Copy and paste "Sub Print_pdf" code above to code module.
- Go back to Excel.
5.3 Create the worksheet button
The button is found in the Form Controls group, here are the steps:
-
- Press with left mouse button on "Developer tab" on the ribbon.
Developer tab missing? - Press with left mouse button on the "Insert" button. A popup menu appears containing multiple controls.
- Press the "Button (Form Control)" shown on the image above next to the blue arrow.
- Press with left mouse button on "Developer tab" on the ribbon.
- Press and hold with mouse button on the worksheet where you want it located.
- Drag with mouse while holding the mouse button to adjust the button size.
- Release the mouse button.
- A dialog box shows up telling you to assign a macro. Press with left mouse button on "Cancel", we need to assign the macro after putting the code in a module explained below.
Change the button name to "Print to PDF".
6. Create a Print button - selection
This VBA macro is assigned to a print button shown in the image above. It lets you specify a cell range you want to print and then shows the print dialog box.
Section 1 above demonstrates how to:
- Create a button on the worksheet.
- Where to put the vba macro.
- How to assign the macro to a print button.
- Save the workbook as a *.xlsm file to keep the macro attached.
I recommend you to follow these steps. Only the macro below is needed.
Sub Print_selection() 'Dimesnion variables and declare data types Dim rng As Range 'Define cell range to print Set rng = Application.InputBox("Select a cell range to print: ", , , , , , , 8) 'Select worksheets rng.Select 'Show print preview rng.PrintOut Preview:=True End Sub
Form controls category
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
This blog post shows you how to manipulate List Boxes (form controls) manually and with VBA code. The list box […]
There are two different kinds of text boxes, Form controls and ActiveX Controls. Form controls can only be used on […]
Macro category
Table of Contents How to create an interactive Excel chart How to filter chart data How to build an interactive […]
Table of Contents Excel monthly calendar - VBA Calendar Drop down lists Headers Calculating dates (formula) Conditional formatting Today Dates […]
Table of contents Save invoice data - VBA Invoice template with dependent drop down lists Select and view invoice - […]
Print category
Have you ever wondered how these lines got there on a worksheet? They are called "print preview lines" and they […]
This article demonstrates different techniques on how to take a screenshot of your worksheet. You can do that by simply […]
This article describes how to print page numbers in consecutive order through specified worksheets and how to repeat column headers […]
Excel categories
19 Responses to “Create a Print button – macro”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
This is what i looking for...
after long searching, finally i found here
thnks Oscar
Dear Sir/Madam
I am very glade to see your site. It very useful for me.
But I am so sorry I can not done it form number 4 till finish.
Best regards
want to try this impressive technology,thanks alot.
Thank you for this information.
I'll try it and I need it very much.
I have a question.
If I have three or more columns A. B. C.
in each of them figures up to 50 lines.
How can I make a new column D. contains the figures of the three columns under each other.
That was good but when I try to close the excel file, a popup window says this file can't be saved and they are not related to vb.
Hi Ibraheem
maybe you saved the file previously but saved it as xls. with a macro added to the sheet you need to resave the file (file-save as-, save as type: "macro enabled worksheet")this is to save the workbook with macros attached as when you attached the print button you have also changed the file type you are working on from xls to a file with macros
i have copied the said macro "Sub Macro1()
Application.Dialogs(xlDialogPrint).Show
End Sub"
But it is not working. the message shows: "Can not run the macro. The macro may not be available in the workbook."
Pls. give me the solution.
file-save as-, save as type: "macro enabled worksheet"
I also face this problem i i get solution by this rout.
enjoy your work.
//////
Hello:
I am working with Excel 2013 trying to add a macro to print too but it's not working. Yes, I have saved it as .xlsm but there is no option to "add macro" when I right-press with mouse over the command button. I can add the macro in VBA but it's like I can't get the two to talk to each other. I'm just getting started with java scripts, macros, etc. and I am addicted! Matter of fact, I probably spend too much time trying to learn about them while at the office. Anyway, thanks for your site. Still helpful - I'll keep researching. I'm sure I'm missing something simple. :)
Hello Colleagues
I what to create Enter button that will update data to the next sheet and print the data sheet at the same time. Please help.
Hi
What shall I add in macros if I want to print more than 1 copy ?
Hi - I am using a mac and getting a "Run-time error 5941" - the requested member of the collection does not exist.
Michael Salim,
I don't have a Mac and I can't find a solution to your problem.
Can you provide the code Excel returns if you record a macro while printing a page?
it so helpful for me
thanks a lot
Oscar...
I need to insert a control that will print the active worksheet to a .pdf file.
How do I make this happen?
Thanks in advance.
God Bless...
Chuck Bradley
Sir please add new invoice button that will auto increment the invoice number
Hi,
i am looking for command to print by Buttons in sheet1 when i select Button A then this will print sheet2 and when i select Button B in sheet1 then this button will print Sheet3.
Please help me how to set this commant.
thank you for this which I was looking for
how to make calling button Find and Replace with Macro in Excel