Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Error handling in vba function

In general, what would be the best way to show an error message from a vba
function ?

I've tried a MsgBox call, but this is anoying when the function editor
(shown after clicking the = button) is used. The function editor tries to
evaluate the function before all parameters are entered completely. This
causes an error in my vba function which then interrupts the parameter
selection with the message box.

Is there some way to determine is the function editor (is it called this way
?) is visible ? so I can surpress the error message in that case ?

TIA for any help !

Jaap Versteegh


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Error handling in vba function

Hi
use the CVErr function in the VBA help

Regards
Frank

-----Original Message-----
In general, what would be the best way to show an error

message from a vba
function ?

I've tried a MsgBox call, but this is anoying when the

function editor
(shown after clicking the = button) is used. The function

editor tries to
evaluate the function before all parameters are entered

completely. This
causes an error in my vba function which then interrupts

the parameter
selection with the message box.

Is there some way to determine is the function editor (is

it called this way
?) is visible ? so I can surpress the error message in

that case ?

TIA for any help !

Jaap Versteegh


.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Error handling in vba function

Hi
use the CVErr function in the VBA help


Thanks for the response Frank, but how would this help ?

Problem:
A vba (worksheet)function needs 2 ranges as parameters, but they both need
to be of the same size. I would like to notify the user when they aren't
rather than just return "#VALUE" in the cell.

So something like:

function MyFunc (MyRange1 as Range, MyRange2 as Range)
On Error GoTo ErrorHandler
if MyRange1.Cells.Count < MyRange2.Cells.Count then
Err.Raise 1000, "MyFunc", "Ranges not of same size !"
end if
' Do the stuff the function does here !
Exit Function
ErrorHandler:
' Now here I would like to tell the user of this function what the problem
is ...
MsgBox Err.Description
' This however causes problems when entering the function from the formula
bar after
' clicking the equal sign - the function is invoked before the second
range has been entered
' completely, so the ranges are not of the same size (yet). The message
box is displayed so
' the user can't complete the entry of the second parameter....

' Re-raise the error so the function will return #VALUE !
Err.Raise 1000
End Function

TIA for any further suggestions...

Regards
Frank




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Error handling in vba function

Hi Jaap,

It is not generally considered a good idea to have a Msgbox inside a UDF:
You could make the function return a string containing the error message,
but usually you make it return an error value as Frank suggested using
CVERR(xlerrValue) or CVERR(XLErrNA) etc.

To detect if the function is being called from the function wizard Alex
Koenig has suggested (and it works):
If (Not Application.CommandBars("Standard").Controls(1).En abled) Then

' function wizard

else

' not function wizard

endif

see http://www.DecisionModels.com/calcsecretsj.htm for more info on UDF
problems.


regards

Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com/FxlV2WhatsNew.htm

"Jaap Versteegh" wrote in message
...
Hi
use the CVErr function in the VBA help


Thanks for the response Frank, but how would this help ?

Problem:
A vba (worksheet)function needs 2 ranges as parameters, but they both need
to be of the same size. I would like to notify the user when they aren't
rather than just return "#VALUE" in the cell.

So something like:

function MyFunc (MyRange1 as Range, MyRange2 as Range)
On Error GoTo ErrorHandler
if MyRange1.Cells.Count < MyRange2.Cells.Count then
Err.Raise 1000, "MyFunc", "Ranges not of same size !"
end if
' Do the stuff the function does here !
Exit Function
ErrorHandler:
' Now here I would like to tell the user of this function what the

problem
is ...
MsgBox Err.Description
' This however causes problems when entering the function from the

formula
bar after
' clicking the equal sign - the function is invoked before the second
range has been entered
' completely, so the ranges are not of the same size (yet). The message
box is displayed so
' the user can't complete the entry of the second parameter....

' Re-raise the error so the function will return #VALUE !
Err.Raise 1000
End Function

TIA for any further suggestions...

Regards
Frank






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Error handling in vba function

You could also use other errors. So your unequal ranges could be

if MyRange1.Cells.Count < MyRange2.Cells.Count then
myFunc = CvErr(xlErrRef)
End Function
End If

and use #Value for some otherv error. Your user instructions could tell the
user what each error means.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Jaap Versteegh" wrote in message
...
Hi
use the CVErr function in the VBA help


Thanks for the response Frank, but how would this help ?

Problem:
A vba (worksheet)function needs 2 ranges as parameters, but they both need
to be of the same size. I would like to notify the user when they aren't
rather than just return "#VALUE" in the cell.

So something like:

function MyFunc (MyRange1 as Range, MyRange2 as Range)
On Error GoTo ErrorHandler
if MyRange1.Cells.Count < MyRange2.Cells.Count then
Err.Raise 1000, "MyFunc", "Ranges not of same size !"
end if
' Do the stuff the function does here !
Exit Function
ErrorHandler:
' Now here I would like to tell the user of this function what the

problem
is ...
MsgBox Err.Description
' This however causes problems when entering the function from the

formula
bar after
' clicking the equal sign - the function is invoked before the second
range has been entered
' completely, so the ranges are not of the same size (yet). The message
box is displayed so
' the user can't complete the entry of the second parameter....

' Re-raise the error so the function will return #VALUE !
Err.Raise 1000
End Function

TIA for any further suggestions...

Regards
Frank








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 #N/A with AVERAGE Function - Average of values in Row Sam via OfficeKB.com Excel Worksheet Functions 13 July 31st 05 03:59 PM
Error handling V. Roe Excel Programming 2 February 27th 04 08:04 PM
Error Handling Todd Excel Programming 1 February 13th 04 11:29 PM
Error Handling James Agostinho Excel Programming 1 January 30th 04 06:40 AM
Error handling John Pierce Excel Programming 3 October 3rd 03 12:17 PM


All times are GMT +1. The time now is 04:40 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"