The basic unit of VBA programming is the macro, which is a block of code in a module that you reference as a unit. So far you've seen that there are two types of macros: command macros (also known as Subprocedures) and function macros (or Function procedures). The Structure of MacroTo recap what you learned earlier, a command macro is allowed to modify its environment, but it can't return a value. Here is the basic structure of a command macro: Sub MacroName (argument1, argument2, ...) [VBA statements] End Sub Here, MacroName is the name of the command macro, and arguments is the optional list of values for the arguments accepted by the macro. For example, Listing 11.1 presents a command macro that enters some values for a loan in various worksheet ranges and then adds a formula to calculate the loan payment.
Listing 11.1. A Sample Command MacroSub EnterLoanData() Range("A1").Value = .08 Range("A2").Value = 10 Range("A3").Value = 10000 Range("A4").Formula = "=PMT(A1/12, A2*12, A3)" End Sub A Function macro, on the other hand, doesn't usually modify its environment, but it does return a value. Here is its structure: Function MacroName (argument1, argument2, ...) [VBA statements] MacroName = returnValue End Function For example, Listing 11.2 is a function macro that sums two ranges, stores the results in variables named totalSales and totalExpenses (see "Understanding Program Variables," later in this chapter, to learn more about variables), and then uses these values and the fixedCosts argument to calculate the net margin: Listing 11.2. A Sample Function MacroFunction CalcNetMargin(fixedCosts) totalSales = Application.Sum(Range("Sales")) totalExpenses = Application.Sum(Range("Expenses")) CalcNetMargin = (totalSales - totalExpenses - fixedCosts)/totalSales End Function Writing Your Own MacroAlthough the Macro Recorder makes it easy to create your own homegrown command macros, you can't use it to create function macros, and there are plenty of macro features that you can't access with mouse or keyboard actions or by selecting menu options. In Excel, for example, VBA has a couple of dozen information macro functions that return data about cells, worksheets, workspaces, and more. Also, the VBA control functions enable you to add true programming constructs such as looping, branching, and decision making. To access these macro elements, you need to write your own VBA routines from scratch. This is easier than it sounds because all you really need to do is enter a series of statements in a module. The problem here, of course, is that you likely don't know any VBA statements yet, so you're not in a position to write your own macros. That's fine, because I'll be introducing you to some VBA a bit later (see "VBA Programming Basics"). For now, let's work through a simple example to illustrate the process. With a module window open and active, follow these steps to write your own command macro:
When you press Enter to start a new line, VBA analyzes the line you just entered and performs three chores:
Running a Command Macro from the Visual Basic EditorAfter you create a command macro, you can run it directly from the Visual Basic Editor:
|