I want the following in Excel:
Two dropdown lists in adjacent cells:
Dropdown list #1 | Dropdown list #1
Dropdown list 1:
One
Two
Three
This solution avoids the use of the volatile OFFSET
function.
For the first Data Validation
located at A2
use this formula:
=$D$1:$F$1
For the second Data Validation
located at B2
use this formula:
= INDEX( $D$2:$F$4, 0, MATCH( $A$2, $D$1:$F$1, 0 ) )
The second formula uses the value selected from the data Validation
in A2
to MATCH
the corresponding column in D1:F1
and applies the column number to the INDEX
function to return the whole column in range D2:F4
.
The function INDEX( reference, row number, column number )
returns the entire column
or row
of the reference
by entering 0 as the row number
or column number
respectively.
Update as promised:
When you're using a List for validation, you have to input a range as shown below.
The OFFSET function allows to to dynamically set a range based on its input criteria.
If you consider this:
=OFFSET(C1,0,0,1,1)
In this instance, the range returned would be C1
as we have no row or column offset and height and width is set to 1
The MATCH function will return an index of where a value appears in a range of cells (range must be either 1 cell wide or 1 cell high)
Based on the above screen print =MATCH("Group2",D1:F1,0)
will return 2, as "Group2" appears in the second cell in the D1:F1
range. ("Group1" would return 1, "Group3" would return 3, and "Group4" would return #N/A as it doesn't exist).
So based on that we can put the MATCH
function in as our 2nd argument in the OFFSET
function, and pick the column that matches the first argument in the MATCH
function.
=OFFSET(C1,0,MATCH("Group2",D1:F1,0),1,1)
will return back range E1
as we've shifted the columns by 2 from C1
because of the MATCH
=OFFSET(C1,1,MATCH("Group2",D1:F1,0),3,1)
will now return back E2:E4
as we've increased the height of the range to 3 and the row offset to 1.
And finally we can change the "Group2" value in the MATCH
function to a cell value that will mean the range will dynamically change.
Here I've used Cell A2
=OFFSET(C1,1,MATCH(A2,D1:F1,0),3,1)
so whatever value is in cell A2
will be used to offset the range.
And the last thing to do is to put the dynamic range into the validation (I used B2
)
This will dynamically set the validation range.
When I'm using OFFSET
function with multiple arguments and I'm not sure that it's returning back the right range, I wrote a small helper User Defined Function that I just put in a VBA module.
Public Function GetAddress(rng As Range) As String
GetAddress = rng.Address
End Function
This allows me to put the offset formula in and it will return back the range address. So I can make sure it's right.
There may be a built in function for this, but I've never found it.