Passing named range to UDF

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
Finding that M$FT in its infinite wisdom chose not to provide a built-in IsDate function to go along with all of the other IsXxx functions, I decided to write my own. Here it is:

Code:
Function MyIsDate(ByVal cell As Variant) As Boolean
MyIsDate = IsDate(cell)
End Function

It works fine if I pass it a cell address or the scalar name of a cell, but not if I pass it a named range of the column the cell is in.

R/CCDE
411/12/19TRUED4: =myisdate(C4)
511/12/19TRUED5: =myisdate(temp)
611/12/19FALSED6: =myisdate(DateUseBy)
711/12/19FALSED7: =myisdate(+DateUseBy)
811/12/19FALSED8: =myisdate(0+DateUseBy)

<tbody>
</tbody>

Column C is named "DateUseBy".
Cell C5 is named "temp".

If I put stop on the UDF, the parameter "cell" shows up as "empty" in the last three calls (D6-D8).

What do I have to do to get Excel to pass the cell contents to the UDF for these calls?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
What is the definition of the name DateUseBy?

Also you might add this to the start of the UDF

Code:
If Typename(cell) = "Range" then Set cell = cell.Cells(1,1)
 
Last edited:
Upvote 0
They didn't because they didn't need to.

Code:
Function JMIsDate(ByVal cell As Range) As Boolean
  JMIsDate = VarType(cell.Value) = vbDate
End Function
 
Upvote 0
They didn't because they didn't need to.
If you mean they didn't need to because it can be done in VBA, then using that logic, there would be no reason for any built in functions. No?

Code:
Function JMIsDate(ByVal cell As Range) As Boolean
  JMIsDate = VarType(cell.Value) = vbDate
End Function

Using that code, I get the same result as with Mike addition: TRUE, TRUE, FALSE, #VALUE , #VALUE .
 
Upvote 0
If you pass a range of cells say C2:C10, what are you expecting as a result?
From your OP it looks like you are trying to check each cell individually, so why are you trying to pass a range of cells instead of just one?
 
Upvote 0
If you pass a range of cells say C2:C10, what are you expecting as a result?
From your OP it looks like you are trying to check each cell individually, so why are you trying to pass a range of cells instead of just one?

Yes, I am trying to check each cell individually. I do this all the time so that I can use a name, rather than an address. Most built-in function that take a cell address will take the one on the same row or column. If they balk, I just add the unary plus (+) and then it works. UDF parameters seem to be different.
 
Upvote 0
Here's an example using named columns to make formulas easier to read and less error prone.

R/CCDEFGHIJ
4PriceS&HTaxCostFormulaDateDoWFormula
5$27.00$1.25$0.45$28.70F5: =C5+D5+E52/04/19MonI5: =TEXT(H6,"ddd")
6$27.00$1.25$0.45$28.70F6: =Price+SandH+Tax2/04/19MonI6: =TEXT(Date,"ddd")
7$49.95$0.00$0.00$49.95F7: =C7+D7+E72/07/19ThuI7: =TEXT(H8,"ddd")
8$49.95$0.00$0.00$49.95F8: =Price+SandH+Tax2/07/19ThuI8: =TEXT(Date,"ddd")
9$2.50$2.50$0.98$5.98F9: =C9+D9+E92/12/19TueI9: =TEXT(H10,"ddd")
10$2.50$2.50$0.98$5.98F10: =Price+SandH+Tax2/12/19TueI10: =TEXT(Date,"ddd")

<tbody>
</tbody>

The columns have the same names as the column headers (Col C is named "Price", Col D is named "SandH", etc.).

Each pair of rows has identical data. The first one uses cell addresses. The second one uses the named ranges.

I find rows 6, 8, and 10 much easier to read and edit. I'd like to use this same mechanism for calls to UDFs.
 
Upvote 0
I think implied intersections are something you can't get at from UDFs -- like 3D ranges, like Excel only evaluating the target of the TRUE conditional of an IF formula.

For the construct you have, and alternative is named columns -- e.g., SandH as =RC4 ($D6 if defined from anywhere in row 6).
 
Upvote 0
I think implied intersections are something you can't get at from UDFs -- like 3D ranges, like Excel only evaluating the target of the TRUE conditional of an IF formula.
Is that what the formulas in Cols F & H are doing, implied intersections?

I guess I could do an explicit intersection, but then the formula gets complicated again.

I could swear that I found a way to do this some time ago, but I cannot find it now. Grrr...

For the construct you have, and alternative is named columns -- e.g., SandH as =RC4 ($D6 if defined from anywhere in row 6).
I don't understand what this means.
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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