gpt4 book ai didi

performance - total = total + ActiveCell.Value 不适用于每个循环

转载 作者:行者123 更新时间:2023-12-04 21:05:58 27 4
gpt4 key购买 nike

我有这段代码,我想在另一张工作表中查找一些单元格,如果它们符合条件,则将相邻单元格添加到返回到调用单元格函数的总数中。

Function collectUtfall(A1 As String, Ax As String)
Dim rng As Range
Dim total As Long: total = 0

Set rng = Sheets("Utfall").Range("M2:O272")

Dim cell As Range

For Each cell In rng
If StrComp(cell.Offset(0, 1).Text, Ax, vbTextCompare) = 0 Then
total = total + ActiveCell.Value
Else

End If
Next

collectUtfall = total

End Function

问题是执行时出现“循环引用”错误。使用 ActiveCell.Value有问题吗以这种方式?

如果我只是尝试一个值,比如 10,它就可以正常工作:
total = total + 10

所以问题必须出在 ActiveCell.Value上。 ?

最佳答案

Function collectUtfall(A1 As String, Ax As String)

Dim rng As Range

Dim total As Long


set total = 0


Set rng = Sheets("Utfall").Range("M2:O272")


Dim cell As Range


For Each cell In rng

If StrComp(cell.Offset(0, 1).Text, Ax, vbTextCompare) = 0 Then
total = total + Cell.Value

Else


End If

Next Cell


collectUtfall = total


End Function

关于performance - total = total + ActiveCell.Value 不适用于每个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18525621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com