【问题标题】:How do I delete duplicates between two excel sheets quickly vba如何快速删除两个excel表之间的重复vba
【发布时间】:2015-11-22 08:34:48
【问题描述】:

我正在使用 vba,我有两张表,其中一张名为“Do Not Call”,A 列中有大约 800,000 行数据。我想使用这些数据检查第二张表中名为“Sheet1”的 I 列.如果它找到匹配项,我希望它删除“Sheet1”中的整行。我已经定制了从类似问题中找到的代码:Excel formula to Cross reference 2 sheets, remove duplicates from one sheet 并运行它,但没有任何反应。我没有收到任何错误,但它不起作用。

这是我目前正在尝试的代码,但不知道为什么它不起作用

Option Explicit
Sub CleanDupes()
Dim wsA As Worksheet
Dim wsB As Worksheet
Dim keyColA As String

Dim keyColB As String
Dim rngA As Range
Dim rngB As Range
Dim intRowCounterA As Integer
Dim intRowCounterB As Integer
Dim strValueA As String


keyColA = "A"
keyColB = "I"

intRowCounterA = 1
intRowCounterB = 1

Set wsA = Worksheets("Do Not Call")
Set wsB = Worksheets("Sheet1")

Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value)
    Set rngA = wsA.Range(keyColA & intRowCounterA)
    strValueA = rngA.Value
    If Not dict.Exists(strValueA) Then
        dict.Add strValueA, 1
    End If
    intRowCounterA = intRowCounterA + 1
Loop

intRowCounterB = 1
Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value)
    Set rngB = wsB.Range(keyColB & intRowCounterB)
    If dict.Exists(rngB.Value) Then
         wsB.Rows(intRowCounterB).delete
         intRowCounterB = intRowCounterB - 1
    End If
    intRowCounterB = intRowCounterB + 1
Loop
End Sub

如果上面的代码不在代码标签中,我深表歉意。这是我第一次在网上发布代码,我不知道我是否正确。

【问题讨论】:

  • 是否需要VBA。我认为 vlookup 可以使这变得非常容易,除非您当然需要一遍又一遍地这样做?很高兴为 VBA 提供帮助,只是可能有点矫枉过正吗?
  • 感谢您的快速响应!我考虑过 vlookup,但老实说,我不确定它是否能满足我的要求。可悲的是,我不熟悉该功能。我需要将它用于其他工作表(仅替换 Sheet1 而不是 Do Not Call Sheet)。如果我打算用其他几张纸来做这件事,我应该研究一下 vlookup 吗?

标签: vba excel


【解决方案1】:

我很尴尬地承认你分享的代码让我感到困惑......无论如何,我使用数组而不是循环遍历工作表值来重写它:

Option Explicit
Sub CleanDupes()
    Dim targetArray, searchArray
    Dim targetRange As Range
    Dim x As Long

    'Update these 4 lines if your target and search ranges change
    Dim TargetSheetName As String: TargetSheetName = "Sheet1"
    Dim TargetSheetColumn As String: TargetSheetColumn = "I"
    Dim SearchSheetName As String: SearchSheetName = "Do Not Call"
    Dim SearchSheetColumn As String: SearchSheetColumn = "A"

    'Load target array
    With Sheets(TargetSheetName)
        Set targetRange = .Range(.Range(TargetSheetColumn & "1"), _
                .Range(TargetSheetColumn & Rows.Count).End(xlUp))
        targetArray = targetRange
    End With
    'Load Search Array
    With Sheets(SearchSheetName)
        searchArray = .Range(.Range(SearchSheetColumn & "1"), _
                .Range(SearchSheetColumn & Rows.Count).End(xlUp))
    End With


    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    'Populate dictionary from search array
    If IsArray(searchArray) Then
        For x = 1 To UBound(searchArray)
            If Not dict.exists(searchArray(x, 1)) Then
                dict.Add searchArray(x, 1), 1
            End If
        Next
    Else
        If Not dict.exists(searchArray) Then
            dict.Add searchArray, 1
        End If
    End If

    'Delete rows with values found in dictionary
    If IsArray(targetArray) Then
        'Step backwards to avoid deleting the wrong rows.
        For x = UBound(targetArray) To 1 Step -1
            If dict.exists(targetArray(x, 1)) Then
                targetRange.Cells(x).EntireRow.Delete
            End If
        Next
    Else
        If dict.exists(targetArray) Then
            targetRange.EntireRow.Delete
        End If
    End If
End Sub

编辑:因为它困扰我,我重新阅读了您提供的代码。它让我感到困惑,因为它没有按照我预期的方式编写并且失败,除非您只检查字符串值。我在这个 sn-p 中添加了 cmets 来表明它在做什么:

'Checks to see if the particular cell is empty.
Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value)
    'Stores the cell to a range for no good reason.
    Set rngA = wsA.Range(keyColA & intRowCounterA)
    'Converts the value of the cell to a string because strValueA is a string.
    strValueA = rngA.Value
    'Checks to see if the string is in the dictionary.
    If Not dict.Exists(strValueA) Then
        'Adds the string to the dictionary.
        dict.Add strValueA, 1
    End If

然后:

 'checks the value, not the value converted to a string.
 If dict.Exists(rngB.Value) Then 

这会失败,因为脚本字典不认为双精度等于字符串,即使双精度转换为字符串时它们是相同的。

修复您发布的代码的两种方法,或者将我刚刚显示的行更改为:

If dict.Exists(cstr(rngB.Value)) Then

或者您可以将Dim strValueA As String 更改为Dim strValueA。

【讨论】:

  • 谢谢!你超出了我的预期!您打算使用的两种解决方案都像一个魅力(您的代码和更改我正在使用的代码)。我也必须承认,我没有很好地理解我正在使用的代码。 :)
【解决方案2】:

因为我有时间,所以这里重写了字典,而是使用了工作表函数。 (受 Vlookup 评论的启发)。我不确定哪个会更快。

Sub CleanDupes()
    Dim targetRange As Range, searchRange As Range
    Dim targetArray
    Dim x As Long
    'Update these 4 lines if your target and search ranges change
    Dim TargetSheetName As String: TargetSheetName = "Sheet1"
    Dim TargetSheetColumn As String: TargetSheetColumn = "I"
    Dim SearchSheetName As String: SearchSheetName = "Do Not Call"
    Dim SearchSheetColumn As String: SearchSheetColumn = "A"

    'Load target array
    With Sheets(TargetSheetName)
        Set targetRange = .Range(.Range(TargetSheetColumn & "1"), _
                .Range(TargetSheetColumn & Rows.Count).End(xlUp))
        targetArray = targetRange
    End With
    'Get Search Range
    With Sheets(SearchSheetName)
        Set searchRange = .Range(.Range(SearchSheetColumn & "1"), _
                .Range(SearchSheetColumn & Rows.Count).End(xlUp))
    End With
    If IsArray(targetArray) Then
        For x = UBound(targetArray) To 1 Step -1
            If Application.WorksheetFunction.CountIf(searchRange, _
                                        targetArray(x, 1)) Then
                targetRange.Cells(x).EntireRow.Delete
            End If
        Next
    Else
        If Application.WorksheetFunction.CountIf(searchRange, targetArray) Then
            targetRange.EntireRow.Delete
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2018-10-01
    • 2019-09-26
    相关资源
    最近更新 更多