UNHIDE SHEET TO PRINT

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
779
Office Version
  1. 365
Hi,

have listbox listing all sheets and the code below to print them the problem is all sheets are hidden, how to get the to unhide print once print done to hide it again,

here is the code:

VBA 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
        
        'If you'd like to print out
        'Sheets(SheetArray()).PrintOut

    End With

End Sub

here the listbox code:

VBA Code:
Private Sub Worksheet_Activate()
Dim Sh

    Me.ListBoxSh.Clear

    For Each Sh In ThisWorkbook.Sheets
        Me.ListBoxSh.AddItem Sh.Name
    Next Sh
End Sub

thank you,
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
See if this works for you:
It will need a bit more code if you they are not "all" hidden to start with and we need to store the initial setting and reset it.

VBA Code:
Sub Print_Sheets()
    Dim i As Long, c As Long
    Dim SheetArray() As String
    Dim sht As Worksheet
    
    ReDim SheetArray(1 To ActiveWorkbook.Sheets.Count)
    c = 0

    With ActiveSheet.ListBoxSh

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

        Sheets(SheetArray()).PrintPreview
        
        For Each sht In Sheets(SheetArray())
            sht.Visible = False
        Next sht
        
        'If you'd like to print out
        'Sheets(SheetArray()).PrintOut
        
    End With

End Sub
 
Upvote 0
Worked good.

just two things:

1. every time I print open object format options no sure why
2. is is possible to put the option that the only worksheet no hide after printing because is the master sheet, how can add that option to keep that sheet visible.


thank you so much for all your help.
 
Upvote 0
If you want to print without the preview just uncomment the preview line.
I have added to avoid hiding the sheet called Master in the code below. Change the word Master to your sheet name.
However it is only printing and hiding the master when is selected in your list box, so consider whether you want to put an if statement like the one I have around your Worksheet_Activate macro when you load the List box.

VBA Code:
Sub Print_Sheets()
    Dim i As Long, c As Long
    Dim SheetArray() As String
    Dim sht As Worksheet
    Dim shtMstrName As String
    
    shtMstrName = "Master"              ' Change to your sheet name for the Master
    
    ReDim SheetArray(1 To ActiveWorkbook.Sheets.Count)
    c = 0

    With ActiveSheet.ListBoxSh

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                'ReDim Preserve SheetArray(c)
                c = c + 1
                SheetArray(c) = .List(i)
                Sheets(SheetArray(c)).Visible = True
            End If
        Next i
        
        ReDim Preserve SheetArray(1 To c)
        'If you want to run PrintPreview
        'Sheets(SheetArray()).PrintPreview
        
        'If you'd like to print out
        Sheets(SheetArray()).PrintOut
        
        For Each sht In Sheets(SheetArray())
            If sht.Name <> shtMstrName Then ' Do not hide the Master if selected
                sht.Visible = False
            End If
        Next sht
        
    End With

End Sub
 
Upvote 0
SHAPE FORMAT.PNG
 
Upvote 0
Hmm, there is nothing in the code you started with or in my modified code that would cause that to happen.
Is there more code to it that you actually running (look for the Shapes word in the code) ?

You need to be running a line that looks something like this for it to happen:
VBA Code:
ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
 
Upvote 0
If you want to print without the preview just uncomment the preview line.
I have added to avoid hiding the sheet called Master in the code below. Change the word Master to your sheet name.
However it is only printing and hiding the master when is selected in your list box, so consider whether you want to put an if statement like the one I have around your Worksheet_Activate macro when you load the List box.

VBA Code:
Sub Print_Sheets()
    Dim i As Long, c As Long
    Dim SheetArray() As String
    Dim sht As Worksheet
    Dim shtMstrName As String
   
    shtMstrName = "Master"              ' Change to your sheet name for the Master
   
    ReDim SheetArray(1 To ActiveWorkbook.Sheets.Count)
    c = 0

    With ActiveSheet.ListBoxSh

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                'ReDim Preserve SheetArray(c)
                c = c + 1
                SheetArray(c) = .List(i)
                Sheets(SheetArray(c)).Visible = True
            End If
        Next i
       
        ReDim Preserve SheetArray(1 To c)
        'If you want to run PrintPreview
        'Sheets(SheetArray()).PrintPreview
       
        'If you'd like to print out
        Sheets(SheetArray()).PrintOut
       
        For Each sht In Sheets(SheetArray())
            If sht.Name <> shtMstrName Then ' Do not hide the Master if selected
                sht.Visible = False
            End If
        Next sht
       
    End With

End Sub
I know this is an old post but i want to say thank you! It seems i was using the exact ssame listbox and print button as OP and having the exact same issue with printing my hidden sheets. Your code worked flawlessly. Thank you so much!!!
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

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