Introduction To VBA
Contents Some Basics Simple VBA Procedure (Macro) To Execute The Procedure Recording A Macro About Macro Recorder VBA Objects Reference
Some Basics Code: You perform actions in VBA by executing VBA code. You write (or record) VBA code, which is stored in a VBA module. Module: VBA modules are stored in an Excel workbook file, but you view or edit a module by using the Visual Basic Editor (VBE). A VBA module consists of procedures. Procedures: A procedure is basically a unit of computer code that performs some action. VBA supports two types of procedures: Sub procedures and Function procedures. 1. Sub: A Sub procedure consists of a series of statements and can be executed in a number of ways. 2. Function: A VBA module can also have Function procedures. A Function procedure returns a single value (or possibly an array). A Function can be called from another VBA procedure or used in a worksheet formula. Objects: VBA manipulates objects contained in its host application. (In this case, Excel is the host application.) Excel provides you with more than 100 classes of objects to manipulate. Collections: Like objects form a collection. Object hierarchy:when you refer to a contained or member object, you specify its position in the object hierarchy by using a period (also known as a dot) as a separator between the container and the member. Active objects: If you omit a specific reference to an object, Excel uses the active objects.
Some Basics (Contd ) Objects properties: Objects have properties. A property can be thought of as a setting for an object. VBA variables: You can assign values to VBA variables. Object methods: Objects have methods. A method is an action that is performed with the object. Standard programming constructs: VBA also includes many constructs found in modern programming languages, including arrays, loops, and so on. Events: Some objects recognize specific events, and you can write VBA code that is executed when the event occurs.
Simple VBA Procedure (Macro) Sub SayHello() Msg = Is your name & Application.UserName &? Ans = MsgBox(Msg, vbyesno) If Ans = vbno Then MsgBox Oh, never mind. Else MsgBox I must be Clairvoyant! End If End Sub
To Execute The Procedure Press F5. Choose Run Run Sub/UserForm. Click the Run Sub/UserForm button on the Standard toolbar.
Recording A Macro Macro Recorder is a tool that converts your Excel actions into VBA code. 1. Activate a worksheet in the workbook (any worksheet will do). 2. Choose Developer Code Record Macro (Excel displays its Record Macro dialog box). 3. Click OK to accept the default setting for the macro (Excel automatically inserts a new VBA module into the workbook s VBA project. From this point on, Excel converts your actions into VBA code. Notice that Excel s status bar displays a blue square. You can click that control to stop recording). 4. Choose PageLayout Page Setup Orientation Landscape. 5. Select Developer Code Stop Recording (or click the blue square in the status bar). Excel stops recording your actions.
About Macro Recorder The macro recorder is appropriate only for simple macros or for recording a small part of a more complex macro. Not all the actions you make in Excel get recorded. The macro recorder can t generate code that performs looping (that is, repeating statements), assigns variables, executes statements conditionally, displays dialog boxes, and so on. The macro recorder always creates Sub procedures. You can t create a Function procedure by using the macro recorder. The code that is generated depends on certain settings that you specify. You ll often want to clean up the recorded code to remove extraneous commands.
VBA Objects Objects have unique properties and methods. You can manipulate objects without selecting them. Properties can return a reference to another object. You can refer to the same object in many different ways.
Reference Excel 2010 Power Programming with VBA by John Walkenbach.
THANK YOU