Solved

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

Posted on 2013-05-29
10
202 Views
Last Modified: 2014-05-23
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
0
Comment
Question by:Phil_Consultant
  • 4
  • 4
10 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 39204980
Phil

Why are you saving over file 1?
0
 
LVL 1

Author Comment

by:Phil_Consultant
ID: 39204986
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
 
LVL 33

Expert Comment

by:Norie
ID: 39205029
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
 
LVL 1

Author Comment

by:Phil_Consultant
ID: 39205083
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Norie
ID: 39205118
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
 
LVL 1

Author Comment

by:Phil_Consultant
ID: 39205243
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
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 39205317
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
 
LVL 1

Author Comment

by:Phil_Consultant
ID: 39205351
Thank you, I am currently looking into that site and will report back with my findings later on. Much appreciated.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now