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 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: 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:
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:
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
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
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
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