Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:If-Then-Else尝试的DCF宏出现问题_Vba - Fatal编程技术网

VBA:If-Then-Else尝试的DCF宏出现问题

VBA:If-Then-Else尝试的DCF宏出现问题,vba,Vba,我正试图写一个宏来预测一个长达五年的DCF,考虑到每个时期不同的预测现金流。我还希望用户可以选择在更短的时间内运行宏。我相信if-then-else语句会让代码出错,但我真的很迷茫。我正在寻找一种使用If/Then和GoTo的方法,根据周期数运行不同的程序 谢谢你帮助一个新手。这是我的(非工作)代码: 我同意0m3r所提到的,将您在这里所做的事情组织成一个案例陈述更有意义,并且我要补充的是,您可以将您的大多数案例过程放入它们自己的子过程中。我还想指出,上面代码中的“句点”输入框超出了If语句的范

我正试图写一个宏来预测一个长达五年的DCF,考虑到每个时期不同的预测现金流。我还希望用户可以选择在更短的时间内运行宏。我相信if-then-else语句会让代码出错,但我真的很迷茫。我正在寻找一种使用If/Then和GoTo的方法,根据周期数运行不同的程序

谢谢你帮助一个新手。这是我的(非工作)代码:


我同意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
我会使用一个循环:

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

使用Select Case语句-
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