【发布时间】:2021-10-05 07:44:24
【问题描述】:
我是一个宏新手,我只是使用嵌套的 for 循环和一个 Excel 表来制作一个简单的宏程序。我在这里附上了我的代码。
我想要的是当我输入 P、Q 和 SGF 值时,我想在“Table4”中逐行查看每个循环索引的结果。 (我的表有四列,它们应该显示 a、b、Y 和 (SGF-Y) 值。)但是这段代码只创建一个新行并显示最后一个循环值的结果。 (当 a=10 和 b=10 时)但我实际上需要 100 个结果。希望你能理解我的问题。谢谢!
[![在此处输入图片描述][1]][1]
Dim SGF As Single
Dim Y As Single
Dim a As Single
Dim b As Single
Dim P As Single
Dim Q As Single
Dim ws As Worksheet
Dim newrow As ListRow
Set ws = ActiveSheet
Set newrow = ws.ListObjects("Table4").ListRows.Add
P = Val(InputBox("Enter SG1 Value:"))
Q = Val(InputBox("Enter SG2 Value:"))
SGF = Val(InputBox("Enter SGF Value:"))
For a = 1 To 10
For b = 1 To 10
Y = 0
Y = Val((P * a) + (Q * b))
With newrow
.Range(1) = a
.Range(2) = b
.Range(3) = Y
.Range(4) = Val(SGF - Y)
End With
Next b
Next a
End Sub
[1]: https://i.stack.imgur.com/jqWbC.png
【问题讨论】:
-
将
Set newrow行移到循环内,在With newrow之前。 -
@CDP1802 哇,谢谢!成功了!