VBA 总结 VlOOKUP 和 COUNTIF 的代码
VBA code that sums up VlOOKUP and COUNTIF
我正在尝试将一列 (column1) 的每个单元格与另一列 (column2) 中的所有单元格进行比较,并得到匹配次数的结果。
在 excel 中,我使用 VLOOKUP 创建了 column3,它给出的结果为“匹配”和“不匹配”,然后使用 COUNTIF 作为“匹配”单元格。有什么办法可以避免这个column3直接得到匹配总数的结果吗?
不胜感激。
您可以使用那个 vba 代码。随意更改它。
Sub GetMatces()
Dim rng1 As Range, rng2 As Range, result As Integer, cell1 As Range,
cell2 As Range
result = 0
Set rng1 = ActiveSheet.Range("A1:A8") 'Here you can put the exact range to run the code or you can set it as a selection with "set rng=application.selection"
Set rng2 = ActiveSheet.Range("b1:b8")
For Each cell1 In rng1
For Each cell2 In rng2
If cell1 = cell2 Then result = result + 1
Next cell2
Next cell1
MsgBox result 'that variable is the sum of the matces. you can do it everything you want
End Sub
您可以尝试以下功能。
=SUM(--(ISNUMBER(MATCH(A1:A4,B1:B4,0))))
对于非 365 版本的 excel,公式可能需要数组条目。数组输入表示按CTRL
+SHIFT
+ENTER
.
确认公式输入
我正在尝试将一列 (column1) 的每个单元格与另一列 (column2) 中的所有单元格进行比较,并得到匹配次数的结果。
在 excel 中,我使用 VLOOKUP 创建了 column3,它给出的结果为“匹配”和“不匹配”,然后使用 COUNTIF 作为“匹配”单元格。有什么办法可以避免这个column3直接得到匹配总数的结果吗?
不胜感激。
您可以使用那个 vba 代码。随意更改它。
Sub GetMatces()
Dim rng1 As Range, rng2 As Range, result As Integer, cell1 As Range,
cell2 As Range
result = 0
Set rng1 = ActiveSheet.Range("A1:A8") 'Here you can put the exact range to run the code or you can set it as a selection with "set rng=application.selection"
Set rng2 = ActiveSheet.Range("b1:b8")
For Each cell1 In rng1
For Each cell2 In rng2
If cell1 = cell2 Then result = result + 1
Next cell2
Next cell1
MsgBox result 'that variable is the sum of the matces. you can do it everything you want
End Sub
您可以尝试以下功能。
=SUM(--(ISNUMBER(MATCH(A1:A4,B1:B4,0))))
对于非 365 版本的 excel,公式可能需要数组条目。数组输入表示按CTRL
+SHIFT
+ENTER
.