by Allen Wyatt
(last updated December 27, 2017)
Gene asked for a way to delete macros from within a macro. He has a large worksheet that uses macros to put together a purchase order. As the last step, the macro deletes the original data that is no longer necessary for the finished purchase order. This pared-down workbook is then saved under a new name. Gene needs the macro, just prior to saving, to delete a macro that is stored in SelectionChange event handler.
There are a couple of ways that this could be handled. One way is to avoid having to do the deletion all together. Instead, have your macro create a new workbook and then transfer, to that workbook, a copy of the data you need. You could then slice and dice the data in the new workbook and save it as your purchase order. The macros in the existing workbook are never copied to the new workbook during the process, so you don't need to worry about deleting them.
Copying all the worksheets in the current workbook to a new workbook is very easy to do. The following macro shows how it is done:
Sub CopyThisWorkbook() Dim CopiedWB As String CopiedWB = "Copy of " & ActiveWorkbook.Name Sheets.Copy ActiveWorkbook.SaveAs Filename:=CopiedWB, _ FileFormat:=xlNormal End Sub
The Copy method, when applied to the Sheets collection, copies all the worksheets in the active workbook to a new workbook and makes the new workbook active. The final command saves the new workbook under a new name.
Of course, if you need some of your macros to be in the new workbook, but not all of them (such as the SelectionChange event handler), then you are probably best to delete what you don't need and simply save under a new name. The following example macro shows how to delete the SelectionChange event handler from the worksheet code for Sheet1.
Sub DeleteProcedure() Dim VBCodeMod As CodeModule Dim StartLine As Long Dim HowManyLines As Long Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule With VBCodeMod StartLine = .ProcStartLine("Worksheet_SelectionChange", _ vbext_pk_Proc) HowManyLines = .ProcCountLines("Worksheet_SelectionChange", _ vbext_pk_Proc) .DeleteLines StartLine, HowManyLines End With End Sub
After the macro is completed, the workbook could be saved, and the desired macro won't be in the saved file. This macro is adapted from information provided at Chip Pearson's Web site, which you should reference if you need additional information on this technique:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3073) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...Discover More
When processing information in a macro, you often need to select different cells relative to the currently selected ...Discover More
Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.