I expect it only works for statically-defined ranges -- not dynamic (or relative) ranges.
Yep, that's it. Here's my code:
Code:
Public Function RngName(rng As Range) As String
Dim rname As String
On Error Resume Next
rname = rng.Name.Name 'Works for absolute ranges
'rname = Range(rng).Name.Name 'Doesn't work for any ranges
On Error GoTo 0
RngName = "[" & rname & "]"
End Function
Here's a table from a test sheet. Three ranges are named as shown. Range1 & Range2 are absolute. RangeR has absolute columns, but relative rows. The code above returns the results in F9:F12 using the expressions shown in G9:G12.
[TABLE="class: grid, width: 720"]
<tbody>[TR]
[TD="align: center"]R/C[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]H[/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD="align: center"]a[/TD]
[TD="align: center"]b[/TD]
[TD="align: center"]c[/TD]
[TD]Range1 = "=Sheet1!$C$6:$E$6"[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD="align: center"]x[/TD]
[TD="align: center"]y[/TD]
[TD="align: center"]z[/TD]
[TD]Range2 = "=Sheet1!$C$7:$E$7"[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD]RangeN = "=Sheet1!$C7:$E7"[/TD]
[TD="align: center"]Formulas[/TD]
[TD="align: center"]Objective[/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD][Sheet1!Range1][/TD]
[TD]F9: =rngname(Range1)[/TD]
[TD][Range1][/TD]
[/TR]
[TR]
[TD="align: center"]10[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD][Sheet1!Range2][/TD]
[TD]F10: =rngname(Range2)[/TD]
[TD][Range1][/TD]
[/TR]
[TR]
[TD="align: center"]11[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD][][/TD]
[TD]F11: =rngname(RangeR)[/TD]
[TD][RangeR][/TD]
[/TR]
[TR]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD][][/TD]
[TD]F12: =rngname(RangeR)[/TD]
[TD][RangeR][/TD]
[/TR]
</tbody>[/TABLE]
Is there an expression that I can use in VBA that will return the values in H9:H12?
Sorry for the confusion earlier. I'm struggling to get my head around ranges and Excel's range notation.