Populate combobox with unique value from a range

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hej,
I want to populate combobox with unique value from a range.
the code working not properly, combobox not sorted and fylls allso with empty line (se the result/picture), (the code works only if the value is in a column and do not hanve eny empy cell)
thanks for help,
here is the code and the data.

VBA Code:
Dim UfDic As Object
Dim Cl As Range

Set UfDic = CreateObject("scripting.dictionary")
UfDic.CompareMode = 1
With Sheets("Sheet1")
  For Each Cl In .Range("AG2", "AI17")
    If Not UfDic.exists(Cl.Value) Then UfDic.Add Cl.Value, CreateObject("scripting.dictionary")
      Set UfDic(Cl.Value)(Cl.Offset(, -1).Value) = Cl
  Next Cl
End With
Me.Page8ComboBox1.List = UfDic.keys

Year1Year2Year3
201720182019
202020212022
202020212022
2018
2015
2016
20172018
2020
2015
2016
2017
201820192020
2019
2019
2018
20212022
 

Attachments

  • Unique ComboBox.PNG
    Unique ComboBox.PNG
    4.3 KB · Views: 11
Without any object

VBA Code:
Private Sub UserForm_Initialize()
  Dim ar As Variant, tmp As Variant, i As Long, j As Long, x As Long, y As Long, a As Long
  Dim sq() As Variant
  
  ar = Sheets(1).Range("AG2:AI17")
  
  For i = 1 To UBound(ar) * UBound(ar, 2)
     x = (i - 1) \ 3 + 1
     y = (i - 1) Mod 3 + 1
     If InStr(Join(sq, "|"), ar(x, y)) = 0 Then
        ReDim Preserve sq(a)
        sq(a) = ar(x, y): a = a + 1
     End If
  Next
  
  For i = 0 To a - 1
     For j = i + 1 To a - 1
        If sq(j) < sq(i) Then tmp = sq(j): sq(j) = sq(i): sq(i) = tmp
    Next
 Next
  
 Me.Page8ComboBox1.List = sq
End Sub
 
Upvote 0
Solution

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Or

VBA Code:
Private Sub UserForm_Initialize()
  Dim ar As Variant, tmp As Variant, i As Long, j As Long, x As Long, y As Long, a As Long
  Dim sq() As Variant
  
  For Each it In Sheets(1).Range("AG2:AI17")
     If InStr(Join(sq, "|"), it.Value) = 0 Then
        ReDim Preserve sq(a)
        sq(a) = it.Value: a = a + 1
     End If
  Next
  
  For i = 0 To a - 1
     For j = i + 1 To a - 1
        If sq(j) < sq(i) Then tmp = sq(j): sq(j) = sq(i): sq(i) = tmp
     Next
  Next
  
  Me.Page8ComboBox1.List = sq
End Sub
 
Upvote 0
Or

VBA Code:
Private Sub UserForm_Initialize()
  Dim ar As Variant, tmp As Variant, i As Long, j As Long, x As Long, y As Long, a As Long
  Dim sq() As Variant
 
  For Each it In Sheets(1).Range("AG2:AI17")
     If InStr(Join(sq, "|"), it.Value) = 0 Then
        ReDim Preserve sq(a)
        sq(a) = it.Value: a = a + 1
     End If
  Next
 
  For i = 0 To a - 1
     For j = i + 1 To a - 1
        If sq(j) < sq(i) Then tmp = sq(j): sq(j) = sq(i): sq(i) = tmp
     Next
  Next
 
  Me.Page8ComboBox1.List = sq
End Sub
hej JEC,
tanks,
your first codes works very nice.
the second code, get error on linje
VBA Code:
For Each it In Sheets(1).Range("AG2:AI17")
It says Variable "it" not defined.
 
Upvote 0
Ahh yes , add a line at top: Dim It as variant
 
Upvote 0
Ahh yes , add a line at top: Dim It as variant
hi JEC,
the second codes works nice too, thans for help
with the bellow code I want to preselect Page8ComboBox1 with current year,
but not working, the combobox contain value year 2021, but do not preselect.
VBA Code:
Page8ComboBox1.Value = Year(Date)
 
Upvote 0
What you want is only possible with a listbox.
Add a listbox1 to your userform and try this. This does not set default value, it just preselects

VBA Code:
Private Sub UserForm_Initialize()
 Dim i As Long
 With ListBox1
   .List = Array(2020, 2021, 2022, 2023)
    For i = 0 To UBound(.List)
      If .List(i) = Year(Date) Then .Selected(i) = True: Exit For
    Next
 End With
End Sub
 
Upvote 0
What you want is only possible with a listbox.
Add a listbox1 to your userform and try this. This does not set default value, it just preselects

VBA Code:
Private Sub UserForm_Initialize()
 Dim i As Long
 With ListBox1
   .List = Array(2020, 2021, 2022, 2023)
    For i = 0 To UBound(.List)
      If .List(i) = Year(Date) Then .Selected(i) = True: Exit For
    Next
 End With
End Sub
it works nice, tanks for help JEC,
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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