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

#### VFA_Consulting

##### New Member
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

#### AFPathfinder

##### Well-known Member
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.

#### VFA_Consulting

##### New Member
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

#### AFPathfinder

##### Well-known Member
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.

• VFA_Consulting

#### VFA_Consulting

##### New Member
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.

You're welcome!

1,102,823
Messages
5,489,075
Members
407,670
Latest member
noelcjf

### This Week's Hot Topics

• Timer in VBA - Stop, Start, Pause and Reset
[CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
• how to updates multiple rows in muliselect listbox
Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
• Delete Row from Table
I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
• Assigning to a variable
I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
• Way to verify information
Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
• Active Cell Address – Inactive Sheet
How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...