Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return from a function in case of error in VBA

Tags:

People also ask

How do you return an error in VBA?

In order to return an error value, the function's return data type must be a Variant. If the return type is any other data type, the CVErr function will terminate VBA execution and Excel will report a #VALUE error in the cell.

How do you escape an error in VBA?

Using Exit Sub Statement in VBA First, decide on which line you want to add the “Exit Sub”. After that, check the structure of the code that will get executed when you run the code. Next, enter the “Exit Sub”. In the end, it's better to have comment that describes why you are using the “Exit Sub” statement.

How do I return a result from a VBA function?

To return the result in VBA function You need to use the function name itself and put it a value. Unfortunately the function not stops there and exits.


I am new to VBA and want to return from a function when I see an error. Not able to do so. Any pointers?

Function GetEditboxValue(control As IRibbonControl, text As String) As String

    If Not IsMissing(text) Then
        If Not IsNumeric(text) Then
            MsgBox "Please enter numeric value only."
            ' I WANT TO RETURN HERE 
        End If
    End If


    If control.id = "xyz" Then
    spaceAboveTable = text
    End If


End Function