Excel VBA简单代码错误

我正在尝试在Excel中编写一个函数:

  1. 遍历每个工作表
  2. 检查提供的单元格中是否存在提供的string
  3. 如果步骤2的计算结果为True,则向返回值添加一个预定的单元格值

我一直在通过我的函数,并得到正确的值,直到函数必须添加第四单元格到我的返回值。

任何人有任何想法是怎么回事? 谢谢!


Function Revenue(row As Integer, col As Integer, str As String) As Integer Dim i As Integer For i = 2 To Worksheets.Count If Worksheets(i).Cells(row, col) = str Then Revenue = Revenue + Worksheets(i).Cells(21, 2) // Bug occurs on 4th iteration Debug.Print Revenue End If Next i End Function 

在使用它们之前,您需要为创build值。