How to check in VBA if a named range exists?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
A macro I am working on needs to access one or more auxilliary tables. The table names are formed by prefixing "tbl" to a data field. So if the data field contains "Body Type", the table name will be "tblBodyType".

I want to add code to check that (a) the table name exists and (b) it is a table.

I checked several "solutions", but they all seemed overly complicated. Probably the simplest is this function.

VBA Code:
Function NameExists(rName As String) As Boolean
Dim errTest As String
On Error Resume Next
errTest = ThisWorkbook.Names(rName).Value
NameExist = CBool(Err.Number = 0)
On Error GoTo 0
End Function

It probably works, but I didn't test it.

But isn't there a simpler way, something like this?

Code:
If range(rName).exists ...

There are dozens of properties accessible after the ".". Surely a way to test if it exists is one of them.

And then if it exists, I need to check that it is a table.

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Will this okay?
 
Upvote 0
Will this okay?
That is a bit simpler that the other function, but it is still a separate function. So I take it that VBA has no simple test for range names. (sigh)
 
Upvote 0
That is a bit simpler that the other function, but it is still a separate function. So I take it that VBA has no simple test for range names. (sigh)
The reason to create a function is when you need to use it multiple time. If you just need to use it one in your code flow then no need to create a function. That example is just a sample subroutine. You can use the code as part of your program,, right?

Example
VBA Code:
Sub Test()

Dim FoundRangeName As Boolean

'.... your program here
On Error Resume Next
FoundRangeName = Len(ThisWorkbook.Names("RangeName").Name) <> 0
On Error GoTo 0

If FoundRangeName Then
    MsgBox "Found Name"
    ' Run this
Else
    MsgBox "Name not found"
    ' Run this
End If

' your program here

End Sub
 
Upvote 0
The reason to create a function is when you need to use it multiple time.
The real reason to create a function is because of a fundamental and decades old deficiency in VBA. If VBA has a simple range(name).exists, a function would not be needed.

If you just need to use it one in your code flow then no need to create a function. That example is just a sample subroutine. You can use the code as part of your program,, right?
Right & right. The function is the way to go. Thanks.
 
Upvote 0
PS: As long as I am writing a function to check if the name exists, maybe I can include a test that the name is a table. Do you have a test for that?
 
Upvote 0
The Range has a Listobject property, so assign the name's referstorange to a variable (within your error handler). If that is not Nothing then test if its Listobject property is also Not Nothing. You could combine the two tests but I would keep them separate.
 
Upvote 0
You can do the if Range Names exists like this:
(does not rely on "On Error Resume Next")

VBA Code:
Sub RangeNameExists()
    Dim NameExist As Boolean
    Dim rngName

    rngName = "tblBodyType"
    NameExist = Evaluate("IsRef(" & rngName & ")")
    
    Debug.Print NameExist

End Sub
 
Upvote 0
You can use the same principle to check if the Name is a table.
If you don't care whether its a Named Range and only whether it is a table you only need the second part.

VBA Code:
Sub RangeNameExists_IsTable()

    Dim rngName
    rngName = "tblBodyType"
    
    Dim NameExist As Boolean
    NameExist = Evaluate("IsRef(" & rngName & ")")
        Debug.Print "Name is Range Name: " & NameExist
    
    Dim IsTable As Boolean
    IsTable = Not IsError(Evaluate("IsRef(" & rngName & "[#Headers])"))
        Debug.Print "Name is Table Name: " & IsTable

End Sub
 
Upvote 0
Solution
You can use the same principle to check if the Name is a table.
If you don't care whether its a Named Range and only whether it is a table you only need the second part.
That worked perfectly. Thank you.

Here's my final code.

VBA Code:
Function IsATable(pTblName As String, pMyName As String) As Boolean

If Not Evaluate("IsRef(" & pTblName & ")") Then
  Call ErrMsg("'" & pTblName & "' is not a valid range name", pMyName)
  IsATable = False: Exit Function: End If

If IsError(Evaluate("IsRef(" & pTblName & "[#Headers])")) Then
  Call ErrMsg("'" & pTblName & "' is not a valid table name", pMyName)
  IsATable = False: Exit Function: End If

IsATable = True
End Function

I'm marking this as the solution. It's more complete.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top