Sub ReorgDataV2()
' hiker95, 06/18/2014, ME785271
Dim a As Variant, o As Variant
Dim i As Long, ii As Long, j As Long
Dim x As Variant, k As Long
Dim lr As Long, nlr As Long, n As Long, sr As Long
Dim brng As Range
With Sheets("Sheet1")
.Columns(4).ClearContents
lr = .Cells(Rows.Count, 2).End(xlUp).Row
a = .Range(.Cells(2, 1), .Cells(lr, 2))
nlr = CountUnique(.Range("B2:B" & lr))
ReDim o(1 To (lr - 1) + nlr, 1 To 1)
ReDim x(1 To nlr)
For i = 1 To UBound(a, 1)
On Error Resume Next
n = WorksheetFunction.Match(a(i, 2), x, 0)
If Err.Number <> 0 Then '***there was no match***
k = k + 1
x(k) = a(i, 2)
End If
On Error GoTo 0
Next i
For i = 1 To k
n = Application.CountIf(Columns(2), x(i))
If n = 1 Then
Set brng = .Range("B1:B" & lr).Find(x(i), LookAt:=xlWhole)
If Not brng Is Nothing Then
ii = ii + 1
o(ii, 1) = x(i)
ii = ii + 1
o(ii, 1) = .Cells(brng.Row, 1)
Set brng = Nothing
End If
ElseIf n > 1 Then
sr = 1
For j = 1 To n
Set brng = .Range("B" & sr & ":B" & lr).Find(x(i), LookAt:=xlWhole)
If Not brng Is Nothing Then
If sr = 1 Then
ii = ii + 1
o(ii, 1) = .Cells(brng.Row, 2).Value
ii = ii + 1
o(ii, 1) = .Cells(brng.Row, 1).Value
sr = brng.Row
Set brng = Nothing
Else
ii = ii + 1
o(ii, 1) = .Cells(brng.Row, 1).Value
sr = brng.Row
Set brng = Nothing
End If
End If
Next j
End If
Next i
.Cells(1, 4).Resize(UBound(o, 1), UBound(o, 2)) = o
.Columns(4).AutoFit
End With
End Sub
Function CountUnique(ByVal Rng As Range) As Long
'' Juan Pablo González, MrExcel MVP, 05/09/2003
'' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set Rng = Intersect(Rng, Rng.Parent.UsedRange)
St = "'" & Rng.Parent.Name & "'!" & Rng.Address(False, False)
CountUnique = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function