gpt4 book ai didi

vba - 如何快速删除两个Excel工作表之间的重复项vba

转载 作者:行者123 更新时间:2023-12-03 02:10:46 26 4
gpt4 key购买 nike

我正在使用 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

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

最佳答案

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

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

编辑:因为这让我很困扰,所以我重新阅读了您提供的代码。它让我感到困惑,因为它不是按照我预期的方式编写的,除非您仅检查字符串值,否则它会失败。我添加了注释来表明它在此代码段中执行的操作:

'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

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

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

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

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

关于vba - 如何快速删除两个Excel工作表之间的重复项vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13665305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com