【发布时间】: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 吗?