[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-05-29
10
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 35

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 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 35

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 35

Accepted Solution

by:
Norie earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

656 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