Results 1 to 6 of 6

Variable as Name

Threaded View

  1. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Variable as Name

    Hi there,

    The function is used to determine whether or not a specifically-named workbook-level Name has been defined in a workbook.

    The following code shows an annotated English-language version of the routine, and also an alternative way of determining the result:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Function mbNameExists__Version_1(sNameToFind As String) As Boolean
    
        Dim bNameExists As Boolean
        Dim nam         As Name
    
    '   Assume that the Name does NOT exist
        bNameExists = False
    
    '   Scan through each workbook-level defined Name
        For Each nam In ThisWorkbook.Names
    
    '       Check whether or not the name of the current Name is the same as the name to find
            If nam.Name = sNameToFind Then
    
    '           The name IS the same, so flag this
                bNameExists = True
    
    '           The name has been located, so there's no need to check anything else
                Exit For
    
            End If
    
        Next nam
    
    '   Return the appropriate value to the calling routine
        mbNameExists__Version_1 = bNameExists
    
    End Function
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Function mbNameExists__Version_2(sNameToFind As String) As Boolean
    
        Dim nam As Name
    
    '   Temporarily disable error handling
        On Error Resume Next
    
    '       Initialise the variable
            Set nam = Nothing
    
    '       Assume that the Name DOES exist, and ignore any error if it does not
            Set nam = ThisWorkbook.Names(sNameToFind)
    
    '   Re-enable error handling
        On Error GoTo 0
    
    '   Return the appropriate value to the calling routine
        If Not nam Is Nothing Then
              mbNameExists__Version_2 = True
        Else: mbNameExists__Version_2 = False
        End If
    
    End Function

    Hope this helps.

    Regards,

    Greg M
    Last edited by Greg M; 09-06-2021 at 07:07 AM. Reason: Typo corrected

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Public variable and Runtime error 91: Object Variable or With block variable not set
    By Frank Nunez in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-31-2020, 04:02 PM
  2. [SOLVED] Object variable or With block variable not set when copying chart to outlook email body
    By jprlimey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-09-2019, 04:17 PM
  3. [SOLVED] Macro to select a variable cell range and copy it down a variable amount of times
    By JPoFresh in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-18-2019, 09:17 PM
  4. Replies: 3
    Last Post: 02-17-2015, 11:53 PM
  5. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  6. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  7. Replies: 3
    Last Post: 11-21-2012, 03:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1