Dynamic Drop-Down list

nymyth

Board Regular
Joined
Mar 4, 2010
Messages
104
Good Afternoon all,

I am in need of assistance. I am trying to create a dynamic drop-down list based on another drop-down list.

I have hundreds of rows of distributors in Column B and the markets they belong to in Column A. I can put the list of distributors into the drop-down, but what i would like to do is have one drop down give me a list of markets, and from that Market Selection I want the second drop-down to show me the distributors that fall in that market. Is this possible? Thanks in advance.


Mid-SouthTriangle Wholesalers Inc., GA
Mid-SouthBeverage South of Augusta, GA
Mid-SouthCoastal Beverage Co. Inc. (Wlmgtn), NC
Mid-SouthHealy Wholesale Co. Inc., NC
Mid-SouthWard Dist. Co. Inc., NC
Mid-SouthMims Distributing Co. Inc., NC
Mid-SouthLong Beverage, NC
Florida AtlanticGold Coast Beverage L.L.C., FL
Florida AtlanticDouble Eagle Dist Inc., FL
Florida AtlanticEagle Brands Sales - Miami, FL
Florida AtlanticStephens Distributing Co., FL
Florida AtlanticBrown Distributing Co of West Palm, FL
Florida AtlanticFlorida Distributing Co., FL
Florida AtlanticWayne Densch Inc., FL
Florida GulfS.R. Perrott Inc., FL
Florida AtlanticCarroll Distributing Co. Inc., FL
Florida AtlanticCity Beverages Ltd. Partnership, FL
ChesapeakePremium Distributors of VA LLC, VA
ChesapeakeChesbay Distributing L.L.C., VA
ChesapeakeBrown Distributing Company, VA
ChesapeakeTri-Cities Beverage Corp., VA

<tbody>
</tbody>
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Thanks, i've tried this, but I keep getting an error. It probably has to do with the fact that column A has multiple rows with the same name.
 
Upvote 0
What kind of an error do you get? It is OK that you have multiple rows with the same name in column A.
 
Upvote 0
Assuming your data sheet is sheet 2, columns "A & B. then try this for results in sheet1 cells "A1 & B1.
Paste the whole code into sheet 1 code module.
Code:
Option Explicit
[COLOR=navy]Dim[/COLOR] dic [COLOR=navy]As[/COLOR] Object

Private [COLOR=navy]Sub[/COLOR] Worksheet_SelectionChange(ByVal Target [COLOR=navy]As[/COLOR] Range)
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] ray(), Q [COLOR=navy]As[/COLOR] Variant, nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]If[/COLOR] Target.Address(0, 0) = "A1" [COLOR=navy]Then[/COLOR]
[COLOR=navy]If[/COLOR] dic [COLOR=navy]Is[/COLOR] Nothing or [a1] = "" [COLOR=navy]Then[/COLOR]
[COLOR=navy]With[/COLOR] Sheets("Sheet2")
    [COLOR=navy]Set[/COLOR] Rng = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
[COLOR=navy]End[/COLOR] With
    [COLOR=navy]Set[/COLOR] dic = CreateObject("scripting.dictionary")
        dic.CompareMode = vbTextCompare
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
            [COLOR=navy]If[/COLOR] Not dic.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
                ReDim Preserve ray(1)
                   nStr = Replace(Dn.Offset(, 1), ",", "")
                    ray(1) = nStr
                    dic.Add (Dn.Value), ray
            [COLOR=navy]Else[/COLOR]
                 Q = dic(Dn.Value)
                  ReDim Preserve Q(UBound(Q) + 1)
                     nStr = Replace(Dn.Offset(, 1), ",", "")
                    Q(UBound(Q)) = nStr
                dic(Dn.Value) = Q
             [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR]

[COLOR=navy]With[/COLOR] Range("A1").Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(dic.keys, ",")
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]

Private [COLOR=navy]Sub[/COLOR] Worksheet_Change(ByVal Target [COLOR=navy]As[/COLOR] Range)

[COLOR=navy]If[/COLOR] Target.Address(0, 0) = "A1" [COLOR=navy]Then[/COLOR]
    [COLOR=navy]With[/COLOR] Target.Offset(, 1).Validation
        .Delete
        .Parent.ClearContents
        .Add Type:=xlValidateList, Formula1:=Join(dic(Target.Value), ",")
    [COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Hey Mick,

I have made some tweaks to the formula, but I am getting stumped when the second selection is a merged cell. I know we really shouldn't use merged cells and it's better to use 'center across selection', but this puts my dropdown button in the middle. Is there any chance the last part of this code can be adjusted to show the list in a merged cell?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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