Please, try the next solution, in the next way:
- Copy the next code in the sheet code module where the list validated cell exists. To do that, right click on the respective sheet name, choose
View Code and paste it in the window which appears. In this way any cell modification in the respective sheet will trigger the Change event:
Option Explicit
Private dict As Object
Private Sub Worksheet_Change(ByVal Target As Range)
Const changedCell As String = "A2" 'address where the validated list cell exists
If Target.address(0, 0) = changedCell Then 'the code works only if changedCell is changed...
Dim lastRBB As Long, shNames As Worksheet, arr
Set shNames = Worksheets("Sheet1")'set the sheet where from the data to be collected exists
lastRBB = shNames.Range("B" & shNames.rows.count).End(xlUp).Row 'last row in column B:B
arr = shNames.Range("A2:B" & lastRBB).Value2 'place the range in an array for faster processing
If dict Is Nothing Then 'if the dictionary has not been loaded (yet):
loadTheDictionary arr 'call the sub loading the dictionary
End If
Application.EnableEvents = False 'to avoid the event to be triggered again
Target.Offset(0, 1).Resize(lastRBB).ClearContents 'clear more than enough rows...
Target.Offset(0, 1).Resize(UBound(dict(Target.value)) + 1).Value2 = _
Application.Transpose(dict(Target.value))
Application.EnableEvents = True 'to prepare the next change event (to be triggered)
End If
End Sub
Sub loadTheDictionary(arr)
Set dict = CreateObject("Scripting.Dictionary")
Dim arrIt, i As Long
For i = 1 To UBound(arr)
If Not dict.Exists(arr(i, 2)) Then
dict(arr(i, 2)) = Array(arr(i, 1))
Else
arrIt = dict(arr(i, 2)) 'in order to change a dictionary array item you need
'to extract it in a variable, to add/eliminate something
'then to place it back as item:
ReDim Preserve arrIt(UBound(arrIt) + 1) 'increase the 1D array dimension with one unit
arrIt(UBound(arrIt)) = arr(i, 1) 'place the following name in the array laste element
dict(arr(i, 2)) = arrIt 'load back the processed array
End If
Next i
End Sub
Now, try playing with selecting different options in list validated cell ("A2").
If you often change the used levels, I can also supply a piece of code able to create a such list data validated cell, placing in it the unique levels...