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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With