Step into udf

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,907
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is my udf:

Code:
Public Function MyFunction(ByRef a As Integer, ByRef b As Integer, ByRef c As Integer) As Integer

    MyFunction = a + b + c

End Function

In Excel, if I typed:

Code:
=MyFunction(1,2,3)

it returns 6, as expected.

If I put a break point in the function, it hits it.


However, if I typed:

Code:
=MyFunction(1,2,apples)

it DOES NOT hit the break point.

Is that normal behaviour?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Yes, it is.
If you change something on a worksheet and put a break point at the start of:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

it will be hit, so with a udf, I expected the same behaviour, regardless of what valid or invalid parameters I pass to it.
 
Upvote 0
That's not a like for like comparison at all. If you called the UDF from a sub and passed invalid parameters, the type mismatch would occur in the calling sub, not in the UDF. That's what is happening in your current scenario; the error is raised in the Excel UI, not in the code.
 
Upvote 0
Hi tiredofit.

I'm not sure here but

It seems that you qualify your udf with integers...when it sees "apples" , a non integer, it does not satisfy the inputs. Should produce an error

plettieri
 
Upvote 0
That's not a like for like comparison at all. If you called the UDF from a sub and passed invalid parameters, the type mismatch would occur in the calling sub, not in the UDF. That's what is happening in your current scenario; the error is raised in the Excel UI, not in the code.
I see, thanks for the explanation.
 
Upvote 0
Hi tiredofit.

I'm not sure here but

It seems that you qualify your udf with integers...when it sees "apples" , a non integer, it does not satisfy the inputs. Should produce an error

plettieri
Yes, I purposely entered a string, thinking it'll still step into the code but it didn't.
 
Upvote 0
If that's really your scenario you would have to program the UDF for that contingency.
VBA Code:
Public Function MyFunction(a, b, c)
Dim ctr As Long
If IsNumeric(a) Then ctr = ctr + 1
If IsNumeric(b) Then ctr = ctr + 1
If IsNumeric(c) Then ctr = ctr + 1
If ctr = 3 Then MyFunction = a + b + c Else MyFunction = "Invalid Input"
End Function

Book1
A
16
Sheet1
Cell Formulas
RangeFormula
A1A1=MyFunction(1,2,3)


Book1
A
1Invalid Input
Sheet1
Cell Formulas
RangeFormula
A1A1=MyFunction(1,2,apples)
 
Upvote 0
If that's really your scenario you would have to program the UDF for that contingency.
VBA Code:
Public Function MyFunction(a, b, c)
Dim ctr
If IsNumeric(a) Then ctr = ctr + 1
If IsNumeric(b) Then ctr = ctr + 1
If IsNumeric(c) Then ctr = ctr + 1
If ctr = 3 Then MyFunction = a + b + c Else MyFunction = "Invalid Input"
End Function

Book1
A
16
Sheet1
Cell Formulas
RangeFormula
A1A1=MyFunction(1,2,3)


Book1
A
1Invalid Input
Sheet1
Cell Formulas
RangeFormula
A1A1=MyFunction(1,2,apples)
Thanks, I was only testing when I came across this.

Won't need to test for non-integer inputs.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top