Printing from ListBox with Hidden sheets

ES407

New Member
Joined
Nov 21, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am running a ListBox that shows hidden sheets in a workbook with the VBA code:

Private Sub ListBoxSh_Click()

End Sub

Private Sub Worksheet_Activate()
Dim sh
Me.ListBoxSh.Clear
For Each sh In ThisWorkbook.Sheets
If sh.Visible = xlSheetVisible Then
Me.ListBoxSh.AddItem sh.Name
End If
Next sh
End Sub

And a Print button with the code:

Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String

With ActiveSheet.ListBoxSh

For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve SheetArray(c)
SheetArray(c) = .List(i)
c = c + 1
End If
Next i

Sheets(SheetArray()).PrintPreview

End With

End Sub


This all works fine. My issue is that I am trying to change this to print my hidden sheets. By changing "If sh.Visible = xlSheetVisible Then" to "If sh.Visible = xlSheetHidden Then" in the ListBox VBA, I get the results I am looking for (ONLY hidden sheets populate the ListBox), but when I hit the print button, I get: "Run-time error '1004': PrintPreview method of Sheets class failed"

any ideas anyone?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I am running a ListBox that shows hidden sheets in a workbook with the VBA code:

Private Sub ListBoxSh_Click()

End Sub

Private Sub Worksheet_Activate()
Dim sh
Me.ListBoxSh.Clear
For Each sh In ThisWorkbook.Sheets
If sh.Visible = xlSheetVisible Then
Me.ListBoxSh.AddItem sh.Name
End If
Next sh
End Sub

And a Print button with the code:

Sub Print_Sheets()
Dim i As Long, c As Long
Dim SheetArray() As String

With ActiveSheet.ListBoxSh

For i = 0 To .ListCount - 1
If .Selected(i) Then
ReDim Preserve SheetArray(c)
SheetArray(c) = .List(i)
c = c + 1
End If
Next i

Sheets(SheetArray()).PrintPreview

End With

End Sub


This all works fine. My issue is that I am trying to change this to print my hidden sheets. By changing "If sh.Visible = xlSheetVisible Then" to "If sh.Visible = xlSheetHidden Then" in the ListBox VBA, I get the results I am looking for (ONLY hidden sheets populate the ListBox), but when I hit the print button, I get: "Run-time error '1004': PrintPreview method of Sheets class failed"

any ideas anyone?
Didn't proof read. the first line of my post should read "I am running a ListBox the shows Visible sheets"
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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