VBA:如果再尝试DCF宏,那么麻烦

时间:2017-04-06 23:04:04

标签: vba

我正在尝试编写一个宏来投影DCF长达五年,允许每个时期的不同预计现金流量。我还希望用户可以选择在较短的时间内运行宏。我相信if then else语句会绊倒代码,但老实说我很丢失。我正在寻找一种方法来使用If / Then和GoTo根据周期数运行不同的程序。

感谢您帮助新手。这是我的(非工作)代码:

 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

2 个答案:

答案 0 :(得分:0)

我会选择0m3r提到的内容,将你在这里进行的内容组织成一个案例陈述更有意义,并且我补充一点,你可以将大多数案例程序放入他们自己的子程序中。我还要提出,上面代码中的“句点”输入框超出了If语句的范围,所以你不会跳回来,你总是要把一个周期运行到exit sub。

你可以尝试这样的事情:

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

答案 1 :(得分: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