Delete Name Range giving a 400 error

Sparkle99

Board Regular
Joined
May 22, 2009
Messages
119
I have a load of code that creates two worksheets, then code to Move the sheets to a new workbook, rename the workbook, save and close. All fine.
But, I need to remove the Names from the sheets.
This is my code block - the ".Move" line is fine. (I tried .COPY instead of .MOVE, same result)
After 1 iteration through the For loop, I get a 400 error.
No names have been removed.

Dim NameRange As Name
Sheets(Array(Sht01, Sht02)).Move

For Each NameRange In Application.ActiveWorkbook.Names
NameRange.Delete
Next

Any ideas what the problem might be?
None of the Names are special - not system names. Just PrintRange and then some Names i defined for coding.
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this way

VBA Code:
Sub DelTables()
Dim Ws As Worksheet
Dim obje As ListObject
    For Each Ws In ActiveWorkbook.Worksheets
        For Each obje In Ws.ListObjects
            obje.Delete
        Next obje
    Next Ws
End Sub
 
Upvote 0
Solution
Try this way

VBA Code:
Sub DelTables()
Dim Ws As Worksheet
Dim obje As ListObject
    For Each Ws In ActiveWorkbook.Worksheets
        For Each obje In Ws.ListObjects
            obje.Delete
        Next obje
    Next Ws
End Sub

Thanks for the help. I've stolen your code for future, but it turns out the issue was with a graphic that was a link to an external file causing the problem.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

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