Run Code From A Module
Run Code From A Module
As a beginner to Excel VBA, you might find it difficult to decide where to put
your VBA code. The Create a Macro chapter illustrates how to run code by
clicking on a command button. This example teaches you how to run code from
a module.
1. Open the Visual Basic Editor.
2. Click Insert, Module.
End Sub
Note: a procedure is either a sub or a function. Learn more about functions and
subs here, if you like.
4. The sub changes the background color of your worksheet to cyan. To achieve
this, add the following code line.
Cells.Interior.ColorIndex = 28
Note: instead of ColorIndex number 28 (cyan), you can use any ColorIndex
number.
To run the procedure, execute the following steps.
5. Click Macros.
Result:
Note: code placed into a module is available to the whole workbook. That
means, you can select Sheet2 or Sheet3 and change the background color of
these sheets as well. The Add a Macro to the Toolbar program illustrates how to
make a macro available to all your workbooks (Excel files). Remember, code
placed on a sheet (assigned to a command button) is only available for that
particular sheet.
VBA
VBA (Visual Basic for Applications) is the programming language of Excel and
other Office programs.
1 Create a Macro: With Excel VBA you can automate tasks in Excel by writing so
called macros. In this chapter, learn how to create a simple macro.
2 MsgBox: The MsgBox is a dialog box in Excel VBA you can use to inform the
users of your program.
3 Workbook and Worksheet Object: Learn more about the Workbook and
Worksheet object in Excel VBA.
4 Range Object: The Range object, which is the representation of a cell (or cells)
on your worksheet, is the most important object of Excel VBA.
5 Variables: This chapter teaches you how to declare, initialize and display
a variable in Excel VBA.
6 If Then Statement: Use the If Then statement in Excel VBA to execute code
lines if a specific condition is met.
7 Loop: Looping is one of the most powerful programming techniques. A loop in
Excel VBA enables you to loop through a range of cells with just a few codes
lines.
8 Macro Errors: This chapter teaches you how to deal with macro errors in Excel.
9 String Manipulation: In this chapter, you'll find the most important functions to
manipulate strings in Excel VBA.
10 Date and Time: Learn how to work with dates and times in Excel VBA.
11 Events: Events are actions performed by users which trigger Excel VBA to
execute code.
12 Array: An array is a group of variables. In Excel VBA, you can refer to a
specific variable (element) of an array by using the array name and the index
number.
13 Function and Sub: In Excel VBA, a function can return a value while a sub
cannot.
14 Application Object: The mother of all objects is Excel itself. We call it the
Application object. The application object gives access to a lot of Excel related
options.
15 ActiveX Controls: Learn how to create ActiveX controls such as command
buttons, text boxes, list boxes etc.
16 Userform: This chapter teaches you how to create an Excel VBA Userform.