JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I just updated my little UDF that returns the assigned name of a range. I added an optional second parameter to return the name of the entire row or column of the target cell.
I would appreciate any comments or suggestions.
Thanks
I would appreciate any comments or suggestions.
Code:
Function GetRangeName(pRange As Range, Optional pRowCol As String = "") As String
Application.Volatile 'Needed so it will update with any changes
Dim pieces() As String 'Array to hold the pieces of the range after splitting
Dim addr As String 'Physical address
On Error Resume Next 'Ignore errors
GetRangeName = "" 'Return null if no name is assigned to the range
Select Case UCase(pRowCol)
Case "ROW" 'If they want the entire row,
GetRangeName = Rows(pRange.Row).Name.Name 'Get that name
addr = pRange.Row & ":" & pRange.Row 'Generate physcial address
Case "COLUMN" 'If they want the entire column,
GetRangeName = Columns(pRange.Column).Name.Name 'Get that name
addr = CvtColNum2Ltr(pRange.Column)
addr = addr & ":" & addr 'Generate physcial address
Case Else 'Otherwise
GetRangeName = pRange.Name.Name 'Get the name of the range
addr = pRange.Address
End Select
If GetRangeName = "" Then 'If range is not named.,
GetRangeName = "*** None ***" 'Return "None"
Exit Function
End If
pieces = Split(GetRangeName, "!") 'Split off the sheet name
If UBound(pieces) = 1 Then GetRangeName = pieces(1) Else GetRangeName = pieces(0)
GetRangeName = GetRangeName & "(" & addr & ")"
End Function
Thanks