繁体   English   中英

Excel VBA运行总和功能

[英]Excel VBA Running Sum Function

我有一个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错误。 请帮助? 谢谢

您的数字似乎太大了,无法容纳integer 尝试将数据类型切换为long

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM