Last active
December 21, 2022 17:01
-
-
Save hdevilbiss/91afd703d0618e6b01021c99cf7577cc to your computer and use it in GitHub Desktop.
Visual Basic functions to use with Named References (under Formulas > Name Manager)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 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