Error values generated in functions

S

Subodh

I need to create a function in VBA Excel.
The code is like this
Function mysub(x as variant) as variant
..................... code goes here ...............
end function
Now if the user enters code mysub(somestring)
or something like that then there is a error message in the Excel Cell
But, I want to get the excel cell not to display such error.
I tried to get the type but it didn't work fine
My code goes like this.

Function newfun(agrus As Variant) As Variant
Dim x As Variant
newfun = argus * agrus
Exit Function
'If IsMissing(argus) Then
'newfun = argus & "No argument in the function"
'Else
x = TypeName(argus)
Select Case x
Case "Range"
newfun = argus & "Cannot take range as argument. Enter Single
cell value"
Case "Null"
newfun = argus & "You didn't entered an argument. Must have
one argument."
Case "Error"
newfun = argus & "You entered a error value. Check the value."
Case "Boolean"
newfun = argus & "You entered boolean value."
Case "Empty"
newfun = argus & "Empty value."
Case Else
newfun = argus & "Other data types. " & TypeName(argus)
End Select
'End If

'newfun = argus & "No you can make a start"
'End If
End Function

Can anyone help
 
G

Gary Brown

Get rid of the 'Exit Function' line or try something like...

'/---------------------------------------
Function newfun(argus As Variant) As Variant
Dim x As Variant
On Error GoTo err_Function
newfun = argus * argus
exit_Function:
On Error Resume Next
Exit Function
err_Function:
newfun = Null
GoTo exit_Function
End Function
'/---------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top