首页 > 解决方案 > VBA - 比较单元格时提高计算效率

问题描述

我正在使用以下函数来确定两个单元格的值是否在两列中。我必须比较 250 组双细胞和 6500 组双细胞。Excel 花了30 秒计算结果。
我可以提高计算效率吗?

Public Function CompareWithTwoCells(twoCells As Range, twoCols As Range)
    Dim result As String
    result = "False"
    For n = 1 To twoCols.Rows.Count
        If twoCols(n, 1) = "" Then
            Exit For
        End If

        If twoCells(1, 1) = twoCols(n, 1) And twoCells(1, 2) = twoCols(n, 2) Then
            result = "True"
            Exit For
        End If
    Next
    CompareWithTwoCells = result
End Function

在此处输入图像描述

标签: vbaexcel

解决方案


你有理由不能使用MATCH=MATCH吗?(假设twoCells是 A1:B1 并且twoCols是 F1:G6500)

=IFERROR(MATCH(A1,$F$1:$F$6500,0)=MATCH(B1,$G$1:$G$6500,0),FALSE)

这在我的机器上几乎是即时的。


推荐阅读