Can a UDF access named ranges

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
Is there a way a UDF can access cells in a named range in the row just above the row from where it was called?

In this example, The "Parm1" is assigned "$D:$D" and "Parm2" is assigned "$E:$E". I would like a UDF that would add up the values in Parm1 and Parm2 in the row one up from the calling row.

1604823376037.png


For example, if called from C8 (as shown in F8), it would add D7 + E7 = 5 + 9 and return 14.

My actual UDF is much more complicated. But if I can get this simple one to work, I can handle the rest.

I already have it working passing the exact cells as parameters as in G8, but the actual UDF takes several more parameters and I want to minimize the chances for typos.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Probably a much shorter way of doing it but as a quick thought maybe...

VBA Code:
Function Jennifer(Parm1 As Range, Parm2 As Range, xrow As Long)
Jennifer = Application.Sum(Intersect(Parm1, Rows(ActiveCell.Offset(xrow).Row)), Intersect(Parm2, Rows(ActiveCell.Offset(xrow).Row)))
End Function

Book2
ABC
1ResultParm1Parm 2
2181
319123
4151114
515616
Sheet1
Cell Formulas
RangeFormula
A3:A4A3=Jennifer(Parm1,Parm2,-1)
A5A5=Jennifer(Parm1,Parm2,-2)
Named Ranges
NameRefers ToCells
Parm1=Sheet1!$B:$BA3:A5
Parm2=Sheet1!$C:$CA3:A5


or

Code:
Function Jennifer()
Jennifer = Application.Sum(Intersect(Range("Parm1"), Rows(ActiveCell.Offset(-1).Row)), Intersect(Range("Parm2"), Rows(ActiveCell.Offset(-1).Row)))
End Function

Book2
ABC
1ResultParm1Parm 2
2181
319123
4151114
525616
Sheet1
Cell Formulas
RangeFormula
A3:A5A3=Jennifer()
Named Ranges
NameRefers ToCells
Parm1=Sheet1!$B:$BA3:A5
Parm2=Sheet1!$C:$CA3:A5
 
Last edited:
Upvote 0
Probably a much shorter way of doing it but as a quick thought maybe...

VBA Code:
Function Jennifer(Parm1 As Range, Parm2 As Range, xrow As Long)
Jennifer = Application.Sum(Intersect(Parm1, Rows(ActiveCell.Offset(xrow).Row)), Intersect(Parm2, Rows(ActiveCell.Offset(xrow).Row)))
End Function
That's great, but I'd like to break it up. I tried this, but it gets a value error.

VBA Code:
Function Jennifer(pParm1 As Range, pParm2 As Range)

Dim SrcRow As Long
SrcRow = ActiveCell.Row - 1

Dim Parm1 As Range
Parm1 = Intersect(Parm1, Rows(ActiveCell.SrcRow.Row))

Dim Parm2 As Range
Parm2 = Intersect(Parm2, Rows(ActiveCell.SrcRow.Row))

Jennifer = Parm1 + Parm2

End Function
 
Upvote 0
VBA Code:
Function Jennifer(pParm1 As Range, pParm2 As Range)

Dim SrcRow As Long, Parm1 As Double, Parm2 As Double
SrcRow = ActiveCell.Row - 1


Parm1 = Intersect(pParm1, Rows(SrcRow))


Parm2 = Intersect(pParm2, Rows(SrcRow))


Jennifer = Parm1 + Parm2

End Function

Book2
ABC
1ResultParm1Parm 2
2181
319123
4151114
525616
Sheet1
Cell Formulas
RangeFormula
A3:A5A3=Jennifer(pParm1,pParm2)
Named Ranges
NameRefers ToCells
pParm1=Sheet1!$B:$BA3:A5
pParm2=Sheet1!$C:$CA3:A5
 
Upvote 0
VBA Code:
Function Jennifer(pParm1 As Range, pParm2 As Range)

Dim SrcRow As Long, Parm1 As Double, Parm2 As Double
SrcRow = ActiveCell.Row - 1

Parm1 = Intersect(pParm1, Rows(SrcRow))

Parm2 = Intersect(pParm2, Rows(SrcRow))

Jennifer = Parm1 + Parm2

End Function

That works.

1604843198123.png


But there are some strange behaviors.

If I change one of the values (D8), they all change to the same result:

1604843295148.png


If I recalculate the sheet (ACS+F9), they all change to zero:

1604844142016.png


And if I delete a column, such as H, they all turn to value errors:

1604843493959.png


???
 
Upvote 0
VBA Code:
Function Jennifer(Parm1 As Range, Parm2 As Range)
    Dim rw As Long
    rw = Application.Caller.Row - 1
    Jennifer = Parm1.Cells(rw) + Parm2.Cells(rw)
End Function
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
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