Check if named range exists!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

trying to find a solution to the following small problem. I need to check to
see if a named range exists, if it does I then need to delete it, if it
doesnt I need to exit sub
The following code is what I have, which works if the Named range exists,
however causes a break and run time error 1004 if the named range does not
exist.

If ActiveWorkbook.Names("Renewal_Report") Is Nothing Then
Exit Sub
Else
ActiveWorkbook.Names("Renewal_Report").Delete
Selection.AutoFilter

End If

Have I used the correct syntax?
 
how about something like this

Sub test()
Dim nm As Name

For Each nm In ThisWorkbook.Names
If nm.Name = "Renewal_Report" Then
nm.Delete
Else
Exit Sub
End If
Next
End Sub
 
dim myName as Name
set myName = nothing
on error resume next
set myName = ActiveWorkbook.Names("Renewal_Report")
on error goto 0

if myname is nothing then
exit sub
end if

myname.delete

'etc
 

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

Back
Top