Populate VBA userform Drop Down based on an existing value of another textbox

egrospeRP

New Member
Joined
Sep 5, 2018
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello Excel Gurus,
For backround, I have an excel table that lists Mitigations related to a risk Item and it is as follows:

Risk No.Mitigation No.
10011
10012
10013
10021
10022
10031
10041
10042
10043
10044

I designed a userform that allows users to update Risk Mitigations Records. This userform has a textbox for "Risk No." that is inherited from a different userform. The problem I am having is that I would like my combo box to display mitigation number based on what the risk number is.

For example, if the userform textbox for risk no is "1001", my mitigation no. combobox should give a list option of 1, 2, and 3. If the risk no. is "1002", it should give a list option of 1 and 2. I'm stuck and I can't seem to find a way to make this work.

UpdateMitScreenshot.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This userform has a textbox for "Risk No."
In that userform 👆 put this code:

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long
  Dim sh2 As Worksheet
  
  Set sh2 = Sheets("Sheet2")    'fit the sheet name.
  
  For i = 2 To sh2.Range("A" & Rows.Count).End(3).Row   'fit the column "A" to Risk no.
    If sh2.Range("A" & i).Text = TextBox1.Value Then    'fit the column "A" to Risk no.
      ComboBox1.AddItem sh2.Range("B" & i).Value        'fit the column "B" to Mitigation no.
    End If
  Next
End Sub
 
Upvote 0
In that userform 👆 put this code:

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long
  Dim sh2 As Worksheet
 
  Set sh2 = Sheets("Sheet2")    'fit the sheet name.
 
  For i = 2 To sh2.Range("A" & Rows.Count).End(3).Row   'fit the column "A" to Risk no.
    If sh2.Range("A" & i).Text = TextBox1.Value Then    'fit the column "A" to Risk no.
      ComboBox1.AddItem sh2.Range("B" & i).Value        'fit the column "B" to Mitigation no.
    End If
  Next
End Sub

This worked! Thanks so much! Can I ask you for a follow up question though? I wanted to check first if the risk no. exist before executing the loading of combo box, If it doesn't, i want it say that the risk no wasn't found and there weren't any existing mitigation to update. I tried the following code but it wouldn't let me exit out of the 'for' loop. What I want it to do is to exit the loop and not load the userform if the Risk item wasn't found.

VBA Code:
Dim i As Long
Dim wsMT As Worksheet
Set wsMT = Sheets("Mitigations")

For i = 2 To wsMT.Range("A" & Rows.Count).End(3).Row
    If txtUpdateMitRiskNo.Value <> wsMT.Range("A" & i).Text Then
           MsgBox "There aren't any existing Mitigations associated with this Risk Item"
    Exit For
    ElseIf wsMT.Range("A" & i).Text = txtUpdateCtrlRiskNo.Value Then
            cboUpdateCtrlNo.AddItem wsMT.Range("B" & i).Value
    End If

Next

End Sub

Thanks for your help. Not sure if I need to post a separate thread for this. If so, please let me know.
 
Upvote 0
It would be easier if you put the names of your forms, your sheets and all your controls.

But let's do the following.

You have an initial form (I'm going to call it "userform2").
In this userform2 put this code.

VBA Code:
Private Sub CommandButton1_Click()
  Dim wsMT As Worksheet
  Dim f As Range
 
  Set wsMT = Sheets("Mitigations")
 
  Set f = wsMT.Range("A:A").Find(txtUpdateMitRiskNo.Value, , xlValues, xlWhole, , , False)
  If f Is Nothing Then
    MsgBox "There aren't any existing Mitigations associated with this Risk Item"
  Else
    With UserForm3      'fit with the name of the userform: "This userform has a textbox for "Risk No.""
      .TextBox1.Value = Me.txtUpdateMitRiskNo.Value
      .Show
    End With
  End If
End Sub
I am putting a button that will open the "other" form, which I have called "userform3".


---------------------------​
If userform3 isn't going to open, then there's no need to load userform3's combobox.
The combobox will be loaded when userform3 is activated. Then put the following code in the userform3

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long
  Dim wsMT As Worksheet
 
  Set wsMT = Sheets("Mitigations")    'fit the sheet name.
 
  For i = 2 To wsMT.Range("A" & Rows.Count).End(3).Row   'fit the column "A" to Risk no.
    If wsMT.Range("A" & i).Text = TextBox1.Value Then    'fit the column "A" to Risk no.
      cboUpdateCtrlNo.AddItem wsMT.Range("B" & i).Value        'fit the column "B" to Mitigation no.
    End If
  Next
End Sub
You already had the code, I just adjusted the name of the sheet. I hope I was clear enough.
--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
It would be easier if you put the names of your forms, your sheets and all your controls.

But let's do the following.

You have an initial form (I'm going to call it "userform2").
In this userform2 put this code.

VBA Code:
Private Sub CommandButton1_Click()
  Dim wsMT As Worksheet
  Dim f As Range
 
  Set wsMT = Sheets("Mitigations")
 
  Set f = wsMT.Range("A:A").Find(txtUpdateMitRiskNo.Value, , xlValues, xlWhole, , , False)
  If f Is Nothing Then
    MsgBox "There aren't any existing Mitigations associated with this Risk Item"
  Else
    With UserForm3      'fit with the name of the userform: "This userform has a textbox for "Risk No.""
      .TextBox1.Value = Me.txtUpdateMitRiskNo.Value
      .Show
    End With
  End If
End Sub
I am putting a button that will open the "other" form, which I have called "userform3".


---------------------------​
If userform3 isn't going to open, then there's no need to load userform3's combobox.
The combobox will be loaded when userform3 is activated. Then put the following code in the userform3

VBA Code:
Private Sub UserForm_Activate()
  Dim i As Long
  Dim wsMT As Worksheet
 
  Set wsMT = Sheets("Mitigations")    'fit the sheet name.
 
  For i = 2 To wsMT.Range("A" & Rows.Count).End(3).Row   'fit the column "A" to Risk no.
    If wsMT.Range("A" & i).Text = TextBox1.Value Then    'fit the column "A" to Risk no.
      cboUpdateCtrlNo.AddItem wsMT.Range("B" & i).Value        'fit the column "B" to Mitigation no.
    End If
  Next
End Sub
You already had the code, I just adjusted the name of the sheet. I hope I was clear enough.
--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
This worked perfectly, thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,224,728
Messages
6,180,600
Members
452,989
Latest member
Ol Reliable

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top