View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Andrew Bauer Andrew Bauer is offline
external usenet poster
 
Posts: 2
Default easy way to test if a Named Range exists

I get it: Suppress errors and try the name. Return True
if no error appeared. I guessed Microsoft forgot to
implement a test like NameExists. Thanks!

-----Original Message-----
Andrew,

Try a function like

Function Name(What As String, _
Optional WB As Workbook) As Boolean

Dim N As Long
On Error Resume Next
N = Len(IIf(WB Is Nothing, ThisWorkbook, WB).Names

(WhatName).Name)
NameExists = (Err.Number = 0)

End Function

Then, you can call this with code like
If NameExists("SomeName") = True Then
' name exists
Else
' name does not exist
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andrew Bauer" wrote in message
...
Is there an easier way to test if a Named Range exists
than this loop:

'a long-winded way to check if "ToolVersion" exists...
fnd = False
For Each x In ActiveWorkbook.Names
If x.Name = "ToolVersion" Then
fnd = True
Exit For
End If
Next x

I need something like this:
if ActiveWorkbook.Names.Item("x").Exists then ...



.