This example creates a function that divides two numbers. If there is an error dividing the numbers, then a variant of type "error" is returned. Otherwise, the function returns the result of the division. The IsError function is used to determine whether the function encountered an error.
Function Div(ByVal a,ByVal b) As Variant
If b = 0 Then
Div = CVErr(2112) 'Return a special error value.
Else
Div = a / b 'Return the division.
End If
End Function
Sub Main()
Dim a As Variant
a = Div(10,12)
If IsError(a) Then
MsgBox "The following error occurred: " & CStr(a)
Else
MsgBox "The result of the division is: " & a
End If
End Sub
|