SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 35 | Next

Jim DeMarco

"Pro Excel 2007 VBA"


Writing a Macro in the VBE
In this example, you??™ll create a macro by typing code directly in the VBE.
Open the file 1-MacroExample01.xlsx (shown in Figure 1-21), and open the VBE.
nNote You will find all the example files and source code for this book at www.apress.com in the
Downloads section of this book??™s home page.
Figure 1-21. Sales data for the first quarter of the year
We see tour sales for the fictitious band ???VBA,??? which are received quarterly by their management
office and need to be totaled. Using R1C1 notation, we??™ll create one subroutine that
will total these numbers, and since it is a relative reference to the cells, we??™ll see that we only
need to create one formula.
First, we??™ll add a standard code module to the project. In the VBE (Alt+F11 from an Excel
workbook), in the Project Explorer section (top-left pane), choose the top-level item, named
VBAProject (1-MacroExample01.xlsx), right-click it, and choose Insert ?¤Module, as shown in
Figure 1-22.
CHAPTER 1 n THE MACRO RECORDER AND CODE MODULES 20
Figure 1-22. Inserting a standard code module (shortcut menu)
The Project Explorer shows our new module, named Module1 by default, as shown in
Figure 1-23.
Figure 1-23. New standard code module added
More Macro Security
In the code pane, create a new empty subroutine called TotalSales, as shown in Listing 1-2,
and save the file.
Listing 1-2. Empty TotalSales Subroutine
Sub TotalSales()
End Sub
The prompt shown in Figure 1-24 will appear.


Pages:
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47