Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-提高比较单元格时的计算效率_Vba_Excel - Fatal编程技术网

VBA-提高比较单元格时的计算效率

VBA-提高比较单元格时的计算效率,vba,excel,Vba,Excel,我使用下面的函数来确定两个单元格的值是否在两列中。 我必须比较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

我使用下面的函数来确定两个单元格的值是否在两列中。 我必须比较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

根据@Zac的建议,添加:

Dim twoCellsArr, twoColsArr
twoCellsArr = twoCells.Value2
twoColsArr = twoCols.Value2
然后将
twocell
twoCols
更改为
twoCellsArr
twoColsArr


如果您的
twoCols
没有更改,并且您正在重复进行比较,我建议使用
字典将
twoCols.Value
存储为键,行号存储为值,然后执行查找并比较它们是否在同一行中。

这是可能的增强的第一步(评论中的解释):


另一个显著的增强是使用数组而不是范围(假设
twocell
为A1:B1,而
twoCols
为F1:G6500),有什么原因不能直接使用
MATCH=MATCH


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

你可能会发现在代码审查时问这个问题更好……我会将你的范围复制到一个数组中,并在数组上执行比较。从内存计算总是比从excel中的范围计算快。虽然我同意@SolarMike。这应该是一个代码审查问题,但很难理解如果希望UDF与
twoCols
中可能的重复项具有精确字符串匹配,请查询任何改进。标准优化包括:复制到数组,嵌套
if
而不是使用
,以及设置
应用程序。屏幕更新=False
。如果可以放宽UDF要求并更改fu选择
Selection
而不是
twocell
,只需运行
twoCols
一次即可(只需将
twoCols
中的行合并,将它们添加到字典中,并检查是否存在)。这仍然很慢。更慢。你尝试过字典方法吗?你确实意识到这种缓慢来自于编写重复的公式,每次都调用自定义函数来检查一个范围,对吗?不是我。TwoCell和twoCols是任意的。我想在任何地方都使用UDF。实际上,它会每两个单元格调用一个UDF。这会阻止我的ExCel这比你的代码快。是你否决了吗?我抵消了否决,因为这是一个有效点。VBA似乎总是测试这两个条件,不像其他语言,如果第一个条件为false,
不会计算。我没有否决。但它确实阻止了我的Excel。我将重试。formul中有一个问题a、 如果A1=F1、B1G1和A1F2、B1=G2和A1=F3、B1=G3,则结果将变为FALSE。我想要的正确结果为TRUE。MATCH函数返回第一个值的索引,该值等于
lookup\u value
,在匹配文本值时,它不会区分大小写字母。
Public Function CompareWithTwoCells(twoCells As Range, twoCols As Range)
    Dim cell As Range
    Dim firstVal As Variant, secondVal As Variant

    firstVal = twoCells(1, 1) ' store first cell value in a variable
    secondVal = twoCells(1, 2) ' store second cell value in a variable
    CompareWithTwoCells = "False"
    For Each cell In twoCols.Columns(1).SpecialCells(xlCellTypeConstants) ' loop through first column not empty values
        If firstVal = cell.Value2 Then ' check one column fisrt
            If secondVal = cell.Offset(, 1) Then ' check second column only if first columns check is true
                CompareWithTwoCells = "True"
                Exit For
            End If
        End If
    Next
End Function
=IFERROR(MATCH(A1,$F$1:$F$6500,0)=MATCH(B1,$G$1:$G$6500,0),FALSE)