Adding multiple values to a combo box based off of a value in another cell

Ah5522

New Member
Joined
Jun 17, 2014
Messages
22
Hello

I have a userform dumping data into an excel table. I'd like for a user to be able to select their name and have a list of their records populate in a record ID combo box.

I wrote the following code that seems to almost be doing what I need, except that it is pulling all of the record ID's in the table not just the ones that match the cboStaff.value ?

Private Sub cboStaff_Change()
Dim x As Range

Set rng = Worksheets("CorporateCoordination").Range("TableCorpCoord[Staff]")

Set x = Worksheets("CorporateCoordination").Range("TableCorpCoord[Staff]").Find(What:=cboStaff.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=True)

On Error GoTo 0

For Each x In rng
With cboRecordId
.AddItem x.Offset(, 1).Value
End With
Next x
End Sub


Could someone help me understand where I have misstepped here? The items are names so do I need to be declaring something as a string to help in the find operation? The userform is the only way staff can enter their names so I don't think its a MatchCase issue..

Thanks
 
Do you have Staff names in the table that do not appear in the MacroData sheet?
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I may have accidentally added a space to one of the staff name cells in the table or something along those lines.

I cleared the contents and deleted the table rows existing and its running again. Thank you. I am going to lock the user interface once this is operational so the userforms will be the only way a user can interact with the data tables without unlocking the sheets, so this shouldn't happen again unless I'm editing and make a mistake.

Once again thank you for being able to answer the right questions to help me on my way!
 
Upvote 0
One way to help ensure that leading/trailing spaces don't cause a problem is
Code:
Private Sub UserForm_Initialize()

   Dim v1 As String, v2 As String, v3 As String
   Dim Cl As Range
   Set dic = CreateObject("scripting.dictionary")
   dic.CompareMode = vbTextCompare

   With Sheets("MacroData")
      For Each Cl In .Range("G3", .Range("G" & Rows.count).End(xlUp))
         If Not dic.exists([COLOR=#ff0000]trim[/COLOR](Cl.Value)) Then dic.Add [COLOR=#ff0000]trim[/COLOR](Cl.Value), CreateObject("scripting.dictionary")
      Next Cl
   End With
   
   With Sheets("CorporateCoordination")
      For Each Cl In .Range("TableCorpCoord[Staff]")
         v1 = [COLOR=#ff0000]trim[/COLOR](Cl.Value): v2 = [COLOR=#ff0000]trim[/COLOR](Cl.Offset(, 1).Value)
         If Not dic(v1).exists(v2) Then
            dic(v1).Add v2, Nothing
         End If
      Next Cl
   End With
   cboStaff.List = dic.keys
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,731
Messages
6,174,173
Members
452,548
Latest member
Enice Anaelle

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