VBA Userform with Multiselection Listbox for one print job

Status
Not open for further replies.

paradadf

New Member
Joined
Dec 4, 2018
Messages
4
I've been trying to create an UserForm for printing from a dinamically generated ListBox of available Sheets on my workbook. It looks like this:

8MgzQ.jpg


I set 1 - fmMultiSelectMulti in the ListBox properties to allow multiselection. Usually, I need the sheets printed in color, as well as a copy printed in black and white. And to be sure everything looks good before spending some ink, there is a checkbox for the print preview.
So far, I managed to capture the selection and store it in a variable called SheetsToPrint. But the print command below is failing with Subscript out of range (Error 9).
Code:
Worksheets(Array(SheetsToPrint)).PrintOut preview:=True
Note: Writing Worksheets(SheetsToPrint) doesn't work either.


If I don't use the variable and change it to the following string, it works as expected:
Code:
Worksheets(Array("Sheet1","Sheet2")).PrintOut preview:=True
And I get what I need in a single print job. Except for the basketball image not being shown/printed properly.
My VBA Code looks like this:
Code:
Private Sub UserForm_Initialize()
    Dim N As Integer
    Do
    N = N + 1
    If Sheets(N).Visible = True Then
        SelectedSheets.AddItem Sheets(N).Name
    End If
    Loop Until N = Worksheets.Count
End Sub
Private Sub SelectAll_Click()
    Dim N As Single
    If SelectAll.Value = True Then
        For N = 0 To SelectedSheets.ListCount - 1
        SelectedSheets.Selected(N) = True
        Next N
    Else
        For N = 0 To SelectedSheets.ListCount - 1
        SelectedSheets.Selected(N) = False
        Next N
    End If
End Sub
Private Sub PrinterButton_Click()
    Application.Dialogs(xlDialogPrinterSetup).Show
End Sub
Private Sub PrintButton_Click()




    Dim vPrev As Boolean
    If PrintPreview.Value = True Then
        vPrev = True
    Else
        vPrev = False
    End If
    
    With SelectedSheets
        For N = 0 To .ListCount - 1
        If .Selected(N) = True Then
            If SheetsToPrint = vbNullString Then
                SheetsToPrint = """" & .List(N) & """"
            Else
                SheetsToPrint = SheetsToPrint & ", " & """" & .List(N) & """"
            End If
        End If
        Next N
        ' Debug
        MsgBox "Print Array:" & vbCrLf & SheetsToPrint
        Me.Hide
    End With
    
    ' Color Config
    If Original.Value = True Then
        Dim sht As Worksheet
        For Each sht In Worksheets(Array(SheetsToPrint))
        With sht.PageSetup
            .BlackAndWhite = False
        End With
        Next
    
        ' Print Original in single print job
        With Worksheets(Array(SheetsToPrint))
            .PrintOut preview:=vPrev
        End With
    End If
    
    ' Grayscale Config
    If Copy.Value = True Then
        Dim shtBW As Worksheet
        For Each shtBW In Worksheets(Array(SheetsToPrint))
        With shtBW.PageSetup
            .BlackAndWhite = True
        End With
        Next
    
        ' Print Grayscale in single print job
        With Worksheets(Array(SheetsToPrint))
            .PrintOut preview:=vPrev
        End With
    End If
End Sub


I´ve tried many combinations and search a lot, but no luck. Any advise is very welcome. I suspect it has something to do with the dimension of SheetsToPrint, but I wasn't able to make it work.
This is my excel file: userform_printout_array.xlsm
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Duplicate https://www.mrexcel.com/forum/excel...orm-multiselection-listbox-one-print-job.html

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread.
Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

Note that sometimes posts from new users require Moderator approval before you can see them on the public forums. When this happens, you should see a message to that effect when you try to post it.
Please be patient and do not attempt to post the question again.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,902
Messages
6,122,161
Members
449,069
Latest member
msilva74

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