VBA 2 comboboxes with NOT-unique values

MarioExcelNoob

New Member
Joined
Nov 9, 2018
Messages
6
hello all

I've been searching for this silly issue for 2 hours online now, on all kind of forums, but without success....
So i'm making this post, hoping someone can help me with my issue.

It shouldn't be very hard, but for some reason, i keep missing something.

Basicly i have a userform with 2 comboboxes. cmbPostalcode and cmbCity.
Now, in Belgium we can have multiple cities with the same postalcode (mainly if smaller villages around bigger cities).
The goal is simple enough: select a postal code, fill the second combobox with only the cities corresponding to that postal code (could be 1 city, could be 5...)

I have the table tblPostcodes in sheet "Lists".
postalcodes in column 'U', cities in 'V'. The columns have headers, so data starts on row 2.

I found how to fill cmbPostalcode with the UNIQUE values from that table, but i fail to link the corresponding cities in the cmbCity combobox.

the current code that is not working:
(i dont get errors, cmbCity is just not getting filled)

VBA Code:
Private Sub cmbPostalcode_DropButtonClick()
Dim vList, d As Object, i As Long
vList = Sheets("Lists").Range("U2", Sheets("Lists").Cells(Rows.Count, "U").End(xlUp)).Value
    Set d = CreateObject("scripting.dictionary")
    d.CompareMode = vbBinaryCompare
    For i = LBound(vList) To UBound(vList)
          d(vList(i, 1)) = 1
    Next
       cmbPostalcode.List = d.keys
       cmbCity.Text = ""
End Sub


Private Sub cmbCity_DropButtonClick()
Dim vList, d As Object, i As Long

 vList = Sheets("Lists").Range("U2", Sheets("Lists").Cells(Rows.Count, "V").End(xlUp)).Value

    Set d = CreateObject("scripting.dictionary")
    d.CompareMode = vbBinaryCompare
    
    For i = LBound(vList) To UBound(vList)
    If vList(i, 1) = cmbPostalcode.Value Then d(vList(i, 2)) = 1
    Next
    cmbCity.List = d.keys
End Sub


thanks in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
There is nothing wrong with the code & it works for me.
Are columns U & V the same length?
 
Upvote 0
There is nothing wrong with the code & it works for me.
Are columns U & V the same length?

Hello Fluff

Thanks for the answer.

Yes the columns are the same length (both down to row 2766).
I've been staring at this code and my comboboxes for a typo or something, but can't find anything.
 
Upvote 0
Are your postcodes numerical?
 
Upvote 0
In that case try
VBA Code:
    If vList(i, 1) = Val(cmbPostalcode.Value) Then d(vList(i, 2)) = 1
All comboBox values are text
 
Upvote 0
Solution
In that case try
VBA Code:
    If vList(i, 1) = Val(cmbPostalcode.Value) Then d(vList(i, 2)) = 1
All comboBox values are text
god... finaly, it works! :D
thank you very much! I would've gone mad over this.
i'll be sleeping good tonight now.

thank you again, have a nice evening!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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