2016-11-29 110 views
2

我有一个名为NumberID的列有大约50k记录的电子表格。我知道有重复,但滚动向上/向下它需要永远找到任何东西加上往往是Excel的速度有点慢。我正在尝试编写一段代码,以便能够查找并计算重复项的数量。查找和计算重复次数

我想写一个快速的方式做到这一点,基本上我的数据是从20行到48210,我试图找到一个总数重复的记录。

Dim lastRow As Long 
Dim matchFoundIndex As Long 
Dim iCntr As Long 
Dim count As Long 
count = 0 
lastRow = Range("B48210").End(xlUp).Row 
For iCntr = 1 To lastRow 
    If Cells(iCntr, 1) <> "" Then 
     matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("B20:B" & lastRow), 0) 
     If iCntr <> matchFoundIndex Then 
      count = count + 1 
     End If 
    End If 
Next 

MsgBox count 

这里即时通讯上= WorkSheetFunction.Match得到一个错误 - 我发现,这个属性可以用来完成我想要做的事。错误说

无法获取工作表功能类的匹配属性。

有人有想法吗?我的vba已经生锈了

+0

如果你的唯一的问题是,匹配误差,这可能是http://stackoverflow.com/questions/17751443的副本/ excel-vba -cant-get-a-match-error-unable-to-the-match-property-of-the-wor –

+0

@TJRockefeller - 代码看起来是否合理,否则 – BobSki

+1

您显示的所有内容正在使用列B,但你在比赛的第一个标准中引用的是使用A栏。我建议改变'Cells(iCntr,1)'到'Cells(iCntr,2)' –

回答

2

因为要“计数的重复数”,一个非常快速的方式这样做是利用Range对象RemoveDuplicates()方法,像如下:

Option Explicit 

Sub main() 
    Dim helperCol As Range 
    Dim count As Long 

    With Worksheets("IDs") '<--| reference your relevant sheet (change "IDs" to youtr actual sheet name) 
     Set helperCol = .UsedRange.Resize(, 1).Offset(, .UsedRange.Columns.count) '<--| set a "helper" range where to store unique identifiers 
     With .Range("A1", .Cells(.Rows.count, 1).End(xlUp)) '<-- reference "IDs" column from row 1 (header) to last not empty cell 
      helperCol.Value = .Value '<--| copy identifiers to "helper" range 
      helperCol.RemoveDuplicates Columns:=1, Header:=xlYes '<--| remove duplicates in copied identifiers 
      count = .SpecialCells(xlCellTypeConstants).count - helperCol.SpecialCells(xlCellTypeConstants).count '<--| count duplicates as the difference between original IDs number and unique ones 
     End With 
     helperCol.ClearContents '<--| clear "helper" range 
    End With 
    MsgBox count & " duplicates" 
End Sub 
+0

@Bobski,你试过这个吗? – user3598756

+0

是的,它给了我一个非常大的数字在96K范围内的东西 - 我认为这需要所有记录的计数* 2 – BobSki

+0

好吧,我测试了它与列A中的一些50k行与预定义数量的重复(只是重复了很多乘以10格图案)并且工作。尝试单步执行代码,查看在工作表中查询的内容以及查询直接窗口(?helperCol.Address或?helperCol.Count)。 – user3598756

2

使用Match因为这是非常低效的许多行。我会补Dictionary与找到的项目,只是测试,看看你以前见过他们:

'Add a reference to Microsoft Scripting Runtime. 
Public Sub DupCount() 
    Dim count As Long 
    With New Scripting.Dictionary 
     Dim lastRow As Long 
     lastRow = Range("B48210").End(xlUp).Row 
     Dim i As Long 
     For i = 1 To lastRow 
      Dim test As Variant 
      test = Cells(i, 2).Value 
      If IsError(test) Then 
      ElseIf test <> vbNullString Then 
       If .Exists(test) Then 
        count = count + 1 
       Else 
        .Add test, vbNull 
       End If 
      End If 
     Next 
    End With 
    MsgBox count 
End Sub 
+0

由于某种原因lastrow = 19但在那里是很多行数据实际上在第20行开始,然后到48210 – BobSki

+0

@Bobski - 在这个例子中'范围'和'单元格'是不合格的。如果你是从一个模块运行它,它们可能不是指正确的工作表,所以你应该完全限定它们。否则,请参见[在VBA中查找上次使用的单元时出错](http://stackoverflow.com/q/11169445/4088852)。 – Comintern

2

你可以用我Duplicate Masteer addin做到这一点。

它提供了一种处理重复数据的快速数组方法。

  • 计数
  • 删除
  • 选择

它超越了Excel的内置功能,因为它允许

  1. 情况下insentitive基础
  2. 上重复匹配
  3. 忽略空白
  4. 甚至RegexP匹配
  5. 运行在多张纸上

enter image description here