Sie sind auf Seite 1von 5

There are several ways to run a macro in Microsoft Excel.

A macro is an action o
r a set of actions that you can use to automate tasks. Macros are recorded in th
e Visual Basic for Applications programming language. You can always run a macro
by clicking the Macros command on the ribbon (Developer tab, Code group). Depen
ding on how a macro is assigned to run, you might also be able to run it by pres
sing a CTRL combination shortcut key (shortcut key: A function key or key combin
ation, such as F5 or CTRL+A, that you use to carry out a menu command. In contra
st, an access key is a key combination, such as ALT+F, that moves the focus to a
menu, command, or control.), by clicking a button on the Quick Access Toolbar o
r in a custom group on the ribbon. or by clicking an area on an object, graphic,
or control. In addition, you can run a macro automatically when you open a work
book.
Note When you set the macro security level in Excel to Disable all macros witho
ut notification, Excel will run only those macros that are digitally signed or s
tored in a trusted location, such as the Excel startup folder on your computer.
If the macro that you want to run is not digitally signed (digital signature: An
electronic, encryption-based, secure stamp of authentication on a macro or docu
ment. This signature confirms that the macro or document originated from the sig
ner and has not been altered.) or located in a trusted location (trusted locatio
n: A folder or file path on your computer or a location on your intranet from wh
ich it is safe to run code. Default trusted locations include the Templates, Add
ins, and Startup folders, and you can specify your own trusted locations.), you
can temporarily change the security level that enables all macros.
What do you want to do?
-------------------------------------------------------------------------------Run a macro
Run a macro by pressing a CTRL combination shortcut key
Run a macro by clicking a button on the Quick Access Toolbar
Run a macro by clicking a button in a custom group on the ribbon
Run a macro by clicking an area on a graphic object
Configure a macro to run automatically upon opening a workbook
-------------------------------------------------------------------------------Run a macro
If the Developer tab is not available, do the following to display it:
Click the File tab, click Options, and then click the Customize Ribbon category.
In the Main Tabs list, select the Developer check box, and then click OK.
To set the security level temporarily to enable all macros, do the following:
On the Developer tab, in the Code group, click Macro Security.
In the Macro Settings category, under Macro Settings, click Enable all macros (n
ot recommended; potentially dangerous code can run), and then click OK.
Note To help prevent potentially dangerous code from running, we recommend that
you return to any one of the settings that disable all macros after you finish
working with macros.
Open the workbook that contains the macro.
On the Developer tab, in the Code group, click Macros.

In the Macro name box, click the macro that you want to run.
Do one of the following:
To run a macro in an Excel workbook, click Run.
Tip You can also press CTRL+F8 to run the macro. You can interrupt the executio
n of the macro by pressing ESC.
To run a macro from a Microsoft Visual Basic for Applications (VBA) (Visual Basi
c for Applications (VBA): A macro-language version of Microsoft Visual Basic tha
t is used to program Microsoft Windows-based applications and is included with s
everal Microsoft programs.) module, click Edit, and then on the Run menu, click
Run Sub/UserForm , or press F5.
Top of Page
Run a macro by pressing a CTRL combination shortcut key
If the Developer tab is not available, do the following to display it:
Click the File tab, click Options, and then click the Customize Ribbon category.
In the Main Tabs list, select the Developer check box, and then click OK.
On the Developer tab, in the Code group, click Macros.
In the Macro name box, click the macro that you want to assign to a CTRL combina
tion shortcut key.
Click Options.
The Macro Options dialog box appears.
In the Shortcut key box, type any lowercase letter or uppercase letter that you
want to use with the CTRL key.
Note The shortcut key will override any equivalent default Excel shortcut key w
hile the workbook that contains the macro is open.
For a list of CTRL combination shortcut keys that are already assigned in Excel,
see the article Excel shortcut and function keys.
In the Description box, type a description of the macro.
Click OK to save your changes, and then click Cancel to close the Macro dialog b
ox.
Top of Page
Run a macro by clicking a button on the Quick Access Toolbar
To add a button to the Quick Access Toolbar that will run a macro, do the follow
ing:
Click the File tab, Options, and then click Quick Access Toolbar.
In the Choose commands from list, select Macros.
In the list, click the macro that you created, and then click Add.
To change the button image of the macro, select the macro in the box to which it
was added, and then click Modify.
Under Symbol, click the button image that you want to use.
To change the name of the macro that is displayed when you rest the pointer on t
he button, in the Display name box, type the name that you want to use.
Click OK to add the macro button to the Quick Access Toolbar.
On the Quick Access Toolbar, click the macro button that you just added.
Top of Page
Run a macro by clicking a button in a custom group on the ribbon
By taking advantage of the customizability of the ribbon in Excel 2010, you can
create a custom group that appears on a tab in the ribbon, and then assign a mac
ro to a button in that group. For example, you can add a custom group named "My
Macros" to the Developer tab, and then add a macro (that appears as a button) to
the new group.
To learn more, see the article Customize the ribbon.

Top of Page
Run a macro by clicking an area on a graphic object
You can create a hot spot on a graphic that users can click to run a macro.
In the worksheet, insert a graphic object, such as a picture, clip art, shape, o
r SmartArt.
To learn about inserting a graphic object, see Add, change, or delete shapes.
To create a hot spot on the existing object, on the Insert tab, in the Illustrat
ions group, click Shapes, select the shape that you want to use, and then draw t
hat shape on the existing object.
Right-click the hot spot that you created, and then click Assign Macro.
Do one of the following:
To assign an existing macro to the graphic object, double-click the macro or ent
er its name in the Macro name box.
To record a new macro to assign to the selected graphic object, click Record, ty
pe a name for the macro in the Record Macro dialog box, and then click OK to beg
in recording your macro. When you finish recording the macro, click Stop Recordi
ng on the Developer tab in the Code group.
Tip You can also click Stop Recording on the left side of the status bar.
To edit an existing macro, click the name of the macro in the Macro name box, an
d then click Edit.
Click OK.
In the worksheet, select the hot spot. This displays the Drawing Tools, adding a
Format tab.
On the Format tab, in the Shape Styles group, click the arrow next to Shape Fill
, and then click No Fill.
Click the arrow next to Shape Outline, and then click No Outline.
Top of Page
Configure a macro to run automatically upon opening a workbook
If you record a macro and save it with the name "Auto_Open," the macro will run
whenever you open the workbook that contains the macro. Another way to automatic
ally run a macro when you open a workbook is to write a VBA procedure in the Ope
n event of the workbook by using the Visual Basic Editor (Visual Basic Editor: A
n environment in which you write new and edit existing Visual Basic for Applicat
ions code and procedures. The Visual Basic Editor contains a complete debugging
toolset for finding syntax, run-time, and logic problems in your code.). The Ope
n event is a built-in workbook event that runs its macro code every time you ope
n the workbook.
Create an Auto_Open macro
If the Developer tab is not available, do the following to display it:
Click the File tab, and then click Options.
In the Customize Ribbon category, in the Main Tabs list, select the Developer ch
eck box, and then click OK.
To set the security level temporarily to enable all macros, do the following:
On the Developer tab, in the Code group, click Macro Security.
In the Macro Settings category, under Macro Settings, click Enable all macros (n
ot recommended, potentially dangerous code can run), and then click OK.
Note To help prevent potentially dangerous code from running, we recommend that
you return to any one of the settings that disable all macros after you finish
working with macros.

If you want to save the macro with a particular workbook, open that workbook fir
st.
On the Developer tab, in the Code group, click Record Macro.
In the Macro name box, type Auto_Open.
In the Store macro in list, select the workbook where you want to store the macr
o.
Tip If you want a macro to be available whenever you use Excel, select Personal
Macro Workbook. When you select Personal Macro Workbook, Excel creates a hidden
personal macro workbook (Personal.xlsb), if it does not already exist, and save
s the macro in this workbook. In Windows Vista, this workbook is saved in the C:
\Users\user name\AppData\Local\Microsoft\Excel\XLStart folder. If you can't find
it there, it may have been saved in the Roaming subfolder instead of Local. In
Microsoft Windows XP, this workbook is saved in the C:\Documents and Settings\us
er name\Application Data\Microsoft\Excel\XLStart folder. Workbooks in the XLStar
t folder are opened automatically whenever Excel starts. If you want a macro in
the personal macro workbook to be run automatically in another workbook, you mus
t also save that workbook in the XLStart folder so that both workbooks are opene
d when Excel starts.
Click OK, and then perform the actions that you want to record.
On the Developer tab, in the Code group, click Stop Recording .
Tip You can also click Stop Recording on the left side of the status bar.
Notes
If you chose to save the macro in This Workbook or New Workbook in step 6, save
or move the workbook into one of the XLStart folders.
Recording an Auto_Open macro has the following limitations:
If the workbook where you save the Auto_Open macro already contains a VBA proced
ure in its Open event, the VBA procedure for the Open event will override all ac
tions in the Auto_Open macro.
An Auto_Open macro is ignored when a workbook is opened programmatically by usin
g the Open method.
An Auto_Open macro runs before any other workbooks open. Therefore, if you recor
d actions that you want Excel to perform on the default Book1 workbook or on a w
orkbook that is loaded from the XLStart folder, the Auto_Open macro will fail wh
en you restart Excel, because the macro runs before the default and startup work
books open.
If you encounter these limitations, instead of recording an Auto_Open macro, you
must create a VBA procedure for the Open event as described in the next section
of this article.
If you want Excel to start without running an Auto_Open macro, hold down the SHI
FT key when you start Excel.
Create a VBA procedure for the Open event of a workbook
The following example uses the Open event to run a macro when you open the workb
ook.
If the Developer tab is not available, do the following to display it:
Click the File tab, and then click Options.
In the Customize Ribbon category, in the Main Tabs list, select the Developer ch
eck box, and then click OK.
To set the security level temporarily to enable all macros, do the following:
On the Developer tab, in the Code group, click Macro Security.
In the Macro Settings category, under Macro Settings, click Enable all macros (n
ot recommended, potentially dangerous code can run), and then click OK.
Note To help prevent potentially dangerous code from running, we recommend that
you return to any one of the settings that disable all macros after you finish
working with macros.

Save and close all open workbooks.


Open the workbook where you want to add the macro, or create a new workbook.
On the Developer tab, in the Code group, click Visual Basic.
In the Project Explorer window, right-click the ThisWorkbook object, and then cl
ick View Code.
Tip If the Project Explorer window is not visible, on the View menu, click Proj
ect Explorer.
In the Object list above the Code window, select Workbook.
This automatically creates an empty procedure for the Open event, such as this:
Private Sub Workbook_Open()
End Sub
Add the following lines of code to the procedure:Private Sub Workbook_Open()
MsgBox Date
Worksheets("Sheet1").Range("A1").Value = Date
End Sub
Switch to Excel and save the workbook as a macro-enabled workbook (.xlsm).
Close and reopen the workbook. When you open the file again, Excel runs the Work
book_Open procedure, which displays today's date in a message box.
Click OK in the message box.
Note that cell A1 on Sheet1 also contains the date as a result of running the Wo
rkbook_Open procedure.
Top of Page

Das könnte Ihnen auch gefallen