Excel Copy a sheet with its code to a new xls file - excel

Please, could you tell me if the following is possible in VBA:
I need to write code that copies a sheet from my workbook to a new freshly created xls file. BUT, the sheet I wanna copy has a Macro behind (it's actually a WorkSheet_Change event handler) that I also want to copy to the new file.
Many thanks
Kind regards,
Miloud B

You can Export the code from the original and then Import it into the copy.
Here's an article that shows some code for doing this: Macro to Add a Macro to New Workbooks

Related

How to copy or duplicate a sheet from xlsm but also copying formatation, macros, etc.?

I need to create a new sheet in a xlsm file that's a copy from another sheet from that same xlsm.
That's kinda easy but the problem's that when I copy it, it won't copy the formatation and macros as well.
And they're the main reason I want to copy it.
Original Sheet
I've tryed the following:
import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
excel.Visible = True
book = excel.Workbooks.Open(r"C:\Users\lbsme\Desktop\reprogramacao\modelo.xlsm", UpdateLinks=0)
model = excel.Worksheets('model')
sheet1 = book.Worksheets.Add()
sheet1.Name = 'Copy'
sheet1 = book.Worksheets('Copy')
model.Range("A1:CF128").Copy()
sheet1.Paste(sheet1.Range('A1'))
And it works... kind of...
It does copy the formulas and part of the formatation but it does not copy the whole formatations or the buttons.
Copied
Also, while copying, it shows the same error messenge over and over again and I have no idea why since there's no other sheets besides the ones shown.
Even if I click "yes to all" it will show the messenge again and again.
I click it about eleven times.
Error
I need my copies to be exatly like the model, including the buttons.
How to copy or duplicate the whole sheet perfectly?
This simple code copies the whole sheet
Sheets("mysheet").Copy After:=Sheets("myothersheet")
Either way I guess you already know that you can just right-click on the sheet tab and then click on "Move or copy"
I don't see the point on copying the macros if it's in the same workbook....
If you want to copy paste the macros' code to modify one of them you could just copy-paste it or if it's really something that you do regularly enough and it's repetitive you could just use a python library to write macros or... I found this to duplicate vba code directly from excel:
https://support.google.com/docs/thread/3126412/how-can-i-create-a-script-or-macro-to-duplicate-and-rename-a-tab-for-each-day-in-a-month?hl=en
If this was helpful please set this as the proper answer! :)
Have a nice one!

Copy a sheet from another open workbook into current workbook

I'm trying to write a macro that takes Sheet1 from another, already open, workbook and copy it into a the workbook that will use the data. The macro will be run from the workbook that will be receiving the copied sheet and the file I want the data from will always be named the same.
I've tried the follow code, but it either just copies incorrectly or doesn't work.
Workbooks("FileIWantToCopyFrom.XLSX").Sheets("Sheet1").Copy
ActiveWorkbook.Worksheets("FileIWantToCopyFrom.XLSX").Select
Sheets("Sheet1").Copy After:=Workbooks("Book1").Sheets(1)
Any help would be much appreciated. Thanks!

Excel crashes without errors when deleting a sheet via a macro

I have a macro that deletes a sheet and replaces it with another. This new sheet is, infact, copied from another workbook. The old sheet contains some buttons. The macro creates the same buttons in the new sheet.
The macro itself is located in a module in the workbook and neither the old sheet nor the new one contain any macros.
One of these buttons is used to run the macro. When the macro is run directly from VBA editor, it runs fine without errors. But when the button on the old sheet is used to run the macro, excel crashes at the line where the old sheet is being deleted.
I understand that this may be because deleting the sheet somehow messes up the macro reference. But I can't move the button to another sheet. So how can I fix this issue?
Any help will be greatly appreciated.

Save specific sheet to a new workbook using vba

I am working on a vba project. My requirement is, to delete/move a existing sheet from an excel file to a new excel file using vba code. That means to Save specific sheet to a new workbook. I have tried it alot but the file which is being saved is not a new workbook. It is the existing one.
Code is as below :
xlWorkbook.Sheets("Sheet3").Copy -- this line opens sheet3 as a new workbook
xlWorkbook.Sheets("Sheet3").SaveAs "***Path where file is be saved***", FileFormat:=56 -- this line save the original excel file including Sheet1 and Sheet2. But requirement is only having Sheet3.
xlWorkbook.Sheets("Sheet3").Delete -- this line delete the Sheet3 from original as expected.
I can not use any kind of reference file or Excel 12.0 Object library. So
Please if anyone can suggest me or help me. Will appreciate.
Use two lines:
xlWorkbook.Sheets("Sheet3").Copy
xlWorkbook.Application.ActiveWorkbook.SaveAs "***Path where file is be saved***", FileFormat:=56

VBA Using a userform data to copy from a workbook into a new workbook

My code is suppose to take input from the user, via userforms, then based on that input go from sheet to sheet and copy specific rows, and paste them into a new workbook. I have been trying all these different ways to create a new workbook, copy and paste, but I haven't had any success. I have gotten error 9, and tried to find a way to fix that. But with all my efforts I have yet to find a solution. I am very new to VBA and could really use some help, even if its just basic explanation of certain ways to copy and paste.

Resources