?
Solved

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

Posted on 2013-05-29
10
Medium Priority
?
210 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 34

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 34

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 34

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 34

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

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!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

777 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