2016-06-28 68 views
0

我在Excel中使用VBA運行MonteCarlo模擬,但在相應的單元格中只收到#Name?錯誤。當我點擊其中一個單元格時,按F2,然後Return錯誤消失,並正確計算該值。這裏有什麼問題?#姓名?使用VBA代碼在單元格中出錯

這是代碼行計算的相應值:

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 
+0

如果它從公式欄工作並且不能從代碼工作,那麼您將擁有本地化版本的Excel和sho或者[使用'FormulaLocal'](http://stackoverflow.com/q/26625383/11683)從代碼或[提供英文](http://stackoverflow.com/a/25251099/11683)。 – GSerg

回答

0

你需要採取的VBA變量出報價:

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

'Start_Rate'和'Standard_Deviation'被定義爲命名範圍。它應該按原樣工作。 – GSerg

相關問題