How to Reuse a VBA Module on Multiple Excel Worksheets
Question details
The user wants to run the same VBA macro on different copied or renamed worksheets within an Excel workbook without encountering run-time errors.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Attempting to use an existing worksheet-specific VBA macro on newly copied or renamed sheets in the same workbook.
- Observed behavior
- The macro triggers a run-time error 1004 when executed on a copied or renamed worksheet because the VBA code contains hard-coded references to the original sheet's name.
Before modifying your VBA code, ensure you have saved a backup copy of your workbook with a .xlsm extension to prevent any accidental data loss during macro edits.
Move Code to a Standard Module and Update References
The most effective way to reuse VBA code across multiple sheets is to place it in a standard module and use dynamic sheet references.
When VBA code is tied to a specific worksheet module or uses hard-coded sheet names, copying the sheet will cause run-time error 1004. Moving the procedure to a standard module makes it globally accessible within the workbook.
Press Alt + F11 on your keyboard to open the Visual Basic Editor (VBE) in Excel.
Navigate to the top menu bar and select Insert, then click on Module to create a new standard module (e.g., Module1).
Locate your existing macro in the specific worksheet module, cut the code, and paste it into the newly created standard module.
Review the pasted code and replace any hard-coded sheet references, such as Sheets("T&M"), with ActiveSheet. This ensures the macro will execute on whichever worksheet is currently active.

Move Data to the Original Macro Sheet
If you prefer not to modify the existing VBA code, you can temporarily move your new data into the original sheet where the macro is configured to run.
Easily Manage and Reuse VBA Macros in WPS Spreadsheet
WPS Office provides seamless support for Excel macros and VBA. You can easily access the Visual Basic Editor, insert standard modules, and run macros across multiple sheets just as you would in Microsoft Excel.
- 1. Enable Developer Tools: Open your workbook in WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled in your ribbon.
- 2. Open Visual Basic Editor: Click the Developer tab on the top ribbon and select Visual Basic, or simply press Alt + F11.
- 3. Insert a Standard Module: Right-click your workbook project in the VBA editor pane, choose Insert, and click Module to paste your reusable macro code.
- 4. Run the Macro: Switch to any worksheet, click the Macros button in the Developer tab, and execute your globally available macro.

Frequently Asked Questions
Why do I get Run-time error 1004 when renaming a copied worksheet with a macro?
This error occurs because your VBA code contains a hard-coded reference to the original sheet's name. When the sheet is renamed, the macro cannot locate the original name. Changing the specific name reference to 'ActiveSheet' resolves this issue.
Can I copy a worksheet module directly to another sheet?
Yes, you can copy the code from one worksheet module and paste it into another worksheet's module. However, if the code is identical and needed across multiple sheets, placing it once in a Standard Module is significantly more efficient and easier to maintain.
Do Worksheet event macros work if I move them to a Standard Module?
No. Event macros, such as Worksheet_SelectionChange, are triggered by specific user actions on a particular sheet. They must remain in the corresponding worksheet object module to function properly.




