Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default How to return Excel errors from VBA?

How can I return Excel errors such as #NUM from a VBA function?

I cannot seem to call ERROR.TYPE() from VBA (e.g.
Application.WorksheetFunction...).

Right now, I return #VALUE by a kludge: assigning 1e308 to a type
Long variable. I could do something similar to return #DIV and
perhaps #REF.

But I would prefer to do thinks more cleanly and self-documenting; and
I would like the flexibility of returning other errors.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default How to return Excel errors from VBA?

Hi,

Here is an example for a function that divides 2 numbers.
- if arguments are not numbers, returns #Val
- if second argument is Zero, then return #Div0
- else returns a/b

Function fun(a As Variant, b As Variant) As Variant
If Not (IsNumeric(a) And IsNumeric(b)) Then
fun = CVErr(xlErrValue)
ElseIf b = 0 Then
fun = CVErr(xlErrDiv0)
Else
fun = CDbl(a) / CDbl(b)
End If
End Function
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


" wrote:

How can I return Excel errors such as #NUM from a VBA function?

I cannot seem to call ERROR.TYPE() from VBA (e.g.
Application.WorksheetFunction...).

Right now, I return #VALUE by a kludge: assigning 1e308 to a type
Long variable. I could do something similar to return #DIV and
perhaps #REF.

But I would prefer to do thinks more cleanly and self-documenting; and
I would like the flexibility of returning other errors.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default How to return Excel errors from VBA?

On Nov 28, 3:20 pm, sebastienm
wrote:
fun = CVErr(xlErrDiv0)


Thanks. And for a list of the Excel error values that I wanted, use
VBA Help to search for "Excel constants", then select xlCVError.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Throwing Circular Errors When No Errors Exist MDW Excel Worksheet Functions 1 August 10th 06 02:15 PM
excel errors [email protected] Excel Programming 1 January 18th 05 12:23 PM
excel errors [email protected] Excel Programming 0 January 18th 05 12:06 PM
EXCEL ERRORS NoWaySpammers New Users to Excel 3 December 18th 04 11:22 AM
Unresolved Errors in IF Statements - Errors do not show in results Markthepain Excel Worksheet Functions 2 December 3rd 04 08:49 AM


All times are GMT +1. The time now is 09:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"