VBA for Excel (macros)

 

Excel VBA tips

 

 

 

 

 

Lesson 1-4: The Code  Window in the Visual Basic Editor of Excel
(Levels: Beginners)

Note: For a better comprehension of the lessons in section 1 on the Visual Basic Editor and the Macro Recorder www.excel-vba.com has created a workbook (vba-tutorial-editor.xls) one of the 25 that are included with the VBA for Excel Desktop Tutorial.

NOTE: You cannot change the font or its color in the code window. You input appears in black, comments appear in green, reserved words in blue and when you make a mistake the color of the font turns to red.

NOTE: For most users the wheel of the mouse doesn't work in the code window. To correct this frustrating situation download and install the free fixer presented on the first page of the tutorial.

The code widow shows the procedures that are part of the element of the project on which you have double clieked in the project window (module, sheet, userform, control or ThisWorkbook). The picture below shows you what you should see in the code window if you double click on Module1 of "excel-visual-basic-editor.xls" in the project window. There is only one VBA procedure in Module 1 and it is named "Macro1" . It is a procedure that have been recorded.

In the window below you see the code created with the Macro Recorder in "excel-visual-basic-editor.xls" .

Visual Basic Editor: code window

In the code window there is a main window in which appears the code. At the top there are two drop-down lists.

When you select a module in the Project window (double click) the right DDList shows you all the procedures in the module as for the left DDList it is useless for modules. You will use the left DDList when you start developing VBA procedures for userforms and controls or when you develop procedures triggered by an event related to the worksheet or the workbook.

When  you select a worksheet in the Project window (double click) the left DDList of the Code window offers you two values (General and Worksheet). When  you select "Worksheet" in the left  DDList the right DDList offers you the 9  VBA events related to the sheet. Select an event and the VBE creates the first and last line of a procedure in the Code window. You then develop your procedure between  these lines. If you don't need to develop a procedure for this event erase the two lines of code.

When  you select a "ThisWorkbook"   in the Project window (double click) the left DDList of the Code window offers you two values (General and Workbook). When  you select "Workbook" in the left  DDList the right DDList offers you the 28 VBA events related to the workbook. Select an event and the VBE creates the first and last line of a procedure in the Code window. You then develop your procedure between  these lines. If you don't need to develop a procedure for this event erase the two lines of code.

When  you double click on a userform  in the Project window the userform appears. Double click on the userform itself or any of the controls   and   the left DDList of the Code window offers you many values. You will see the userform and the list of controls on the userform. Depending on the element that you choose you will find in the right DDList the events related to the element. Select an event and the VBE creates the first and last line of a procedure in the Code window. You then develop your procedure between  these lines. If you don't need to develop a procedure for this event erase the two lines of code.

You click within one of the procedure and you go to the "Run" icon of the toolbar and the procedure is executed. To stop the execution of a VBA procedure at any time you click (repeatedly) on the "Esc" key.

When you want to stop the execution while testing step by step (using the "F8" key) you click on the "Reset" icon of the toolbar .

Many things can happen in the left gray margin of the Code window.

When you left click in the gray margin at the level of any line of code the line turn to brown and a brow dot appears in the margin. You have just set a breakpoint   VBA for Excel Breakpoint. At this point if you click within the procedure and run it from the toolbar   it will stop at this breakpoint.

When you click within the procedure and then click on the "F8" key the "Sub" line turns to yellow and a small arrow appears in the gray margin VBA for Excel step by step. If you left click on the yellow arrow in the gray margin and hold the button you can drag the arrow to any other line where you would want to start your test. Just remember that if you are working with declared variables you must hit "F8" a second time before you drag the arrow down.

When you right click in the gray margin 2 contextual menus can  appear depending if you are testing "F8" or not. If any line is yellow the menu is the following:

VBA for Excel code window menu

In this menu only one item is interesting it is "Set Next Statement" . When you select this item the line at which level you have right click in the gray margin become the next line to be executed. It turns to yellow and you can resume your "F8" testing from there.

If you are not testing the procedure another menu appears when you right click in the gray margin or anywhere within the code:

VBA for Excel code menu second

The is really nothing that is interesting in this menu.

You will create you first VBA procedure in this window in lesson 1-5 and you will use these tips when you  test your procedures in lesson 6.