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

时间:2013-08-30 05:29:50

标签: performance excel vba for-loop foreach

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

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

有关

1 个答案:

答案 0 :(得分:0)

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