I'm a total noob so please be indulgent,
I'm working on a macro for an excel project.
I have an userform which contain checkboxes, I also have a few labels, my goal is to enable or disable specific labels if checkboxes are true or false.
I have :
- a module in which I store my functions and subs
- a main module
- the userform
Of course i could write in the userform :
Private Sub Checkbox1 ()
If Userform.Checbox1 = true then
Userform.label.enable = true
End if
However i have a few chekboxes and labels and i'd like to create a sub or function in order to simplify my code.
In the module in which i store my function a wrote :
Sub EnableMyLabels(Checkbox as object , Label as object)
If Userform.Checkbox = true then
Userform.label.enable = true
End If
and in my userform i tried to used it like this :
Call EnableMyLabels (Checkbox1 , Label1)
But it doesen't work, I can't find how i should write it, Is what i'm trying to do even possible ?
Thanks
CodePudding user response:
You should try the same principle but using Events.
On your VBE, at the Userform module select "Checkbox1" on the top dropdown.
Then select the method "Click" on the right top dropdown.
A method called Checkbox1_Click should have been created.
That code will run whenever the user clicks the checkbox.
Include all your "label logic" (or logic for other cotrols too) there.
CodePudding user response:
Your Sub should be like
Sub EnableMyLabels(cb As MSForms.CheckBox, lbl As MSForms.Label)
If cb.Value = True Then
lbl.Enabled = True
End If
End Sub
And the call
EnableMyLabels Checkbox1, Label1
You might want to call it from an Event on the Checkbox, eg
Private Sub CheckBox1_Change()
EnableMyLabels CheckBox1, Label1
End Sub