Find value of combobox1 selection, add adjacent cells to combobox2 without duplicates.

Mike Neal

New Member
Joined
May 24, 2020
Messages
38
Office Version
  1. 2013
Platform
  1. Windows
I have a combobox that the user selects a location from. Based on the location selected I have a second combobox that the user can select an equipment ID from and then a third combobox that the user can select a task from. Everything is working ok except for the second combobox. It populates the equipment IDs but has duplicates. I would like to remove the duplicates. Any help would be appreciated. Thank you.

Here is the code I am using for the second combobox:

Private Sub TASKS_LOCATION_SELECT_BOX_DropButtonClick()
'*********************************************************************************************************************
'This routine looks at the Location selected and populates the Equip ID combobox with ID's set to that Location.
'*********************************************************************************************************************
TASKS_EQUIP_ID_LIST.Clear 'Clears old entries from list.

Dim txtVal As String

If IsNull(Me.TASKS_LOCATION_SELECT_BOX.value) = False Then 'Looks at selected Location.
txtVal = Me.TASKS_LOCATION_SELECT_BOX.value
Else
txtVal = ""
End If

Dim rng As Range
Set rng = ThisWorkbook.Sheets("Task Due Dates").Range("C2:C750") 'Where to look for Location.
Dim rCell As Range

For Each rCell In rng.Cells 'Check each cell in range.

If rCell.value = txtVal Then 'If cell contains a Location.

With Me.TASKS_EQUIP_ID_LIST

.AddItem rCell.Offset(0, -2).value 'Add Equip ID to list.

End With

End If

Next rCell 'Check each cell.

End Sub


Here is my spreadsheet:
Equip IDTask IDLocation
R501T101REVLIS 2
R400T202REVLIS 4
R300T101REVLIS 3
R300T105REVLIS 3
R400T107REVLIS 4
R303T105AKROCHEM
R303T107AKROCHEM
R305T107REVLIS 3
R400T106REVLIS 4
R501T105REVLIS 2
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
How about
VBA Code:
   TASKS_EQUIP_ID_LIST.Clear 'Clears old entries from list.

   Dim txtVal As String
   Dim rCell As Range
      
   If Me.TASKS_LOCATION_SELECT_BOX.Value = "" Then Exit Sub
   txtVal = Me.TASKS_LOCATION_SELECT_BOX.Value
   
   With CreateObject("scripting.dictionary")
      For Each rCell In ThisWorkbook.Sheets("Task Due Dates").Range("C2:C750")
         If rCell.Value = txtVal Then .Item(rCell.Offset(, -2)) = Empty 'If cell contains a Location.
      Next rCell 'Check each cell.
      Me.TASKS_EQUIP_ID_LIST.List = .Keys
   End With
 
Upvote 0
How about
VBA Code:
   TASKS_EQUIP_ID_LIST.Clear 'Clears old entries from list.

   Dim txtVal As String
   Dim rCell As Range
     
   If Me.TASKS_LOCATION_SELECT_BOX.Value = "" Then Exit Sub
   txtVal = Me.TASKS_LOCATION_SELECT_BOX.Value
  
   With CreateObject("scripting.dictionary")
      For Each rCell In ThisWorkbook.Sheets("Task Due Dates").Range("C2:C750")
         If rCell.Value = txtVal Then .Item(rCell.Offset(, -2)) = Empty 'If cell contains a Location.
      Next rCell 'Check each cell.
      Me.TASKS_EQUIP_ID_LIST.List = .Keys
   End With
Fluff,
This code puts the list in with no duplicates but something weird seems to be happening. When I click on the TASKS_EQUIP_ID drop down there are blanks. If I click on a blank the drop down closes and there is an Equip ID present. IF I click on the drop down again everything is blank and when I click on a different blank a different Equip ID is there when the drop down closes.
 
Upvote 0
Try
VBA Code:
Me.TASKS_EQUIP_ID_LIST.List = Application.Transpose(.Keys)
 
Upvote 0
Fluff,
I changed the code like you suggested. The values are now visible but duplicates are back in the drop down. Thank you for the assistance.
 
Upvote 0
If you are getting duplicates it sounds as though some of the values may have leading/trailing spaces.
 
Upvote 0
I have the format on the sheet to center the data in the cell but I don't have any spaces (As in hitting the space bar before or after entering the data).
 
Upvote 0
Oops, missed abit, it should be
VBA Code:
  TASKS_EQUIP_ID_LIST.Clear 'Clears old entries from list.

   Dim txtVal As String
   Dim rCell As Range
      
   If Me.TASKS_LOCATION_SELECT_BOX.Value = "" Then Exit Sub
   txtVal = Me.TASKS_LOCATION_SELECT_BOX.Value
   
   With CreateObject("scripting.dictionary")
      For Each rCell In ThisWorkbook.Sheets("Task Due Dates").Range("C2:C750")
         If rCell.Value = txtVal Then .Item(rCell.Offset(, -2).Value) = Empty 'If cell contains a Location.
      Next rCell 'Check each cell.
      Me.TASKS_EQUIP_ID_LIST.List = .Keys
   End With
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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