See if a Range Name exists?

P

plh

Dear Gurus,
I need to find a way to inquire whether or no a given range name exists, without
generating and error. Something like:

If (Sheets("MySheet").Names("MyRangeName")) EXISTS then
DoSomething
End if

I tried
If IsError(Sheets("MySheet").Names("MyRangeName").Delete) Then
DoSomething
End If

And many variations on that theme but this just generates the error as if I
said:
Sheets("MySheet").Names("MyRangeName").Delete
by itself when that name is not in existence.

The error that this does generate, "Application Defined" is so broad that
trapping it is not really an option, because it has such a large probability of
Beta Error, if you will.

So: is there some way to test and see if a given range name exists?
Thank You,
-plh
 
S

Steve Yandl

Function RngNameExists(strName As String) As Boolean
For Each strRngName In ThisWorkbook.Names
If strRngName.NameLocal = strName Then
RngNameExists = True
Exit Function
Else
RngNameExists = False
End If
Next strRngName
End Function


Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top