Skip to content

Instantly share code, notes, and snippets.

@hdevilbiss
Last active December 21, 2022 17:01
Show Gist options
  • Save hdevilbiss/91afd703d0618e6b01021c99cf7577cc to your computer and use it in GitHub Desktop.
Save hdevilbiss/91afd703d0618e6b01021c99cf7577cc to your computer and use it in GitHub Desktop.
Visual Basic functions to use with Named References (under Formulas > Name Manager)
' Some custom Visual Basic functions that I like to use
' User MikeD on https://stackoverflow.com/a/37611071/12621376
' Check whether a given string is a valid name in Activeworkbook.names
Function isNamedRange(ByVal rangeName As String)
Dim n As Name
isNamedRange = False
For Each n In ActiveWorkbook.Names
If n.Name = rangeName Then
isNamedRange = True
Exit For
End If
Next
End Function
' Get the location of a given named range (under Formula > Name Manager) or display an error
' Example: airSpeed = Range(GetRefersTo("airSpeed")).Value
Function GetRefersTo(ByVal namedRef As String) As String
If isNamedRange(namedRef) Then
GetRefersTo = ActiveWorkbook.Names(namedRef).RefersTo
Else
MsgBox "This named reference does not exist in this worksheet: " & vbNewLine & namedRef
Debug.Print "This named reference does not exist in this worksheet:" & vbNewLine & namedRef
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment