Trying to check if defined name exists and then delete it but I get run time error (using answer previously posted to this forum)

bdautrich

New Member
Joined
Sep 22, 2022
Messages
19
Office Version
  1. 2013
Platform
  1. Windows
DomInicB posted

Good morning kenreavy

This little routine will return a msgbox if the named range exists and will do nothing if it doesn't. Any good?

Sub Test()
On Error GoTo Last
If Len(ThisWorkbook.Names("MyRange").Name) <> 0 Then MsgBox "Range Exists"
Last:
End Sub

HTH

As an answer to the question posted a while back.



I thought this answer would apply to my situation. I am trying to check is a defined name exists and then delete it if it does. The spreadsheet has a defined name of Strains. Here is the code below .

If Len(ThisWorkbook.Names("Strains").Name) <> 0 Then Names("Strains").Delete

I get a run time error 1004 Application defined or object defined error. I tried changing ThisWorkbook to ActiveWorkbook but got the same error.
 

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.
Well declared victory too early. I re-entered the code send by Mark858. The Strains sheets is there and the defined names of Strains is on that sheet from the output of the code Mark858 provided. Here is the output.

Strains=Strains!$A$1:$A$151
7​

Here is the code that is generating the run time error. The error is still run time error 1004. Application defined or object defined error.

' Delete defined names so we do not get duplicates

MsgBox ("ThisWorkbook.Names(Strains!Strains).Name = " & ThisWorkbook.Names("Strains!Strains").Name)
MsgBox ("Len(ThisWorkbook.Names(Strains!Strains).Name) = " & Len(ThisWorkbook.Names("Strains!Strains").Name))

If Len(ThisWorkbook.Names("Strains!Strains").Name) <> 0 Then Names("Strains!Strains").Delete

What the application is doing is copying worksheets from a previous version of the application I am maintaining. These worksheets have defined names on them so if I just copy the sheets I get duplicates and I want to used the defined names from the sheets I copied.

Thanks for any help you can provide

Bruce
 
Upvote 0
I
Well declared victory too early. I re-entered the code send by Mark858. The Strains sheets is there and the defined names of Strains is on that sheet from the output of the code Mark858 provided. Here is the output.

Strains=Strains!$A$1:$A$151
7​

Here is the code that is generating the run time error. The error is still run time error 1004. Application defined or object defined error.

' Delete defined names so we do not get duplicates

MsgBox ("ThisWorkbook.Names(Strains!Strains).Name = " & ThisWorkbook.Names("Strains!Strains").Name)
MsgBox ("Len(ThisWorkbook.Names(Strains!Strains).Name) = " & Len(ThisWorkbook.Names("Strains!Strains").Name))

If Len(ThisWorkbook.Names("Strains!Strains").Name) <> 0 Then Names("Strains!Strains").Delete

What the application is doing is copying worksheets from a previous version of the application I am maintaining. These worksheets have defined names on them so if I just copy the sheets I get duplicates and I want to used the defined names from the sheets I copied.

Thanks for any help you can provide

Bruce
I found away around problem. Took out check to see if defined name exists and it works
 
Upvote 0

Forum statistics

Threads
1,216,523
Messages
6,131,151
Members
449,626
Latest member
Stormythebandit

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