Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default UDF/VBA Function, trap error, avoid #VALUE!

I have created a function that returns a string. When it fails, Excel
reports "#VALUE!". I would like to report my description of the error
instead. I have tried to use error handling, but it seems to be
ignored. In the area in which I "Try to do some useful Stuff", an error

occasionally occurs. The debugger shows that control does not transfer
to my ErrorHandler. Instead, execution ends immediately, and the cell
value is set to "#VALUE!".

Public Function myFunction() As String
On Error GoTo ErrorHandler


' Try to do some useful Stuff


myFunction = "RESULT"


FunctionExit:
Exit Function


ErrorHandler:
Err.Clear
myFunction = "My Error Description"
Resume FunctionExit


End Function


Thanks for your assistance!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default UDF/VBA Function, trap error, avoid #VALUE!

This is the exact line:
Set theDateRec = fiscalCalendar.Item(CStr(dateIn))

Note that:
theDateRec is a class, calendarRecord
fiscalCalendar is a collection of calendardRecords
The collection is Keyed by Dates

The error occurs when an invalid date/key is passed.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default UDF/VBA Function, trap error, avoid #VALUE!

I have created a new function to try to recreate an example function
that mimics my intent:

Public Function HelloWorld(causeError As Boolean) As String

On Error GoTo ErrorHandler
If (causeError) Then
Err.Raise 1234
Else
HelloWorld = "Hello World"
End If

FunctionExit:
Exit Function

ErrorHandler:
Err.Clear
HelloWorld = "ERROR HAPPENED"
Resume FunctionExit

End Function

=HelloWorld(FALSE) returns "Hello World"
=HelloWorld(TRUE) returns #VALUE!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default UDF/VBA Function, trap error, avoid #VALUE!

Hmm... I get

=HelloWorld(FALSE) returns "Hello World"
=HelloWorld(TRUE) returns "ERROR HAPPENED"

I can cause an error if I use an argument that can't be coerced to a
Boolean:

=HelloWorld("TRUE ") returns #VALUE!

In article .com,
wrote:

I have created a new function to try to recreate an example function
that mimics my intent:

Public Function HelloWorld(causeError As Boolean) As String

On Error GoTo ErrorHandler
If (causeError) Then
Err.Raise 1234
Else
HelloWorld = "Hello World"
End If

FunctionExit:
Exit Function

ErrorHandler:
Err.Clear
HelloWorld = "ERROR HAPPENED"
Resume FunctionExit

End Function

=HelloWorld(FALSE) returns "Hello World"
=HelloWorld(TRUE) returns #VALUE!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default UDF/VBA Function, trap error, avoid #VALUE!

Hmmm. Well, thanks for your inputs.. I can't figure out why my code
works for you and not me. I've been poking around in Excel options,
etc. to no avail.

Thanks!!

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default UDF/VBA Function, trap error, avoid #VALUE!

I am not too familiar with VBA/Excel, etc. I wonder if I am doing
something different at another level. I choose Tools/Macros/Visual
Basic Editor, then I create new code module and paste this code in
there. Is that the right way? Is that what you are doing as well? I am
trying to figure out our difference.

Thanks!!

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
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Shell function and error trap tmort[_13_] Excel Programming 0 August 3rd 05 01:10 AM
Error Trap TEB2 Excel Programming 2 March 15th 05 05:34 PM
Avoid error with simple subtraction function wjsubs[_6_] Excel Programming 1 July 9th 04 07:27 PM
How to avoid error 2015 when using ActiveCell.Offsett in own function Torben Laursen Excel Programming 2 February 18th 04 03:53 PM


All times are GMT +1. The time now is 02:26 PM.

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

About Us

"It's about Microsoft Excel"