# CVErr
{: .no_toc }

Returns a **Variant** of subtype **Error** containing an error number specified by the user.

Syntax: **CVErr(** *errornumber* **)**

*errornumber*
: *required* Any valid error number.

Use the **CVErr** function to create user-defined errors in user-created procedures. For example, a function that accepts several arguments and normally returns a string can evaluate the input arguments to ensure they are within an acceptable range. If they aren't, the function is unlikely to return the expected result. In this event, **CVErr** returns an error number that tells the caller what action to take.

Note that implicit conversion of an **Error** is not allowed. For example, the return value of **CVErr** cannot be directly assigned to a variable that is not a **Variant**. An explicit conversion (using [**CInt**](/tB/Modules/Conversion/CInt.md), [**CDbl**](/tB/Modules/Conversion/CDbl.md), and so on) of the value returned by **CVErr** can be assigned to a variable of the appropriate data type.

### Common Errors

| Error | When |
|---|---|
| [5 (`&H5`)](/Reference/Run-time-Errors.md#5), *Invalid procedure call or argument* | *errornumber* rounds to a number less than 0 or greater than 65,535 |
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | *errornumber* is **Null** |

### Example

This example uses the **CVErr** function to return a **Variant** whose **VarType** is **vbError** (10). The user-defined function `CalculateDouble` returns an error if the argument passed to it isn't a number. Use **CVErr** to return user-defined errors from user-defined procedures or to defer handling of a run-time error. Use the **IsError** function to test whether the value represents an error.

```tb check_build
' Call CalculateDouble with an error-producing argument.
Sub Test()
    Debug.Print CalculateDouble("345.45robert")
End Sub

' Define CalculateDouble Function procedure.
Function CalculateDouble(Number)
    If IsNumeric(Number) Then
        CalculateDouble = Number * 2    ' Return result.
    Else
        CalculateDouble = CVErr(2001)   ' Return a user-defined error number.
    End If
End Function
```

### See Also

- [Error](/tB/Modules/Conversion/Error.md) function
