Consulting

Results 1 to 14 of 14

Thread: Print Hidden Worksheets from Userform

  1. #1

    Print Hidden Worksheets from Userform

    Hi All,
    I have some code which i created a few years ago, but i'm wanting to change it so that it only shows and prints certain worksheets which would be hidden. at the minute the code just prints any active worksheets.

    this is the code.

    Sub PrintSheet()
    
        Dim i As Integer
        Dim TopPos As Integer
        Dim SheetCount As Integer
        Dim PrintDlg As DialogSheet
        Dim CurrentSheet As Worksheet
        Dim cb As CheckBox
        Application.ScreenUpdating = False
    
    
    '   Check for protected workbook
        If ActiveWorkbook.ProtectStructure Then
            MsgBox "Workbook is protected.", vbCritical
            Exit Sub
        End If
    
    
    '   Add a temporary dialog sheet
        Set CurrentSheet = ActiveSheet
        Set PrintDlg = ActiveWorkbook.DialogSheets.Add
    
    
        SheetCount = 0
    
    
    '   Add the checkboxes
    
    
        TopPos = 40
        For i = 1 To ActiveWorkbook.Worksheets.Count
            Set CurrentSheet = ActiveWorkbook.Worksheets(i)
    '       Skip empty sheets and hidden sheets
            If Application.CountA(CurrentSheet.Cells) <> 0 And _
                CurrentSheet.Visible Then
                SheetCount = SheetCount + 1
                PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
                    PrintDlg.CheckBoxes(SheetCount).Text = _
                        CurrentSheet.Name
                TopPos = TopPos + 13
            End If
        Next i
    
    
    '   Move the OK and Cancel buttons
        PrintDlg.Buttons.Left = 240
    
    
    '   Set dialog height, width, and caption
        With PrintDlg.DialogFrame
            .Height = Application.Max _
                (68, PrintDlg.DialogFrame.Top + TopPos - 34)
            .Width = 230
            .Caption = "Select sheets to print"
    
    
        End With
    
    
    '   Change tab order of OK and Cancel buttons
    '   so the 1st option button will have the focus
        PrintDlg.Buttons("Button 2").BringToFront
        PrintDlg.Buttons("Button 3").BringToFront
    
    
    '   Display the dialog box
        CurrentSheet.Activate
        Application.ScreenUpdating = True
        If SheetCount <> 0 Then
            If PrintDlg.Show Then
                For Each cb In PrintDlg.CheckBoxes
                    If cb.Value = xlOn Then
                        Worksheets(cb.Caption).Activate
                        ActiveSheet.PrintOut
    '                   ActiveSheet.PrintPreview 'for debugging
    
    
                    End If
                Next cb
            End If
        Else
            MsgBox "All worksheets are empty."
        End If
    
    
    '   Delete temporary dialog sheet (without a warning)
        Application.DisplayAlerts = False
        PrintDlg.Delete
    
    
    End Sub

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    have you tried changing:
     CurrentSheet.Visible Then
    to
    Not( CurrentSheet.Visible) Then

  3. #3
    I have multiple worksheets hidden, i only want to select a few of them.

  4. #4
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    How are you going to select them??

  5. #5
    the userform has check boxes

  6. #6
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    I might be wrong but I'm pretty sure you cannot print hidden sheets maybe try something along the lines of this:
    application.screenupdating = false
    if UserFormCheckBox Then Sheet#.visible= true
    sheet#.print
    Sheet#.visible= false
    application.screenupdating = true
    - I HAVE NO IDEA WHAT I'M DOING

  7. #7
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    This slight modification will allow your hidden sheets to appear on your userform.
    wonderful code BTW.
    For i = 1 To ActiveWorkbook.Worksheets.Count
            Set CurrentSheet = ActiveWorkbook.Worksheets(i)
             '       Skip empty sheets and hidden sheets
            checkme = CurrentSheet.Visible
            CurrentSheet.Visible = True
            If Application.CountA(CurrentSheet.Cells) <> 0 And _
            CurrentSheet.Visible Then
                SheetCount = SheetCount + 1
                PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
                PrintDlg.CheckBoxes(SheetCount).Text = _
                CurrentSheet.Name
                TopPos = TopPos + 13
            End If
            CurrentSheet.Visible = checkme
        Next i
    - I HAVE NO IDEA WHAT I'M DOING

  8. #8
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    And this will allow you to print
     For Each cb In PrintDlg.CheckBoxes
                    If cb.Value = xlOn Then
                        Worksheets(cb.Caption).Visible = True
                        Worksheets(cb.Caption).Activate
                        ActiveSheet.PrintOut
                        Worksheets(cb.Caption).Visible = False
                         '                   ActiveSheet.PrintPreview 'for debugging
                         
                         
                    End If
                Next cb
    - I HAVE NO IDEA WHAT I'M DOING

  9. #9
    Mincus1308,
    That has allowed me to print of hidden worksheets perfectly.

    Once i have printed my workbook always moves onto the last worksheet tab, how would i stop that i want to keep it on a specific worksheet.

  10. #10
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    When you say 'specific worksheet' do you always mean sheet1? or like the last active worksheet?

    the reason that this is happening is your For loop counts through all your worksheets (from the first to the last)
    and sets 'CurrentSheet = ActiveWorkbook.Worksheets(I)' (so the last sheet in the workbook will be held in the variable CurrentSheet)
    and near the end of execution you asked to activate 'CurrentSheet' (activating the sheet held by the variable)
    So the code above will always terminate with the last worksheet selected

    Something to keep in mind - I'm pretty sure you cannot 'activate' or 'select' a worksheet if it is hidden.

    there are a number of different ways you can activate or select a worksheet but
    If you know what worksheet you would like to end on you can add this near the end of you code:
    Sheet#.Visible = True
    Sheet#.Activate

    If you know the worksheet name or code name and need more help with this problem just reply and ill show you where to place it.
    - I HAVE NO IDEA WHAT I'M DOING

  11. #11
    the worksheet name is always "summary" and its the first sheet in the workbook

  12. #12
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    Ok.
    Sheet names can almost always be changed and VBA is a little relentless when it comes to the finer points of grammar.
    I recommend using the worksheets code name which cannot be changed as easily.

    if you look at the little picture you can see that I have a worksheet named "Sheet 1" and it has the code name Sheet1.
    I can call the worksheet using any of the following methods:
    1) Sheet1.Select (Using Code Name)
    2) Worksheets("Sheet 1").Select (Using the Worksheet Name)


    Example.jpg


    Assuming that you worksheet named "summary" is Code named Sheet1 I would sub in the following code:
     '   Delete temporary dialog sheet (without a warning)
        Application.DisplayAlerts = False
        PrintDlg.Delete
         'SET USERS FOCUS TO WORKSHEET "summary"
         Sheet1.Visible = True
         Sheet1.Select
         
    End Sub
    - I HAVE NO IDEA WHAT I'M DOING

  13. #13
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    Nathandavies, Did that work?
    - I HAVE NO IDEA WHAT I'M DOING

  14. #14
    Yes, I have run multiple tests this morning.

    Thanks for you help on this!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •