我正在嘗試編寫一個宏來投射長達五年的DCF,從而允許爲每個期間使用不同的預計現金流量。我也希望用戶可以選擇運行較少年的宏。我相信如果其他語句正在破壞代碼,但我確實很失落。我正在尋找一種方法來使用If/Then和GoTo來根據週期數來運行不同的程序。VBA:如果那麼嘗試使用DCF宏還是會遇到問題
感謝您幫助新手。這裏是我的(非工作)代碼:
Sub DCFFiveYears()
Dim x As Double, CF1 As Double, CF2 As Double, CF3 As Double, CF4 As Double, CF5 As Double, DR As Double, Periods As Integer
DR = InputBox("Enter the discount rate as a decimal. ", "Discount Rate")
If Periods = 1 Then GoTo OnePeriod
ElseIf Periods = 2 Then GoTo TwoPeriod
ElseIf Periods = 3 Then GoTo ThreePeriod
ElseIf Periods = 4 Then GoTo FourPeriod
ElseIf Periods = 5 Then GoTo FivePeriod
End If
Periods = InputBox("Enter the number of periods.", "Periods")
OnePeriod:
CF1 = InputBox("Please enter the predicted YEAR-1 cash flow", "Cash Flow")
Dim x As Double
x = CF1/(1# + DR)
Range("A1") = x
Selection.NumberFormat = "0.00"
Selection.Style = "Currency"
Exit Sub
TwoPeriod:
CF1 = InputBox("Please enter the predicted YEAR-1 cash flow", "Cash Flow")
CF2 = InputBox("Please enter the predicted YEAR-2 cash flow", "Cash Flow")
Dim x As Double
x = (CF1/(1# + DR)^1) + (CF2/(1# + DR)^2)
Range("A1") = x
Selection.NumberFormat = "0.00"
Selection.Style = "Currency"
Exit Sub
ThreePeriod:
CF1 = InputBox("Please enter the predicted YEAR-1 cash flow", "Cash Flow")
CF2 = InputBox("Please enter the predicted YEAR-2 cash flow", "Cash Flow")
CF3 = InputBox("Please enter the predicted YEAR-3 cash flow", "Cash Flow")
Dim x As Double
x = (CF1/(1# + DR)^1) + (CF2/(1# + DR)^2) + (CF3/(1# + DR)^3)
Range("A1") = x
Selection.NumberFormat = "0.00"
Selection.Style = "Currency"
Exit Sub
FourPeriod:
CF1 = InputBox("Please enter the predicted YEAR-1 cash flow", "Cash Flow")
CF2 = InputBox("Please enter the predicted YEAR-2 cash flow", "Cash Flow")
CF3 = InputBox("Please enter the predicted YEAR-3 cash flow", "Cash Flow")
CF4 = InputBox("Please enter the predicted YEAR-4 cash flow", "Cash Flow")
Dim x As Double
x = (CF1/(1# + DR)^1) + (CF2/(1# + DR)^2) + (CF3/(1# + DR)^3) + (CF4/(1# + DR)^4)
Range("A1") = x
Selection.NumberFormat = "0.00"
Selection.Style = "Currency"
Exit Sub
FivePeriod:
CF1 = InputBox("Please enter the predicted YEAR-1 cash flow", "Cash Flow")
CF2 = InputBox("Please enter the predicted YEAR-2 cash flow", "Cash Flow")
CF3 = InputBox("Please enter the predicted YEAR-3 cash flow", "Cash Flow")
CF4 = InputBox("Please enter the predicted YEAR-4 cash flow", "Cash Flow")
CF5 = InputBox("Please enter the predicted YEAR-5 cash flow", "Cash Flow")
Dim x As Double
x = (CF1/(1# + DR)^1) + (CF2/(1# + DR)^2) + (CF3/(1# + DR)^3) + (CF4/(1# + DR)^4) + (CF5/(1# + DR)^5)
Range("A1") = x
Selection.NumberFormat = "0.00"
Selection.Style = "Currency"
Exit Sub
ErrorMessage:
MsgBox "Invalid input. Code has terminated.", , "Error!"
End Sub
與SELECT CASE語句工作 - https://stackoverflow.com/documentation/vba/1873/流量控制結構#噸= 201704062312125690151 – 0m3r