Hi everyone,
I am trying to make a userform where each sheet has its own corresponding checkbox. The goal is to print the selected sheets into a pdf upon clicking OK button.
I have managed to somewhat achieve my goal. But it feels like it is far from being optimal. I had to assign 2 different arrays and for loops. One to get the number and name of the sheets to be printed. A second one to assign the data received from the first for loop. And use that second array as the main array for the print function. I had to hardcode the range of the first array as I don't know how to do it more dynamically. As of right now my code is as follows:
What I ask is how can I make it so that I don't need a second array and a second for loop? Do I have to actually hardcode the first array's range? Or is above code ideal or foolproof in terms of what I am trying to do?
Honestly, I have only a very basic understanding of VBA. I had to google and check every function and how to use them for everything on this code until I was able to do what I needed. But, I don't trust myself with the code, it returned all sorts of errors in each and every step until I finally got to above state. It feels like it will end up giving some errors in a way that I don't know how
Thanks in advance,
Michael
I am trying to make a userform where each sheet has its own corresponding checkbox. The goal is to print the selected sheets into a pdf upon clicking OK button.
I have managed to somewhat achieve my goal. But it feels like it is far from being optimal. I had to assign 2 different arrays and for loops. One to get the number and name of the sheets to be printed. A second one to assign the data received from the first for loop. And use that second array as the main array for the print function. I had to hardcode the range of the first array as I don't know how to do it more dynamically. As of right now my code is as follows:
VBA Code:
Private Sub OKButton_Click()
Dim names(20) As String, isheets() As String
Dim iCount As Long, i As Long
Dim checkbox As Control
iCount = 0
For Each checkbox In Me.Controls
If checkbox.Value = True Then
iCount = iCount + 1
names(iCount - 1) = checkbox.Caption
End If
Next checkbox
ReDim isheets(1 To iCount)
For i = 1 To iCount
isheets(i) = names(i - 1)
Next i
ThisWorkbook.Sheets(isheets).PrintOut
End Sub
What I ask is how can I make it so that I don't need a second array and a second for loop? Do I have to actually hardcode the first array's range? Or is above code ideal or foolproof in terms of what I am trying to do?
Honestly, I have only a very basic understanding of VBA. I had to google and check every function and how to use them for everything on this code until I was able to do what I needed. But, I don't trust myself with the code, it returned all sorts of errors in each and every step until I finally got to above state. It feels like it will end up giving some errors in a way that I don't know how
Thanks in advance,
Michael