简体   繁体   English

删除重复但区分大小写

[英]Remove duplicates but with case sensitive

I am trying to remove duplicates but with case sensitivity.我正在尝试删除重复项但区分大小写。 For example, ABC123 is not the same as abc123, hence, do not remove it.例如,ABC123 与 abc123 不同,因此不要删除它。 But ABC123 and ABC123 is the same, hence, remove them.但是 ABC123 和 ABC123 是一样的,所以去掉它们。

This is my current code:这是我当前的代码:

Dim oDic As Object, vData As Variant, r As Long
Set oDic = CreateObject("Scripting.Dictionary")
With worksheets(4).Range("A7:A" & lastRow)
  vData = .Value
 .ClearContents
End With
With oDic
 .comparemode = 0
 For r = 1 To UBound(vData, 1)
 If Not IsEmpty(vData(r, 1)) And Not .Exists(vData(r, 1)) Then
 .Add vData(r, 1), Nothing
 End If
 Next r
 Range("A7").Resize(.Count) = Application.Transpose(.keys)
End With

Some background:一些背景:

  • The entire dataset has about 800k records整个数据集大约有 80 万条记录
  • The script has no error, but the result is wrong.脚本没有错误,但结果是错误的。 When I remove duplicate (regardless of case sensitivity, I have 400k left) but running this script, 450k (which sounds legit), but only 60k records have data, 390k shows #N/A.当我删除重复项(不管区分大小写,我还剩下 400k)但运行此脚本时,450k(听起来合法),但只有 60k 记录有数据,390k 显示#N/A。 So I have no idea where went wrong.所以我不知道哪里出错了。

Thanks in advance!提前致谢!

As stated in the first comment, Application.Transpose has a limitation of 65,536 array rows.如第一条评论所述, Application.Transpose有 65,536 个数组行的限制。 Please, try the next function able to transpose without such a limitation:请尝试下一个 function 能够在没有此类限制的情况下转置:

Function TranspKeys(arrK) As Variant
   Dim arr, i As Long
   ReDim arr(1 To UBound(arrK) + 1, 1 To 1)
   For i = 0 To UBound(arrK)
        arr(i + 1, 1) = arrK(i)
   Next i
   TranspKeys = arr
End Function

After copying the functionin the same module where your existing code exists, only modify it as:在您现有代码所在的同一模块中复制该功能后,只需将其修改为:

Range("A7").Resize(.Count,1) = TranspKeys(.keys)

Unique Values Case-Sensitive唯一值区分大小写

  • Transpose has its limitations and is best avoided (what's a few more lines).转置有其局限性,最好避免(还有几行)。
Option Explicit

Sub DictWith()
    
    With Worksheets(4)
        
        Dim LastRow As Long: LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        If LastRow < 7 Then Exit Sub
        
        With .Range("A7:A" & LastRow)
            
            Dim Data As Variant
            
            If .Rows.Count = 1 Then
                ReDim Data(1 To 1, 1 To 1)
                Data(1, 1).Value = .Value
            Else
                Data = .Value
            End If
            
            With CreateObject("Scripting.Dictionary")
                
                .CompareMode = vbBinaryCompare
                
                Dim Key As Variant
                Dim r As Long
                
                For r = 1 To UBound(Data, 1)
                    Key = Data(r, 1)
                    If Not IsError(Key) Then
                        If Len(Key) > 0 Then
                            .Item(Key) = Empty
                        End If
                    End If
                Next r
                
                Dim rCount As Long: rCount = .Count
                If rCount = 0 Then Exit Sub
                
                ReDim Data(1 To rCount, 1 To 1)
                r = 0
                
                For Each Key In .Keys
                    r = r + 1
                    Data(r, 1) = Key
                Next Key
                
            End With
            
            .Resize(rCount).Value = Data
            .Resize(.Worksheet.Rows.Count - .Row - rCount + 1) _
                .Offset(rCount).ClearContents ' clear below
        
        End With
    
    End With

End Sub

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

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