我正在尝试使用VBA来计算两个不同工作表中两个不同列之间存在的重复项。我在下面有这个语法,但返回的数字总是很高。例如,刚才它返回了13041,当时只有45行要检查其中一张纸?
如何调整它以便它返回两列之间存在的重复的准确计数&表?
Sub CountIF()
ApplAcatAon.ScreenUpdatAng = False
DAm stNow As Date
DAm matches As Anteger
stNow = Now
matches = 0
DAm arr As VarAant
arr = Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Value
DAm varr As VarAant
varr = Sheets("Sheet2").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Value
DAm x, y, match As Boolean
For Each x An arr
match = False
For Each y An varr
Af x = y Then match = True
matches = matches + 1
Next y
Af Not match Then
Sheets("Sheet2").Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = x
End Af
Next
ApplAcatAon.ScreenUpdatAng = True
End Sub
答案 0 :(得分:0)
几点:
你为什么讨厌这封信?
varr
正在查看错误的范围 - 您正在使用Range
而不是Sheets("Sheet2").Range
,因此将查看活动工作表,我假设它是Sheet1
。请尝试使用此代码:
With Sheets("Sheet2")
varr = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value
End With
最后,最重要的是,你究竟期待这回归到底是什么?如果第一个工作表在前两个使用的行中只有1
,而第二个工作表在前三个使用的行中有1
,则此函数将说明有6个重复(每个匹配3个匹配) Sheet1中的2个值)。如果那不正确
答案 1 :(得分:0)
If
的语法是
If condition Then
doSomething
doSomethingMore
End If
或
If condition Then doSomething
当你做
时If x = y Then match = True
matches = matches + 1
这意味着matches
每次都会递增,即使这两个条目不匹配也是如此。你可能想要做的是
If x = y Then
match = True
matches = matches + 1
End If
我不确定为什么它最终会成为13041,因为如果arr
和varr
具有相同数量的元素,它应该是一个正方形数字。