3

I am really a freshman to study the VBA. I am confused about how to add an error message in a function subroutines.

Here is my problem, when I finished identify a function, how can I add an error message like this: "Please enter the value in an increasing order"?

e.g: If I type =triangular(3,2,1), where the number is in a decreasing order, I should get an error message.

Here is my code:

Public Function triangular(Minimum As Single, mostlikelyvalue As Single, maximum As Single) As Single

    Dim uniform As Single
    Dim d As Single

    Randomize
    Application.Volatile

    d = (mostlikelyvalue - Minimum) / (maximum - Minimum)
    uniform = Rnd

    If uniform <= d Then
        triangular = Minimum + (maximum - Minimum) * Sqr(d * uniform)
    Else
        triangular = Minimum + (maximum - Minimum) * (1 - Sqr((1 - d) * (1 - uniform)))
    End If

End Function
1

2 Answers 2

2

You can test for incorrect order, or also an invalid entry directly in your function and return that rather than use error handling

  • Changed variable names to help avoid errors and confusion with existing function
  • Use a variant function to hold either the result or one of the two customised error messages
  • You may as well use Doubles rather than Singles

code

Public Function triangular(dbMinimum As Double, dbMostlikelyvalue As Double, dbMaximum As Double)

    Dim uniform As Double
    Dim d As Double
    Dim dbCnt As Double


    dbCnt = dbMinimum * dbMostlikelyvalue * dbMaximum
    If dbCnt = 0 Then
        triangular = "at least one value is zero"
        Exit Function
    End If

    If dbMostlikelyvalue > dbMaximum Or dbMinimum > dbMostlikelyvalue Then
        triangular = "values not sorted"
        Exit Function
    End If

    Randomize
    Application.Volatile

    d = (dbMostlikelyvalue - dbMinimum) / (dbMaximum - dbMinimum)
    uniform = Rnd

    If uniform <= d Then
        triangular = dbMinimum + (dbMaximum - dbMinimum) * Sqr(d * uniform)
    Else
        triangular = dbMinimum + (dbMaximum - dbMinimum) * (1 - Sqr((1 - d) * (1 - uniform)))
    End If

End Function
Sign up to request clarification or add additional context in comments.

3 Comments

the "consistency" check doesn't account for dbMinimum being > dbMaximum nor it handles allowable equalities: it could be recoded as If Not (dbMinimum <= dbMostlikelyvalue And dbMostlikelyvalue <= dbMaximum And dbMaximum > dbMinimum) Then. Furthermore the dbCnt check should be either avoided (the previous check avoids division by zero issue) or recoded to check for any single value not allowed to be zero and, in any case, its message should be reworded to "at least one value is zero" since missing values would error out right at the function call
@user3598756 if the max exceeds the mid, and the mid exceeds the min, then by logical extension the max has to be bigger than the min. that check is redundant. The equality check is also handled appropriately already. Agree on the missing value part though - the calling function would have errored before it was called.
@brettdj, if you are referring to my proposed check then please note that it has "equals" in the first two checks, so that the 3rd one is still necessary to avoid division by zero
1

Try this

Public Sub Sample
On Error Goto Err
'call your function here
'some more codes here

Exit Sub 'if all goes well code ends here

Err: 'Error handler
MsgBox Err.Description

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.