Urgent - print macro button

jin

New Member
Joined
Oct 16, 2008
Messages
8
Hi,

I need to know how to make a box that says "print" which will print out pages in my excel file... What I am picturing is this:

Tab 1 Y
Tab 2 Y
Tab 3 N
... etc.

Making a table as the one above, and depending on whether I input "Y" or "N", it will print that tab...

i've seen this in some financial models and would like to incorporate it into my model. If you could offer some detailed help, it would be greatly appreciated. Please note that I am a complete beginner and would need to be taken step-by-step.

Thanks again.
 
OK,

I tweaked the code a bit to return to the starting sheet:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> SelectSheets()<br>    <SPAN style="color:#007F00">'   John Walkenbach</SPAN><br>    <SPAN style="color:#007F00">'   www.j-walk.com</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> TopPos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> SheetCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> PrintDlg <SPAN style="color:#00007F">As</SPAN> DialogSheet<br>    <SPAN style="color:#00007F">Dim</SPAN> CurrentSheet <SPAN style="color:#00007F">As</SPAN> Worksheet, FinalSheet <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> cb <SPAN style="color:#00007F">As</SPAN> CheckBox<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br><br>    <SPAN style="color:#00007F">Set</SPAN> FinalSheet = ActiveSheet<br>    <br><SPAN style="color:#007F00">'   Check for protected workbook</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> ActiveWorkbook.ProtectStructure <SPAN style="color:#00007F">Then</SPAN><br>        MsgBox "Workbook is protected.", vbCritical<br>        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#007F00">'   Add a temporary dialog sheet</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> CurrentSheet = ActiveSheet<br>    <SPAN style="color:#00007F">Set</SPAN> PrintDlg = ActiveWorkbook.DialogSheets.Add<br><br>    SheetCount = 0<br><br><SPAN style="color:#007F00">'   Add the checkboxes</SPAN><br>    TopPos = 40<br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> ActiveWorkbook.Worksheets.Count<br>        <SPAN style="color:#00007F">Set</SPAN> CurrentSheet = ActiveWorkbook.Worksheets(i)<br><SPAN style="color:#007F00">'       Skip empty sheets and hidden sheets</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Application.CountA(CurrentSheet.Cells) <> 0 And _<br>            CurrentSheet.Visible <SPAN style="color:#00007F">Then</SPAN><br>            SheetCount = SheetCount + 1<br>            PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5<br>                PrintDlg.CheckBoxes(SheetCount).Text = _<br>                    CurrentSheet.Name<br>            TopPos = TopPos + 13<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br><br><SPAN style="color:#007F00">'   Move the OK and Cancel buttons</SPAN><br>    PrintDlg.Buttons.Left = 240<br><br><SPAN style="color:#007F00">'   Set dialog height, width, and caption</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> PrintDlg.DialogFrame<br>        .Height = Application.Max _<br>            (68, PrintDlg.DialogFrame.Top + TopPos - 34)<br>        .Width = 230<br>        .Caption = "Select sheets to print"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br><SPAN style="color:#007F00">'   Change tab order of OK and Cancel buttons</SPAN><br><SPAN style="color:#007F00">'   so the 1st option button will have the focus</SPAN><br>    PrintDlg.Buttons("Button 2").BringToFront<br>    PrintDlg.Buttons("Button 3").BringToFront<br><br><SPAN style="color:#007F00">'   Display the dialog box</SPAN><br>    CurrentSheet.Activate<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> SheetCount <> 0 <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> PrintDlg.Show <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cb <SPAN style="color:#00007F">In</SPAN> PrintDlg.CheckBoxes<br>                <SPAN style="color:#00007F">If</SPAN> cb.Value = xlOn <SPAN style="color:#00007F">Then</SPAN><br>                    Worksheets(cb.Caption).Activate<br>                    ActiveSheet.PrintOut<br><SPAN style="color:#007F00">'                   ActiveSheet.PrintPreview 'for debugging</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> cb<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        MsgBox "All worksheets are empty."<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#007F00">'   Delete temporary dialog sheet (without a warning)</SPAN><br>    Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>    PrintDlg.Delete<br><br><SPAN style="color:#007F00">'   Reactivate original sheet</SPAN><br>    FinalSheet.Activate<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

My bad for not catching that, I haven't used the code in years.
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I have a new question that I need help with! :(

In a new file, I am trying to find a macro to automate the print process (like the macro given above) under certain categories... eg...

1 computer
1 monitor
1 phone
2 can
2 fax

And a macro that will distinguish the first column into "1" and "2", and I can click a button and print all the "1" lines...

Can you help me out please? Thank you, as always..
 
Upvote 0
Welcome to the Board!

Here's a great Print routine by John Walkenbach that will list all of your sheets in a list box and allow you to select the ones you want to print:

Sub SelectSheets()
' John Walkenbach
' www.j-walk.com
Dim i As Integer
Dim TopPos As Integer
Dim SheetCount As Integer
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim cb As CheckBox
Application.ScreenUpdating = False

' Check for protected workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox "Workbook is protected.", vbCritical
Exit Sub
End If

' Add a temporary dialog sheet
Set CurrentSheet = ActiveSheet
Set PrintDlg = ActiveWorkbook.DialogSheets.Add

SheetCount = 0

' Add the checkboxes
TopPos = 40
For i = 1 To ActiveWorkbook.Worksheets.Count
Set CurrentSheet = ActiveWorkbook.Worksheets(i)
' Skip empty sheets and hidden sheets
If Application.CountA(CurrentSheet.Cells) <> 0 And _
CurrentSheet.Visible Then
SheetCount = SheetCount + 1
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(SheetCount).Text = _
CurrentSheet.Name
TopPos = TopPos + 13
End If
Next i

' Move the OK and Cancel buttons
PrintDlg.Buttons.Left = 240

' Set dialog height, width, and caption
With PrintDlg.DialogFrame
.Height = Application.Max _
(68, PrintDlg.DialogFrame.Top + TopPos - 34)
.Width = 230
.Caption = "Select sheets to print"
End With

' Change tab order of OK and Cancel buttons
' so the 1st option button will have the focus
PrintDlg.Buttons("Button 2").BringToFront
PrintDlg.Buttons("Button 3").BringToFront

' Display the dialog box
CurrentSheet.Activate
Application.ScreenUpdating = True
If SheetCount <> 0 Then
If PrintDlg.Show Then
For Each cb In PrintDlg.CheckBoxes
If cb.Value = xlOn Then
Worksheets(cb.Caption).Activate
ActiveSheet.PrintOut
' ActiveSheet.PrintPreview 'for debugging
End If
Next cb
End If
Else
MsgBox "All worksheets are empty."
End If

' Delete temporary dialog sheet (without a warning)
Application.DisplayAlerts = False
PrintDlg.Delete

' Reactivate original sheet
CurrentSheet.Activate
End Sub


Hope that helps,
I was just looking for something exactly like this. Works great. Thanks!
 
Upvote 0
I have a new question that I need help with! :(

In a new file, I am trying to find a macro to automate the print process (like the macro given above) under certain categories... eg...

1 computer
1 monitor
1 phone
2 can
2 fax

And a macro that will distinguish the first column into "1" and "2", and I can click a button and print all the "1" lines...

Can you help me out please? Thank you, as always..

Sorry I missed your reply (must have been out of town)...You can do what you want with Data-->Auto Filter. Filter on 1 and print, then repeat for 2. It's easily recordable.
 
Upvote 0
Hi Smitty,

Or anyone else out there.

This is a brilliant code for printing making life a lot easier. I have two questions to sdaot it for me.

I would like to know if there is a way to make it print graphs that are in a worksheet.

I have a workbook with approx 8 graphs, one graph per page. However the checkboxes do not include any of these graph pages.

Secondly, is there a way that it would allow us to choose how many copies to print?

Thanks All

Charllie
 
Upvote 0
Here's a great Print routine by John Walkenbach that will list all of your sheets in a list box and allow you to select the ones you want to print:

Smitty,

Much thanks to you and John Walkenbach for this script, very useful indeed ...... :pray:

Rather than the user have to chose which sheets to print, can it be tweaked to automatically only print certain sheets based on whether or not specific cells on the 1st sheet contains results?
i.e. Sheet 1 has a series of 10 cells that contain results from the tables on the other 5 sheets, but I don't want to print sheets 2,3,4,5 or 6 if the results in the table on sheet 1 are zero.
As I'm just having fun with the algebra side of Excel at the moment, any guidance on this would be gratefully appreciated.
 
Upvote 0
You could do something along these lines:

Code:
Dim ws as Worksheet
  For Each ws in ActiveWorkbook.Worksheets
     If ws.Range("A1").Value <> 0 Then ws.PrintOut
  Next ws
End SUb
 
Upvote 0
Can the ability to select all sheets be added to this.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,581
Messages
6,125,658
Members
449,247
Latest member
wingedshoes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top