0

Good morning everyone. I am having a bit of difficulty trying to get a button working properly in Excel 2007, VBA. What this button does is group worksheets based on the check box value being true. Then it will display the print dialogue so the the user can print to PDF or to their printer.

I am not sure how to go about this. I did try using:

If me.chkTable1 = True Then Sheets(Table3.Name).Select
If me.chkTable2 = True Then Sheets(Table4.Name).Select

However I found that with this method the previous worksheet will deselect. I thought that maybe an array will work. That if the value of me.chkTableX = True then the array would be populated with the worksheet. Then at the end I could select the array for printing.

I am fairly new, so my apologies if I am missing something.

1 Answer 1

0

Something like this will work:

Sub sheetselect()
Dim wsStr() As Variant
Dim ostr() As Variant

ReDim wsStr(0 To 0)

If Me.chktable1 = True Then
    wsStr(UBound(wsStr)) = table3.Name
    ReDim Preserve wsStr(UBound(wsStr) + 1) As Variant
End If
If Me.chktable2 = True Then
    wsStr(UBound(wsStr)) = table4.Name
    ReDim Preserve wsStr(UBound(wsStr) + 1) As Variant
End If
ReDim ostr(0 To UBound(wsStr) - 1)
For k = 0 To UBound(wsStr) - 1
    ostr(k) = wsStr(k)
Next k
Sheets(ostr).Select
End Sub

It loads an array with the names of the sheets. Then uses this array to select the proper sheets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.