简体   繁体   English

Excel /有效地识别(准确)字符串在特定表列(35K行)中的出现

[英]Excel / Efficiently identify (exact) occurences of a string within a specific table column, 35K rows

I've a table with 35000 rows, each row should have a unique (alphanumeric) ID, but I need to check this. 我有一个包含35000行的表,每行应具有唯一的(字母数字)ID,但是我需要检查一下。 Using countif takes an age to calculate, what would be the most efficient (least calculation time). 使用countif需要花一些时间来计算,这将是最有效的(最少的计算时间)。

[ID] | [Occurences]
[A7A8S8D9] | [<formula>]
[F6F7F8F9] | [<formula>]

If you don't mind a VBA solution this will place 'Duplicate' next to all duplicate ID's except the first. 如果您不介意VBA解决方案,则会在所有重复ID之外的所有重复ID旁边放置“重复”。

I tested it on 35,000 numeric ID's created using =RANDBETWEEN(1, 1000) and it ran in 671ms. 我在使用=RANDBETWEEN(1, 1000) 1,1000 =RANDBETWEEN(1, 1000)创建的35,000个数字ID上进行了测试,它的运行时间为671ms。 It may run faster if you pass the range into an array. 如果将范围传递给数组,它可能会运行得更快。

'Remove line to remove timing.
Private Declare Function GetTickCount Lib "kernel32" () As Long

'Will not mark first occurrence as duplicate.
Public Sub CheckForDuplicates()

    Dim rng As Range
    Dim rCell As Range
    Dim dict As Object

    'Remove next two lines to remove timing.
    Dim TC As Long
    TC = GetTickCount

    'Update sheet name as required.
    With ThisWorkbook.Worksheets("Sheet1")
        'Defines range as A2 to last row containing data (providing there's no empty cells in col A).
        '-1 at end of line as we're starting on row 2.
        Set rng = .Range("A2").Resize(.Cells(Rows.Count, 1).End(xlUp).Row - 1)
    End With
    Set dict = CreateObject("Scripting.Dictionary")

    With dict
        For Each rCell In rng
            If .Exists(rCell.Value) Then
                rCell.Offset(, 1) = "Duplicate"
            Else
               .Add rCell.Value, rCell.Value
            End If
        Next rCell
    End With

    'Remove line to remove timing.
    MsgBox GetTickCount - TC & "ms elapsed."

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM