PDA

View Full Version : [SOLVED:] Show/Hide a Textbox Based on Value of a Combo Box in a USerform



MikeeRDX
12-06-2016, 12:09 PM
I have a Userform with combo boxes and textboxes. CboYesNo contains dropdown option to select either Yes or No. I would like to have textbox1 (txtYes) appear (or show) when the value in CboYesNo says "Yes" and hide when it is not "Yes" or blank. Likewise, I would like textbox2 (txtNo) to show when the value in CboYesNo says "No".

I've tried various codes but none have worked so far. Can someone help me with this? Many thanks.....

MikeeRDX
12-06-2016, 12:59 PM
I have a Userform with combo boxes and textboxes. CboYesNo contains dropdown option to select either Yes or No. I would like to have textbox1 (txtYes) appear (or show) when the value in CboYesNo says "Yes" and hide when it is not "Yes" or blank. Likewise, I would like textbox2 (txtNo) to show when the value in CboYesNo says "No".

I've tried various codes but none have worked so far. Can someone help me with this? Many thanks.....

SOLVED: I found this solution on another site and it worked. Sharing in case others may find it useful.


Private Sub ComboBox1_Change()

If ComboBox1 = "Yes" Then
txtYes.Visible = True 'Set first text box to visible
txtYes.Enabled = True 'Enable the text box
txtNo.Visible = False 'Hide the second text box
txtNo.Enabled = False 'Disable the second text box
ElseIf ComboBox1 = "No" Then
txtYes.Visible = False 'Hide the first text box
txtYes.Enabled = False 'Disable the first text box
txtNo.Visible = True 'Set the second text box visible
txtNo.Enabled = True 'Enabled the second text box
Else 'Hide and disable both text boxes
txtYes.Visible = False
txtYes.Enabled = False
txtNo.Visible = False
txtNo.Enabled = False

End If

End Sub

SamT
12-06-2016, 01:06 PM
Two things
If not visible you don't need to set Enabled = False


Private Sub ComboBox1_Change()
txtYes.Visible = ComboBox1 = "Yes"
txtNo.Visible = ComboBox1 = "No"
End Sub

MikeeRDX
12-06-2016, 01:19 PM
Great! Thank you SamT