需要排列Excel vba列中的重复文本值

我在列中有300个名字,我想排列它们,并且要包含重复的值,例如:如果史密斯是名单上的#3和#4,他们将被放在那里。

我当前的代码,这是不工作的:

Sub karp() Dim Prescribers As Worksheets Dim n, r, LR As Long Dim name1, name2 As String LR = Cells(Rows.Count, "b").End(xlUp).Row For n = 4 To LR 'to loop through the range for a match For a = 1 To 273 'number of possible name ranks Sheets("Names").Cells(n, 3) = a 'places rank score name1 = Sheets("Names").Cells(n, 3).Value 'checks name name2 = Sheets("Names").Cells(n + 1, 3).Value 'checks next name If name1 <> name2 Then Next a Next n End If Next n End Sub 

任何build议修理?

如果我正确理解,那么你可以使用这个:

 Sub karp() Dim i&, Cl As Range, rn& Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary") Dic.CompareMode = vbTextCompare i = Cells(Rows.Count, "b").End(xlUp).Row rn = 1 For Each Cl In Range("C4:C" & i) If Not Dic.exists(Trim(Cl.Value) & Trim(Cl.Offset(, 1).Value)) Then Dic.Add Trim(Cl.Value) & Trim(Cl.Offset(, 1).Value), rn rn = rn + 1 End If Next Cl For Each Cl In Range("C4:C" & i) Cl.Offset(, 2).Value = Dic(Trim(Cl.Value) & Trim(Cl.Offset(, 1).Value)) Next Cl End Sub 

产量

在这里输入图像描述