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

VFA_Consulting

New Member
Joined
Dec 13, 2017
Messages
3
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(Value<Minimum,-1,IF(Value>Maximum,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?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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([COLOR=#0000ff]dblValue As Double, dblMax As Double, dblMin As Double[/COLOR])

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) [COLOR=#0000ff]As Integer[/COLOR]

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.
 
Upvote 0
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
 
Upvote 0
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 [COLOR=#0000ff]Range[/COLOR], PIMax As [COLOR=#0000ff]Range[/COLOR], PIMin As [COLOR=#0000ff]Range[/COLOR]) As Integer

    If PIValue[COLOR=#0000ff].Value [/COLOR]>= PIMax[COLOR=#0000ff].Value [/COLOR]Then
        PICheck = 1
    ElseIf PIValue[COLOR=#0000ff].Value [/COLOR]<= PIMin[COLOR=#0000ff].Value [/COLOR]Then
        PICheck = -1
    Else
        PICheck = 0
    End If

End Function

Give this a shot and see if it's working.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top