Excel 2010
I'm trying to write a macro for the command button below.
I have a Userfrom with ~90 check boxes and 2 CommandButtons.
Result wanted:
When the ShowSelection CommandButton is clicked, the columns containing the same headings as the Checked Checkboxes' captions is shown, while the remaining columns in range E to CS remain hidden.
As far as I can tell, my code is not actually finding the checkboxes, or at least isn't finding an active one before it faults out.
Tweaks, robustness, and suggestions are all requested.
Private Sub ShowSelection_Click()
Range("E32:CS32").Columns.Hidden = True
Dim CB As Control 'CB = checkbox
Dim AR As String
AR = "AssignRoles"
For Each CB In Me.Controls
If TypeName(CB) = "CheckBox" Then
KeepGoing:
Next
RoleSelection.Hide
End Sub
I'm trying to write a macro for the command button below.
I have a Userfrom with ~90 check boxes and 2 CommandButtons.
Result wanted:
When the ShowSelection CommandButton is clicked, the columns containing the same headings as the Checked Checkboxes' captions is shown, while the remaining columns in range E to CS remain hidden.
As far as I can tell, my code is not actually finding the checkboxes, or at least isn't finding an active one before it faults out.
Tweaks, robustness, and suggestions are all requested.
Private Sub ShowSelection_Click()
Range("E32:CS32").Columns.Hidden = True
Dim CB As Control 'CB = checkbox
Dim AR As String
AR = "AssignRoles"
For Each CB In Me.Controls
If TypeName(CB) = "CheckBox" Then
'^based on other code I've seen, this, theoretically, finds only my check boxes
If CB.Value = True Then '^If the box is checked
Dim KeepItem As String
KeepItem = ActiveControl.Caption
'^the first time this text is active, it returns the caption for one of the CommandBoxes on my userform, not the text from a checkbox
Worksheets(AR).Range("E32:CS2").Select
Selection.Find(What:=KeepItem, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'^ My code runs to here, then I get "Run-Time error '91': Object Variable or With Block variable not set" and clicking debug highlights the "Find" lines
On Error GOTo KeepGoing
'^ Need to create option. Text should always be found. Need to test during debug. If text not found, then need to check captions and doc text
ActiveCell.Columns.Hidden = False
End If
End IfKeepGoing:
Next
RoleSelection.Hide
End Sub