Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Error messages from user defined Excel worksheet function

I would disagree that showing a message box in a function that will be used
in a worksheet is desirable. It certainly doesn't conform to the way other
functions work. When an error does occur, should the msgbox pop up everytime
there is a recalc? also, even though you show the messagebox, your code go
aheads and raises another error by trying to execute with the flawed ranges.

Since the error is a reference, I would show a #Ref and get out.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

I am developing a VBA function to be used as an Excel worksheet function.
Some error checking is included in the VBA. My problem is that Excel
executes the function before all input arguments have been specified. This
gives rise to some very disturbing messages from my error checking.

How can I prevent Excel from running the function during the specification
of input ?

I have attached a screen dump with a small example that illustrates the
problem. The VBA is below. The screen dump shows the situation when I have
entered ARange and is going on to enter BRange. I click C3 and (while
holding down the Shift key) I try to click D4. But inbetween Excel runs the
function and recognizes that BRange is too small, i.e. BRange is C3.

Any ideas ? I could suggest one myself : Remove all calls to MsgBox, and
assign an error value #VALUE! to the function. Any better ideas ?

Please also answer to (AndThis).

Kind regards,
Helge

'=================== VBA start ===================
Option Explicit
Option Base 1

Function AAAA(ByVal aRange As Range, _
ByVal bRange As Range) As Variant

Dim Nrow_a As Integer, Nrow_b As Integer, Ncol_a As Integer, Ncol_b As
Integer
Dim R As Integer, C As Integer

Nrow_a = aRange.Rows.Count
Nrow_b = bRange.Rows.Count
Ncol_a = aRange.Columns.Count
Ncol_b = bRange.Columns.Count

If Not (Nrow_a = Nrow_b And Ncol_a = Ncol_b) Then
Call MsgBox("The two input ranges should have the same number of rows
and columns.", _
vbCritical, "ERROR")
End If

AAAA = 0
For R = 1 To Nrow_a
For C = 1 To Ncol_a
AAAA = AAAA + aRange.Cells(R, C) * bRange.Cells(R, C)
Next C
Next R

End Function
'=================== VBA end ===================




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Error messages from user defined Excel worksheet function

If the range argument is improper, I would think #Ref, but I guess that is up
to you.

I don't see any .jpg file.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

#Ref or #VALUE! ?
Excel uses #VALUE! as shown in the attached JPG file.
--- and it is errors equivalent to the one shown in the JPG file that I
would like to trap. (The two ranges are not of the same size.)
Regards,
Helge
_______________________


"Tom Ogilvy" wrote in message
...
I would disagree that showing a message box in a function that will be used
in a worksheet is desirable. It certainly doesn't conform to the way
other
functions work. When an error does occur, should the msgbox pop up
everytime
there is a recalc? also, even though you show the messagebox, your code
go
aheads and raises another error by trying to execute with the flawed
ranges.

Since the error is a reference, I would show a #Ref and get out.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

I am developing a VBA function to be used as an Excel worksheet function.
Some error checking is included in the VBA. My problem is that Excel
executes the function before all input arguments have been specified.
This
gives rise to some very disturbing messages from my error checking.

How can I prevent Excel from running the function during the
specification
of input ?

I have attached a screen dump with a small example that illustrates the
problem. The VBA is below. The screen dump shows the situation when I
have
entered ARange and is going on to enter BRange. I click C3 and (while
holding down the Shift key) I try to click D4. But inbetween Excel runs
the
function and recognizes that BRange is too small, i.e. BRange is C3.

Any ideas ? I could suggest one myself : Remove all calls to MsgBox, and
assign an error value #VALUE! to the function. Any better ideas ?

Please also answer to (AndThis).

Kind regards,
Helge

'=================== VBA start ===================
Option Explicit
Option Base 1

Function AAAA(ByVal aRange As Range, _
ByVal bRange As Range) As Variant

Dim Nrow_a As Integer, Nrow_b As Integer, Ncol_a As Integer, Ncol_b
As
Integer
Dim R As Integer, C As Integer

Nrow_a = aRange.Rows.Count
Nrow_b = bRange.Rows.Count
Ncol_a = aRange.Columns.Count
Ncol_b = bRange.Columns.Count

If Not (Nrow_a = Nrow_b And Ncol_a = Ncol_b) Then
Call MsgBox("The two input ranges should have the same number of
rows
and columns.", _
vbCritical, "ERROR")
End If

AAAA = 0
For R = 1 To Nrow_a
For C = 1 To Ncol_a
AAAA = AAAA + aRange.Cells(R, C) * bRange.Cells(R, C)
Next C
Next R

End Function
'=================== VBA end ===================







  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Error messages from user defined Excel worksheet function

Try the below adjustments. (untested)

Function AAAA(ByVal aRange As Range, _
ByVal bRange As Range) As Variant

Dim Nrow_a As Integer, Nrow_b As Integer, Ncol_a As Integer, Ncol_b As
Integer
Dim R As Integer, C As Integer
Dim rng as Range

Nrow_a = aRange.Rows.Count
Nrow_b = bRange.Rows.Count
Ncol_a = aRange.Columns.Count
Ncol_b = bRange.Columns.Count
On error resume next
set rng = Application.Caller
On error goto 0
if rng is nothing then
If Not (Nrow_a = Nrow_b And Ncol_a = Ncol_b) Then
Call MsgBox("The two input ranges should have the same number of rows
and columns.", _
vbCritical, "ERROR")
End If
End if
AAAA = 0
For R = 1 To Nrow_a
For C = 1 To Ncol_a
AAAA = AAAA + aRange.Cells(R, C) * bRange.Cells(R, C)
Next C
Next R

End Function

--
Regards,
Tom Ogilvy

"Helge V. Larsen" (AndThis) wrote in
message ...
#Ref or #VALUE! ?
Excel uses #VALUE! as shown in the attached JPG file.
--- and it is errors equivalent to the one shown in the JPG file that I
would like to trap. (The two ranges are not of the same size.)
Regards,
Helge
_______________________


"Tom Ogilvy" wrote in message
...
I would disagree that showing a message box in a function that will be

used
in a worksheet is desirable. It certainly doesn't conform to the way
other
functions work. When an error does occur, should the msgbox pop up
everytime
there is a recalc? also, even though you show the messagebox, your

code
go
aheads and raises another error by trying to execute with the flawed
ranges.

Since the error is a reference, I would show a #Ref and get out.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

I am developing a VBA function to be used as an Excel worksheet

function.
Some error checking is included in the VBA. My problem is that Excel
executes the function before all input arguments have been specified.
This
gives rise to some very disturbing messages from my error checking.

How can I prevent Excel from running the function during the
specification
of input ?

I have attached a screen dump with a small example that illustrates the
problem. The VBA is below. The screen dump shows the situation when I
have
entered ARange and is going on to enter BRange. I click C3 and (while
holding down the Shift key) I try to click D4. But inbetween Excel runs
the
function and recognizes that BRange is too small, i.e. BRange is C3.

Any ideas ? I could suggest one myself : Remove all calls to MsgBox,

and
assign an error value #VALUE! to the function. Any better ideas ?

Please also answer to (AndThis).

Kind regards,
Helge

'=================== VBA start ===================
Option Explicit
Option Base 1

Function AAAA(ByVal aRange As Range, _
ByVal bRange As Range) As Variant

Dim Nrow_a As Integer, Nrow_b As Integer, Ncol_a As Integer, Ncol_b
As
Integer
Dim R As Integer, C As Integer

Nrow_a = aRange.Rows.Count
Nrow_b = bRange.Rows.Count
Ncol_a = aRange.Columns.Count
Ncol_b = bRange.Columns.Count

If Not (Nrow_a = Nrow_b And Ncol_a = Ncol_b) Then
Call MsgBox("The two input ranges should have the same number of
rows
and columns.", _
vbCritical, "ERROR")
End If

AAAA = 0
For R = 1 To Nrow_a
For C = 1 To Ncol_a
AAAA = AAAA + aRange.Cells(R, C) * bRange.Cells(R, C)
Next C
Next R

End Function
'=================== VBA end ===================









  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Error messages from user defined Excel worksheet function

Guess it doesn't show in the communities interface (or I don't know where to
look).

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
If the range argument is improper, I would think #Ref, but I guess that is

up
to you.

I don't see any .jpg file.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

#Ref or #VALUE! ?
Excel uses #VALUE! as shown in the attached JPG file.
--- and it is errors equivalent to the one shown in the JPG file that I
would like to trap. (The two ranges are not of the same size.)
Regards,
Helge
_______________________


"Tom Ogilvy" wrote in message
...
I would disagree that showing a message box in a function that will be

used
in a worksheet is desirable. It certainly doesn't conform to the way
other
functions work. When an error does occur, should the msgbox pop up
everytime
there is a recalc? also, even though you show the messagebox, your

code
go
aheads and raises another error by trying to execute with the flawed
ranges.

Since the error is a reference, I would show a #Ref and get out.

--
Regards,
Tom Ogilvy


"Helge V. Larsen" wrote:

I am developing a VBA function to be used as an Excel worksheet

function.
Some error checking is included in the VBA. My problem is that Excel
executes the function before all input arguments have been specified.
This
gives rise to some very disturbing messages from my error checking.

How can I prevent Excel from running the function during the
specification
of input ?

I have attached a screen dump with a small example that illustrates

the
problem. The VBA is below. The screen dump shows the situation when I
have
entered ARange and is going on to enter BRange. I click C3 and

(while
holding down the Shift key) I try to click D4. But inbetween Excel

runs
the
function and recognizes that BRange is too small, i.e. BRange is C3.

Any ideas ? I could suggest one myself : Remove all calls to MsgBox,

and
assign an error value #VALUE! to the function. Any better ideas ?

Please also answer to (AndThis).

Kind regards,
Helge

'=================== VBA start ===================
Option Explicit
Option Base 1

Function AAAA(ByVal aRange As Range, _
ByVal bRange As Range) As Variant

Dim Nrow_a As Integer, Nrow_b As Integer, Ncol_a As Integer,

Ncol_b
As
Integer
Dim R As Integer, C As Integer

Nrow_a = aRange.Rows.Count
Nrow_b = bRange.Rows.Count
Ncol_a = aRange.Columns.Count
Ncol_b = bRange.Columns.Count

If Not (Nrow_a = Nrow_b And Ncol_a = Ncol_b) Then
Call MsgBox("The two input ranges should have the same number

of
rows
and columns.", _
vbCritical, "ERROR")
End If

AAAA = 0
For R = 1 To Nrow_a
For C = 1 To Ncol_a
AAAA = AAAA + aRange.Cells(R, C) * bRange.Cells(R, C)
Next C
Next R

End Function
'=================== VBA end ===================









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
User defined function returns imprecise value when used in worksheet JohnM[_3_] Excel Discussion (Misc queries) 3 December 1st 09 12:52 PM
User Defined Function use any worksheet Babylynn Excel Discussion (Misc queries) 2 April 1st 09 06:23 PM
#Name? Error in User Defined VB Function idgity Excel Worksheet Functions 2 August 30th 05 08:58 PM
User-defined data type; Error: Only User-defined types... tiger_PRM Excel Programming 1 July 18th 04 03:32 PM
Automatic calculation of user-defined worksheet function Bart Deschoolmeester Excel Programming 2 October 8th 03 04:37 PM


All times are GMT +1. The time now is 08:16 PM.

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"