mercredi 16 septembre 2015

How do I have one Userform event procedure (checkbox click) control multiple IF THEN statements?

Hey so I am a beginner with VBA but here is my dilemma:

Creating an excel userform with VBA -

I have a combobox with the numbers 1-8, the number of textboxes shown for the user to fill out is based off of the number selected in the combobox.

I also have a checkbox which, when "TRUE", enables the user to create a custom title for the output of the textbox.


Ex: Number of Goals = 2
Custom Label Goals? [x] (True)

Goal 1 : $1,000,000
"New Home"

Goal 2 : $50,000
"Boat"


My issue is that I cannot get the number of custom label boxes to be the same as the number of goals selected. So when Number of goals is 2, 8 custom goal textboxes still show up if the checkbox is TRUE.

I found a way using the code below, but when I enter the second IF THEN statement, the first no longer responds to the clicking the checkbox:

Private Sub cbIMPLBL_Click()

If cboIMP.Value = "1" And cbIMPLBL.Value = "True" Then
   txtIMPLBL1.Visible = True
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
Else
   txtIMPLBL1.Visible = False
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
End If

If cboIMP.Value = "2" And cbIMPLBL.Value = "True" Then
   txtIMPLBL1.Visible = True
   txtIMPLBL2.Visible = True
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
Else
   txtIMPLBL1.Visible = False
   txtIMPLBL2.Visible = False
   txtIMPLBL3.Visible = False
   txtIMPLBL4.Visible = False
   txtIMPLBL5.Visible = False
   txtIMPLBL6.Visible = False
   txtIMPLBL7.Visible = False
   txtIMPLBL8.Visible = False
End If

End Sub

Any help here would be greatly appreciated. As I am just beginning is there a different way I am supposed to be doing this, e.g. not writing all of this out individually, but as a control or function (not sure what the term would be) of some sort?

Thank you so much!!!!




Aucun commentaire:

Enregistrer un commentaire