Deleting VBA Code in a Copied Worksheet

Written by Allen Wyatt (last updated August 1, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

Peter regularly needs to copy a master worksheet to a copy in a new workbook. He can do this just fine with VBA. However, the worksheet that he's copying has code associated with it, and the code gets copied too. Peter doesn't want the code in the copy, but he does want it left in the original. He wonders if there is a way, in his macro, to delete the worksheet code in the just-copied worksheet.

How you go about this depends on how you want the finished workbook (the one to which you are copying) to look. Primarily, will the finished workbook contain macros—any macros—or not? If the answer is no, it won't contain any macros, then you can easily accomplish the task by saving the new workbook in XLSX format. That way Excel takes care of getting rid of all the macros for you. You can do this by including a line in your code similar to the following when you save your workbook:

ActiveWorkbook.SaveAs FileName:="MyExcelFile.xlsx", _
  FileFormat:=xlOpenXMLWorkbook

If, however, you only want to get rid of the worksheet code but save the new workbook with any other macro code intact, then you'll need to take a different approach. In this instance, you could include the following line in your macro:

ActiveWorkbook.VBProject.VBComponents("Sheet1")

This assumes that the new workbook is the active workbook, and that the worksheet whose code you want to delete is named Sheet1. You could also use these lines to accomplish the same task:

wsName = ActiveSheet.CodeName
With ThisWorkbook.VBProject.VBComponents(wsName).CodeModule
    .DeleteLines 1, .CountOfLines
End With

Note that wsName should be declared as a string variable.

For a boatload of other ways you can affect different code modules programmatically, you can't go wrong by referring to Chip Pearson's excellent information here:

http://cpearson.com/excel/vbe.aspx

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7612) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Specifying Date Formats in Headers

Don't like the default date format used by Excel when you place the date in a header or footer? You can use a macro to ...

Discover More

Relative Worksheet References when Copying

Copy a formula from one place to another and Excel helpfully adjusts the cell references within the formula. That is, it ...

Discover More

Quickly Accessing the Column Tab

If you need to quickly display the Column tab of the Table Properties dialog box, here are some handy tricks you can use. ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Ctrl+Break Won't Work to Stop a Macro

When you need to stop a macro while it is running, you normally press Ctrl+Break. What are you to do if the keypress ...

Discover More

Calling a Macro from the Workbook_Open Event

You can run a Personal.xlsb macro from within your Workbook_Open code, but you may get an error if you don't make sure ...

Discover More

Getting a File Name

Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2020-08-01 07:20:29

Peter

Thanks for your help. It ran smoothly and did exactly what I wanted. The copy of the spreadsheet then saved as an .xlsx

The only significant change I made was to replace ThisWorkbook with ActiveWorkbook:
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
.DeleteLines 1, .CountOfLines
End With
Otherwise it would have deleted code in my source spreadsheet where the code was running.
I also found it caused an error if the code window that was to be cleared was open in the VBA editor.
Thanks again, Peter


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.