Search code examples
excelvbacomboboxtextboxuserform

How to add a label and textbox based on combo box selection on a userform in excel


I am currently working on a userform to create an order for users at a company to send to my dept.

At the moment i have come to a standstill as i am struggling to work out the following.

I have a combobox which has a list of products our business offers. Based on the selection i want to be able to add labels and textbox which require the user to enter data for example.

If this selection in the combo box then Enter name, date required, location of user etc.

Also this needs to be specific to the combobox selection.

Any help would be much appreciated :)

UPDATE

Apologies, as i did not have any code for that function I did not add any Here is the code i have.

Private Sub CommandButton1_Click()
Windows("RFS User Form Mock.xlsm").Visible = True
End Sub

Private Sub LegendDefinition_Change()
LegendDefinition.Locked = True
End Sub

Private Sub RequestList_Change()
Dim i As Long, LastRow As Long
    LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
    If Sheets("Definition").Cells(i, "A").Value = (Me.RequestList) Then
    Me.DefinitionBox = Sheets("Definition").Cells(i, "B").Value
    End If
    Next
End Sub

Private Sub RequestList_DropButtonClick()
Dim i As Long, LastRow As Long
    LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
    If Me.RequestList.ListCount = 0 Then
    For i = 2 To LastRow
    Me.RequestList.AddItem Sheets("Definition").Cells(i, "A").Value
    Next i
    End If
End Sub

Sub UserForm_Initialize()
   SiteList.List = Array("Birmingham", "Bristol", "Cardiff", "Chelmsford", "Edinburgh", "Fenchurch Street", "Glasgow", "Guernsey", "Halifax", "Homeworker", "Horsham", "Ipswich", "Jersey", "Leeds", "Leicester", "Lennox Wood", "Liverpool", "Manchester", "Peterborough", "Redhill", "Sunderland", "Madrid")
End Sub

Private Sub VLookUp_Change()
VLookUp.Locked = True
End Sub

Solution

  • When posting a question, you are expected to provide some code showing where you're standing trying to address the problem. Here's nevertheless a short demo that will give you a starting point.

    Create a new UserForm and put a combobox, a label and a textbox on it; ensure they're named ComboBox1, Label1 and TextBox1, respectively.

    Then, paste this code in the form's module:

    Option Explicit
    
    Private Sub ComboBox1_Change()
        Dim bVisible As Boolean
    
        'Only show the label and the textbox when the combo list index is 1, which corresponds to "Item 2".
        'Note: bVisible = (ComboBox1.Text = "Item 2") would also work.
        bVisible = (ComboBox1.ListIndex = 1)
        Label1.Visible = bVisible
        TextBox1.Visible = bVisible
    End Sub
    
    Private Sub UserForm_Layout()
        'Populate the combo.
        ComboBox1.AddItem "Item 1", 0
        ComboBox1.AddItem "Item 2", 1
    
        'Note: the code below could be removed by setting the corresponding
        'design-time properties from the form designer.
        ComboBox1.Style = fmStyleDropDownList
        Label1.Visible = False
        TextBox1.Visible = False
    End Sub
    

    Then press F5 to show the form. You'll notice that the label and textbox are only visible when the combo shows "Item 2". The visibility adjustment is performed within the ComboBox1_Change event handler.

    If you plan on having numerous controls shown / hidden depending on your combo's value, you could group them into one or more Frame controls, and show / hide those frames instead.