查找相同数据的问题
我在ExcelA列输入一列数据,该列数据中有若干数据是相同的.我编个程序,将A列数据中相同的数据查找出来.我想对查找出来的相同数据编个序号,如A列中有三个"南海"和两个"武汉"的记录.我想在B列添加辅助序号,以便对相同数据进行分类编号,如在B列对三个"南海"添加序号1,对两个"武汉"添序号2.查找相同数据程序我已编出如下,但不知如何在程序中对相同数据添加序号. 请哪位高手能编出这个添加序号的程序,并对程序加以说明.谢谢!
相找相同数据的程序如下:
For a = 1 To 15
For b = 1 To 15
If a <> b Then
If Cells(a, 1) = Cells(b, 1) Then
Cells(a, 2) = 1
End If
End If
Next b
Next a
[解决办法]
Sub setNum() Dim str As String Dim rng As Range, rngA As Range Set rngA = Range(Cells(2, 1), Range("A:A").End(xlDown)) str = "0," For Each rng In rngA If Application.WorksheetFunction.CountIf(Range("A1:A" & rng.Row - 1), rng.Value) > 0 Then If InStr(str, rng.Value) = 0 Then str = str & UBound(Split(str, ",")) & rng.Value & "," End If rng.Offset(0, 1).Value = Mid(str, InStr(str, rng.Value) - 1, 1) End If Next rng End Sub
[解决办法]
理解正确