Excel宏用于对名称进行排序,仅包含带有x出现的名称

时间:2013-06-25 21:53:51

标签: excel vba

我需要帮助为Excel创建一个计算一列名称的宏。如果名称出现2次或更多次,请删除重复项。如果出现的次数少于2次,请从列表中删除该名称。有人可以帮忙吗?

例如,列:

  1. John Smith
  2. John Smith
  3. Jane Smith
  4. Patty Smith
  5. Jim Smith
  6. Jim Smith
  7. Jim Smith
  8. 宏变成:

    1. John Smith
    2. Jim Smith

1 个答案:

答案 0 :(得分:0)

你可以试试这个..

假设它是单元格A1 ..您将在B列中获得结果..您可以在获得结果后删除C列。

Sub CheckOccurance()
Dim LastA, r As Range
Dim x, x2, Bn As Long
Dim s As String
Dim IsDoub As Boolean

Set LastA = Range("A65536").End(xlUp)
Bn = 1

For x = 1 To LastA.Row
  If Not Cells(x, 3) = "x" Then
    s = Cells(x, 1)
    Cells(x, 3) = "v"
    IsDoub = False
    For x2 = x + 1 To LastA.Row
      If Cells(x2, 1) = s Then
        Cells(x2, 3) = "x"
        IsDoub = True
      End If
    Next
    If IsDoub Then
      Cells(Bn, 2) = s
      Bn = Bn + 1
    End If
  End If
Next
End Sub