This thread was amazingly helpful to me..
So basically I tried to measure the Run Time myself too..
All 4 Codes do almost similar Run-Times, but these 2 (UsingArray & KyleLoops) are almost instantaneous..
I used only 50K rows (Which isn't too much, but as much as I would normally require), so that Transpose wont cause issues.
Sub UsingArray()
Dim InputVal As String, R As Variant, Counter As Long, NewVal(), C As Long
InputVal = Range("E1").Value2
R = Range("A1").CurrentRegion.Value2
ReDim NewVal(1 To UBound(R))
For Counter = LBound(R) To UBound(R)
If R(Counter, 1) = InputVal Then
C = C + 1
NewVal(C) = R(Counter, 2)
End If
Next Counter
ReDim Preserve NewVal(1 To C)
Range("F1").Value2 = InputVal & " " & Join(NewVal, ",")
End Sub
Sub KyleLoops()
Dim InputVal As String
Dim R As Variant, Counter As Long, Temp As String
InputVal = Range("E4").Value2
R = Range("A1").CurrentRegion.Value2
For Counter = LBound(R) To UBound(R)
If R(Counter, 1) = InputVal Then Temp = Temp & R(Counter, 2) & ","
Next Counter
Range("F4").Value2 = InputVal & " " & Left$(Temp, Len(Temp) - 1)
End Sub
Sub UsingDictionary()
Dim Dict As Scripting.Dictionary, R As Variant, Counter As Long, InputVal As String
Set Dict = New Scripting.Dictionary
InputVal = Range("E2").Value2
With Dict
R = Application.Transpose(Range("A1:B" & Cells(Rows.Count, 1).End(xlUp).Row).Value2)
For Counter = LBound(R, 2) To UBound(R, 2) Step 1
If .Exists(R(1, Counter)) Then
.Item(R(1, Counter)) = .Item(R(1, Counter)) & "," & R(2, Counter)
Else
.Add (R(1, Counter)), R(2, Counter)
End If
Next Counter
Range("F2").Value2 = InputVal & " " & .Item(InputVal)
End With
End Sub
Sub UsingLoops()
Dim LR As Long, Counter As Long, InputVal As String, A
LR = Cells(Rows.Count, 1).End(xlUp).Row
InputVal = Range("E3").Value2
For Counter = 1 To LR
If Cells(Counter, 1).Value2 = InputVal Then
A = A & "," & Cells(Counter, 2).Value2
End If
Next Counter
Range("F3").Value2 = InputVal & " " & Right(A, Len(A) - 1)
End Sub
Bookmarks