Hello,
Am trying to get dynamic population of 2nd combobox based on match from criteria in combobox 1.
if column a = bears and column b = colours of bears then
when I select bears in combobox one, combobox 2 would populate with colors of bear.
I am think of having a combobox 1 change event that evaluates each row in a specific range (does it match the criteria?) if so, then add 2nd cell (column b) of that row to the combobox 2.
I know it would probably involve match and offset, add item and loop, but I am not sure what the syntax is.
Please advise
Here are a couple of places to look:
http://en.allexperts.com/q/Excel-105...es-other-1.htm
http://www.ozgrid.com/VBA/dependent-combobox.htm
or you can use the easier Data Validation:
http://www.contextures.com/xlDataVal02.html
Microsoft MVP - Excel
Where there is a will there are many ways. Pick One!
Please read the Forum Rules
If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below
Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.
Preferred Charities: Lupus Canada and Sick Kids Foundation.
Feel Free to Donate if you want to, for the assistance you received today.
To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.
If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.
Doing this will ensure you get the result you need!
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
Check out the free Excel Toolbar
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Code Tags: Make your code easier for us to read
You forgot the link you promised in the other post:
http://www.excelforum.com/excel-prog...ing-match.html
Microsoft MVP - Excel
Where there is a will there are many ways. Pick One!
Please read the Forum Rules
If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below
Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.
Preferred Charities: Lupus Canada and Sick Kids Foundation.
Feel Free to Donate if you want to, for the assistance you received today.
okay, this is what I mean,
looking at a list with two levels of classification, it will populate the 2nd combobox with only the items that match the criteria specified in the 1st combobox
eg. If I select Big Cats in the 1st combobox, the 2nd combobox will offer the choices of those rows that have "Big Cats" in the first column, thus offering choices like "lynx","lion" etc.
I am trying it this way as the real sheet as at least 200 items and I'll be darned if I'm gonna make that into a dependant list setup.
Try this code in the combobox1 change event
Code:Private Sub ComboBox1_Change() Dim cmb1 As String Dim rng As Range Dim frng As Range With Application .ScreenUpdating = False ComboBox2.Clear cmb1 = ComboBox1.Value Set rng = Range("A6").CurrentRegion rng.AutoFilter Field:=1, Criteria1:=cmb1 Set frng = Range(rng.Range("B2"), rng.Range("B1").End(xlDown)).SpecialCells(xlCellTypeVisible) For Each r In frng ComboBox2.AddItem r Next r ActiveSheet.ShowAllData .ScreenUpdating = True End With End Sub
wow. Thank you. Exactly what I was looking for.
Question, could you explain the code a little bit,
especially the portion with references to B1 and B2
and what is r mean?
This needs the lists separating, but should be easier to maintain
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
Check out the free Excel Toolbar
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Code Tags: Make your code easier for us to read
BigBas,
I've used your example as it will be easier on the user to add items rather then dealing with separated lists.
the problem is that:
1) for criteria that acutally do not have any items in the list, the macro hangs and excel crashes. E.g. if I select "dogs" and there is only "Big Cats" and "Bears" in the list, error occurs.
Can you help with some error handling code so that combobox2 says "none availible" if that error occurs?
2) can an evaluation portion be added so that:
if the 7th column of r = "availilble" then it gets added to combobox2,
else it doesn't get added?
If you could provide any insight on this, I would be most appreciative
Kuraitori:
There are going to be a few approaches to best resolving your problem; it will be based on which works best for you.
1. I personally think your best bet is to limit combobox selections to options in the list. This way, an end user cannot select an option that is not on the list, thereby resolving the problem. If you absolutely must allow that a user type in unavailable options, I have added an amended code. Instead of entering the code in the combobox_change() even, it will be entered in the combobox_lostFocus() event. The disadvantage to this is that the code does not fire until the entry in completed. In order to be complete, the user will have to click outside of the textbox.
2. I'm not sure I understand what you need. Can you please update your dummy data sample so I can see what you are looking for?Code:Private Sub ComboBox1_LostFocus() Dim cmb1 As String Dim rng As Range Dim frng As Range With Application .ScreenUpdating = False ComboBox2.Clear cmb1 = ComboBox1.Value If Sheets("Sheet1").ComboBox1.MatchFound = False Then MsgBox "None Available" Exit Sub Else Set rng = Range("A6").CurrentRegion rng.AutoFilter Field:=1, Criteria1:=cmb1 Set frng = Range(rng.Range("B2"), rng.Range("B2").End(xlDown)).SpecialCells(xlCellTypeVisible) For Each r In frng ComboBox2.AddItem r Next r End If ActiveSheet.ShowAllData .ScreenUpdating = True End With End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks