Excel crashes without errors when deleting a sheet via a macro - excel

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.

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.

Copy data and a button with macros to another workbook

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.

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?

Default Starting Cell in new worksheet

I have been unable to find a solution for this problem: When adding a new worksheet in an Excel workbook, the active cell moves to D20. This behavior is consistent across ALL workbooks that I open or use (new or opening existing workbooks). There are no macros on "ThisWorkbook" or any of the sheets. Has anyone seen this before?
Did you (or someone else) maybe create a custom sheet template?
https://www.rondebruin.nl/win/s9/win016.htm
I saved a single-sheet workbook (with D20 selected) as "sheet.xlt" in my XLSTART folder, and each time I add a new sheet in a workbook I get that same selection in the new sheet.

Macro is saved on Personal workbook but does not show up in Macro list

I recorded a macro in Personal workbook, saved the file that I recorded with .xlsm. Then closed this workbook.
I assume that I can reuse this macro in other workbooks.
Next day I open a new work book trying to use the macro that I created yesterday. the Macro list/menu shows blank. the macro that i created yesterday is not there.
I tried to created a few ones, sames process but the new macros are not saved to use for new workbook. Can someone shed some light?
I use excel 2007

Resources