Why is this UDF getting a #VALUE error?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a simple UDF to access a range of cells in the calling sheet. Here's the code:

VBA Code:
Public Function Test(pWts As Range) As Long

MsgBox "Hi"

Dim NumWts
NumWts = Range(pWts).rows.Count   'This statrement gets the #VALUE error

Test = 0

End Function

I've used that exact statement in other code and it works. What am I doing wrong here?

It is called from a sheet that passesd it a range of 4 cells. I'm including a screenshot. I'm not using xl2bb because the UDF is in an add-in and I didn't know what xl2bb would do with that. I can frepost using xl2bb if that will work.

1632470566550.png
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
It should just be:

Code:
pWts.rows.Count

since pWts is a Range object, not the name/address of a range.
 
Upvote 0
Solution
It should just be:

Code:
pWts.rows.Count

since pWts is a Range object, not the name/address of a range.

Aha! :eek:(y)(y) (Where is that banging head on wall emoji?)

I checked the other code and, of course, in that I am passing the3 name of a range, not the range itself.

Thank you.

Y'now, I think if the AAs at m$ft tried a little harder, they could make the error messages even more obtuse and less helpful. Hey, I have an idea, why not replace all of the error codes with just one "#ERROR"? Or maybe, "#WKBWWT"?
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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