excel vba named range address code example

Example 1: excel vba how to get a range's full address including sheet name

'VBA function to retrieve the full address of a range:

Function FullAddress(r As Range)
    With r
        FullAddress = "'" & .Parent.Name & "'!" & .Address
    End With
End Function

'--------------------------------------------------------------------

MsgBox FullAddress		'Displays: 'My Sheet'!$A$1:$Z$99

Example 2: vba change named range address

Dim wb As Workbook
Dim nr As Name

Set wb = ActiveWorkbook
Set nr = wb.Names.Item("myRange")

' give an absolute reference:
nr.RefersTo = "=Sheet1!$C$1:$C$9"

' or, resize relative to old reference:
With nr
    .RefersTo = .RefersToRange.Resize(100, 1)
End With

Tags:

Vb Example