【问题标题】:How to add values of a loop to new rows of a table in Excel?如何将循环的值添加到 Excel 中表格的新行?
【发布时间】: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 哇,谢谢!成功了!

标签: excel vba loops


【解决方案1】:

这就是你想要的(根据我的最佳解释)?
备注:
我已将变量语法更改为我更熟悉的语法。
随心所欲地改变。但最好在名称中包含数据类型(例如,“in”或“i”表示整数)
另外:在大多数情况下,您不需要“设置”范围。避免这种情况可能意味着您破坏撤消链的风险较小。

Sub Test()
    
''' When using whole numbers: Integers are best
    Dim in1%, in2%

''' Where fractions are required, doubles are best
''' Though you should consider rounding any result that involves doubles (or singles for that matter)
    Dim dbY#, dbSG1#, dbSG2#, dbSGF#
    
''' Getting unvalidated user inputs (You should consider validating)
    dbSG1 = Val(InputBox("Enter SG1 Value:"))
    dbSG2 = Val(InputBox("Enter SG2 Value:"))
    dbSGF = Val(InputBox("Enter dbSGF Value:"))
    
''' Expand the table per the algorithm
    For in1 = 1 To 10
        For in2 = 1 To 10
        ''' Calculate 'Y'
            dbY = Val((dbSG1 * in1) + (dbSG2 * in2))
            
        ''' Add results to table
            With ActiveSheet.ListObjects("Table4").ListRows.Add
                .Range(1) = in1
                .Range(2) = in2
                .Range(3) = dbY
                .Range(4) = dbSGF - dbY
            End With
        Next in2
    Next in1

End Sub

【讨论】:

    【解决方案2】:
        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
    
    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))
            Set newrow = ws.ListObjects("Table4").ListRows.Add
            With newrow
                .Range(1) = a
                .Range(2) = b
                .Range(3) = Y
                .Range(4) = Val(SGF - Y)
            End With
        Next b
    Next a
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多