excel - 为什么 VBA For Next Loop 计算不正确?{VBA}

标签 excel vba vba6

我是 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/

相关文章:

java - 解析超大 Excel 2007 文件的最佳语言

excel - 返回有效数字位数的函数

vba - MS Access VBA : how to trap case where delete hangs due to low memory or disk space?

vba - 删除 Material list 中的行

excel - 如何迭代动态增加的字典?似乎excel VBA预编译了For循环的 "limit"

excel - 从 VBA 中的范围中删除特殊字符

excel - 如何按 "numerical"顺序对大纲数字进行排序?

java - 如何在Spring映射中将列表的某些值设置为可选?

java - 读取 Excel 文件时出现 FileNotFound 异常

excel - 如何将 Range 传递给 NPV 公式的 VBA 基本函数