Copy data and a button with macros to another workbook - excel

I want to copy all the data of the workbook into another workbook,
and one of the worksheets has a button which has a macro within itself.
workbook.worksheets.copy()
code above copy the data successful, but a broken button with it.
When I click the button on the new wb, the message "Sorry we couldn't find..XXXXX. Is it possible it was moved, renamed, or deleted?" popped up.
XXXXX is the path of origin workbook.
And I can't see any module in new workbook.
Is there a method could copy all the contents into the new wb?

If I understood correctly, you want to transfer a macro/module from one workbook to another using a vba macro.
The button obviously wont work if the underlying macro is not transferred.
However, transferring a macro can only be done manually.
There is no way to copy or transfer a macro using another macro in VBA.

Related

How to use same macro to read all worksheets

I have a excel workbook which already has a macro say "covert". It has two worksheets. First includes configurations (variables) required to run macro and second includes rows (data) on which macro is applied. On first sheet there's one button on-click of this rows are converted into JSON. I checked configuration of this button, 'convert' macro is assigned to it.
Now, I want to make a copy of second sheet which will have similar data and I want to use same macro to read this newly created sheet with slight change in macro.
As soon as I copy sheet with data, I can see macro is also duplicated.
To make macro to decide which sheet should be read, I've added a row in first sheet and then I am adding below code to fetch config.
Dim configSheet As Worksheet
Set configSheet = ThisWorkbook.Worksheets("Configuration")
With configSheet
VAR_SHEET = .Range("B8").value
Then selecting particular sheet using below code.
With ThisWorkbook.Worksheets(VAR_SHEET)
Now, the problem is even after making changes in macro, it is always reading first sheet instead of considering variable.
When you copy a sheet with code like that, any buttons on the sheet do not "auto-adjust" to call code in the copy: they still call the same subs as they did originally (assuming non-activeX button).
Any buttons on the sheet which call code in the sheet module will need to be re-linked to the code in the copy.

Excel VBA - Copy module from a protected VBA project into a new workbook

My problem with Excel's VBA code protection:
In my current project, I wrote some VBA code (in WB1) that shall produce a new workbook (WB2). In this new workbook, a button (B2) is created. This button shall execute some code, when pressed. The macro that shall be executed is copied from the original workbook (WB1). This all works fine, when the VBA code in the original workbook (WB1) is not protected.
However, when I add protection to the VBA code in the original workbook, the module that contains the macro is not copied into the new workbook (WB2). And thus the button (B2) has no functionality.
My question: Has anyone of you run into the same problem and is there an other solution than removing the protection from the original workbook's VBA code?

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.

Adding Module through code

I have a master workbook containing a macro which opens another workbook containing a demand forecast. The workbook opened through the macro is downloaded from a customer portal and is all new every day without possibility of editing it beforehand.
The macro then loops through the information and creates new readable and more intuitive worksheets. However, on a few of these worksheets I would like to add some event-driven code to give tooltips when mousing over or selecting cells.
Is there any possibility (without installing add-ons from vanilla Excel 2010) to add code to worksheet objects created during a macro?
The layout of the processed workbook is more or less static, so I was wondering if I should create a template file and then copy the input into it. That would allow me to have coded the events before data is added. Is this the best possibility?
As Dan pointed out, you could use Application.VBE.ActiveVBProject to programmatically add code modules to a workbook. But doing this requires more lenient Macro security settings (which are set to untrusted, by default) and this is not recommended.
When I have to do something similar, I use three workbooks:
The data containing workbook
This book has no macro functionality
A template workbook containing the necessary macros
A macro enabled workbook to facilitate the transition.
Use workbook #3 to open workbook #1 and copy its data into workbook #2. Save a copy of workbook #2 and close it. Repeat this process as necessary.
It isn't the prettiest solution, but it keeps your code modular.

Resources