My data table looks like this:
Router Name
ABC QQQ
ABC SPY
ABC GOOG
EFG QQQ
EFG GOOG
I m trying to create this table:
Name Router
QQQ ABC,EFG
SPY ABC
GOOG ABC,EFG
Thanks in advance.
You can do it fairly easily with a macro.
This macro assumes your data is in columns A and B, and you want the results to start in column D. You may need to change the range assignments to suit.
It also assumes that router and name are in adjacent columns, and that router is in the leftmost column. Some code may need to be changed if this is not the case.
To enter this Macro (Sub), <alt-F11> opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.
To use this Macro (Sub), <alt-F8> opens the macro dialog box. Select the macro by name, and <RUN>.
===============================
Option Explicit
Sub CreateTable()
Dim rRouter As Range, rName As Range, c As Range
Dim sFirstAddress As String
Dim rDest As Range
Dim vResults() As Variant
Dim i As Long
Dim collName As Collection
Set rRouter = Range("A1", Cells(Cells.Rows.Count, "A").End(xlUp))
Set rName = rRouter.Offset(columnoffset:=1)
Set rDest = Range("D1")
'Get Unique List of Names
Set collName = New Collection
On Error Resume Next
For Each c In rName
collName.Add Item:=c.Value, Key:=CStr(c.Text)
Next c
On Error GoTo 0
'set up results array
ReDim vResults(0 To 1, 1 To collName.Count)
For i = 1 To collName.Count
vResults(0, i) = collName(i)
Next i
'get routers associated with each name
For i = 2 To UBound(vResults, 2) 'i = 1 --> Label
With rName
Set c = .Find(what:=vResults(0, i), LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=False)
sFirstAddress = c.Address
Do
vResults(1, i) = vResults(1, i) & "," & c.Offset(columnoffset:=-1).Value
Set c = .FindNext(after:=c)
Loop While Not c Is Nothing And c.Address <> sFirstAddress
End With
vResults(1, i) = Mid(vResults(1, i), 2)
Next i
vResults(1, 1) = "Routers"
'output results
Set rDest = rDest.Resize(rowsize:=UBound(vResults, 2), columnsize:=2)
rDest = WorksheetFunction.Transpose(vResults)
End Sub
============================