Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

help fill dynamic combobox based on column

0

hello

i   have   two   codes    don't   work  both    i  no  know   why     what  i  want  filling   dynamically  combobox   when  i   dropdown combobox    also    if   contain    col   b   duplicted  value    should  ignore the

Sub ComboBox1_DropButton_Click()

    Dim i As Range

    With Sheets("sheet1")
        Set i = .Range("b2:b" & .Range("b" & .Rows.Count).End(xlUp).Row)
    End With
    Me.ComboBox1.ListFillRange = i.Address

End Sub

Private Sub ComboBox1_Change()
ComboBox1.RowSource = ""
ComboBox1.List = ActiveSheet.Range("B2", ActiveSheet.Range("B" & Rows.Count).End(xlUp)).Value


End Sub
Answer
Discuss

Answers

0
Selected Answer

Leopard,

I think the first problem in ComboBox1_Change() is that you set i as a Range but, in fact, the .ListFillRange property is a String (which describes a range).

In the attached spreadsheet Combobox listfill..., this is shown as follows... You'll see a light blue ComboBox in column C. If you click the Down arrow, you will see the list reflects (originally) B2:B4 (i.e. -, a, b and c). (If you pick a value, this simple macro will confirm your selection:

Private Sub ComboBox1_Change()
MsgBox "User chosen value= " & ComboBox1.Value 'Confirm the user's choice after click

End Sub

and I guess you'd expect the Change sub to do something with the value selected, not to modify the ComboBox).

The following static sub can be run to show that the string approach works. Run it from the VBA Editor and you'll see the range changes to cover B2:B9 (end of used range in B). If you add values in B10 etc. and run the macro again, it should extend the Listfill to include any new values.

Private Sub Set_CB_List()

Dim i As String

i = "B2:B" & Range("B" & Rows.Count).End(xlUp).Row ' Form a string, detecting end of range in column B

ComboBox1.ListFillRange = i 'Set the listfill proprty to the new string

End Sub

Now while that works in by a static way, it doesn't seem to work dynamically e.g. on the ComboBox1_DropButton_Click() event.

However I found the article on another website Populate a userform combo box in Excel with a dynamic list

Revision 2: I have now tried to follow the instructions in that link and it works (with correctly named sheet, ComboBox and Range Name), adding new colours added to the range Revision 3 It now has a Command Button which launches the UserForm (where the Combobox list changes dynamically in size and content as expected) - see second attached file Dynamic list in ComboBox with button v0_b.xlsm

Credit go to the authors of that webpage naturally but if this works, kindly mark this Answer as Selected

Revision 4: If your data is stored in a table, an alternative approach is suggested by Don's video tutorial Automatically Updating Dependent Drop Down List Menus in Excel.

The first 3 minutes of that illustrate that a table "extends itself" for data validation purposes (in the video) but it also works for a Combobox :-).

In the attached file Combobox listfill from table test v0_b.xlsm, I've written some data in cells E3:E9 then selected them and used "Insert Table" to convert them to a table (with a header row). I then created the pink-purple Comboxbox to the right and in Properties, set ListFillRange to E4:E9.

That populates the ComboxBox with that range BUT if you enter a value in the next cell E10, that is added to the list (and the ListFillRange now appears as E4:E10). 

Discuss

Discussion

hi, john    actually   after  i   applied  many  steps   form  what  you  provide  website  it  gives  me  error in this  line 
For Each rngColor In ws.Range("colorlist")

 method  range  of  object "workseet failed 
leopard (rep: 88) Nov 12, '20 at 2:24 pm
Hi Leopard. 

Did you try my macros anyway? Did you consider using the Set_CB_List() code but with the open workbook event? 

As I said, I didn't try that solution (but provided the link for you to do so).

Given what you say, have you checked that you have both a range named as "colorlist" and a worksheet called LookupLists? (The latter is needed to match one part of their subroutine:
Set ws = Worksheets("LookupLists")
John_Ru (rep: 6142) Nov 12, '20 at 2:47 pm
Leopard,

See revised Answer (last paragraphs) and new file. I suspect you didn't apply a name correctly when you tried.
John_Ru (rep: 6142) Nov 13, '20 at 3:24 am
i 'm  sure  about  names of  sheet  and  range  i  updated  a new  file  you can check 
leopard (rep: 88) Nov 13, '20 at 7:28 am
Leopard

I can't see what's wrong with your file (and note that you changed formulae to refer to Sheet1 instead) but the code from the webpage link DID work correctly for me (as per my new file in the thoird revision of my Answer, which now has a button to launch the UserForm, as yours does). Please try it!

This is not meant to be a discussion forum so I don't intend to work more on this.

I'm sorry that you don't feel that I answered your question- I tried my best and gave a few suggestions.
John_Ru (rep: 6142) Nov 13, '20 at 11:18 am
thanks  john   for  theses  files  actually   they  work   i  no  know   where  is  the  problem    i  try   finding out    and  i  consider   you  answered   my  question
thanks   again 
leopard (rep: 88) Nov 15, '20 at 2:13 pm
Leopard- please see the revisied answer (from Revision 4). Perhaps this approach might work for you.
John_Ru (rep: 6142) Nov 18, '20 at 2:11 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login