Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel: Dependent Dropdown Lists on Dynamic Content

Tags:

excel

I've imported a list of clients and their websites from the Google Analytics (GA) API in the following format:

Account     Profile
Client 1    www.client1.com
Client 1    sub.client1.com
Client 1    tst.client1.com
Client 2    www.client2.com
Client 3    www.client3.com
Client 3    sub.client3.com

What I need is a dropdown list with the unique accounts names and then a second dropdown list with the dependent profiles of the chosen account. Something that looks like this:

Account     Profile
Client 1    www.client1.com
            sub.client1.com
            tst.client1.com

We add accounts and profiles to GA all the time, so the list will need to be refreshed every time we run the report.

How would I go about doing this?

like image 776
Kurt Avatar asked Feb 13 '26 10:02

Kurt


1 Answers

As long as you have this list sorted by Account, you can use a mixture of Offset CountA to give you all the ranges you need.

Steps would be:

a. Get unique values from Account column (i'm using VBA for this step but you could use Frequency formula to mash this too -- I'd avoid it if you can use VBA for this). Something along the lines:

    Sub FindUniqueValues(SourceRange As Range, TargetCell As Range)
        SourceRange.AdvancedFilter xlFilterCopy, , TargetCell, True
    End Sub

(I've called this from my worksheet_change event for ease but if you are using vba to pull G.A. data, just integrate into that)

b. Feed this to Named Range (Say AccountsRange) that has the forumla =$D$2:INDEX($D$2:$D$5000,COUNTA($D$2:$D$5000)) -- This assumes you specified D1 as TargetCell for previous step and unique values won't be more than 4999 (1st value is row title in my case -- Hence starting from D2)

c. Add this Named Range AccountsRange as source for Data Validation List -- Your account combo box is done at this point. I'm going to assume you put this combo box in E2 and I'll build on this to derive the second ComboBox

d. Create a new Named Range say ProfilesRange with formula =INDEX($B$1:$B$5000,MATCH($E$2,$A$1:$A$5000,0),1):INDEX($B$1:$B$5000,COUNTIF($A‌​$1:$A$5000,$E$2)+MATCH($E$2,$A$1:$A$5000,0)-1,1) Note: if you copy paste from here the above formula will contain a line-break invisible character after COUNTIF($A and excel will complain about invalid formula, just retype $A$1 part of COUNTIF and it should be fine

A2 is the start of Account column of G.A. imported data (A1 being the title and assuming maximum of 4999 data rows here) E2 is the Comboboxed cell from previous AccountsRange dropdown step. -1 in Match step is needed as the offset needs to be 0 for first value row and not 1 which will be returned from Match. Second 1 is column offset (it can be more if you need to hop several columns to get secondary data). CountIf is pretty self explanatory :) This basically gets us the number of cells we need in final range from the starting point.

e. Create your new Data Validation List that uses ProfilesRange as its source and your secondary combo box is done.

Issues that may arise:

  1. When you change Accounts Combobox, Profiles combobox will probably error out as the values in profiles are unlikely to be present in new client. You could either ignore the error (it will fix itself once you select correct value from newly refreshed dropdown or throw a bit more of VBA fairy dust in there to clear the drop down cell when the value changes in E2.

  2. Data Validation List compalains about evaluating to an error when you try to setup ProfilesRange. It is safe to ignore this error and continue. It basically means nothing computable was returned by ProfilesRange (most probably because you haven't selected anything in AccountsRange that actually computes to a ProfilesRange -- of course assuming your actual cell ranges for formulae used here are actually correct for your sheet)

Thanks to osknows for his tip and improvement on forumlae (in comments below): Avoid use of OFFSET its very slow over large ranges and is volatile. =OFFSET($D$2,0,0,COUNTA($D$2:$D$5000) can be replaced with =$D$2:INDEX($D$2:$D$5000,COUNTA($D$2:$D$5000)) for the named range. (this tip has been incorporated into this solution now, so you won't see the original Offset forumlae unless you check version history)

like image 132
Maverik Avatar answered Feb 16 '26 00:02

Maverik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!