Circular Macro in Excel - How to create a macro to create a macro?

I am creating "Macro 1" within "file 1" that opens "file X", creates a pivot table, formats the data, saves as "file 1" then creates a button (assigned macro is "Macro 1" from "file 1") and saves again.

I've played with saving before/after/during and seems that each time I save over "file 1" the macro module 1 disappears. Essentially I want to create a circular macro so that I never need to leave "file 1". I want file 1 to do all the work then save with the macro ready to go the next time.

Any thoughts on this? Is this possible?

My thought is I should be able to create a macro in "file 2", have a button in "file 1" to open the workbook "file 2" behind the scenes to do all the formatting and such, then save the formatted data as "file 1". Problem is, I need the button with "macro 1" to also save, but it does not. I would prefer to have the macro saved in the local workbook because this is a shared file. I would like to avoid having to share a macro file to each users XLSTART folder...

Any help is much appreciated, thank you.

Phil
LVL 1
Phil_ConsultantAsked:
Who is Participating?
 
NorieConnect With a Mentor VBA ExpertCommented:
Why don't you save the output in the workbook with the code?

Then save that workbook with the new output and the code intact.

That would mean you had a one-stop shop.

You can write code to create code but it would require changes in security settings to allow programmatic access to the VBE.

That can be a problem and it's sometimes better to use another approach, eg using a template workbook with the code already in it.

Here's a good link http://www.cpearson.com/Excel/vbe.aspx.
0
 
NorieVBA ExpertCommented:
Phil

Why are you saving over file 1?
0
 
Phil_ConsultantAuthor Commented:
I would like file 1 to be a one-stop shop. You open it, click the button, save it... and do the same thing later as needed.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
NorieVBA ExpertCommented:
But why are you saving over it?

Actually, why are you saving it?

I thought the code in it opened other files, ran code on them to create a pivot table, format etc then saved the file that was opened.
0
 
Phil_ConsultantAuthor Commented:
To answer your question, I am saving the file to reference later. The need for the file is only a short time while validating numbers, then it will be overwritten later as needed. I can easily create a second file with the output no trouble at all, but I was asked to make it all in one file if possible.

It's hard to explain and it's not the optimal situation but I am asked to create the output while using the source file only so that users are not browsing to/from separate files. It's pretty much sometime for Access but I lack Access skills to complete the project.

The main question is, can I create a macro that itself creates another macro in the output workbook? I know how to create a macro that creates a pivot table, or formats, or opens Outlook, etc... but can I record or write code that creates a macro in the output? Essentially, this will allow me to create a circular macro, if possible.
0
 
NorieVBA ExpertCommented:
Where are you creating the output?

In the workbook with the code or in the workbook you are opening?

Is the workbook you are opening only really a data source?

One thing that isn't particularly clear, to me anyway, is why you need to create code if it's already there in the workbook you are going to save?
0
 
Phil_ConsultantAuthor Commented:
The output will be in a separate workbook but then I will save that separate workbook as the original, or "file 1", after all formatting. This way, it remains a one stop shop for the user.

Sorry I didn't make this clear but the macro module 1 (and code) disappears from the original workbook, or  "file 1", when I save over it with the formatted output. I made sure to be saving the output as a macro enabled workbook when overwriting the "file 1" but it always erases the macro within the file. Is that because the macro needs to be saved in the output file prior to saving as the original? If so, that's what my question is getting at - can I create a macro within the output file bundled up within the original macro (a macro created by a macro). Also, the output file is created from a software package each time we run a report.

Thank you for taking the time to address my question.
0
 
Phil_ConsultantAuthor Commented:
Thank you, I am currently looking into that site and will report back with my findings later on. Much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.