Hi there, first post on here and pretty novice at Excel VBA.
I found some great code which works really well for outputting a single column's distinct values with a count of occurrences (used for sort order) to a ListBox via another worksheet but I was wondering how I could expand the range to link multiple columns.
I am trying to improve a data entry spreadsheet so that if a user searched for a geographic place name in the ListBox it would also fill country and all the administrative subdivisions into their respective cells in the active cell row. The maximum columns I would need to use is 4 in the range("U2:Y" & LastRow) Where U is the highest level (country) and the user would be searching from the lowest level available from the source material.
Any thoughts/pointers would be greatly appreciated!
Thanks in advance, Ali
I found some great code which works really well for outputting a single column's distinct values with a count of occurrences (used for sort order) to a ListBox via another worksheet but I was wondering how I could expand the range to link multiple columns.
I am trying to improve a data entry spreadsheet so that if a user searched for a geographic place name in the ListBox it would also fill country and all the administrative subdivisions into their respective cells in the active cell row. The maximum columns I would need to use is 4 in the range("U2:Y" & LastRow) Where U is the highest level (country) and the user would be searching from the lowest level available from the source material.
Any thoughts/pointers would be greatly appreciated!
Thanks in advance, Ali
VBA Code:
Sub Summarize(rngSource As Range, rngTarget As Range)
Dim d As New Scripting.Dictionary
Dim rng As Range
Dim var As Variant
For Each rng In rngSource
If d.Exists(rng.Value) Then
d(rng.Value) = d(rng.Value) + 1
Else
d.Add rng.Value, 1
End If
Next rng
rngTarget = "Value"
rngTarget.Offset(, 1) = "Count"
Set rng = rngTarget.Offset(1)
For Each var In d.Keys
rng = var
rng.Offset(, 1) = d(var)
Set rng = rng.Offset(1)
Next
End Sub