I have this code below that deletes specific sheets on a workbook. I want this macro to be able to run whenever I close the document and save it with these sheets deleted.
Formula:Please Login or Register to view this content.
I have this code below that deletes specific sheets on a workbook. I want this macro to be able to run whenever I close the document and save it with these sheets deleted.
Formula:Please Login or Register to view this content.
Try placing the code in the Before Save Event for the Workbook
Alan עַם יִשְׂרָאֵל חַי
Change an Ugly Report with Power Query
Database Normalization
Complete Guide to Power Query
Man's Mind Stretched to New Dimensions Never Returns to Its Original Form
Does this work?
Formula:Please Login or Register to view this content.
What happens when you close the spreadsheet? I think you have to tell us if it works for you. I would remove or comment out the line
Please Login or Register to view this content.
I implemented the code and it didn't delete the sheets when I closed the document. Do I have to activate the macro before the close?
As Alan said you should comment out Sub Deletesheets. Or you could call it instead.
Please Login or Register to view this content.
Sorry forgot to mention I did comment out the Sub DeleteSheets
The macro should work if you comment out the second sub statement. For what it's worth I'd use something like this to account for if one of the sheets doesn't exist.
If what you are using doesn't work I'd suggest checking that you have placed it in the "ThisWorkbook" object and not a worksheet object or module in the vba project. (Ctrl+R to view project explorer and then under the project for the workbook you are using you need "microsoft excel objects" and then "thisworkbook").Please Login or Register to view this content.
Thanks yud. Are you not able to store this type of macro in a personal xls folder or does it have to be specific to your actual worksheet?
If you put in your personal workbook it would only run when you close the personal workbook.
By your question I assume you want it to apply to every workbook you open and not have to put it in the code of each workbook?
Maybe try having a look here: http://www.cpearson.com/excel/AppEvent.aspx
It only has to run for specific workbooks with the three sheet names in them. But I understand my mistake now and thanks for that link, very helpful.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks