Can't delete worksheet

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Am getting an error message when trying to delete a worksheet in VBA, wondering where I'm going wrong in the code and/or what might be preventing it from working.

here is my code

Code:
Workbooks("Data").Save
Workbooks("without Data").Sheets("Sheet1").Unprotect "Password"
Workbooks("without Data").Sheets("MainMenu").Activate
Application.DisplayAlerts = False
Workbooks("without Data").Sheets("Sheet1").Delete
Application.DisplayAlerts = True
Workbooks("Data").Sheets("Sheet1").Copy Before:=Workbooks("without Data")
Workbooks("Data").Close
 
Still can't get around this problem.

Have tried renaming the sheet as "sheet50" copying in the new sheet as "sheet1" then deleting "sheet50" but it still doesn't work.

It renames, and copies in, but doesn't delete.

Could it have anything to do with there being an activex control on the page?
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Solved it! :)

Had to activate the worksheet first, apparently it will only let me delete the worksheet if that's the one in view

Rich (BB code):
Workbooks("Data").Save
Workbooks("without Data").Sheets("Sheet1").Unprotect "Password"
Application.DisplayAlerts = False
Workbooks("without Data").Sheets("Sheet1").Activate
Workbooks("without Data").Sheets("Sheet1").Delete
Application.DisplayAlerts = True
Workbooks("Data").Sheets("Sheet1").Copy Before:=Workbooks("without Data")
Workbooks("Data").Close

No idea why that's the case, because it wasn't when I opened a new workbook and tried to delete a sheet in there (to check that it wasn't to do with the security settings) because that worked fine deleting sheet3 when sheet1 was the active sheet
 
Upvote 0
I don't really understand why you need to unprotect the sheet if it isn't protected at first?
It could be the reason though because it is somehow protected because I don't see anything wrong with your code.
Just my thoughts.
Thanks
 
Upvote 0
This is really strange.. It doesn't matter if your VB code is protected I used to do that in all my VB codes and its working fine even deleting a worksheet(s).
i know you cannot delete a worksheet only if there is only one (you cannot really delete sheet1) and if it is protected as VoG said.

Have you tried to rename it before deleting?
 
Upvote 0
Yes, I have tried renaming it.

I thought I fixed it yesterday, but that's wasn't the case, I only got it working in the immediate window.

I have now removed the display alerts lines and it brings up the prompt to delete and then deletes fine if you click yes.

So I'm assuming the problem is with the way my code currently handles the alert, any way around this?
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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