Dear all, 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:
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've 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).
Note: Using only Worksheets(SheetsToPrint) doesn't work either
If I don't use the variable and change it to the following string, it works as expected:
And I get what I need in a single print job. Except for the basketball image not being shown/printed properly :S.
I´ve tried many combinations and search a lot, but no luck. Any advise is very welcome. I suspect the problem has something to do with dimensions of SheetsToPrint, but I wasn't able to correct it myself.
This is my excel file: https://www6.zippyshare.com/v/H02nqoxN/file.html
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've 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:
[COLOR=#303336][FONT=inherit]Worksheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]Array[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]SheetsToPrint[/FONT][/COLOR][COLOR=#303336][FONT=inherit])).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]PrintOut preview[/FONT][/COLOR][COLOR=#303336][FONT=inherit]:=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]True[/FONT][/COLOR]
If I don't use the variable and change it to the following string, it works as expected:
Code:
[COLOR=#303336][FONT=inherit]Worksheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]Array[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Sheet1"[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Sheet2"[/FONT][/COLOR][COLOR=#303336][FONT=inherit])).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]PrintOut preview[/FONT][/COLOR][COLOR=#303336][FONT=inherit]:=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]True[/FONT][/COLOR]
And I get what I need in a single print job. Except for the basketball image not being shown/printed properly :S.
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">[COLOR=#101094][FONT=inherit]Private [FONT=inherit]Sub[/FONT][COLOR=#303336][FONT=inherit] UserForm_Initialize[/FONT][/COLOR][COLOR=#303336][FONT=inherit]()[/FONT][/COLOR]
[/FONT][/COLOR]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
</code>End [COLOR=#101094][FONT=inherit]Sub[/FONT][/COLOR]
I´ve tried many combinations and search a lot, but no luck. Any advise is very welcome. I suspect the problem has something to do with dimensions of SheetsToPrint, but I wasn't able to correct it myself.
This is my excel file: https://www6.zippyshare.com/v/H02nqoxN/file.html