#名称?带有VBA代码的单元格中的错误

时间:2016-06-28 10:37:37

标签: excel vba excel-vba formula

我在Excel中使用VBA运行MonteCarlo Simulation,但我在相应的单元格中只收到#Name?个错误。当我点击其中一个单元格时,按F2然后Return错误消失并正确计算该值。这有什么不对?

这是计算相应值的代码行:

ActiveCell.Formula = "=Start_Rate * EXP(NORM.S.INV(RAND())* Standard_Deviation * (" & i & " ^1/2)) "

这就是整个代码(如有必要):

Sub MC_Simulation()

Dim i As Integer
Dim k As Integer
Dim StartCell As Range
Dim start_row As Integer
Dim start_column As Integer

iterations = Worksheets("Run_MC").Range("MC_Simulations").Value
Duration = Worksheets("Run_MC").Range("Duration").Value
Mean = Worksheets("Run_MC").Range("Mean").Value
Start_Rate = Worksheets("Run_MC").Range("Start_Rate").Value
Standard_Deviation = Worksheets("Run_MC").Range("Standard_Deviation").Value

start_row = 15
start_column = 1

For i = 1 To Duration
    For k = 1 To iterations

    Worksheets("Run_MC").Cells(start_row, start_column + i).Select
    Selection.Value = i
    Worksheets("Run_MC").Cells(start_row + k, start_column).Select
    Selection.Value = k
    Worksheets("Run_MC").Cells(start_row + k, start_column + i).Select
    ActiveCell.Formula = "=Start_Rate * EXP(NORM.S.INV(RAND())* Standard_Deviation * (" & i & " ^1/2)) "
    'Selection.Value

    Next k
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

您需要从引用中获取VBA变量:

ActiveCell.Formula = "=" & Start_Rate & "*EXP(NORM.S.INV(RAND())*" & Standard_Deviation & "*(" & i & "^1/2))"