2016-11-29 235 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