# How to copy sheet code and macros from one workbook to another?



## fraz627 (Jan 6, 2023)

How would I go about coping the sheet code (just the code) and macros to another workbook?


----------



## Micron (Jan 6, 2023)

Wouldn't it be easier to just copy the workbook and remove all the data/formatting from the new file. Unless you find code that does what you're asking, it would take maybe 20 times longer to write it than doing that. Or you could open each sheet, copy & paste to new sheets one by one. AFAIK, those 'macros' would automatically be registered when you compile the project.


----------



## fraz627 (Jan 7, 2023)

what I would like to do is have the sheet code, and macros enhance an individual pre existing sheet.


----------



## Micron (Jan 7, 2023)

Assuming you know how to open the vb editor, I would just copy the code (which is the same thing as a macro in Excel as far as I'm concerned) and paste into the sheet module of the target workbook. You might have to worry about things like sheet names and ranges that might be hard coded.


----------



## fraz627 (Jan 7, 2023)

Thanks that will work for sheet code but I forgot to mention User forms using a macro


----------



## Micron (Sunday at 12:29 AM)

Then unless you have a copy of that user form with the same name in the new wb you don't need it? If you're saying you want to copy the form as well as its code then I don't know how that might be done in Excel. Easy to do in Access. Maybe you copy the whole workbook and edit/strip out the code/forms that you don't need in the copy?


----------



## Alex Blakenburg (Sunday at 7:25 AM)

For standard modules, class modules & forms, the quickest way is probably to have both workbooks open and in the VBA editor drag the module from the current workbook to the target workbook

Option 2 in the web page > How to Copy or Import VBA Code to Another Workbook - Excel Campus

For ThisWorkbook & Sheet modules you will need to copy paste the actual code (or export and import it but that seems a longer process)


----------



## Micron (Sunday at 11:41 AM)

If you want all of the code, that's faster than just copying the whole workbook? All the code would be in the copy, no?


----------



## Alex Blakenburg (Sunday at 8:50 PM)

Micron said:


> If you want all of the code, that's faster than just copying the whole workbook? All the code would be in the copy, no?


You are of course correct, all the code would be in the copy but the OP has indicated the time saving will most likely be lost due to having to recreate sheets that are to be retained in the target workbook.


----------

