通过Excel计算运行多个值

问题描述 投票:0回答:1

我团队的另一位成员构建了精心设计的电子表格,用于计算我们需要的特定值。但是,它目前设置如下:

  • 更改单元格A1,A2,A3中的值。
  • 发生了一系列相互依赖的计算,填充了数百个单元格。那百个单元格,然后通过另一个公式合并回我需要的值。
  • 有用的输出显示在单元格D1中

我的任务是生成一系列ifs。如果A1 = 4,A2 = 4.7,A3 = 0.2,D1会是什么?

记录输出时,对10k +其他有效的A1,A2和A3组合重复此操作。

我的本能只是把它当作编程语言中的一个函数来对待,并使它成为一个输入和吐出输出的函数......但是我不知道该怎么做。有关如何做到这一点的任何指导?同事运行的功能非常复杂,因此就像我可能对SUM或其他东西那样“拖放”而言,跨多个行或列复制该逻辑并不容易。

excel vba
1个回答
1
投票

感谢Michael Murphy在评论中给了我一些搜索的东西。我要离开这里,所以未来的人们可以找到它。

Sub test()
'
' test Macro
'
    Dim a_array As Variant
    Dim b_array As Variant
    Dim c_array As Variant
    Dim rw_cnt As Integer

    rw_cnt = 2
    ' This selects each of the things to combo
    a_array = ThisWorkbook.Sheets(2).Range("A2:A5")
    b_array = ThisWorkbook.Sheets(2).Range("B2:B5")
    c_array = ThisWorkbook.Sheets(2).Range("C2:C5")

    For Each a_el In a_array
        For Each b_el In b_array
            For Each c_el In c_array
                worth = compute_worth(a_el, b_el, c_el)
                ' This writes the output where we can track it
                ThisWorkbook.Sheets(2).Cells(rw_cnt, 6) = a_el
                ThisWorkbook.Sheets(2).Cells(rw_cnt, 7) = b_el
                ThisWorkbook.Sheets(2).Cells(rw_cnt, 8) = c_el
                ThisWorkbook.Sheets(2).Cells(rw_cnt, 9) = worth
                rw_cnt = rw_cnt + 1
            Next a_el
        Next b_el
    Next c_el
End Sub


Function compute_worth(a, b, c)
    ' This puts the value into the original sheet then extracts the output
    ThisWorkbook.Sheets(1).Range("A1") = a
    ThisWorkbook.Sheets(1).Range("A2") = b
    ThisWorkbook.Sheets(1).Range("A3") = c
    worth = ThisWorkbook.Sheets(1).Range("D1").Value
    compute_worth = worth
End Function
© www.soinside.com 2019 - 2024. All rights reserved.