How do I get a macro to move between workbooks?

5 pts.
Tags:
Macros
Microsoft Excel
Variables
VBA
Workbooks
I have a template (workbook #1) that I would like to copy data out of and paste into the workbook that I run the macro from (workbooks 2, 3, 4, 5...). The problem is that when I record the macro from workbook, excel codes the name of that workbook, making the macro unusable in workbooks 3, 4, 5.... Is there a was to define the workbook that you run the macro from as the workbook that you want the recorded actions to take place on. Below are the first few lines of the macro I recorded. The workbook "Aplanalp_Michael 296753.xlsx" is the workbook I recorded the macro from. Again, I would like to activate any workbook that I run the macro from, not just "Aplanalp_Michael 296753.xlsx". Thanks for your help. Windows("ModificationTemplate.xls").Activate Application.Goto Reference:="PostAug06Contributions" Selection.Copy Windows("Aplanalp_Michael 296753.xlsx").Activate
ASKED: June 19, 2008  1:19 AM
UPDATED: February 18, 2014  10:09 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

By default, when you create a macro for a Microsoft Excel worksheet, that macro works only on that worksheet. This behavior is ok as long the macro automates tasks that are unique to that worksheet. But have you ever found yourself recreating the same macro on more than one worksheet? If you have (and even if you haven’t), you can copy your macros to a worksheet called personal.xls. From there, your macros become available to you whenever you start Excel, and you can run them against the data on any open worksheet.

Of course, you need to be careful here. You don’t want to run macros at random, because doing so might corrupt your data and cost you time and money. But if you have macros that you can use in a variety of situations, using a personal.xls file can also save you time and money.
Getting started

To follow the steps in this column, you need an Excel worksheet that contains at least one macro. If you need a sample macro, follow these steps:

1. Start Excel and open a new, blank workbook.
2. On the Tools menu, point to Macro, and then click Visual Basic Editor.
3. In the Project window, double-click ThisWorkbook.
4. Copy and paste this code into the code window:

<i> sub test()
MsgBox “This is only a test.”
end sub</i><b>

5. Save the file to your default working folder.
6. Quit the Visual Basic Editor and close the workbook.

Note When you work with macros, you need to think about security. For more information about macro security, see Use Office security features to increase macro safety.
Create a personal.xls file

To create and use a personal.xls file, you follow this basic process:

* Use Microsoft Windows Explorer to create a blank personal.xls file in the folder called XLStart. If you installed Microsoft Office in the default location, you’ll find \XLStart under C:\Program Files\Microsoft Office\Office 10. If you installed Office in a different location, find \XLStart there.
* Hide the file in Excel (Window menu, Hide command).
* Unhide the file (in the world of Excel, “unhide” is a real word), copy your macros from their parent worksheets to the file, and then hide personal.xls again.

You keep personal.xls hidden unless you want to add macros to it. Whenever you start Excel, personal.xls opens automatically and runs in the background. If you don’t hide the file, Excel opens it and allows you to add data to it.
To create the personal.xls file

1. Start Windows Explorer.
2. Navigate to C:\Program Files\Microsoft Office\Office 10\XLStart. If you installed Excel or Microsoft Office in another location, navigate to the \XLStart folder in that location.
3. In the right pane of Windows Explorer, right-click, point to New, and then click Text Document.
4. By default, Windows Explorer allows you to rename the file. Rename it personal.xls.
5. If Windows asks you whether you want to change the file name extension, click Yes.

To hide personal.xls

1. Start Excel and open personal.xls.
2. On the Window menu in Excel, click Hide, and then quit Excel.
3. When asked if you want to save your changes, click Yes. The workbook will open silently and become available the next time you start Excel.

Add macros to personal.xls

When you create a new macro, you can choose to save it to your personal.xls file. In addition, you can add your existing macros to personal.xls. The following steps explain how to perform both actions.
To add new macros

1. On the Tools menu in Excel, point to Macro, and then click Record new macro.
2. In the Store macro in list, click Personal Macro Workbook.

Saving a new macro to personal.xls
3. Click OK and record your macro normally.

To add existing macros

1. Open personal.xls, and on the Window menu, click Unhide.
2. In the Unhide dialog box, ensure that personal.xls is selected, and then click OK.
3. Open the worksheet that contains the macro you want to copy, and on the Tools menu, point to Macro, and then click Macros.
4. Select the macro you want to add to personal.xls, and then click Edit. This step starts the Visual Basic Editor and opens the code module for the macro.
5. On the File menu in the Visual Basic Editor, click Export File.
6. Locate the \XLStart folder and click Save to save the code module in that folder.
7. Go back to personal.xls, press ALT+F11 to start the Visual Basic Editor there, and on the File menu, click Import File.
8. Navigate to the \XLStart folder, click the module that you just exported, and then click Open.
9. Save the module, quit the Visual Basic Editor, and then hide personal.xls (Window menu, Hide command). Quit Excel, clicking Yes to save your changes.
10. One final task: Go back to the \XLStart folder and delete any code modules. If you leave the module files in the folder, they appear automatically the next time you start Excel.

Use personal.xls

You use personal.xls by starting Excel normally and running macros as needed.

1. Open a new, blank worksheet, and use your favorite method to enter some data.
2. On the Tools menu, point to Macro, and then click Macros.
3. Select the macro you want to run, and then click Run.

Okay, that’s pretty basic, but keep these facts in mind as you go:

* Excel distinguishes between macros that reside in personal.xls and those that reside on the open worksheet. In the following figure, the Apply Sum macro resides on the local worksheet. The remaining macros reside on personal.xls:

The difference between macros stored locally and in personal.xls

* You can’t edit macros that reside in personal.xls unless you open that file. For example, repeat steps 1 and 2 at the beginning of this section, select a macro that resides in personal.xls, and then click Edit. Excel displays the following message: Cannot edit a macro on a hidden workbook. Unhide the workbook using the Unhide command.
* In addition to containing macros, personal.xls can store information that you use frequently. For example, you can store an address or some boilerplate text in a cell and use a macro to copy that data and paste it onto another worksheet. Go ahead and use your creativity!

A few words of caution

Remember the caution at the beginning of this column? It’s worth repeating: Don’t run just any macro against any data. If you do, you can corrupt that data. In addition:

* Get into the habit of checking your title bar when you open new or existing worksheets. If you’ve opened personal.xls, hide it, quit Excel, click Yes to save your changes, and start over.
* Make sure you use digitally signed macros that come from trusted sources, and implement a high level of macro security. For more information about digital signing, trusted sources, and macro security, see the following Power User columns:
o Use Office security features to increase macro safety
o The Office security settings in depth
o Using Office security features with your default folders</b>

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • gricypops

    I wish to archive worksheets from one workbook to another once I hav finished with that client?  I have various sheets, named after the client to move, but my macro moves the same client over and over again.  Please help???

    Nicole

    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following