下图为统计重复的名字及重复出现的次数的一个简单问题的处理方法
代码如下:(实现方式有多种,下面是最笨的方法实现)
Sub test()
Dim counter As Integer
Dim flag As Boolean
counter = 1
Worksheets("sheet1").Cells(counter, "H") = "重复的名字"
Worksheets("sheet1").Cells(counter, "J") = "出现的次数"
counter = counter + 1
For i = 3 To Worksheets("sheet1").UsedRange.Rows.Count Step 1
Name = Worksheets("sheet1").Cells(i, 1).Value
For j = 2 To i - 1
If Name = Worksheets("sheet1").Cells(j, 1).Value Then
flag = False
For k = 2 To counter
If Name = Worksheets("sheet1").Cells(k, "H").Value Then
Worksheets("sheet1").Cells(k, "J").Value = Worksheets("sheet1").Cells(k, "J").Value + 1
flag = True
Exit For
End If
Next
If flag = False Then
Worksheets("sheet1").Cells(counter, "H") = Name
Worksheets("sheet1").Cells(counter, "J") = 2
counter = counter + 1
End If
Exit For
End If
Next
Next
End Sub
代码如下:(实现方式有多种,下面是最笨的方法实现)
Sub test()
Dim counter As Integer
Dim flag As Boolean
counter = 1
Worksheets("sheet1").Cells(counter, "H") = "重复的名字"
Worksheets("sheet1").Cells(counter, "J") = "出现的次数"
counter = counter + 1
For i = 3 To Worksheets("sheet1").UsedRange.Rows.Count Step 1
Name = Worksheets("sheet1").Cells(i, 1).Value
For j = 2 To i - 1
If Name = Worksheets("sheet1").Cells(j, 1).Value Then
flag = False
For k = 2 To counter
If Name = Worksheets("sheet1").Cells(k, "H").Value Then
Worksheets("sheet1").Cells(k, "J").Value = Worksheets("sheet1").Cells(k, "J").Value + 1
flag = True
Exit For
End If
Next
If flag = False Then
Worksheets("sheet1").Cells(counter, "H") = Name
Worksheets("sheet1").Cells(counter, "J") = 2
counter = counter + 1
End If
Exit For
End If
Next
Next
End Sub