Consulting

Results 1 to 9 of 9

Thread: Solved: Check if Named Range exists

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    25
    Location

    Solved: Check if Named Range exists

    How would you check in VBA if the named range already defined on the sheet?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub TestRange()
    Dim t As Range, Txt As String
    Txt = "Test1"
    On Error Resume Next
    Set t = Range(Txt)
    If Err <> 0 Then
    MsgBox "There is no range called " & Txt
    Exit Sub
    Else
    Err.Clear
    Range(Txt).Select
    End If
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    You could also use a function (modifying MD's code a little bit)

    [VBA]Function TestRange(Txt As String) As String
    Dim t As Range
    On Error Resume Next
    Set t = Range(Txt)
    If Err <> 0 Then
    TestRange = "No Range"
    Set t = Nothing
    Exit Function
    Else
    Err.Clear
    TestRange = "Range Exists"
    End If

    Set t = Nothing
    End Function[/VBA]

    Say my range was "Joe" then in any cell I would write: =TestRange("Joe")

    Side note: it is not case-sensitive.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    VBAX Regular
    Joined
    Aug 2005
    Posts
    25
    Location
    Thank you, function is definetely what I need, I will try it.

  5. #5
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Don't forget to mark this thread as "Solved" - go to the "Thread Tools" option at the top of the screen.

  6. #6
    VBAX Regular
    Joined
    Aug 2005
    Posts
    25
    Location
    This is really bazaar. I am using your function above, just changed it to 1 for range exist and 0 to range does not. Works great from a cell, also works fine , when I call it via vba using a static range:

    If RangeExist("del_mcc_auth1") = 1 Then
    MsgBox "Range Exists: "
    Else
    MsgBox "Range Does not exist: "

    End If

  7. #7
    VBAX Regular
    Joined
    Aug 2005
    Posts
    25
    Location
    This is bazaar. I have tested your function above from a cell, works perfect. I only changed the result to integer, 0 for no range, 1 for range exists.

    If I use from VBA this way it works fine:

    [VBA]
    If RangeExist("server") = 1 Then
    MsgBox "Range Exists: "
    Else
    MsgBox "Range Does not exist: "

    End If
    [/VBA]
    where server is an existing range, it will return 1.

    if I use it with a variable:

    [VBA]
    If RangeExist("c") = 1 Then
    MsgBox "Range Exists: "
    Else
    MsgBox "Range Does not exist: "

    End If
    [/VBA]
    where c=server1, it returns 0.

    Can't figure out what is going on.

    Any ideas?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Because c is a variable, it doesn't go into quotes

    [VBA]
    If RangeExist(c) = 1 Then

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Innany,

    I would be careful with using "= 1" to be true. False only equals zero, and all other values are considered true. What I would do is:

    [VBA]If RangeExist("whatever") <> 0 Then
    MsgBox "Range Exists: "
    Else
    MsgBox "Range Does not exist: "

    End If[/VBA]
    Just incase the function returns something other than 0 and 1....that is of course unless you set the function to either 1 or 0 in your code....in that case, nevermind.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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