Excel VBA运行求和函数

时间:2015-07-15 19:42:39

标签: excel vba excel-vba

我有一个UDF试图将一列数字的运行总和制成表格。如果列A是我的数据而列B是UDF输出,它将如下所示:

Column A | Column B
5        | 5
10       | 15
1000     | 1015

等等。

这是我的代码:

Option Explicit
Public Function runningSum(myCell As range) As Integer

Dim rowNum As Integer
Dim colNum As Integer
Dim tempSum As Integer
Dim i As Integer
Dim ws As Worksheet

rowNum = myCell.row
colNum = myCell.Column
tempSum = 0
Set ws = ActiveSheet

With ws
    For i = 1 To rowNum
        tempSum = tempSum + ws.Cells(i, colNum).Value
    Next i
    runningSum = tempSum
End With
Exit Function

End Function

我遇到的问题是:在第39次和每次连续迭代时,我得到一个#VALUE错误。请帮忙?感谢

1 个答案:

答案 0 :(得分:0)

似乎您的号码太大而integer无法容纳。尝试将数据类型切换为long