Hi..
Here's another that should work pretty fast..
Bumbled my way through this.. still trying to grasp Dictionaries.. snb's website is a life saver..
Note: I have assumed that the real names won't be like "Name 1, Name 2" etc etc.. and that they will be like "Bob, Billy, James" etc
Trying to sort Names that included numbers (like in your sample) was a pain.. so i hope my assumption is correct?
Private Sub CommandButton1_Click()
Dim Z, x, i As Long, cnt As Long
With Range("A1").CurrentRegion
.Sort [A1], 1, , , , , , xlYes
End With
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
Z = Range("A1").CurrentRegion
For i = 2 To UBound(Z)
If Z(i, 1) <> "" Then
If Not .Exists(Z(i, 1)) Then
cnt = 1
.Item(Z(i, 1)) = Z(i, 2) & "|" & Z(i, 3) & "|" & Z(i, 4) & "|" & cnt
Else
cnt = cnt + 1: .Remove Z(i, 1)
.Item(Z(i, 1)) = Z(i, 2) & "|" & Z(i, 3) & "|" & Z(i - (cnt - 1), 4) & "|" & cnt
End If
End If
Next i
x = Application.Transpose(Array(.keys, .items))
Sheets("Sheet1").Cells(2, 1).Resize(.Count).Value = Application.Transpose(.keys)
For i = LBound(x) To UBound(x)
Sheets("Sheet1").Cells(i + 1, 2).Resize(, 4).Value = Split(x(i, 2), "|")
Next i
Sheets("Sheet1").Select
End With
End Sub
One question..
In the sample workbook i have attached.. should the value in Sheet1 (C12) be 3500 or 2000 (after you have clicked the button)..?
If so..
Change this:
.Item(Z(i, 1)) = Z(i, 2) & "|" & Z(i, 3) & "|" & Z(i - (cnt - 1), 4) & "|" & cnt
to this:
.Item(Z(i, 1)) = Z(i, 2) & "|" & Z(i - (cnt - 1), 3) & "|" & Z(i - (cnt - 1), 4) & "|" & cnt
oh.. and turn screen updating off before the code then back on at the end.. i forgot that..