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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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