Results 1 to 6 of 6

Thread: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

  1. #1
    New Member
    Join Date
    Dec 2017
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    I do a lot of forecasting and am tired of having to repeatedly write a formula from scratch to test whether or not my forecasts fall within the prediction interval. To be clear what I mean, is that I may predict a value of 1,000 with an 80% chance it will be within 900 and 1100. I need to test for dozens of rows at a time, which values fell within such an interval. I tried to create a UDF to make this easier:

    Public Function PICheck(Value, Maximum, Minimum)
    PICheck = IF(ValueMaximum,1,0))
    End Function

    I get a Compile Error: Syntax Error. I'm sure I'm just overlooking some little thing, but despite all my experimenting and Googling, I cannot divine what it is. Does anyone see it?

  2. #2
    Board Regular
    Join Date
    Apr 2012
    Location
    Texas, USA
    Posts
    519
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    You need to specify the data types of those properties (Value, Maximum, Minimum) and you'll want to avoid using "Value" as a variable name:
    Code:
    Public Function PICheck(dblValue As Double, dblMax As Double, dblMin As Double)
    It probably wouldn't hurt to give the function a data type as well.
    Code:
    Public Function PICheck(dblValue As Double, dblMax As Double, dblMin As Double) As Integer
    Also, the syntax of your PICheck value would be the value and not a formula (the formula is the "=PICheck( , , )" that you put in the cell). I'm not sure where you are using the Minimum value so you may not need it. I'm also not sure if you are trying to multiply the value by the max or if the value is equal to the max.
    Code:
    If dblValue = dblMax Then
        PICheck = 1
    Else
        PICheck = 0
    End If
    Altogether (untested), it would be something like this:
    Code:
    Public Function PICheck(dblValue As Double, dblMax As Double, dblMin As Double) As Integer
    
        If dblValue = dblMax Then
            PICheck = 1
        Else
            PICheck = 0
        End If
    
    End Function
    If you need additional help on this, let us know.
    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

    If you can get it into Excel, imagination is the usual limitation.

  3. #3
    New Member
    Join Date
    Dec 2017
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    Sorry, something I happened in pasting the function and I failed to proofread it. And thank you so much for taking a look at it. OK, I think I'm getting there, but I still have an error. To be clear, the purpose of the function is to be able to specify a Maximum and a Minimum that are contained in their respective cells in a row, then test if the value (PIValue) is between these two. If PIValue is above the max, output should be 1; if it is below the min, it should be -1, if it is in between the two values, output should be 0. Based on your feedback, I put together the code below. But I am getting an error on the ElseIf line. Any thoughts? Public Function PICheck(PIValue As Double, PIMax As Double, PIMin As Double) As Integer If PIValue >= PIMax Then PICheck = 1 ElseIf PIValue =< PIMin PICheck = -1 Else PICheck = 0 End If End Function

  4. #4
    Board Regular
    Join Date
    Apr 2012
    Location
    Texas, USA
    Posts
    519
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    Code tags make posting and reading VBA code much easier (the "#" button in the tools bar).

    I'd assume you are referencing other cells in the properties of the function, right (a better practice than hard coding the values in the cell formula)? Those Double types would need changed to Range and then we would need to add the ".Value" syntax to extract the doubles from those cells.

    Code:
    Public Function PICheck(PIValue As Range, PIMax As Range, PIMin As Range) As Integer
    
        If PIValue.Value >= PIMax.Value Then
            PICheck = 1
        ElseIf PIValue.Value <= PIMin.Value Then
            PICheck = -1
        Else
            PICheck = 0
        End If
    
    End Function
    Give this a shot and see if it's working.
    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

    If you can get it into Excel, imagination is the usual limitation.

  5. #5
    New Member
    Join Date
    Dec 2017
    Posts
    3
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    Thank you so much! Not only did that work, but you've taught me a bit more and gotten me closer to VBA self-reliance! And thank you for your patience with learning how to effectively post.

  6. #6
    Board Regular
    Join Date
    Apr 2012
    Location
    Texas, USA
    Posts
    519
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Simple conditional output failure aka Prediction Interval Testing - should be an easy VBA fix

    You're welcome!
    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

    If you can get it into Excel, imagination is the usual limitation.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •