How to generate new workbooks with vba button intact - vba

I have a worksheet that has a macro (as a button) that generates a separate worksheet w/ some analysis on it. Is it possible to generate a new workbook with the old workbook's VBA intact and with the new workbook's button referring to the new workbook's VBA?

Related

VBA Userform read another workbook dynamically without opening it

I'm fairly new to VBA. I'm trying to create a user form where the user can select another workbook as read only and copy some of the data to the current workbook. I don't want the other workbook to open.
I found this https://www.encodedna.com/excel/copy-data-from-closed-excel-workbook-without-opening.htm but the excel document is hard coded. Is it possible to select a workbook dynamically, through a file dialog without that workbook popping up?
Thank you

Creating a button with macro on a different workbook

I currently have a macro which creates another workbook and adds a button to it. I've assigned a macro to the button, but I believe I still need the master workbook to actually run the macro.
Is there a way to have the macro run on the new workbook without referring to the master workbook? In other words, is it possible to programmatically add a button to a new workbook with a macro and have it be independent of the original workbook?
Thanks in advance!
I'd go like this:
have your "master" workbook with a template worksheet whit both an ActiveX button and its corresponding code in its code pane
say its name is "TemplateSheetWithButtonAndCode"
in your macro use
Worksheets("TemplateSheetWithButtonAndCode").Copy
ActiveWorkbook.SaveAs "c:\MyFolder\MyWorkbooK"
this will generate a new workbook with a copy of your "template" worksheet as its only worksheet, along with necessary code to have the button work
Place the button on a template worksheet. Place the button's associated macro in that worksheet's code area.
If you export the worksheet to another workbook, you will also be exporting the button and the macro.

Copy a worksheet to a new workbook and have a button point to the macro in the NEW workbook?

I have a worksheet that looks like a form. It has a "Reset" button to clear the values.
There is a macro that I can run that copies that sheet into a new Workbook and emails it to someone. The module that contains the macros gets copied into it and all is almost ok. The problem is, the button on the worksheet that runs the reset macro still points back to the original document. How can I have it point to the macro within itself?
Edit: One thing to point out is that I do have macros within that sheet that fire off if cells are changed. Those work great and don't try and load the original file. It's just the ones within the button that don't work.
You should use 'ActiveX form button' instead of 'simple form button' (which I guess you use). Then you have to put reset procedure within ActiveX button event (double click on it to edit that procedure). As the result of coping the sheet both button and the procedure would be moved to new workbook which will break any reference to original file.

Triggering external macro on button click from normal excel workbook (.xlsx)

Stated simply my question is as follows Is it possible for a button on a worksheet in a normal excel workbook (.xlsx not .xlsm) to trigger a macro in another file specifically an installed excel add in (.xlam).
Here is some background on why I want to achieve this. I have a workbook that many users need to be able to view but only some need to be able to update by filling in a form on another sheet and calling a macro in an add in. The worksheet should not contain any macros to avoid security warnings when opened by normal users. I can do this by having a ribbon button in the add in that the user clicks which will then check that the correct workbook is open and that the form is filled in etc. before executing the update code. However the interface would be nicer if the button instead of appearing on the ribbon was on the worksheet just below the form. Therefore my question is it possible to trigger an external macro from a button click in a non macro enabled workbook.
Yes, you can assign an external macro (which should be .xlsm file) to a non-macro-enabled workbook (.xlsx) button. My xlsm macro resides in the same directory as the xlsx workbook for simplicity. (Note: I am using Excel 2010)
Firstly, you must open your xlsm macro in the same instance of Excel window (i.e. do not open a new instance of Excel) so that your xlsx workbook will be able to see/access it.
Right-click on your xlsx workbook button and select "Assign Macro..."
Make sure you select Macros in: All Open Workbooks
All macros in open workbooks will be shown (this is why it is important to do step 1).
Select the desired macro from the list then click OK.
I don't see how you can execute a macro from a control in a non-macro-enabled workbook.

Saving Non-VSTO copy of VSTO Workbook

I am trying to save a ListObject from a .NET 3.5 Excel 2007 VSTO Workbook to a new sheet (done) and save that new sheet to a new workbook (done) without that workbook requiring the VSTO customization file (!!!!!).
Has anyone had any luck with this? The only way I've had any success is just saving as a CSV file but that's not exactly acceptable in this case. I'd rather not save to a CSV just to copy back to a XLS file.
worksheet.SaveAs(saveDialog.FileName, Excel.XlFileFormat.xlOpenXMLWorkbook)
If I understand correctly, you do not want the new workbook file to depend or load any VSTO customization?
Try this MSDN link to remove VSTO customization assemblies from workbooks.
Ok this didn't end up working for me and here's why. The answer is still correct but I want to clarify for future users.
I have a ListObject I wanted to save in an external workbook using VSTO. Creating a new Worksheet and using SaveAs would rename the current Workbook to that and therefore I would have to close the entire workbook to remove the Customization.
What I should have done from the beginning is this:
Create the Worksheet and populate the ListObject on said Worksheet. Then use .Copy() with no parameters to create a new workbook. How do i find the workbook then though? I simply named the Worksheet Now.Ticks.ToString() and looked for any open workbook with the ActiveSheet.Name as Now.Ticks.ToString(). For this application, it doesn't need to be more in depth then that. I the saved THAT workbook and then closed it. Since the workbook was created with Copy it had no customizations on it and problem solved.