Anda di halaman 1dari 6

If you perform a task repeatedly in Microsoft Excel, you can automate the task with a macro.

A macro is a series of commands and functions that are stored in a Microsoft Visual Basic module and can be run whenever you need to perform the task. For example, if you often enter long text strings in cells, you can create a macro to format those cells so that the text wraps. Recording macros When you record a macro, Excel stores information about each step you take as you perform a series of commands. You then run the macro to repeat, or "play back," the commands. If you make a mistake when you record the macro, corrections you make are also recorded. Visual Basic stores each macro in a new module attached to a workbook. Making a macro easy to run You can run a macro by choosing it from a list in the Macro dialog box. To make a macro run whenever you click a particular button or press a particular key combination, you can assign the macro to a toolbar button, a keyboard shortcut, or a graphic object on a worksheet. Managing your macros After you record a macro, you can view the macro code with the Visual Basic Editor to correct errors or change what the macro does. For example, if you wanted the text-wrapping macro to also make the text bold, you could record another macro to make a cell bold and then copy the instructions from that macro to the text-wrapping macro. The Visual Basic Editor is a program designed to make writing and editing macro code easy for beginners, and provides plenty of online Help. You don't have to learn how to program or use the Visual Basic language to make simple changes to your macros. With the Visual Basic Editor, you can edit macros, copy macros from one module to another, copy macros between different workbooks, rename the modules that store the macros, or rename the macros. Macro security Excel provides safeguards that help protect against viruses that can be transmitted by macros. If you share macros with others, you can certify them with a digital signature so that other users can verify that they are from a trustworthy source. Whenever you open a workbook that contains macros, you can verify their source before you enable them. Create a macro

ABOUT MACROS

1. 1. 2. 3. 4.
use.

Record a macro Set the security level to Medium or Low. How? On the Tools menu, click Options. Click the Security tab. Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to

On the Tools menu, point to Macro, and then click Record New Macro. In the Macro name box, enter a name for the macro. Notes o The first character of the macro name must be a letter. Other characters can be letters, numbers, or underscore characters. Spaces are not allowed in a macro name; an underscore character works well as a word separator. o Do not use a macro name that is also a cell reference or you can get an error message that the macro name is not valid. 2. If you want to run the macro by pressing a keyboard shortcut key, enter a letter in the Shortcut key box. You can use CTRL+ letter (for lowercase letters) or CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on the keyboard. The shortcut key letter you use cannot be a number or special character such as @ or #.

2. 3.

Note The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open. 3. In the Store macro in box, click the location where you want to store the macro. If you want a macro to be available whenever you use Excel, select Personal Macro Workbook. 4. If you want to include a description of the macro, type it in the Description box. 5. Click OK. 6. If you want the macro to run relative to the position of the active cell, record it using relative cell references. On the Stop Recording toolbar, click Relative Reference so that it is selected. Excel will continue to record macros with relative again, so that it

references until you quit Excel or until you click Relative Reference is not selected. 7. Carry out the actions you want to record.
8.

On the Stop Recording toolbar, click Stop Recording

Create a macro using Microsoft Visual Basic 1. On the Tools menu in Microsoft Excel, point to Macro, and then click Visual Basic Editor. 2. On the Insert menu, click Module. 3. Type or copy your code into the code window of the module. 4. If you want to run the macro from the module window, press F5. 5. When you're finished writing your macro, click Close and Return to Microsoft Excel on the File menu. Create a startup macro Automatic macros, such as Auto_Activate, are designed to run when you start Microsoft Excel. For more information about these macros, see Visual Basic Help.

1. 1. 2. 3. 4.
use.

Copy part of a macro to create another macro Set the security level to Medium or Low. How? On the Tools menu, click Options. Click the Security tab. Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to Open the workbook that contains the macro you want to copy. On the Tools menu, point to Macro, and then click Macros. In the Macro name box, enter the name of the macro that you want to

copy. Click Edit. 6. Select the lines of the macro you want to copy. To copy the entire macro, make sure to include the Sub and End Sub lines in the selection.

2. 3. 4. 5.

7.
8.

On the Standard toolbar, click Copy . Switch to the module where you want to place the code. Click Paste Tip .

9.

You can view your Personal Macro Workbook file (Personal.xls) at any time by opening it in the Visual Basic Editor (Alt+F11). Because Personal.xls is a hidden workbook that is always open, you must unhide it if you want to copy a macro. Run a macro

1. 1. 2. 3. 4. 2. 3. 4.

Run a macro Set the security level to Medium or Low. How? On the Tools menu, click Options. Click the Security tab. Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to

use.

Open the workbook that contains the macro. On the Tools menu, point to Macro, and then click Macros. In the Macro name box, enter the name of the macro you want to run. 5. Do one of the following: Run a macro in a Microsoft Excel workbook o Click Run. If you want to interrupt, press ESC. Run a macro from a Microsoft Visual Basic module 1. Click Edit.

2.

Click Run Sub/UserForm

Tip If you want to run a different macro while you are in the Visual Basic Editor, click Macros on the Tools menu. In the Macro name box, enter the name of the macro you want to run, and then click Run. Start a macro from a keyboard shortcut 1. On the Tools menu, point to Macro, and then click Macros. 2. In the Macro name box, enter the name of the macro you want to assign to a keyboard shortcut key. 3. Click Options. 4. If you want to run the macro by pressing a keyboard shortcut key, enter a letter in the Shortcut key box. You can use CTRL+ letter (for lowercase letters) or CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on the keyboard. The shortcut key letter you use cannot be a number or special character such as @ or #. Note The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open. 5. If you want to include a description of the macro, type it in the Description box. 6. Click OK. 7. Click Cancel. Start a macro from a button or graphic control Click the button or graphic control so that sizing handles appear. With the graphic object selected, right-click a sizing handle of the button or graphic object to display the shortcut menu. 3. On the shortcut menu, click Assign Macro. 4. Do one of the following:

1. 2.

o To assign an existing macro to the button or graphic object, enter the name of the macro in the Macro name box, and then click OK. o To record a new macro to assign to the selected graphic object, click

Record. When you finish recording the macro, click Stop Recording on the Stop Recording toolbar. o To write a new macro in the Visual Basic Editor, click New. To edit an existing macro, click the name of the macro in the Macro Name box, and then click Edit. Note If you assign a macro to a button or other object that is already in use as a hyperlink, the hyperlink information is deleted. From then on, clicking the button or object runs the macro instead. Start a macro from an area, or hotspot, on a graphic object Draw another object to create a hot spot for an existing object. 2. With the second graphic object selected, right-click a sizing handle of the button or graphic object to display the shortcut menu. 3. On the shortcut menu, click Assign Macro. 4. Do one of the following: o To assign an existing macro to the button or graphic object, enter the name of the macro in the Macro name box, and then click OK. o To record a new macro to assign to the selected graphic object, click 1. Record. When you finish recording the macro, click Stop Recording on the Stop Recording toolbar. o To write a new macro in the Visual Basic Editor, click New. To edit an existing macro, click the name of the macro in the Macro Name box, and then click Edit. 5. On the Format menu, click AutoShape, and then click the Color and Lines tab. 6. Under Fill, click No Fill in the Color box. Under Line, click No Line in the Color box. 7. Repeat these steps for each hotspot you want to create. You do not need to create a new graphic object for each hotspot. Start a macro from a toolbar button 1. On the Tools menu, click Customize. 2. Do one of the following: o If the toolbar that contains the button is not visible, click the Toolbars tab, and then select the check box next to the toolbar name. o If the button you want to run the macro from is not on a toolbar, click the Commands tab, and then click Macros in the Categories list. In the Commands list, drag the Custom button onto a toolbar. 3. Right-click the toolbar button, and then click Assign Macro on the shortcut menu. 4. Enter the name of the macro in the Macro name box, and then click OK. Edit a macro Before you edit a macro, you should be familiar with the Visual Basic Editor. The Visual Basic Editor can be used to write and edit macros attached to Microsoft Excel workbooks. 1. Set the security level to Medium or Low.

1. 2.

How? On the Tools menu, click Options. Click the Security tab.

use.

3. 4. 2. 3. 4. 5.

Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to

On the Tools menu, point to Macro, and then click Macros. In the Macro name box, enter the name of the macro. Click Edit. For Help with the Visual Basic Editor, on the Help menu, click Microsoft Visual Basic Help. Delete a macro 1. Open the workbook that contains the macro you want to delete. 2. On the Tools menu, point to Macro, and then click Macros. 3. In the Macros in list, click This Workbook. 4. In the Macro name box, click the name of the macro that you want to delete.

5.

Click Delete.

Stop a macro Do one of the following: If you want to stop a macro that's currently running, press ESC, and click End in the Microsoft Visual Basic dialog box. If you want to prevent automatically running a macro when you start Microsoft Excel, hold down SHIFT during startup. Copy a macro module to another workbook 1. Set the security level to Medium.

use.

1. 2. 3. 4.

How? On the Tools menu, click Options. Click the Security tab. Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to

2. Open the workbook that contains the module you want to copy and the workbook you want to copy the module to. 3. On the Tools menu, point to Macro, and then click Visual Basic Editor. 4.
5. On the View menu, click Project Explorer . Drag the module you want to copy to the destination workbook.

Troubleshoot macros When I click the Refresh button, a message tells me that my macro changes will be lost. This message appears when you have made changes to a macro in the Visual Basic Editor and have also changed the copy of your workbook in the Microsoft Script Editor. To save the changes to your macro, do the following: 1. Click No. 2. Switch to the Visual Basic Editor window. 3. Export any modules you have changed.

4. In your workbook in Microsoft Script Editor, click Refresh Refresh toolbar. 5. Click Yes. 6. Import the modules to restore the changes to your macro.

on the

While recording a macro, I recorded an action I didn't want. To undo an unwanted action in a macro, you can open the macro in the Visual Basic Editor and remove any unwanted steps. If you don't want to use the Visual Basic Editor, record the macro again without the unwanted action. A macro I recorded sometimes produces an error message. A macro you record may not run properly in every situation. If the macro cannot run, Microsoft Excel displays an error message. Some macros depend on certain options or settings in Excel. For example, a macro that searches for bold text won't run properly if bold text isn't displayed. If a macro you've recorded produces an error message, note the error number. You can then search for "error messages" in Visual Basic Help and find information about the message you received.

Anda mungkin juga menyukai