Select Sheets To Print Based On Checkbox Values

hzrvkt

New Member
Joined
Jul 10, 2004
Messages
33
I have a worksheet with a userform with 24 checkboxes that I am attempting to use to open a workbook with 24 different worksheets. I want to use the checkboxes to select which sheets to print. The checkbox.captions correspond to the worksheet names and the checkbox.names correspond to the position of the worksheets I want to print. I have tried building a string to work from without success. My latest attempt was to build an array, but I haven't fared much better. I've worked on this till me head is spinning. Please help!

Code:
Sub SOTForm()

    Dim MyArray() As Variant
    Dim wb As Workbook
    Dim i As Integer
    Dim j As Integer
    Dim s As Integer
    Dim ctrl As Control
    
    Set wb = Workbooks.Open("me.xlsx", ReadOnly:=True)
        j = 0
            For Each ctl In UserForm1.Controls
                If TypeOf ctl Is MSForms.CheckBox Then
                    If UserForm1.Controls(ctl.Name).Value = True Then
                        j = j + 1
                    End If
                End If
            Next
        For s = 0 To j
            For i = 1 To 24
                If ThisWorkbook.UserForm1.Controls("checkbox" & i).Value = True Then
                MyArray(s) = i
            End If
            Next i
        Next s
    wb.Sheets(Array(mystring)).Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
    IgnorePrintAreas:=False

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is a classic case of overthinking it! Lol


Code:
Sub SOTForm()

    Dim MyArray() As Variant
    Dim wb As Workbook
    Dim i As Integer
    
    Set wb = Workbooks.Open("me.xlsx", ReadOnly:=True)

            For i = 2 To 24
                If UserForm1.Controls("Checkbox" & i).Value = True Then
                    wb.Sheets(i).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
                End If
            Next i
ActiveWorkbook.PrintOut
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,552
Members
452,652
Latest member
eduedu

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