簡體   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