我已经在 Excel 中构建了一个财务模型,但想知道大量假设的敏感性。我有一个假设选项卡,可输入到模型中(Col D8:D235),在同一张表中,我对这些假设进行了一些更改(Col M : Col R)。
我基本上想在循环中将 D 列中的值替换为每一列中的值,并保存相应的输出值。 在 for 循环中,我想用 M、N、O、P 等列更改假设表 D 列中的值,以检索 I28 中“输出”表中创建的输出值: I57,随后我希望将其存储在 K 列、L 列等中,然后用下一组输入值替换假设表(D 列)中的值。
我对VBA的了解为零,所以我希望有人能帮助我。
为了说明这一点,我想我现在正确地标出了所涉及的列,但不知道如何在 VBA 中编写“for 循环”。
Sub SensitivityAnalysis()
'Define variables
Dim inputVariables As Range
Dim inputSet As Range
Dim initialVariables As Range
Dim Output As Range
Dim outputTable As Range
Dim i As Integer
'Set input and output ranges
Set inputVariables = Worksheets("Assumption sheet").Range("D8:D235")
Set inputSet = Worksheets("Assumption sheet").Range("M8:R235")
Set Output = Worksheets("Output sheet").Range("I28:I57")
Set outputTable = Worksheets("Output sheet").Range("K28:P57")
For i = 1:6
inputVariables.clear()
inputVariables = inputSet(i)
outputTable(i) = Output
End Sub
提前致谢。
我尝试编写一个for循环,但失败了
请测试下一段代码。它在
inputSet
的所有列之间进行迭代,复制覆盖inputVariables
中现有的每一列,计算输出表并将输出保存在相应的列中:
Sub ProcessAssumptions()
Dim WB As Workbook, wsAss As Worksheet, wsOut As Worksheet, inputVariables As Range, inputSet As Range
Dim Output As Range, outputTable As Range, i As Long
Set WB = ActiveWorkbook 'use here the workbook you need
Set wsAss = WB.Worksheets("Assumption sheet")
Set wsOut = WB.Worksheets("Output sheet")
Set inputVariables = wsAss.Range("D8:D20") ' wsAss.Range("D8:D235")
Set inputSet = wsAss.Range("M8:R20") ' wsAss.Range("M8:R235")
Set Output = wsOut.Range("I28:I32") 'wsOut.Range("I28:I57")
Set outputTable = wsOut.Range("K28:P32") 'wsout.Range("K28:P57")
'Iterate between inputSet columns and do processing:
For i = 1 To inputSet.Columns.count
inputVariables.value = inputSet.Columns(i).value 'copy the inputSet as value, not as formula
wsOut.Calculate 'if other sheets are involved you should use WB.Calculate
outputTable.Columns(i).value = Output.value 'save the output in the corresponding colummn
Next i
MsgBox "Ready..."
End Sub