| Macros beginning with Worksheet_ or Workbook_ are Event macros and are covered on the Event macros page. | 
| If these instructions do not help then try
Visual Basic for Applications 101 (Excel) « Microsoft Excel for Windows -- Visual Basic for Applications 101 [archived copy] | 
What is a Macro: A stored series of commands and functions that can be run to perform a specific task or tasks. In Excel a macro could be a complete program. Macros may be run from a toolbar button, or menu, from a shape in the workbook, or from another macro.
What is an Event Macro: Event macros are triggered by an event such as opening a double clicking a cell, right click, opening a workbook, or a activating a worksheet. (Event macros)
What is a Function: Excel has builtin functions like IF, AND, OR, SUM which you use in your worksheet. Functions can invoke other functions to return a value.
What is a User Defined Function: You can make up your own functions or use functions made up by someone else, either or both would be User Defined Functions (UDF), because they are not built into Excel (not built-in).
Your personal.xls file (personal.xlsm in Excel 2007) is where you store your own macros and User Defined Functions (UDF), that you want to be available from any workbook. For macros you only want available in a single workbook you would install macros in that workbook.
Recording a macro: If you want to program something, or issue a series of commands more or less simulating what you do one at a time at the keyboard you can create a macro. The usual way to start into macros is to record a macro. Macros in Excel are written in VBA (Visual Basic for Applications). Macros are sometimes made into Addins or Addins may be programmed in another language such as C++, but we'll skip over addins.
Recording a macro in Excel creates a series of actions in VBA, to duplicate what you enter -- it does not record individual keystrokes.
Macros and UDF can be stored in your workbook or in another workbook. Personal.xls (personal.xlsm in Excel 2007) is just another workbook, but it is commonly used and described for the macros that you want available to more than one workbook for your own use. The personal.xls (personal.xlsm in Excel 2007) is normally started when you bring up Excel. When you start up Excel all files are opened that are in your XLSTART directory, so you don't want other workbooks or files in that directory. (remember that) This also applies to your Alternate Startup Directory under Tools, options, general which for most should have no file designated. You can find your XLSTART directory using FIND it would be in the same directory as Excel is invoked from. You won't have one at this point, and expect that the following will create one for you.
To make sure that there is a DIM statement for each variable and that the usage conforms to the definitions it is imperative that you use Option Explicit which in included at the top of modules in Excel 2000 by default. (VBE Tools, Editor, [x] Require Variable Definition). Be sure to have this option on to help keep you out of serious trouble. There are additional hints on my Proper page mentioned later on.
Tools menu, Options becomes Office Button in upper left corner of Excel 2007, then click on options (at bottom), and then guess at which item on the left to use, for instance what was Tools (menu), Options (submenu), View (tab) becomes Office(button), Advanced (button), then Display heading instead of View.
Additions of toolbar buttons, and Menus will have to be figured out later, also will have to check out Change the ribbon in Excel 2007 by Ron de Bruin.
Let's record a macro. Hopefully you are using XL97 or later because there would be some minor modification otherwise. (would be simpler)
Bring up the Visual Basic Editor (VBE) standard with normal layout of windows: Project window (left), Code window (right), Immediate window at the bottom. [Picture Alt+F11 -- Project view"]
Within the Project Explorer select your project, which is your workbook name. You will install the provided macro in your workbook, or if you want the macro to always be accessible you would install in your personal.xls (personal.xlsm in Excel 2007) workbook.
If you do not see modules in your project (workbook) which you've selected, then invoke the Insert (menu), and then module [picture], the first one created in a project would be module1. You can put several macros in a module or create a new module for some addition macros. You can rename a module with help of F4.
Select the module with a double click on the module name (i.e. module1)
Something to keep in mind when you use a macro is that you can not undo use of macro with Ctrl+Z.
 the Run Sub/UserForm, Break, and Reset buttons must all be dark blue (not grayed out).  If the middle button is grayed out, you are in break mode from
macro failure or a chosen break point (debugging) -- fix your macro errors and then press the Run button to continue, or the Reset button to allow starting over.  (the picture at right shows being in break mode)
The buttons look like VCR buttons Play, Pause, Stop.
the Run Sub/UserForm, Break, and Reset buttons must all be dark blue (not grayed out).  If the middle button is grayed out, you are in break mode from
macro failure or a chosen break point (debugging) -- fix your macro errors and then press the Run button to continue, or the Reset button to allow starting over.  (the picture at right shows being in break mode)
The buttons look like VCR buttons Play, Pause, Stop. 
Now do the what you may have already done in record a macro, but this time when you choose a name
Now that you have a macro, switch over to the Visual Basic Editor(VBE) Alt+F11 you can use Alt+F11 to get back, but you may find it easier to just select the workbook with the cursor later. Anyway in the VBE look for your macro under personal.xls (personal.xlsm in Excel 2007), modules -- if you do not see the listing use Ctrl+R, when you click on module1 it will bring up a code window as would F7(view, code) and you will see your Macro1 there. To help distribute your code so that you would have all of your code open at one time Excel will start a new module when you record your first macro for the day. You probably want to put related code together that will be open at the same time.
Now it's your turn, suggest you create a user defined function, to go into your personal.xls (personal.xlsm in Excel 2007) for that suggest you take a look at a my Install Macros and User Defined Functions page.
BTW, some of this is actually in HELP, that's where I found it when I was told I should use a macro. But then that was Excel 95, which was probably a bit easier to get started in.
![[fx]](../icons/fx.jpg) ,
or from the insert menu (then
,
or from the insert menu (then ![[fx]](../icons/fx.jpg) function...).
 function...).
Unlike macros you will need to include the project library (i.e. personal.xls!) if not in the
same workbook unless you set up references or make if part of an addin.  Otherwise, you must include personal.xls (personal.xlsm in Excel 2007)when invoking the function:
   =personal.xls!GetFormula(D4)
or =personal.xlsm!GetFormula(D4) in Excel 2007)
Usage Example for a User Defined Function:
  =GetFormula(D4)
   =personal.xls!GetFormula(D4)   or   =personal.xlsm!GetFormula(D4) in Excel 2007
This page also includes information on calling a macro from another macro, and examples of cell formatting. These are my favorites because GetFormula was the first UDF that I wrote, and I received help from the newsgroups. The page is fully documented.
Usage Examples on that page:
For the macros on that page you make a selection of cells
and then run the macro, and the macro only applies to the selected cells.
If you look at what I have in my toolbars, you can probably get a better idea of some of my favorites. toolbars.htm and how my toolbar menus are structured in bar hopper.
In the Visual Basic Editor you should close the modules before closing your VBE session, because when Excel is reopened modules that were open will be reopened.
If you are providing macro examples, please indent your code so that it is readable. If you are posting code to a newsgroup try to break lines yourself, rather than expecting someone to fix coding based on syntax errors.
Searching Excel newsgroups -- Google Advanced Group search:
  
http://groups.google.com/advanced_group_search?q=group:*Excel* 
  be sure to look at the entire
thread on anything that looks promising.  [xlnews.htm], 
Searching the web with Google Advanced Web Search:
  http://www.google.com/advanced_search?lr=lang_en
When
searching the web include both the word SUB (for macros)   and Excel. [xlnews.htm]
You might want to remove junk websites using
Customize Google in Firefox 
to eliminate search hits that really don't have web type
content by excluding those that contain newsgroup postings. 
A less reliable way is to exclude some words associated with newsgroup postings: 
-HTH  and  -"posted by" in your Google web search (note the negative signs in front to exclude items). 
You should at least know how to install a macro before posting a programming question. Hopefully, this page helped with that, if you didn't already know.
Questions not involving with programming should be asked in a different newsgroup see list of Excel newsgroups.
Many problems involve not hiding the personal macro workbook before attempting to run macros. Your personal macro workbook should be in your XLSTART directory and if you let Excel create it for you, it would have been created in the correct place. In Windows Vista the XLSTART directory is in a completely different location than earlier versions of Windows.
From Excel try Alt+F8, look for your macro in the list, if not there you did not install the macro in the correct place or the workbook that you installed it in (personal.xls /personal.xlsm)) is not open. An exception to this are private macros which have to be called by name and are not seen in the list. Any macro with parameters is by definition private, and the parameters even if optional can only be used when called from another macro.
 
If your macro is in the list, 
 try invoking the macro from there  
with the Run button on right that might show up a problem such as being 
in Break mode due to a previous macro failure.  If you are in 
break mode, fix the error and press the square Rest button in 
the VBE (Visual Basic Editor).
 try invoking the macro from there  
with the Run button on right that might show up a problem such as being 
in Break mode due to a previous macro failure.  If you are in 
break mode, fix the error and press the square Rest button in 
the VBE (Visual Basic Editor).  
If your assigned shortcut key does nothing, it could be because you are in break mode, and you would see that if you tried to invoke from Alt+F8 macro list. From the macro list, press the options button, you should see your assigned shortcut there.
On the Excel Window menu, make sure that your personal.xls (or personal.xlsm) workbook is hidden. The reason for hiding is so that you don't have to specify the workbook name with the macro. With a shortcut key assigned it really wouldn't matter because the workbook name was included when you created the shortcut. If you unhide your personal macros, don't forget to hide it again.
Those with Excel XP (2002)may run into a situation where Excel thinks a workbook is bad and quarantines it. Look in HELP (F1) for “Disabled items” (Dave Peterson, programming, 2005-04-28)
If the macro does not run see topic under Running the Macro you Just Installed.
Loss of the Stop Recording toolbar:  
 You should use the black button to Stop recording a macro.  Clicking on the Exit button will turn off the
Recording toolbar so you will no longer have a choice between turning
“Relative Reference” on or off because the only place
you can change is on the Recording toolbar.  To restore the toolbar:  Start recording a macro, then View, toolbars, 
toolbars (tab), (check) Stop Recording.    
The relative button allows you to switch at any time
between the two while recoding a macro example of the difference in coding:
You should use the black button to Stop recording a macro.  Clicking on the Exit button will turn off the
Recording toolbar so you will no longer have a choice between turning
“Relative Reference” on or off because the only place
you can change is on the Recording toolbar.  To restore the toolbar:  Start recording a macro, then View, toolbars, 
toolbars (tab), (check) Stop Recording.    
The relative button allows you to switch at any time
between the two while recoding a macro example of the difference in coding: 
    ActiveCell.Offset(-5, 5).Range("A1").Select
    Range("G31").Select
 
Macro won't run, summary of things to check   (#wontrun)
![[fx]](../icons/fx.jpg) and
choose "user defined" on the left side.
 and
choose "user defined" on the left side.
Check that you have calculation on automatic, that you are not in formula view, and that you that you did not enter your formula into a text formatted cell. If you see #NAME! then your function is not found possibly misnamed.
Help with and transitioning between Office Versions
For the lucky people not on Excel 2007...
Please send your comments concerning this web page to: David McRitchie send email comments
Copyright © 1997 - 2006, F. David McRitchie, All Rights Reserved