Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Copying Worksheet Code Automatically.
by Allen Wyatt
(last updated April 22, 2019)
Tim correctly notes that a user can right-click on a worksheet tab, then select View Code to open a VBA code sheet for the worksheet. He has code written that automatically manipulates cells, columns, and rows. This code needs to be available on every worksheet in a workbook, even if the user adds new worksheets. Tim wonders if there is a way, using VBA, to have the code of one worksheet automatically copied to a new worksheet in the workbook.
There are a few ways you can approach this problem. One way—and perhaps the simplest way—is to remove the macros from the worksheet's code sheet and move them to the ThisWorkbook module. The worksheet's code sheet is what you see when you right-click a worksheet tab. Code in that sheet intended to handle events that occur in the worksheet and only in that worksheet. If you move the code to the ThisWorkbook module, then events can still be handled, but those events apply to all worksheets in the workbook.
For instance, when you right-click on a worksheet tab and look at the code window, you are initially working in the Worksheet_SelectionChange event. If you wanted to move this code to the ThisWorkbook module, you could place it within the Workbook_SheetChange event.
If such a "level change" of your code won't work for some reason, then another approach is to create a template worksheet within the workbook. Give it a name such as "MyMaster," and make sure it includes all the code that you want to add to your newly created worksheets. You can even hide this worksheet, if desired, so it doesn't distract the users. Then, place the following macro into the ThisWorkbook module:
Private Sub Workbook_NewSheet(ByVal Sh As Object) Dim tmpName As String tmpName = Sh.Name Sheets("MyMaster").Copy Before:=Sheets(Sh.Name) Application.DisplayAlerts = False Sheets(Sh.Name).Delete Application.DisplayAlerts = True Sheets("MyMaster (2)").Name = tmpName End Sub
This code is triggered every time a new worksheet is added to the workbook. It looks at the name of the newly added worksheet (which will be something like "Sheet4") and saves that name in a temporary variable. The code then copies the MyMaster worksheet to the workbook (which also copies the macros in the worksheet), deletes the worksheet that was originally created, and then renames the new MyMaster copy to have the same name as the original worksheet.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7880) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Copying Worksheet Code Automatically.
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!
Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.Discover More
Macros are often used to process the data stored in a worksheet. Some of these processing needs can be pretty specific to ...Discover More
If your macro closes workbooks, you'll want to make sure that it will save any changes you made to the workbook. Here's ...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.