mercredi 14 juillet 2021

VBA - Userform - How do I shorten my code to account for all checkboxes

I have to pre-face this by saying I'm incredibly new to VBA or coding in general so go easy on me.

I have written some code on a userform that contains a number of checkboxes and a number of DTPickers.

The code looks like so:

Private Sub CheckBox11_Click()
If CheckBox11.Value = True Then
DTPicker22.Enabled = True
Else
DTPicker22.Enabled = False
End If
End Sub

Private Sub CheckBox12_Click()
If CheckBox12.Value = True Then
DTPicker24.Enabled = True
Else
DTPicker24.Enabled = False
End If
End Sub 

If you can imagine the Userform contains a lot of checkboxes that have clauses next to them, upon their completion the DTPicker will enable to enter the date of completion.

Now whilst this does what I want, it only enables one DTPicker when the checkbox is ticked per private sub. There has to be some way to make this into a loop so I wouldn't need to create a hundred different private subs for every single checkbox click event surely.

If you have some code could you please also tell me where to put it, as in, into what event because I'm a bit lost.




Aucun commentaire:

Enregistrer un commentaire