Hi, I am quite new to VBA code so am hoping someone can help. I am trying to use individual check boxes to hide sheets/multiple sheets when ticked. The first I set up works & hides/unhides the relevant sheet, but when I add a new checkbox, if wont hide the 2nd sheet. I am looking at doing this using multiple checkboxes linked to individual sheets. If each check box is ticked, I want all sheets hidden & then unhidden by unchecking.
These are the sheet names & I have set up 2 check boxes so far - capacity works however individual 1 doesn't.
The sheets I want to hide:
This is the code I have entered:
Private Sub CheckBox1_Click()
If Sheets(CheckBox1.Caption).Visible = xlHidden Then
Sheets(CheckBox1.Caption).Visible = xlSheetVisible
Else
Sheets(CheckBox1.Caption).Visible = xlHidden
End If
End Sub
Private Sub CheckBox2_Click()
If Sheets(CheckBox2.Caption).Visible = xlHidden Then
Sheets(CheckBox2.Caption).Visible = xlSheetVisible
Else
Sheets(CheckBox2.Caption).Visible = xlHidden
End If
End Sub
This is the error message that comes up when I check individual 1:
Run-time error '9'. Subscript out of range.
As mentioned, very new to this, so any assistance would be greatly appreciated.
Thanks in advance.
These are the sheet names & I have set up 2 check boxes so far - capacity works however individual 1 doesn't.
The sheets I want to hide:
Capacity |
Individual 1 |
Individual 2 |
Entity 1 Financials |
Entity 2 Financials |
Entity 3 Financials |
Entity 4 Financials |
This is the code I have entered:
Private Sub CheckBox1_Click()
If Sheets(CheckBox1.Caption).Visible = xlHidden Then
Sheets(CheckBox1.Caption).Visible = xlSheetVisible
Else
Sheets(CheckBox1.Caption).Visible = xlHidden
End If
End Sub
Private Sub CheckBox2_Click()
If Sheets(CheckBox2.Caption).Visible = xlHidden Then
Sheets(CheckBox2.Caption).Visible = xlSheetVisible
Else
Sheets(CheckBox2.Caption).Visible = xlHidden
End If
End Sub
This is the error message that comes up when I check individual 1:
Run-time error '9'. Subscript out of range.
As mentioned, very new to this, so any assistance would be greatly appreciated.
Thanks in advance.