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
Solved

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

Posted on 2013-05-29
10
206 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

856 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