ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Throwing #Value in a UDF written in Excel VBA (https://www.excelbanter.com/excel-programming/448230-throwing-value-udf-written-excel-vba.html)

Dom[_4_]

Throwing #Value in a UDF written in Excel VBA
 
I just wrote a UDF in excel's VBA. If the arguments are wrong, I'd
like to have #Value appear in the cell. Is there a way to throw this
exception in a VBA function?

Maurizio Borrelli

Throwing #Value in a UDF written in Excel VBA
 
Il giorno giovedì 21 febbraio 2013 20:11:38 UTC+1, Dom ha scritto:

Hi,
try

Public Function m(a)
'...
m = CVErr(xlErrValue)
End Function

I just wrote a UDF in excel's VBA. If the arguments are wrong, I'd
like to have #Value appear in the cell. Is there a way to throw this
exception in a VBA function?


isabelle

Throwing #Value in a UDF written in Excel VBA
 
hi Dom,

one possibility is to declare the arguments of type Optional

Sub test()
MyFonction Arg1:=0, Arg2:=56765765
MyFonction Arg1:=453
MyFonction Arg2:=56765765
MyFonction
End Sub


Function MyFonction(Optional Arg1 As Variant, Optional Arg2 As Variant)
As Variant
MsgBox "Arguments manquants:" & vbCrLf & _
IsMissing(Arg1) & " / " & IsMissing(Arg2)
End Function

each argument can be verified by type
TypeName
VarType
IsDate
IsNumeric
IsEmpty
IsMissing
IsArray
IsObject
IsNull

isabelle

Le 2013-02-21 14:11, Dom a écrit :
I just wrote a UDF in excel's VBA. If the arguments are wrong, I'd
like to have #Value appear in the cell. Is there a way to throw this
exception in a VBA function?


Dom[_4_]

Throwing #Value in a UDF written in Excel VBA
 
On Feb 21, 2:41*pm, isabelle wrote:
hi Dom,

one possibility is to declare the arguments of type Optional

Sub test()
* * *MyFonction Arg1:=0, Arg2:=56765765
* * *MyFonction Arg1:=453
* * *MyFonction Arg2:=56765765
* * *MyFonction
End Sub

Function MyFonction(Optional Arg1 As Variant, Optional Arg2 As Variant)
As Variant
* * *MsgBox "Arguments manquants:" & vbCrLf & _
* * * * *IsMissing(Arg1) & " / " & IsMissing(Arg2)
End Function

each argument can be verified by type
* * * TypeName
* * * *VarType
* * * *IsDate
* * * *IsNumeric
* * * *IsEmpty
* * * *IsMissing
* * * *IsArray
* * * *IsObject
* * * *IsNull

isabelle

Le 2013-02-21 14:11, Dom a crit :



I just wrote a UDF in excel's VBA. *If the arguments are wrong, I'd
like to have #Value appear in the cell. *Is there a way to throw this
exception in a VBA function?- Hide quoted text -


- Show quoted text -


Thanks, Maurizio, Exactly what I wanted.

Maurizio Borrelli

Throwing #Value in a UDF written in Excel VBA
 
Il giorno giovedì 21 febbraio 2013 20:47:26 UTC+1, Dom ha scritto:

YW!

Thanks, Maurizio, Exactly what I wanted.




All times are GMT +1. The time now is 03:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com