我是 VBA 新手 - 尝试使用 VBA 进行简单计算,但无法使其正常工作。任何帮助将不胜感激。
基本上我有一行,结果应该等于前一个结果+当前根据单元格
这就是我的意思
一个
乙
C
D
(1)第 1 行
1
2
3
4
(2)结果
1
(1+2)
(1+2)+3
((1+2)+3)+4
我一直用for Next 但无法生成我想看到的结果,第一个输出是正确的,但是在第一次计算之后,所有正在进行的计算都不正确
Sub DeltaAllocationToCRSSum()
Range("A2").Value = 1 ' Set starting value for the first cell in the result roll
Dim Column1 As Integer
Dim Column2 As Integer
For Column1 = 1 to 4
For Column2 = 2 To 5
Cells(2,Column2) = Cells(2,Column1)+Cells(1,Column2) 'meaning B2 = A2 + B1 for the first calculation
Next Column2
Next Column1
所以这段代码总能得到第一次计算的正确结果,但连续的结果总是错误的。有人知道是什么问题吗?对不起,这个问题可能很基础,但我自己无法弄清楚......谢谢你的帮助
最佳答案
你只需要一个for循环,试试这个
Sub DeltaAllocationToCRSSum()
Range("A2").Value = 1 ' Set starting value for the first cell in the result roll
Dim Column1 As Integer
For Column1 = 1 To 4
Cells(2, Column1 + 1) = Cells(2, Column1) + Cells(1, Column1 + 1) 'meaning B2 = A2 + B1 for the first calculation
Next Column1
End Sub
关于excel - 为什么 VBA For Next Loop 计算不正确?{VBA},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66109098/