E-Learning Excel VBA Programming Lesson 2
E-Learning Excel VBA Programming Lesson 2
1. 2. 3. 4. 5.
st 1
Session
VBA is EVENT DRIVEN LANGUAGE OBJECT-BASED LANGUAGE What is IDE ( Integrated Development Environmetn) How to go to VBA IDE Various Features of IDE - VBE Menu - Project Window - Code Window - Properties Window - Immediate Window 6. Option Explicit - Why Option Explicit - How to set Auto for Option Explicit 7. Code Modules - General Purpose Code Modules - Work Book Code Modules - Work Sheet Code Modules
Ameetz.com
Any more modules you add will be listed as new members of the collection. You can double-click on any of them and view its contents in the Code Window. The one property that a module has is its Name. You can give more meaningful names than just Module1 or Module2 by changing the name in the properties window while the module is the current object of affection active object.
Ameetz.com
Ameetz.com
There is one and only one code module per workbook that is associated with Workbook Event handling. At the technical level, this module, along with the worksheet event handling modules are Class Modules. That need not concern you. Just be aware that if you want to do any coding that deals with events that occur at the workbook level, you do it in this module.
Workbook Events Just what are the workbook events? You can get a complete list of them from the code window while the Workbook Code module content is displayed: You can display that content by double-clicking the ThisWorkbook object in the VBAProject window. Youll get a display similar to above picture
Ameetz.com
code to deal with something you want to happen when the workbook is opened.
With your cursor placed inside of any Workbook related procedure, even just a stub, you can then use the pull-down on the right to find a list of all the available event handlers for the workbook, as shown below :
NOTE: If the cursor is not in a workbook event handling procedure, the list on the right will show you a list of non-workbook event procedure names in the module.
Ameetz.com
Worksheet Events Just what are the worksheet events? You can get a complete list of them from the code window while any Worksheet Code module content is displayed: You can display that content by double-clicking any worksheet object in the VBAProject window. The code module for that sheet will be displayed as given below:
Ameetz.com
For worksheets, when you choose the Worksheet item in the left pulldown list, the default event is the Worksheet_SelectionChange(ByVal Target As Range) event. This even triggers any time you make a new selection on the sheet such as simply moving to another cell. The new cell becomes the selection, and thus youve had a selection change. As with the Workbook events, you can now get a complete list of Worksheet Events available to be programmed against by using the right-side pull-down (indicated as (Declarations)). This list is much shorter than the Workbooks list, as there are 9 events (from Excel 2003) provide considerable versatility in dealing with worksheets. Out of the list, the Change() event is probably the one that most often has code associated with it. A Change() occurs when a user alters the contents (value) of one or more cells on the sheet. Worksheet formula recalculations dont trigger this event, but they do trigger the Calculate() event.
Ameetz.com
Often in worksheet event stubs provided by the VBE you will see reference to two special objects (sometimes more or others also): Cancel and/or Target. Target represents the Range [which is an object that represents a single cell, a group of cells, one or more rows and/or one or more columns] that is active at the time the event took place. Think of Target as the actual object itself. Anything you do to Target is done to the actual Range that it represents. Changes made to Target will appear on the sheet itself. The Cancel object is a Boolean type object. A Boolean object can only have one of two conditions assigned to it: TRUE or FALSE. By default a Boolean object is FALSE (and has a numeric value of zero). If your code sets Cancel = TRUE then the underlying event action is cancelled: the DoubleClick never takes place or the RightClick never gets completed. These are handy events to use to take very special actions with you can have someone double-click in a cell (and set Cancel = True) to begin a series of events unique to that cell. A real world example of this type of thing in one application I developed is that in a data area matrix that has dates in the top row, a double-click on a date causes all rows with an empty cell in that column to become hidden: a kind of auto filter based on empty cells for that one column.
Ameetz.com
Ameetz.com
SUBS Sub procedures are just like Functions, except that they do not return a value in the same way that a Function does. They can accept arguments, or not, just like a Function does. VBA Sub Procedure Example
Ameetz.com
The word Macro is slang for the word VBA procedure. A procedure is defined as a named group of statements that are run as a unit. A statement is simply 1 complete line of code. VBA procedures are used to perform tasks such as controlling Excels environment, communicating with databases, calculating equations, analyzing data etc.
Ameetz.com
Ameetz.com
Sub Procedures Sub procedures are written when you want to command Excel like creating a chart, analyzing data, coloring cells, copying and pasting data. Function Procedures Function procedures are created when you want to make your own custom worksheet functions or perform a calculation that will be used over and over again. Note that Sub procedures can also do calculations. What to Write So if you want to do a task in Excel, you write a Sub procedure, if you want to write a custom worksheet function, you write a Function procedure. Are their grey areas between the two, yes, but do not worry about them when first starting out
Ameetz.com
In this example, you create an Excel macro that converts selected formulas to their current values. Sure, you can do this without a macro, but its a multistep procedure. To convert a range of formulas to values, you normally complete the following steps: 1. Select the range that contains the formulas to be converted. 2. Copy the range to the Clipboard. 3. Choose EditPaste Special. 4. Click the Values option button in the Paste Special dialog box, which is shown in Figure 2-1. 5. Click OK. 6. Press Esc. This clears the cut-copy mode indicator (the moving border) in the worksheet.
Ameetz.com
Here comes the hands-on part. Follow these instructions carefully: 1. Select the range of cells that contains your formulas. The selection can include both values and formulas. In my case, I chose the range A1:D10. 2. Choose ToolsMacroRecord New Macro. The Record Macro dialog box appears, as shown in Figure 2-3. 3. Enter a name for the macro. Excel provides a default name, but its better to use a more descriptive name. ConvertFormulas is a good name for this one.
No Spaces
Ameetz.com