JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,535
- Office Version
- 365
- Platform
- Windows
I've run into a little speed bump in some code that is trying to access a named range of data using the name of that range, rather than the range itself. I know some will argue against doing that, but let's leave that aside for now.
To illustrate the problem, I wrote the two little test UDFs below. They both return the address of the target range. The first one, RangeTest1, gets the range passed as a range parameter. It works perfectly.
The second, RangeTest2, gets the name of the range passed as a string parameter. It also works, but with one little glitch. If the range is absolute ($C$R), it returns that address. But if either part of the range address is relative (C$R, $CR, or CR), it returns the address as if it were called from A1.
Here's the code:
And here's a sample of it in action:
<tbody>
</tbody>
The range names and definitions are shown in Col H. rngWeights is absolute ($D$6:$E$6). rngRatings is half relative ($Dn:$En) so the row numbers adjust to match the row number of the calling cell.
The results of the UDF calls are in Col J and the expressions for those calls are in Col K.
In J6 & J7, RangeTest1 returns the correct address for rngWeights. Similarly, in J10 & J11, RangeTest2 also returns the correct address for rngWeights.
In J8 & J9, RangeTest1 returns the correct address for rngRatings adjusting the row numbers to match the row numbers of the calling cells. In J12 & J13, however, RangeTest2 returns the address for rngRatings as if it had been called from A1.
Is there a way that I can get RangeTest2 to return the correct relative address?
Now I know I can get the address of the calling cell and replace the row number, but I'd like a command that does that itself.
Thanks
To illustrate the problem, I wrote the two little test UDFs below. They both return the address of the target range. The first one, RangeTest1, gets the range passed as a range parameter. It works perfectly.
The second, RangeTest2, gets the name of the range passed as a string parameter. It also works, but with one little glitch. If the range is absolute ($C$R), it returns that address. But if either part of the range address is relative (C$R, $CR, or CR), it returns the address as if it were called from A1.
Here's the code:
Code:
'Return address of range, passed as range argument
Public Function RangeTest1(pRange As Range) As String
RangeTest1 = pRange.Address
End Function
'Return address of range, name passed as string argument
Public Function RangeTest2(pRangeName As String) As String
Dim rng As Range
On Error Resume Next
Set rng = Range(pRangeName)
On Error GoTo 0
If rng Is Nothing Then
RangeTest2 = "n/a"
End If
RangeTest2 = rng.Address
End Function
R/C | C | D | E | F | G | H | J | K |
5 | Features | F1 | F2 | F3 | F4 | Ranges | UDF Results | Formulas |
6 | Weights | 1 | 3 | 5 | 7 | rngWeights = $D$6:$G$6 | $D$6:$G$6 | J6: =rangetest1(rngWeights) |
7 | Prod1 | 2 | A | 4.5 | Y | rngRatings = $D7:$G7 | $D$6:$G$6 | J7: =rangetest1(rngWeights) |
8 | Prod2 | 4 | B | 6.2 | Y | rngRatings = $D8:$G8 | $D$8:$G$8 | J8: =rangetest1(rngRatings) |
9 | Prod3 | 6 | C | 5.0 | N | rngRatings = $D9:$G9 | $D$9:$G$9 | J9: =rangetest1(rngRatings) |
10 | $D$6:$G$6 | J10: =rangetest2("rngWeights") | ||||||
11 | $D$6:$G$6 | J11: =rangetest2("rngWeights") | ||||||
12 | $D$1:$G$1 | J12: =rangetest2("rngRatings") | ||||||
13 | $D$1:$G$1 | J13: =rangetest2("rngRatings") |
<tbody>
</tbody>
The range names and definitions are shown in Col H. rngWeights is absolute ($D$6:$E$6). rngRatings is half relative ($Dn:$En) so the row numbers adjust to match the row number of the calling cell.
The results of the UDF calls are in Col J and the expressions for those calls are in Col K.
In J6 & J7, RangeTest1 returns the correct address for rngWeights. Similarly, in J10 & J11, RangeTest2 also returns the correct address for rngWeights.
In J8 & J9, RangeTest1 returns the correct address for rngRatings adjusting the row numbers to match the row numbers of the calling cells. In J12 & J13, however, RangeTest2 returns the address for rngRatings as if it had been called from A1.
Is there a way that I can get RangeTest2 to return the correct relative address?
Now I know I can get the address of the calling cell and replace the row number, but I'd like a command that does that itself.
Thanks