Print Certain Sheets from a user form

jeffmoseler

Well-known Member
Joined
Jul 16, 2004
Messages
540
I would like to use a userfrom to choose which of 4 sheets I would like to print from my workbook. There are many examples out there of populating a dialog box with all the sheets in a workbook and checking the ones you want to print, but my workbook has many sheets and I only want to use 4 of them. Are there any resources out there that teach you how to create a userform with a specific list of sheets to print from . I would also like a text box next to them that someone can enter the number of copies they need.

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
All you would need for the first part would be a way to populate with only the names of the 4 sheets.

How to do that really depends on how you determine what those sheets are.

Can they be distinguished from all the other sheets by something? eg name
 
Upvote 0
Yes, the name of the sheets are Waybill, Commercial Invoice, Packing Slip, Labels.

If I could just figure out how to get a user form to give me 4 check boxes, one for each sheet, and print the sheet that's assigned to each check box I think I could add the other features I need from there.

Any ideas?
 
Upvote 0
Try this.

Create a userform, add a listbox and a command button.

Add this code to the userform module.
Code:
Option Explicit
 
Private Sub CommandButton1_Click()
Dim I As Long
    
    For I = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(I) Then
            ' print selected worksheet
            
            MsgBox "Printing " & ListBox1.List(I) & " sheet."
            
        End If
    Next I
    
    Unload Me
    
End Sub
 
Private Sub UserForm_Initialize()
 
    With ListBox1
        .List = Array("Waybill", "Commercial Invoice", "Packing Slip", "Labels")
        .ListStyle = fmListStyleOption
        .MultiSelect = fmMultiSelectMulti
    End With
 
    With CommandButton1
        .Caption = "Print Selected Sheets"
        .WordWrap = True
    End With
    
End Sub

This code won't print the worksheets as it is but to do that just needs a few lines of code, maybe even one.

Also I don't know how you actually want to print the worksheets, all in one go, separately etc.:)
 
Upvote 0
OK, everything looks good. It is a little different than I was thinking but it will work just fine. The problem is this, I can do what you have there with out a problem. What I need help with is figuring out how to get the data from the listbox in the userform to perform the print job. If I choose "Commercial Invoice" which is record 2 in the Listbox, how do I transmit that to a command telling Excel to print he sheet titled "Commercial Invoice"?
 
Upvote 0
Something like this although I know there has to be a better way to do this:

Code[]
Dim Ps, Ci, La, BOL, Pf, Printer As String
Ps = CheckBox3.Value
Ci = CheckBox1.Value
La = CheckBox5.Value
BOL = CheckBox2.Value
Pf = CheckBox4.Value
Printer = Label1.Caption

If Printer = "" Or Printer = "Not Found" Then Exit Sub
Application.ActivePrinter = Printer
If Ps = True Then
Sheets("Packing Slip").PrintOut
End If
If Ci = True Then
Sheets("Commercial Invoice").PrintOut
End If
If La = True Then
Sheets("Labels").PrintOut
End If
If BOL = True Then
Sheets("Waybill").PrintOut
End If
If Pf = True Then
Sheets("Printable Form").PrintOut
End If
'ActiveSheet.PrintOut
Unload UserForm1
End Sub
Code[]
 
Upvote 0
Jeff

Did you try the code I posted at all?

The worksheet names you specified are listed in the listbox - so use that list to get the names of the ones to print out.
 
Upvote 0
I did, but I'm not sure how to extract the names from the Listbox. What is the value in the list box? Is it the value of what is selected? What if there are multiple names selected?

Does that help explain my confusion? I'm not sure where to start.
 
Upvote 0
Jeff

It's pretty straightforward to get the value or multiple values.

What you do for a multiselect listbox is loop through all the items in the list and check their Selected property.

If it's true then the item has been selected and you can get the value of the item using the listbox's List property and the ListIndex of the item.

One thin to be careful about is that listboxes are 0-based.

Sorry I can't post any code - if I don't stop typing soon this machine might be on a collision course with a badly-tuned TV.:)
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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