2017-04-06 46 views
2

我正在嘗試編寫一個宏來投射長達五年的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 
+0

與SELECT CASE語句工作 - https://stackoverflow.com/documentation/vba/1873/流量控制結構#噸= 201704062312125690151 – 0m3r

回答

0

我與0m3r提到什麼去的,更有意義的組織你打算什麼就在這裏變成一個case語句,我想補充一點,你可以適合大多數你的案例程序納入他們自己的子程序中。我還會提交上面代碼中的「句點」輸入框,它超出了If語句的範圍,所以你不會跳回來,並且總是要運行一個時段進入退出子。

你可以嘗試這樣的事:

Dim DR as double 

Sub DCFFiveYears() 

Dim x As Double, CF1 As Double, CF2 As Double, CF3 As Double, CF4 As Double, CF5 As Double,Periods As Integer 

DR = InputBox("Enter the discount rate as a decimal. ", "Discount Rate") 
Periods = InputBox("Enter the number of periods", "Periods") 
Select Case Periods 
    Case 1 
     OnePeriod 
    Case 2 
     TwoPeriod 
    Case 3 
     ThreePeriod 
    Case 4 
     FourPeriod 
    Case 5 
     FivePeriod 
End Select 



End Sub 
Sub 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" 
End Sub 

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" 
End Sub 


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" 
End Sub 

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" 
End Sub 


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" 
End Sub 



ErrorMessage: 

MsgBox "Invalid input. Code has terminated.", , "Error!" 

End Sub 
0

我會用一個循環:

Sub DCFFiveYears() 

    Dim x As Double 
    Dim DR As Double 
    Dim Periods As Integer 
    Dim p As Integer 
    Dim CF As Double 

    Periods = InputBox("Enter the number of periods.", "Periods") 
    DR = InputBox("Enter the discount rate as a decimal. ", "Discount Rate") 

    x = 0 
    For p = 1 To Periods 
     CF = InputBox("Please enter the predicted YEAR-" & p & " cash flow", "Cash Flow") 

     x = x + CF/(1# + DR)^p 
    Next 
    With Range("A1") 
     .Value = x 
     .NumberFormat = "0.00" 
     .Style = "Currency" 
    End With 
End Sub 
相關問題