ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check if named range exists! (https://www.excelbanter.com/excel-programming/360143-check-if-named-range-exists.html)

steve_doc

Check if named range exists!
 
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?

Gary Keramidas

Check if named range exists!
 
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

--


Gary


"steve_doc" wrote in message
...
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?




Dave Peterson

Check if named range exists!
 
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


steve_doc wrote:

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?


--

Dave Peterson


All times are GMT +1. The time now is 11:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com