1

My data is arranged in this way:

rowA     B                   
1   ABC Description 1
2   XYZ Description 2
3   MNODescription 3
4   ABC Description 4
5   MNODescription 5

Now in another worksheet in cell A1 there should be a drop down list based on Column A above. On Clicking it ABC, XYZ & MNO shall be shown in drop down. Then when I select, say, ABC in A1; in B1 both the descriptions (Description 1 & Description 4) shall be shown in a drop down list.

Michael
  • 32,527
  • 49
  • 210
  • 370
ET90
  • 11
  • 1

2 Answers2

0

I wrote this function which concatenates all matches:

Public Function ConcatMatches(ByRef rgFind As Range, ByRef rgSource As Range, ByVal lngOffset As Long) As String
  Dim rgHit As Range, firstAddress As String, noWrap As Boolean
  Set rgHit = rgSource.Find(rgFind.Value)

  'ensure no wrapping occurs to avoid infinite loops
  firstAddress = rgHit.Address
  noWrap = True

  Dim concat As String
  While Not (rgHit Is Nothing) And noWrap
    If concat <> "" Then
        concat = concat & ", "
    End If
    concat = concat & rgHit.Offset(0, lngOffset)

    'find next and ensure we didn't wrap back to first hit
    Set rgHit = rgSource.Find(rgFind.Value, rgHit)
    noWrap = (firstAddress <> rgHit.Address)
  Wend

  ConcatMatches = concat
End Function

And this function to display only the unique values from a range (for the data validation), enter it as an Array Formula + use a dynamic named range. I show how to use them below:

Public Function GetUniques(rgList As Range) As Variant
    'prepare return array matching calling range dimensions
    Dim CallerRows As Long, CallerCols As Long, CallerAddr As String
    Dim RowNdx As Long, ColNdx As Long, v As Variant
    With Application.Caller
        CallerRows = .Rows.Count
        CallerCols = .Columns.Count
    End With
    Dim Result() As Variant: ReDim Result(1 To CallerRows, 1 To CallerCols)
    'fill with result with blank strings
    For RowNdx = 1 To CallerRows
        For ColNdx = 1 To CallerCols
            Result(RowNdx, ColNdx) = ""
        Next ColNdx
    Next RowNdx

    'filter out uniques
    Dim dict As Variant: Set dict = CreateObject("Scripting.Dictionary")
    For Each v In rgList.Cells
        dict(v.Value) = 1
    Next v

    'push uniques to first column of resulting array
    RowNdx = 1
    For Each v In dict.Keys()
        Result(RowNdx, 1) = v
        RowNdx = RowNdx + 1
    Next v
     GetUniques = Result
End Function
  1. Enter the formula as shown on the picture below and press CTRL+SHIFT+ENTER:

Enter Formula

  1. Open the name manager with CTRL+F3 and define a dynamic named range with the following formula =OFFSET(Sheet4!$C$2,0,0,MATCH("*",Sheet4!$C$2:$C$6,-1),1):

dynamic named range

  1. Use the Dynamic named range as a list for the data validation:

Data Validation with Unique codes

  1. works as expected:

Final Result

Notice : The array formula is not dynamic to the values entered and may need to be updated to match the number of rows when additional rows are added - remember to always press CTRL+SHIFT+ENTER when updating the range.

UDF entered as Array Formula:

UDF entered as Array Formula

Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41
  • the named range created automatically is limited, editing the named range definition through the name manager CTRL+F3 and following this guide: http://www.ozgrid.com/Excel/DynamicRanges.htm will give you flexible named ranges – Vincent De Smet May 08 '15 at 10:33
  • ok, this made me break my head: FindNext doesn't work from a UDF: http://stackoverflow.com/questions/28715135/udf-using-findnext-seems-to-abort-without-warning – Vincent De Smet May 08 '15 at 11:32
  • This is what I used to write the UDF returning unique values (which needs to be entered as a Array Formula: http://www.cpearson.com/excel/returningarraysfromvba.aspx – Vincent De Smet May 08 '15 at 13:44
  • Hey Vincent De Smet. Thanks for taking time to respond to my question. However, I am not yet able to get the answer as to how to use it. Also, when I see your pictures above it shows that all the descriptions are concatenated and then can be shown in drop down. But I want them separately. – ET90 May 12 '15 at 06:42
  • Suppose if in cell D1 I select ABC from dropdown, then in cell E1 there shall be a drop down which lists Description 1 & Description 4 thereby enabling me select either Description 1 or Description 4 as per my choice. Likewise, if I select MNO in D1 (from dropdown) then in the drop down of cell E1, the list should show Description 2 & Description 5 separately so that I may choose either Description 2 or Description 5. Like this, the results in drop down of cell E1 are dependent on the value of cell D1. – ET90 May 12 '15 at 06:43
  • wow, I see - I really am failing reading comprehension :) - I just re-read your question and I misunderstood it (again). In this case, we'd need 2 vba functions that are entered as array formulas and 2 dynamic ranges over those array formulas. It's possible, I will try to do this later. – Vincent De Smet May 12 '15 at 07:44
-1

Please check my approach for this:

  1. Data need not be sorted
  2. Supports Many to Many relation
  3. No VBA
  4. No named ranges
  5. Don't need to know unique items names or amount beforehand

OPEN IMAGES IN NEW TAB TO SEE BETTER:

In Sheet1 (The Data sheet): enter image description here
As formulas: enter image description here

In Sheet2 (The helper sheet): enter image description here
As formulas:
enter image description here

In Sheet3 (The result sheet): enter image description here

Israel
  • 1,184
  • 2
  • 13
  • 26