Sunteți pe pagina 1din 109

VBA Section 1 of 24: Introduction to Macros in Excel VBA is " Visual Basic for Application" .

It is a programming language that allows users to program macros to accomplish complex tasks within an application like Excel, Word, Power Point, Access, etc. With VBA for Excel you can develop small procedures (macros) that will make your professional life easier and allow you to do more in less time. But VBA is also a very powerful programming language with which you can develop within Excel real programs that will accomplish in a few minutes very complex tasks. With VBA for Excel you can develop a program that does EXACTLY what you need and it is easy to learn. Where does VBA come from? In the '70's a new computer language appeared called Basic. Some of you used it on advanced calculators (TI 59) and on old COCO64 from Radio Shack. At the time, RAM (memory) and CPU's (the brain of the computer) were very, very small and there were limits to what you could do with the language. With today's computers came Visual Basic (VB). It is still Basic but a lot of elements are pre-programmed making the user's task much simpler. Microsoft adopted the language and introduced it as a component of all its applications. Should you learn VBA or VB? Let's say that VB is for programmers and VBA is for users (or developers). If you are not a programmer adopt VBA for Excel because most of the functions that are needed to organize and analyze business data are pre-programmed in Excel. It costs a lot less to develop financial and management applications in Excel than to re-invent the wheel and do everything in VB. What is the difference between VB, VBA for Excel, VBA for Access, VBA for Word, VBA for Project, etc? To answer this question I will ask you to imagine a meeting of five English speaking persons, a lawyer, an accountant, a physician, a chemist and a psychologist. They all speak English but when it is time to talk about their work none of them totally understand the other. It is the same with VB and VBA's. The objects, properties and methods vary substantially from one sub-language to the other. But basically if you speak English you can speak VB or any of the VBA's. VBA for Excel is the most user friendly because of the macro recorder that can assist you in writing the code and because you can test your procedures step by step while seeing it at work directly in your workbook. Along the test you can

modify things, go back a few lines and re test them. Not only the language is easy but the programming toll is very user friendly. Who is this 24 sections tutorial for? This website has been designed for accountants, productions planners, production supervisors, sales people, financial analysts and other business data analysts with no or little background in programming . It has also been designed for those who play VBA by hear. There are very few books on VBA and they are all written by computer geeks who have no respect for non-programmers. So a lot of people learn how to use VBA by trial and error and miss the good stuff that makes programming very easy. VBA is a programming language for users. It is simple and easy to learn. Anybody can develop simple macros (VBA procedures) and with time and interest you can get to a point where you can develop very complex procedures to accomplish very sophisticated tasks. You can expect to develop much better applications than any programmer because you: - know your data well (financial, accounting, sales, marketing, inventory, etc.) - know Excel well (SUMPRODUCT, INDEX/MATCH and the Excel database functionalities). No need to learn about very rarely used functionalities like pivot tables, solver, goal seek, scenarios, group, outline and other specialized tools. If you have more that 10 years of experience in your field of expertise (Accounting, Finance, Production, Engineering, etc..) you may want to learn VBA to become an Excel VBA Developer (the market and the money are very good). What are these 24 sections? Please don't skip section 2 on vocabulary. Some of the things presented later will sound like Martian. If you speak Martian move on... else.... Then in the next section you will discover the programming tools starting with the Visual Basic Editor (VBE) the most user friendly programming environment on the market. In sections 4, 5 and 6 you will see how to use the Visual Basic Editor to create, test and modify your VBA procedures. Section 7 is about security and then you are introduced to the Macro Recorder a unique feature, the greatest assistant that allows you to create macros while you are doing manually what you want the macro to do. You will then learn about EVENTS that trigger macros (section 9).

Finally there are 16 sections of VBA code (the words and sentences to talk with Excel). You will learn how to work with all the objects of Excel, the application, the worksheets, the workbooks, the cells and ranges and other objects. You will learn how to use VBA to work with the database functionalities and how to handle the errors. VBA Chapter 2 of 24: Programming Language in Excel NOTE: This section is about programming vocabulary. If you are looking for code (the words used to talk to Excel in VBA) go to VBA sections 10 to 25. In the universe of VBA for Excel (Macros) there is some vocabulary that you might want to adopt so that others can understand you and so that you can understand the others. Excel Macros: Short for " macro commands" . A series of instructions performed by Excel rather than you. VBA Procedure: A set of instructions that you want the computer to execute. In the old days when computers were small and slow and VBA was not well developed these instructions were called Excel MACROS. Code: When you are writing instructions in VBA it is said that your are coding or writing code. Show me your code and I'll show you mine. All over this website pieces of code are presented in bold green font. To write VBA code you will be using 5 types of components: objects, properties, methods, functions and statements Objects: They are the building blocks of your Excel projects they are (among others) the Application (Excel), the Workbooks, the Worksheets, the Cells and the Ranges, the Charts, the Drawings, the Controls (command buttons, text boxes, list boxes, etc.) and others. Properties: Think of the verb to be. All objects have properties that you can set and modify through VBA. The font can be bold (Selection.Font.Bold = True), the sheet can be visible or not (Sheets(" SuchandSuch" ).Visible = True), a workbook has a name (ActiveWorkbook.Name), a cell can carry a value or a formula (Activecell.Value = 10, Activecell.Formula = " =A1+B2" ), etc. The set of properties differ from one object to the other. A worksheet cannot be bold and a workbook cannot carry a formula. Excel will tell you when you are trying to use a property that doesn't exist for the object that you are working with. Methods: Think of the verb to do. You might want an object to close, to open, to be copied or pasted, etc. (ActiveWorkbook.Close, ActiveCell.Copy). Again

Excel will tell you when you are trying to use a method that doesn't apply to the object that you are working with. Functions: You can and you will use all the Excel functions within your VBA code but you can also use VBA functions like UCASE, LCASE, NOW(), etc. (Activecell.Value=NOW())... Statements: IF..THEN, DO...LOOP, FOR...NEXT, WITH...END WITH, EXIT FOR, EXIT DO, EXIT SUB Events: For a procedure to start and be executed an event must happen. One event that everybody knows about is the click on the button. Other events include opening a workbook, activating a sheet, modifying the value of a cell, etc... . A VBA project can comprise 4 types of components: a workbook, worksheets, modules and userforms Workbooks: A workbook is an Excel file (something.xls) also called spreadsheet. The object " ThisWorkbook" is the workbook within which the macro is created. The code ThisWorkbook.Close will close the workbook within which the active macro is running. Worksheets: An Excel workbook can comprise 256 worksheets each having 65,536 rows and 256 columns. In a future version of Excel there will be 1,000,000 lines per sheet. Modules: It is kind of a file in which you store most of your VBA procedures (macros). Modules are created and named in the Visual Basic Editor. UserForms: They are specialized sheets that you create to allow the user to submit parameters (values). They are used extensively when working with VB but much less with VBA for Excel. They are also used extensively in databases, accounting, manufacturing and sales programs because there are no regular sheets in these environments. When you work with Excel it is so much easier to ask you users to submit parameters on a regular worksheet. And then a final word Controls: They are the command buttons, the check boxes, the labels, the text boxes, the list boxes, the option buttons and other gizmos that you put on userforms or worksheets. Example: Sub proTest () Range(" A2" ).Value= 2

ThisWorkbook.Close Application.Quit End Sub These four lines constitute a procedure (macro) named " proTest" . Always name your macros starting with the prefix " pro" and a first capital letter for any significant word comprised in the name like " proInfo" , " proRawData" , " proWhatever" . You will see later how important this prefix and capital letter habit becomes. " Range(" A2" )" , " ThisWorkbook" and " Application" are objects, " Value" is a property and " Close" and " Quit" are methods. This VBA procedure will be assigned to a button (control) and when a user clicks on it (the event) the VBA procedure will run. This VBA procedure tells the computer that when the user clicks on the button, cell " A2" takes a value of 2 and then you will be asked if you want to save the workbook , the workbook and Excel are closed. VBA Chapter 3 of 24: The Visual Basic Editor in Excel (VBE) When you want somebody to do some work for you you open your Email program and you send him a message in a language that he understands (English, Spanish, French...). When you want Excel to do some work for you you open the Visual Basic Editor and you write the instructions in a language that Excel understands VBA ( Visual Basic for Application). You will develop, test and modify VBA procedures (macros) in the Excel Visual Basic Editor (VBE). It is a very user friendly development environment. The VBA procedures developed in the Excel Visual Basic Editor become part of the workbook in which they are developed and when the workbook is saved the VBA components (macros, modules, userforms. etc.) are saved at the same time. Print this page and Open Excel In Excel open the workbook that you have just downloaded. You have just opened Excel and at the same time you have opened the Visual Basic Editor (VBE). It is not yet visible though. The VBE is integrated into Excel and you can open it from the Excel menu bar " Tools/Macro/Visual Basic Editor" . From the VBE you can go back to Excel by clicking on the Excel button at the top/left of its screen . So if the VBA toolbar is visible in Excel (View/Toolbar/VBA) you can . navigate from VBE to Excel using the two buttons The Excel Visual Basic Editor

When you work with the VBE there always should be 3 windows that are opened in it. The Project window (1), the Code window (2) and the Properties window (3). You probably don't see the 3 windows on your screen right now. We will fix that below.

In the menu bar of the VBE choose " View" and select " Project Explorer" and the Project window (1) opens. Then go back to the menu bar and select " Properties Window" and the Properties widow (3) opens below the Project window. You don't call the Code window (3) from the menu bar. It opens when you double click on any element of a project in the Project window. Double click on " Sheet1" in the Project window. You will discover more about each of these windows in sections 3A, 3B and 3C. You will learn how to create, modify and test your VBA procedures. You will even se how you can test your procedures step by step with the F8 key whle seing it live at work in your Excel workbook. You can resize the 3 windows by placing the cursor over the borders (*) and dragging them right, left, up or down. The VBE Menu Bar Here is the menu bar of the Visual Basic Editor. There are just a few functions that you will use and here they are.

As seen above at the very beginning of you programming career you will go to the " View" item to activate the Project Explorer and the Properties window. Activate also 2 toolbars: the Standard Toolbar and the Edit Toolbar by going to " View/ Toolbars" . You should never have to go back there ever. Once in a while you will be using " Debug/Clear All Breakpoints" to remove the breakpoints that you have set. You will also go to " Tools/VBAProject Properties/Protection" to protect you VBA code with a password. You won't really be using anything else from the menu bar. The VBE Toolbars Here are the two toolbars that should always be visible at the top of the VBE. The " Standard" toolbar (top) and the " Edit" toolbar (bottom).

From the Standard toolbar you will be using the Undo/Redo arrows like you do in Excel to correct an error that you have just made while writing in the code window. You will also use the " Run" arrow to start a procedure. You first click within the procedure in the code window and you click on the blue arrow You will also use the " Reset" icon of the toolbar. to terminate a step by step execution. to

When you start working with userforms you wil use the " Toolbox" Icon call and hide the " Toolbox" You will use the object browser explained below.

to find objects, methods and properties as

Finally if you click anywhere in the code window, this tool will tell you on what line you are. So if you are talking with somebody about your code on the phone you can tell your correspondent to select the same line. From the Edit toolbar you will be using two tools. First the tool that allows you to indent lines or segments of your code to make it clearer to read . You will

also use the comments tools that automatically adds apostrophes to the line or the lines that you have selected. Adding an apostrophe to a line transforms it into a comment that is not executed. The more comment lines the easier you or the ones that will follow you will be able to read and understand you code. The VBE Object Browser So when you click on the icon for the object browser the following window appears covering the whole code window. When the window is open enter a word in the text box beside the binoculars and the search results appear in the " Search Results" window. Select one of the line in this window and click on the F1 key. The help window opens and you get all kinds of information on the topic that you have chosen. To close the object browser window just click on the X (top/right) and the code you were working on comes back.

In the sub-sections (3A, 3B and 3C) you will discover the Project Window, the Properties Window and the Code Window. For a better comprehension of these section www.excel-vba.com has created " excel-visual-basiceditor.xls".

A final note: Unfortunately the wheel on your mouse doesn't work in the VBE VBA Chapter 3A of 24: The Project Window in the Visual Basic Editor of Excel Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. The Project window shows you all the workbooks that are open.

If you click on the minus (-) signs in the Project window you see the objects that are part of the different workbooks (projects). There are Sheets in a workbook, there is always the ThisWorKbook object (in which you store the procedures that you want to start automatically when the workbook is opened) and, there can be Forms (see sections 23 to 25) and Modules (files in which you write and regroup your VBA procedures (macros) one or many per module). In the picture above you can see that the project " Book1.xls" comprises 3 sheets and ThisWorkbook. " vba-tutorial-editor.xls" has 6 sheets, one userform, two modules plus the " ThisWorkbook" object. If in the project window you double click on a sheet, on ThisWorkbook or on Module1 you will see two things happening. In the " Property window" you see the properties of the selected objects and in the " Code window" you see the code related to the selected object.

In " vba-tutorial-editor.xls" you will see that there is code in both modules. Single click on " modTest" in the Project window and notice that there are 2 procedures in the module. We will test " proVisible" at the end of secion 3A and we will test " proTest" in section 5 Single click on " Module1" in the project window. There is a recorded macro there that we will be modifying in section 6 of this tutorial. You can add forms and modules to any workbook by right clicking anywhere within one of the projects and select " Insert" in the contextual menu. You can now insert modules and userforms...forget about the Class Module.

Right click within the project " vba-tutorial-editor.xls" and add a userform and see what happens.

A form (UserForm2) has been added to the list of objects of the project and the form itself plus the toolbox appear in the code window. If the " Toolbox" window doesn't show use its icon from the toolbar at the top to call it . You will discover in sections 23, 24, 24A, 24B and 25 the wonderful world of userforms. You create them to get input from your users. Double click on " Sheet1" in the project window and the form disappears. Double click on " UserForm2" in the project window and the form comes back. You can copy a module or a userform from one workbook to the other by simply left clicking on it, holding and dragging it to the other project. Try it, drag Module1 of " vba-tutorial-editor.xls" to " Book1.xls" . By doing this you make all the VBA procedures developed in " vba-tutorial-editor.xls" part of " Book1.xls" . Do the same with " UserForm2" . Now go to Excel and close " vba-tutorial-editor.xls" so that only " Book1.xls" is open.

Select cell " A1" and click on " SHIFT/CTRL/A" . The macro from " vba-tutorialeditor.xls" now works in " Book1.xls" . Close " Book1.xls" and re-open " vba-tutorial-editor.xls" . Go to the VBE and let's discover the other windows. VBA Chapter 3B of 24: The Properties Window in the Visual Basic Editor of Excel Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. The Properties window shows you the properties of the object that is selected in the Project Window or of the control that you have selected on a userform. Click once on any object in the Project window and its properties appear in the Properties window. You can modify any of the properties from the properties window. The list of properties changes as you select different types of components (ThisWorkbook, sheets, modules, userforms or controls). You can set the beginning properties in the Properties window and change them later with VBA with lines of code like: Sheets(" Test 1" ).Visible = xlVeryHidden Sheets(" Test 1" ).Visible = True Sheets(" Test 1" ).Visible = False For example, click on " Sheet1" in the Project window and you will see the following properties for the sheet: There are 3 properties that you will work with: (Name) property which is the VBA name of the sheet, Name property which is the caption (the name on the tab in Excel) and the Visible property where you make a sheet either -1-xlSheetVisible, -0- xlSheetHidden or....yes yes yes...-2- xlSheetVeryHidden. Click on Sheet3 of " vba-tutorial-editor.xls" and see that it is very hidden see also that Sheet2 is hidden and that the other sheets are visible.

The " VeryHidden" property is very useful. You make a sheet very hidden if it contains information that you want to hide to the user. He can only access this sheet through the VBE and if the procedure is protected by a password he can't get to it. To change any property click in the text box of the property. If you click on the text box of the Visible property of Sheet1 you will see this:

The small arrow reveals a drop-down list with the three possible values for this property. Select any and go to Excel to see that the property of Sheet1 has changed.

Click on Module1 of " vba-tutorial-editor.xls" in the Project window and your property window will look like this one.

There is only one property, the name. Please, please, please name your modules. There is nothing less informative than Module1, Module2, Module3.... It is so much friendlier to see modImport, modDatabase, modExportData, modWhatever. Give a significant name to your module starting with the prefix " mod " and a first capital letter for any significant word comprised in the name. You will see later how important this prefix and capital letters habit becomes. To see the properties of userforms and their controls see section 23 to 25. If you click on ThisWorkbook in the Project window you will see this:

Unless you are a " real" programmer who is trying to reinvent the wheel you will not work with these properties. The ThisWorkbook object (as you will see in the section on events) is where you store the procedures that you want executed when the workbook is opened. You could have there a line of code like: Sheets(" Introduction" ).Select Range(" A1" ).Select If you want you workbook to always open showing the sheet " Introduction" with the cell " A1" selected. You can decide to show a message box or a userform when the workbook is opened and you can even develop very complex procedures like importing a new set of data from the central database or the WEB to make sure that any analyses or reports that is created by the workbook is using the most recent data. VBA Chapter 3C of 24: The Code Window in the Visual Basic Editor of Excel Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. 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. 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. If there were many procedures in the

module you could use the drop-down list at the top right (" Declaration" ) to get to other procedures. In the window below you see the code created with the Macro Recorder in " vbatutorial-editor.xls" .

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 the left DDList is useless for modules. You will use this 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 . 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 . 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:

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:

The is really nothing that is interesting in this menu. You will create you first VBA procedure in this window in chapter 4 and you will use these tips when you test your procedures in chapter 5. VBA Chapter 4 of 24: Creating Macros in the Visual Basic Editor for Excel Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. Now that you have discovered the Visual Basic Editor and its 3 windows you can create your first VBA procedure (macro). Open Excel and a new workbook. In cells " A1" to " A5" of the sheet " Sheet1" enter five first names. In cells " B1" to " B5" enter 5 last names. You can enter 5 names or even more the procedure will stop when the cell in column " A" is empty. Go to the Visual Basic Editor. First let's add a new module. Go to the VBE and right click in the Project window . Select " Insert/Module" . You now have a new module. In the properties window double click on the " (Name)" property box and enter " modTest" . We now have a new module with a name.

In the VBAProject window double click on the new module " modTest" " and let's go to the code window to develop a macro. Write " Sub proTest ()" and click " Enter" . VBE adds a line " End Sub" . We will write our code between these two lines so make some room for it by setting the cursor at the beginning of " End Sub" and clicking " Enter" a few times. Just below " Sub proTest()" write your name preceded by an apostrophe (') ' Peter Clark and click " Enter" . Notice that the font in the line you have just written is green. Because of the apostrophe VBA will consider the text as a remark (REM) and will not bother with it. You can add any number of REM lines anywhere to make your code easier to understand. It is recommended to always start procedures with three Rem lines (your name, your phone number and your Email address or the address of your website) so that people can get in touch with you if they experience problems with your VBA procedure. Click " Enter" again to insert an empty line. Do not hesitate to insert empty lines anywhere to make your code more easily readable. Here are the 6 lines of code that you will be writing (or copy/paste them from this webpage): Sheets(" Sheet1" ).select Range(" C1" ).Select Do Until Selection.Offset(0, -2).Value = " " Selection.Value = Selection.Offset(0, -2).Value & " " & Selection.Offset(0, -1) Selection.Offset(1, 0).Select Loop Range(" A1" ).Select The first 2 lines tell VBA where to start the procedure. If you don't mention it VBA will start the procedure from whatever cell is selected in your workbook. Line 3 to 6 is the actual task that you want VBA to perform. You are saying: do something until the value of the cells 2 columns left of the selected cell in empty (double quotation marks). You are saying: do and redo until there is no first name on the row that is selected. What is the task? 1- To assemble in column " C" the first name from column " A" and the last name from column " B" with a space in between.

2- Then move down one cell. When the task has been accomplished go to cell A1. Now go back to Excel go to " Tools/Macro/Macros" select the macro " proTest" and click " Run" . Erase column C and do it again. Add first names and last names to your list and do it again. Congratulations you have created your first VBA procedure. You will find this same procedure in the spreadsheet " vba-tutorial-editor.xls" . A command button on the sheet allows you to start it by clicking on it. Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. In no other programming environment can you test a procedure while seeing it at work step by step. Here is how it works Print this page and follow the instructions. Close every program on your computer. Open Excel and one of the workbooks mentioned above. Open the Visual Basic Editor. On the Window status bar at the bottom of your screen you can see that Excel is open and also the VBE. Right click on the status bar in the empty space where I have added the orange stars:

The following menu appears:

Click on " Tile Windows Vertically" and the screen will look SOMEWHAT like this. Yours will be full screen.

The Excel workbook occupies half of the screen and the VBE occupies the other half. Excel can be in the left half of the screen or the right half. Make the Code window wider in the VBE so that you can see most of the code (see how in section 3 of this website). Testing and modifying a VBA procedure In the Excel half of your screen select the sheet " Test 2" Testing the entire procedure Click anywhere within the procedure " proTest" in the VBE and click on the " Run" icon: In this case you probably didn't see much happening in the VBE window but you can see in the Excel window that the procedure has been executed. Delete all values in column C for the next exercise. Testing the entire procedure step by step

1- Click anywhere within the macro " proTest" and press the F8 key at the top of your keyboard. The fist line of code turns to yellow and a small arrow appears in the margin. We are in the execution process.

NOTE: While you are running the macro step by step you can stop the execution at anytime by clicking on the stop button in the toolbar. The line that is highlighted in yellow is the line that will be executed next time you click on F8. 2- Click again on F8 until the macro has been executed (no more yellow line). Watch the Excel screen at each step and see what happens. Modifying the procedure while testing the entire procedure step by step You can modify the code while running the procedure step by step. Follow the instructions below: 1- In the Excel window delete all the values in column C. 2- Click anywhere within the macro " proTest" and press the F8 key. 3- Click 4 more times until you reach the following line:

4- In the line above change C1 to C3 5- Click on the small yellow arrow in the margin with the left button of your mouse, HOLD the button down and drag the yellow arrow back up on the line that you have just modified. 6- Click F8 again. 7- Finish running the procedure with the F8 key or the " Run" icon . See that the procedure has worked only from cell C3 down. You have modified a procedure while running it. 8- Cancel the change that you have made in the code, change " C3" back to " C1" Delete all values in column C for the next exercise.

Testing part of the procedure You can test only part of a procedure. For example: 1- Click F8 to start the execution. Drag the yellow arrow to line 11:

2 - Click F8 one more time and then click on the stop button of the toolbar

You have executed just part of a procedure. Delete all values in column C for the next exercise. Now let's do it another way. 1- Click anywhere within the procedure " proTest" and click on F8 only one time. 2- Right click in the gray margin at the level of line 11 (same as the exercise above) and select " Set Next Statement" in the contextual menu. 3 - Click F8 one more time and then click on the stop button of the toolbar .

You have executed just part of a procedure. Delete all values in column C for the next exercise. Start your test there. When you have a long procedure you can test the top part of the procedure the bottom part or any intermediate part using breakpoints and dragging the yellow arrow around. Let's test the top part: 1- At the level of the line 13 " Loop" left click in the gray margin and the line is highlighted in red/brown. This is a breakpoint.

2- Click anywhere within the procedure " proTest" and click on the " Run" icon:

3- When the line with the breakpoint also carries the yellow arrow

In the Excel window you can see that all the lines above have been executed. One name has been created in cell C1 and cell C2 is selected.

4- Stop the execution with the icon next exercise.

and delete all values in column C for the

Note: You remove the breakpoints by clicking on the brown dot or if you have many breakpoints you can go to the menu bar " Debug/Clear All Breakpoints" Now let's test the bottom part 1- In the Excel window select cell C4 2- Set a breakpoint on line 12: 4- Click anywhere within the procedure " proTest" and click on F8 only one time 5- Click and drag the yellow arrow to line 12 (where the breakpoint is). 6- Finish running the procedure with " Run" icon selected. . See in Excel that cell A1 is

You have executed the bottom part of your procedure VERY IMPORTANT NOTE: When you start working with variables in VBA you need to declare them before you move on to any other lines of code that you want to execute. You click on F8 once, you click on F8 a second time and the yellow arrow moves down to the first line of code after the Dim's. From there you can start moving around. That is why it is recommended to " DIM" all you variables at the beginning of the procedure. VBA Chapter 6 of 24: VBA Security and Protection in Excel Sometimes you send a workbook with macros to a colleague. If he can't get them to work it is probably because his security setting is at " High" . Just tell him how to change it in " Tools/Macros/Security" . An Excel file (.xls) cannot be infected by one of these viruses that appear regularly on the Internet but somebody can develop VBA procedures (macros) that can harm your data and your computer seriously. So set the security level of Excel to " Medium" (Tool/Macro/Security) and each time you open a workbook that contains macro the following dialog window will appear.

Adopt the same attitude as you have with documents attached to Emails. If you know the origin of the file you may enable the macros if not click on " Disable Macros" and you are fully protected. You can look at the workbook but the VBA procedures (macros) are not operational. You can go to the Visual Basic Editor to take a look at the macros and if they don't look suspicious close the workbook and reopen it enabling the macros. If in any of the modules or other elements of the workbook you see " API Function" it means that the programmer is trying to access your computer through Microsoft Windows THAT IS SUSPICIOUS. Protecting the code As an Excel-VBA Developer you might want to protect your code so that nobody else may modify it. In the VBE editor go to " Tools/VBAProject Properties/Protection" . Check the box and submit a password. Make sure that you save the password somewhere you will remember because cracking VBA passwords is very expensive. Protecting the Workbook There are many levels of protection that you can set for the workbook. First you might want to forbid anybody from opening the workbook unless they know a password. To do so in Excel go to " File/Save As" and click on " Tools/General Options" .

...Discover More in the Downloadable Tutorial


What more in the Spreadsheets
You can ask users to submit a password to run certain macros. Develop a userform with a text box and a command button. In the text box you can modify the " PasswordChar" property so that when you enter the password nobody around can

read it. Use an asterisk, an ampersand or any other character. See how it s done in "vba-form-password.xls" In one of your workbook you might want to hide a sheet that contains confidential information (salaries and other parameters). If you just hide the sheet users can unhide it. There is a VBA way to hide a single sheet without protecting the entire workbook. To view the sheet one has to go to the Visual Basic Editor and change the property of the sheet. If your code is protected, he also needs the password. So the sheet is hidden and only you can get to it to modify its content. See how it s done in "vba-tutorial-editor.xls"

VBA Chapter 7 of 24: The Excel Macro Recorder Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. One of the tool that makes the programming environment in Excel the most user friendly is the Macro Recorder. In this section you will work with the Macro Recorder and you will run the macro that you have recorded. With the Excel macro recorder you cannot develop a macro that will damage Excel or your computer. The bolder you are in your trials the more you will learn. Even after 10 years of programming you will still use the macro recorder daily. Not to learn anymore but to write code (VBA words and sentences) for you. Would you rather write the following code or copy/paste it from a recorded macro? Selection.Replace What:=" :" , Replacement:=" " , LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False The Excel macro recorder is the best teacher that you can have and will remain the best assistant for the rest of your VBA developer's life. The Excel Macro Recorder has tendencies to overdo it sometimes. We will see in section 8 how to modify a recorded macro. Print this page and follow the instructions step by step. IMPORTANT NOTE: To be able to run macros or develop them in Excel the level of security of you version of Excel must be set to " Medium" . From then on every time you open a workbook you will be asked if you want to "

Enable Macros" . To modify the security setting of Excel go to " Tools/Macros/Security" and follow the instructions. 1- Open Excel. 2- On the menu bar go to " Tools/Macro/Record New macro" and the following dialog window appears:

In the window you see that a macro called " Macro1" will be created. You can change the name if you want. In " Store macro in:" is a list of all the spreadsheets that are open, select " This Workbook" which is the workbook in which you are actually workings. You can also write a comment in " Description" . At this point you can choose to attach the macro to a key from your keyboard. If you do so it means that to get Excel to run the macro you will just have to click on the set of keys that you have chosen. For the purpose of this exercise enter capital A in the " Shortcut Key:" text box. Later you will be able to run the macro by just holding the CTRL and the SHIFT keys and clicking on A. (NOTE: Always select a capital letter so that you don't deactivate important functions that are already attached to some lower case letters by Excel). If you don't link your macro to a combination of keys now you can do it later. When you click on " OK" Excel will record a macro named as you specified and attach it to the workbook that you are working in. When you save the spreadsheet that you are working in the macro will be saved at the same time. It also means that Macro1 will disappear from you computer when you delete the spreadsheet in which it resides. In section 4 you will see how easy it is to transfer a macro to any other workbook. So you can create and try your macros in one workbook and when it works you can transfer it to " Your macros" 0 workbook or any other workbook. 3- Click on " OK" . The following toolbar might appear in the middle of the screen. You can click on the " X" for now we will talk about it later.

4- Let's now record a macro with the Excel macro recorder: Select cell A1 and enter 34 Select cell A2 and enter 55 Select cell A3 and enter the formula =A1 + A2 Select cell A2 and change the color of the font to red Select cell A1 and change the background color to blue Select cell A3 and change the size of the font to 24 5- Go to the menu bar " Tools/Macro/Stop Recording"

The macro has been recorded by the Excel macro recorder now let's run it 6- There are two ways to run the macro. If you have entered a letter in the " Shortcut Key:" text box at the beginning you can just hold the CTRL and the SHIFT keys and click on the letter that you have chosen (A). You can also go to the menu bar " Tools/Macro/Macros..." select " Macro1" in the list and click on " Run" . The macro is executed. 7- Go to Sheet2 and run the macro again. You see that you can run a macro on any sheet of the workbook. 8- Keep this workbook open and open a new workbook. Make sure cell " A1" is selected and run the macro again (SHIFT/CTRL/A). You can see that the macro that you have created will run in any spreadsheet as long as the workbook in which it has been created is open. 9- Close the workbook (save it or not) in which you have created the macro. Open a new workbook and try to run the macro again SHIFT/CTRL/A. It doesn't work because the macro only runs when the workbook in which it resides is open. Go to the menu Tools/Macro/Macros. Macro1 is not there anymore so you can't run it and the shortcut key is free. Remember that a macro can be run in any workbook as long as the workbook in which it has been created is open. You have done it, you have recorded your first macro with the Excel macro recorder and used it, congratulations. Second Exercise For this second exercise u se the spreadsheet " vba-tutorial-editor.xls" . Macros will not run on protected sheets, hidden sheets and very hidden sheets. 1-Close all workbooks and open the workbook " vba-tutorial-editor.xls" . While you are on the sheet " Introduction" try clicking " SHIFT/CTRL/A" or run " Macro1" from the menu bar " Tools/Macro/Macros" and you will get the following window telling you about a bug:

Click on " End" for now. You have just learned that a macro cannot run if the sheet is protected. In section 13 on " Worksheets" you will learn how to programmatically protect and unprotect the sheets. For now go to " Tools Protection/Unprotect Sheet" . Run the macro again and it works 11-You can run the macro on sheet " Test1" but there are 2 other test sheets in this workbook one named " Hidden" and the other one named " veryHidden" . 12- To run the macro on the sheet " Hidden" you must first unhide it . In Excel go to the menu bar " Format/Sheet/Unhide" , select cell " A1" and run the macro again. In section 13 on " Worksheets" you will learn how to programmatically hide and unhide the hidden sheets and the " very hidden " sheets" . 13- You cannot unhide the " veryHidden" sheet from Excel so you cannot run the macro on it. This type of very hidden sheets is used a lot to hide price lists, salary lists and other confidential information. Even if you go to " Format/Sheet/Unhide" you will not see its name. You can unhide that kind of sheet manually in the Visual Basic Editor only if the project is not password protected. Learn about this precious sheet status in section 13 on " Worksheets" . 14- Delete column A on the sheet " Test1" . Select cell B6 and run " Macro1" . You see that cell B6, A2 and A3 are modified. In the next sectionsyou will discover how to modify your recorded macros VBA Chapter 8 of 24: Modifying Macros in the Excel Visual Basic Editor Note: The images and the text below refer to the workbook " vba-tutorialeditor.xls" that you can download from above. Let's begin by modifying the VBA procedure that we have recorded earlier (Macro1). Open Excel, open the workbook " vba-tutorial-editor.xls" and go to the Visual Basic Editor. Double click on Module1 in the Project window and the following code will appear in the Code window.

Click in the code window and print the module for future reference " File/Print/Current Module"

Print this page and follow the instructions step by step. The Macro Recorder (MR) has a very special way to write code. The procedure above works well so we could leave it alone. But just to make it clearer let's make it simpler.

First let's discover a neat trick. Click on the line that reads Selection.Font.ColorIndex = 3 between the Color and Index. Look at the " Standard" toolbar at the top of your screen and you will see this:

This means that the cursor is on line 15 column 25 of the module. This functionality is very useful when you talk with somebody else on the phone about your procedure. The first line of code reads like this:: Range(" A1" ).Select We then entered 34 in it. The MR writes ActiveCell.FormulaR1C1=" 34" You will rarely use the FormulaR1C1 property. You can replace this line simply by: ActiveCell.Value=34 note that there are no apostrophes around 34. This means that 34 is a number. You would need apostrophes if you were entering a text in the cell like " Peter" or a serial number like " 3452945" . Line 2 and 3 of the procedure read like this: Range(" A2" ).Select ActiveCell.FormulaR1C1 = " 55" There is no need to select a cell before you enter a value or a formula in it. Again we will avoid the FormulaR1C1 thing and we wont select the cell we will just give it a value so replace lines 2 and 3 by this: Range(" A2" ).Value=55 Remember that you don't need to select a cell to give it a value Line 4 and 5 of the procedure read like this: Range(" A3" ).Select ActiveCell.FormulaR1C1 = " =R[-2]C+R[-1]C" The form " =R[-2]C+R[-1]C" is called a relative formula. You will only use this format in certain tight spots. R[-2]C basically means 2 rows up same column. Again we will avoid the FormulaR1C1 and again we wont select the cell we will just enter a formula so replace lines 4 and 5 by this: Range(" A3" ).Formula=" =A1+A2" Line 6 and 7 of the procedure read like this: Range(" A2" ).Select Selection.Font.ColorIndex = 3 Here we wont select the cell we will just specify that we want to change the color of the font so replace lines 6 and 7 by this: Range(" A2" ).Font.ColorIndex = 3

Three is red. I always use the MR in these situation because I don't feel like remembering all the color codes. Line 8 to 12 of the procedure read like this: Range(" A1" ).Select With Selection.Interior .ColorIndex = 41 .Pattern = xlSolid End With The MR uses a lot of " With..End With" but I don't. The code developed by the MR would read in plain English: Select cell A1 (cell A1 becoming the Selection) then for the Selection.Interior make the ColorIndex 41 and the Pattern xlSolid. Here is a simpler version so replace lines 8 to 12 by this: Range(" A1" ).Interior.ColorIndex = 41 Range(" A1" ).Interior.Pattern = xlSolid Line 13 to 25 of the procedure read like this: Range(" A3" ).Select With Selection.Font .Name = " Arial" .Size = 24 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Again MR uses " With...End With" and also modifies ALL the properties of the font each time. We just want the font's size to be changed to 24 so replace lines 13 to 25 by this: Range(" A3" ).Font.Size= 24 Let's change the name of the VBA procedure to " proTest1" and remove the comments. It now looks like this: Sub proTest1() Range(" A1" ).Select ActiveCell.Value = 34 Range(" A2" ).Value = 55 Range(" A3" ).Formula = " =A1+A2" Range(" A2" ).Font.ColorIndex = 3 Range(" A1" ).Interior.ColorIndex = 41

Range(" A1" ).Interior.Pattern = xlSolid Range(" A3" ).Font.Size = 24 End Sub VBA Chapter 9 of 24: VBA Events in Excel When does the VBA procedure (macro) start? When an EVENT happens. The event is what triggers the VBA Excel procedure. Clicking on a text box on the worksheet 95% of the VBA procedures that you develop are triggered by a click on a text box located on a worksheet. I prefer using text boxes rather than VBA command buttons because they are much easier to maintain and allow much more creativity in the design. You can use the font that you like and the background color that fits your needs. If you are a little creative you can add 3D effects, special borders and the likes. A few note on Excel text boxes: I always keep the Excel drawing toolbar visible at the bottom of my screen

You create text boxes by a left click on the icon , let the button go, then go to the worksheet left click, hold and stretch the text box. When the border of the active text box is made of diagonal lines you can work the text inside the text box. If you click again on the border it becomes a set of dots and you then can work the text box itself. Right click on the border in any of the two states and you will see that the menus are different.

First you develop a macro in a module in the VBE. Then you click ob the text box and when the border becomes a set of dots right click on it and select " Assign a macro" . Select a macro from the list that is offered to you. A simple macro to call a userform would look like this: Sub proUserFormWeighing() frmWeighing.Show End Sub

You can assign a VBA macro to a text box and also to a WordArt, a picture or any other shape from the " Drawing" toolbar. Once a button (image, word art or text box) has been assigned a macro or an hyperlink you need to select it with a right click to modify it. Download one of these buttons (right click on it in your browser and choose " Save image as" ). Save it on your desktop:

Insert the image that you have imported on the first sheet " Insert/Picture/From File/Desktop/..........gif" . Once the image has been added to the sheet, right click on the image, select " Assign Macro" and select a macro from the list. Click " OK" . Now click on the image. You can " borrow" all kinds of buttons from the Internet or create your own from the " Design" toolbar and use them as triggers for your VBA procedures. Events related to the worksheet or the cell In the VB editor double click on " ThisWorbook" in the project window. At the top of the code window select " Workbook" in the left drop-down list and in the right one you will see 28 different events. Choose one and develop your procedure. The event that is used most of the time is the " Open" event. If for example you want your workbook to open on the sheet " Introduction" cell " A1 the code will be: Private Sub Workbook_Open() Sheets(" Introduction" ).select Range(" A1" ).select End Sub Events related to the worksheet In Excel right click on the tab of any sheet and chose " View Code" or double click on the name of any sheet in the project window of the VBE. At the top of the code window select " Worksheet" in the left drop-down list and in the right one you will see 9 different events. Choose one and develop your procedure. Remember that any procedure that you develop under the event " Activate" or any other will run EACH time that the event happens. Developing procedures triggered by events of the worksheet is tricky and I don't personal develop too

many. There are 2 events under worksheet that are really events related to cells: Change and Calculate. Events related to the cell There are not realy an event related to the cell but two events related to the sheet can be use to trigger macros when something happens in a cell. The events are " Change" and " Calculate" . Private Sub Worksheet_Change(ByVal target As Range) If target.Address = " $A$1" Then MsgBox " OK" If target.Column = " 1" Then MsgBox " OK" If target.Row = " 1" Then MsgBox " OK" If target.Row = " 1" Or target.Row = " 2" Then MsgBox " OK" If target.Value = " 1" Then MsgBox " OK" End Sub A message box saying " OK will appear when you enter any value in cell A1 (first " If" ), when you enter any value in the first column (second " If" ), when you enter any value on the first row (third " If" ), when you enter any value in columns 1 or 2 (forth " If" ) and if you enter 1 in any cell (fifth " If" ). Private Sub Worksheet_Calculate() If Range(" A1" ).Value > 100 Then MsgBox " OK" End Sub Enter a formula in cell " A1" (like " =C5" ). When the result of the formula in " A1" is higher than 100 a message box says " OK" . Try entering 99 or 500 in cell " C5" . From the Menu In Excel you can run an Excel VBA macro by going to the menu " Tool/Macro/Macros.." then select the macro from the list and click " Run" .

Open many workbooks with macros in them. When you go to the menu " Tool/Macro/Macros.." you will notice that you have access to all the macros from all the open workbooks. This means that you can store ALL your useful Excel macros in a single workbook (call it myMacros.xls) and have access to them while the workbook is opened. Let's say for example that you have designed a macro that multiplies the content of a cell by 2. If " myMacros.xls" is open you can call this Excel macro from any cell in any other workbook that is open. No need to copy your essential macros in all your workbooks just open myMacros.xls and put them to work. Clicking on a Key of your Keyboard First you need to program a key. To do so go to " Tool/Macro/Macros.." then select a macro from the list. Click on " Options" and follow the instructions. A suggestion, assign your macros to upper case keys (" Shift/A" instead of " Shift/a for example) to make sure that you don't use one of the many lower case keys that are already used by Excel. You can also assign a macro to a key when you use the Excel Macro recorder. Other Events See sections 23 and 24 for VBA events related to userforms and controls VBA Chapter 10 of 24: VBA Code General Tips You have learned how to create VBA procedures (macros) in a previous section. In this section you will find many small ones illustrating the different objects,

methods and properties that you can use to make things happen in VBA for Excel. You will also discover statements, functions and variables. Let's start by a few general tips. - All VBA procedures (macros) start with Sub with a set of parentheses at the end Sub proTest() In the parentheses you will submit variables that you carry from one procedure to the other. I always use the prefix " pro" at the beginning of a procedure and I use upper case letters at the beginning of a new word in the name of the procedure like in proTest above or like in proAddData A VBA procedure always end with End Sub but you can always exit a procedure with: Exit Sub - All VBA sentences must be on a single line. When you need to write long sentences of code and you want to force a line break to make it easier to read you must add a space and an underscore at the end of each line. Here is an example of a single sentence broken into 3 lines: Range(" A1:E9" ).Sort Key1:=Range(" C2" ), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortTextAsNumbers - Do not hesitate to use the macro recorder to avoid typos. - Write your code in lower case letters. If the spelling is right, VBE will capitalize the necessary letters. If it doesn't.... check your spelling. - Note:Quotes within quotes must be doubled for example: MsgBox " My name is Peter" is OK and will result in: My name is Peter But if you want the name Peter to be between quotes you cannot write: MsgBox " My name is " Peter" " you must double the quotes: MsgBox " My name is " " Peter" " " - At a certain point you will realize how useful variables are in programming. When you start using variables always activate the " Option Explicit" . Although, you are forced to declare variables, there are many advantages. If you have the wrong spelling for your variable, VBE will tell you. You are always sure that your variables are considered by VBE.

Declare all your variables (Dim) at the beginning of the procedure, it will simplify the testing of your code. VBA Chapter 11 of 24: Error Handling in VBA for Excel There are two types of errors that can occur when working with VBA for Excel, the VBA errors and the Excel errors. The VBA errors are those caught by VBA that trigger the error dialog window and that are generated when there are typos or logical errors in the code. The Excel error are the ones that VBA doesn't catch and that you handle with " IF" statements. Handling Excel Errors An Excel error happens when for example you are asking a user to submit a number in cell A1 of a worksheet and that the number submitted is either too small or too large. In the following example you are expecting a user to submit a number between 100 and 200. If the value is smaller than 100 you want to tell the user but you agree to let the VBA procedure go on. If the value is greater than 200 you want to tell the user and stop the procedure. Here is the code: Sub proTestExcelErrors() If Range(" A1" ).Value < 100 Then MsgBox " Value submitted is to small." ElseIf Range(" A1" ).Value > 200 Then MsgBox " Value submitted is to large." Exit Sub End If ' here is the rest of the procedure. MsgBox " The procedure goes on." End Sub VBA for Excel Errors A VBA error happens when VBA doesn't understand or accept what you have written. A VBA error happens when: - the name of an object, a property or a method is misspelled, - an invalid method or property is called for a certain object. For example you cannot write ActiveSheet.Value because the sheet has no value. It has a name, it can be visible or not but it has no value. - an invalid value is sent to a variable. For example you cannot store a text (" Peter" ) in a variable that you have declared as Integer (Dim varVariable as Integer).

- a value is not found by the Find method ( Selection.Find...). For example, you are looking for " Peter" in column A but it is not there. - a variable has not been declared while working with the Option Explicit activated. - and more. It is extremely important to handle these errors for programmers who develop huge programs or video games that will be distributed on a large scale. That is why there are huge testing teams to test and try to find bugs in programs before they are released in the public. And even then.....Microsoft Internet Explorer....security bugs.... So the first step in handling errors is to test you program. When you are developing VBA procedures in Excel for yourself it is not really important to handle errors. You work with your program and correct them as they happen. If you develop programs for a few people with whom you have close links you can forget about errors also. But if you are developing programs for large scale distribution you must handle the errors remembering the it will add a lot to the cost of your application. You can even form you own testing team. VBA Errors Discovered while Typing the Procedure, while Trying to Run the Procedure or while Running the Procedure A lot of errors are caught by the Visual Basic Editor (VBE) and you are alerted not as you write the code but as you try to get to the next line while writing the code. The font of the line turns to red and a message box tells you what is wrong. Other errors are not detected as you write the code but as you try to run your VBA procedure or as you run it. While typing The VBE points at a lot of errors as you write the code. Try to write Range(" A1" .Value for example. You will be alerted to the fact that a parenthesis is missing with the message box saying " Expected: End of Statement" (image below). You get alerted for missing parentheses, double quotes, commas, periods and other " separators" . But VBE will not alert you for typos like Range(" A1" ).Value or anything that is between double quotes Range(" namedField" ).Value. The VBE won't alert you either if you misspell the name of a variable.

You correct these errors and then you can run your macros step by step with the F8 key or by going to the menu " Tools/Macro/Macros" . At this point other VBA errors will be identified by the VBE. While Trying to Run the Procedure A second set of errors is identified by the VBE as soon as you try to run the procedure. If there are some specific errors in the code the execution doesn't even begin. A different dialog window appears depending if you are running the procedure from the VBE, from Excel or from Excel when the procedure is password protected. The type of errors in this second category include the absence of End Sub, a End If missing, a Next or Loop missing an undeclared variable and some others. When these errors are identified by the VBE and the dialog window appears you don't have to worry about some of the code having been executed. The execution doesn't even start. If you were trying to execute the procedure from Excel and that the procedure is password protected (protection activated) the following type of message box appears.

Click on " OK" , go to the VBE, unlock the project and run the procedure. The first line of code is highlighted in yellow, the problem is highlighted in blue and a different message box appears. Fix the problem, click on the blue arrow on the toolbar to resume the execution or click on the blue square on the toolbar and restart from the VBE or from Excel.. if you were trying to execute the procedure from the VBE the following type of message box appears. If you were trying to execute the procedure from Excel and that the procedure is not password protected or the protection has been deactivated, the VBE opens and the following type of message box appears.

The first line of code in the code window of the VBE is highlighted in yellow and the problem is highlighted in blue. Fix the problem, click on the blue arrow on the toolbar to resume the execution or click on the blue square on the toolbar and restart from the VBE or from Excel. While Running the Procedure A third set of errors is identified by the VBE as it tries to execute a certain incorrect sentence in your procedure. This type of error includes when you are asking VBA to open a workbook that it can't find, when you are asking VBA to activate a sheet that doesn't exit or when you are using the Find method and the value that you are looking for is not found. From wherever the procedure has been started the following message box appears. If the procedure is protected by a password and the protection is active the " Debug" button is not active (gray font) in which case the only solution is to end the procedure at the point where the error was found. This means that part of the task has been accomplished with whatever problems this creates.

If the procedure is not password protected you can click on " Debug" . The VBE will open and the faulty sentence will be highlighted in yellow. Fix the problem, click on the blue arrow on the toolbar to resume the execution or click on the blue square on the toolbar and restart from the VBE or from Excel. If the procedure is being tested from the VBE you can click on " Debug" . The faulty sentence will be highlighted in yellow. Fix the problem, click on the blue

arrow on the toolbar to resume the execution or click on the blue square on the toolbar and restart from the VBE or from Excel. Handling VBA Errors Here is the basic way to handle an error. The first thing you create is an address where VBA will jump if there is an error. In this example the address is addJump with the NOT OPTIONAL colon at the end. Below addJump is what is supposed to happen of there is an error and above is the procedure that should run if there are no errors. Notice that the last line of code in the section above addJump is Exit Sub because if there are no errors you don't want the rest of the code to be executed. Example 1: Sub proTestErrorHandler() On Error GoTo addJump Workbooks.Open " xxxxxx" Exit Sub addJump: MsgBox " An error has occurred, call Peter at 1 514-257-0734" End Sub Example 2: Sub proTestErrorHandlerNoError() On Error GoTo addJump MsgBox " Everything is fine." Exit Sub addJump: MsgBox " An error has occurred, call Peter at 1 514-257-0734" End Sub Copy/Paste both examples in a module of your own and run them. The first example will generate a message box saying An error has occurred, call Peter at 1 514-257-0734 because an error has occurred. The workbook xxxxxx can't be found. The second example will generate a message box saying Everything is fine.

If you run the procedure without the error handler: Sub proTestErrorHandler() Workbooks.Open " xxxxxx" End Sub You end up with the following message box from VBA.

If you just want errors to be ignore you write On Error Resume Next at the beginning of the procedure. Copy/Paste the following procedure in a module of your own and run it. It will generate a message box saying An error has occurred but we have ignored it. Sub proTestErrorHandlerIgnore() On Error Resume Next Workbooks.Open " xxxxxx" MsgBox " An error has occurred but we have ignored it." End Sub VBA Chapter 12 of 24: VBA for Excel for the Application Application Application is a VBA object, IT IS EXCEL. Whenever you want Excel to do something or you want to change a property of Excel, you will use the object Application. Over 300 properties and methods apply to Application but here are the mot important ones. Quit The following line of code closes Excel altogether.

Application.Quit CutCopyMode After each Copy/Paste operation, you should empty the clipboard with the following line of code to make sure that the computer memory doesn't overload. ActiveSheet.Paste Application.CutCopyMode=False DisplayAlerts When you don't want Excel to ask you things like " A file already exists....." or " Do you wan to save this file..." you will use the following line of code at the beginning of your VBA procedure. Application.DisplayAlerts = False Then at the end Application.DisplayAlerts = True ScreenUpdating When you don't want to see your screen follow the actions of your VBA procedure, you start and end your code with the following sentences: Application.ScreenUpdating = False Application.ScreenUpdating = True Calculation When you are working with a workbook in which there are a lot of formulas and you want to temporarily deactivate the calculations you will use: Application.Calculation = xlManual (Don't forget the xl before the Manual) MAKE SURE that at the end of the procedure you add this line of code: Application.Calculation = xlAutomatic If during the execution of the procedure you want a single sheet to be calculated you will use: Activesheet.Calculate

...Discover More in the Downloadable Tutorial


What more in the Spreadsheets
With VBA you can develop an application that goes to the WWW each minute to extract financial data and create a database that you can analyse and use to generate a chart.

See how it is done in "vba-example-data-web.xls" 50 or more of your colleagues send you their data in the form of an Excel database. You need to open each of them and copy/paste the data in your own master database. Or you use DIR and VBA will go in the directory where these 50 workbooks reside, open each of them and create your master database. See how it is done in "vba-example-conso-databases.xls" 50 or more of your colleagues send you their data in the form of table in Excel. You need to open each of them and add the numbers to your own master table. Or you use DIR and VBA will go in the directory where these 50 workbooks reside, open each of them add the numbers to your master table. See how it is done in "vbaexample-conso-tables.xls"

VBA Chapter 13 of 24: VBA for Excel for the Workbooks To develop a VBA procedure that is triggered by an event relating to the workbook (when you open it, when you save it...) see the section on events. About the PATH Before we talk about workbooks let's talk about the path. The path is the directory that Excel points at when you use " File/Open" , File/Save As" or " File/Save" . If you open an Excel file from Explorer the " File/Open" path is the directory by default of your version of Excel (usually C:/My Documents) if you have not changed it " Tools/Options/Save" ). The " File/Save As" or " File/Save" path is the directory of the file you have just opened. On the contrary if you open Excel first and from there you open a file all paths are the directory of the file that you have just opened. This reality becomes a real concern when you start opening and saving more than one workbook within a VBA procedure. For example if you create a new workbook within a VBA procedure the line of code: ActiveWorkbook. SaveAs " Test.xls" will save the workbook in the default directory if you have opened the workbook containing the VBA procedure from Explorer but will save the workbook in the directory of the workbook that contains the VBA procedure if it has been opened through Excel. You might need to write: ActiveWorkbook. SaveAs ThisWorkbook.Path & " /" & " Test.xls" to save it in the same directory as the workbook containing the VBA procedure. If you want to change the directory at the beginning of the procedure you can use: ChDir ThisWorkbook.Path and " File/Open, " File/Save As" or " File/Save" will all point at the directory that contains the VBA Procedure.

You can also use the following approach to make sure where you save your workbooks or open them from: Sub proTest() Dim varPath as String varPath= ThisWorkbook.Path or you can open another workbook from the VBA procedure and set the path to this workbook Workbooks.Open " book1.xls" varPath=ActiveWorkbook.Path then you can open or save workbooks with these lines of code Workbooks.Open varPath & " /" & " book1.xls" Workbooks.SaveAs varPath & " /" & " book1.xls" End Sub The other important " objects" What you call a spreadsheet or an Excel file VBA calls it a workbook. So if somebody talks about workbooks you may suspect that this person knows about VBA. For very basic code working with only one workbook you need to know about the difference between ActiveWorkbook and ThisWorkbook. ThisWorkbook is the workbook within which your VBA procedure runs as the ActiveWorkbook is the workbook that is live on screen. There can be many workbooks opened at the same time but the ActiveWorkbook is the one that you see when you look at Excel. So you can understand that if ThisWorkbook is live on screen it is also the ActiveWorkbook and you can use either objects. There are many methods and properties that you can use with ActiveWorkbook and ThisWorkbook. You can Save, Close or Print the workbook with the following code: ThisWorkbook.Close this code will close the workbook and end the procedure that is running. ActiveWorkbook.Close If you want to close ThisWorkbook without saving it: ThisWorkbook.Saved = True ThisWorkbook.Close

If you want to close the active workbook without saving it: ActiveWorkbook.Saved = True ActiveWorkbook.Close You can save the workbook that is active whit this code: ActiveWorkbook.Save ThisWorkbook.Save When you want to save the active workbook under another name or in another directory you will use the following code: ActiveWorkbook.SaveAs " suchAndSuch.xls" but make sure that you have read the segment on Path above. To make sure that your workbook is saved in the right directory you can use both the name and the path: ActiveWorkbook.SaveAs " C:/suchAndSuch.xls" To print an entire workbook you will code: ActiveWorkbook.PrintOut or ActiveWorkbook.PrintOut Working with Many Workbooks and Files When you start working with more than the workbook in which resides your VBA procedure you need to add a few objects to your vocabulary: Workbooks and Windows. Opening another workbook To open another workbook within a VBA procedure you simply write (notice that " Workbooks" is plural): Workbooks.Open " suchAndSuch.xls" but make sure that you have read the segment on Path above. To make sure that you are looking in the right directory use both the path and the filename Workbooks.Open " C:\suchAndSuch.xls" You might not want to hard code the name of the file (so that if it changes you don't have to modify the code). Enter the name of the file in cell " A1" of sheet1 for example and write this: Workbooks.Open Sheets(" sheet1" ).Range(" A1" ).Value The way I prefer to do it is by storing the name of the file in a variable of the string type like this: Sub proTest() Dim varFileName as String

varFileName=Sheets(" sheet1" ).Range(" A1" ).Value Workbooks.Open varFileName End Sub You can also write the path in a cell and the file name in another cell: Workbooks.Open Sheets(" sheet1" ).Range(" A1" ).Value & Sheets(" sheet1" ).Range(" A2" ).Value You can also work with 2 variables for the path and the file name as shown above: Workbooks.Open varPath & varFileName For both approaches it is important to know if the final backslash for the path has been entered in the cell or in the variable (C:\Documents vs C:\Document\). You can either create a text box to ask the user to make sure that the final backslash is there or use an IF statement to avoid any problem. For example if the path is entered in cell A1 of Sheet1 this is the code: If Left(Sheets(" sheet1" ).Range(" A1" ).Value,1)=" \" than Workbooks.Open Sheets(" sheet1" ).Range(" A1" ).Value & Sheets(" sheet1" ).Range(" A2" ).Value Else Workbooks.Open Sheets(" sheet1" ).Range(" A1" ).Value & " \" & Sheets(" sheet1" ).Range(" A2" ).Value End If If you need to open a TXT, CSV or any other type of files you can use the macro recorder to do it and you will get something like this: Workbooks.OpenText Filename:= _ " C:\Book1.txt" , Origin:=xlMSDOS, _ StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _ Space:=True, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers _ :=True If I don't want the path and file name to be hard coded I use the same approach as shown above for Workbooks.Open using addresses or variables replacing " C:\Book1.txt" by the address or the variable name (no quotation marks). I then delete the ,TrailingMinusNumbers:=True because certain older versions of Excel don't understand this argument. Closing, Saving and Printing Workbooks

You have learned how to close the ActiveWorkbook or ThisWorkbook above but you can also close a workbook that is open but not active with: Workbooks(" Book1.xls" ).Close If you don't want to hard code the name of the file you can use the address of a cell in which you have entered the file name or a variable in which you have stored the file name as above with the " Open" method. Workbooks(Range(" A1" ).Value).Close Workbooks(varFileName).Close Notice the absence of double quotes in the last two sentences. You have learned how to save the ActiveWorkbook or ThisWorkbook above but you can also save a workbook that is open but not active with: Workbooks(" Book1.xls" ).Save If you don't want to hard code the name of the file you can use the address of a cell in which you have entered the file name or a variable in which you have stored the file name as above with the " Open" method. Workbooks(Range(" A1" ).Value).Save Workbooks(varFileName).Save Notice the absence of double quotes in the last two sentences. You have learned how to saveAs the ActiveWorkbook or ThisWorkbook above but you can also save a workbook that is open but not active with: Workbooks(" Book1.xls" ).SaveAs " Such and Such.xls" If you don't want to hard code the name of the file you can use the address of a cell in which you have entered the file name or a variable in which you have stored the file name as above with the " Open" method. Workbooks(Range(" A1" ).Value).SaveAs Range(" A2" ).Value Workbooks(varFileName).SaveAs varOtherFilename Notice the absence of double quotes in the last two sentences. To save an Excel workbook as TXT, CSV or any other type of file use the macro recorder and as shown above modify the code to suit your needs. To print an entire workbook you will code: Workbooks(" Book1.xls" ).PrintOut Moving around Workbooks When two workbooks are open and you want to move from one to the other you will use: ThisWorkbook.Activate and Windows(" OtherWorkbook.xls" ).Activate When I work with two workbooks I usually declare a variable in which I store the name of the second workbook and I move from one to the other with ThisWorkbook.Activate and Windows(varThatWorkbook).Activate. Here is an example:

Sub proTest() Dim varThatWorkbook as String Workbooks.Open " book1.xls" varThatWorkbook=ActiveWorkbook.Name and then I move from one to the other until I close the other workbook ThisWorkbook.Activate Windows(varThatWorkbook).Activate Windows(varThatWorkbook).Close End Sub VBA Chapter 14 of 24: VBA for Excel for the Worksheets To develop a VBA procedure that is triggered by an event relating to the worksheet (when you select it, when you leave it...) see the section on events. A sheet has two "Name" properties. When you click on a sheet's name in the VBAProject window of the Visual Basic Editor (VBE) each sheet has two properties called Name. One with parentheses and one without. A worksheet has two names the one that appears on its tab in Excel (let's call this property "caption" ) and the one it has as VBA object in the VBAProject window of the Visual Basic Editor (VBE) (by default: Sheet1, Sheet2....). So you can activate Sheet1 whose caption is "Balance" with both: Sheets("Balance" ).Select Worksheets("Balance" ).Select not forgetting the parentheses and the double quotes. Or you can select Sheet1 with: Sheet1.Select There are 2 reasons to prefer the third method. First there is less to key in and secondly if ever you or your user change the caption of the sheet there is no need to review and correct your code accordingly. To make your code clear rename the sheets in the Visual Basic Editor. Select the sheet in the VBAProject Window and modify the name (the one with parentheses) in the Property Window. In the example below the name of the sheet (as VBA object) is "shBalance" as for the caption, it can be anything else. Notice the "sh" prefix and the capital B. Again the prefix is important because you can have a variable called Balance (varBalance) a named field called Balance (nfBalance) so the "sh" makes it clear that you are working with a sheet. As for the capital "B" it is a protection against typos. When you write the code you don't

capitalise any letter. If it is written right VBA will automatically capitalise the right letter. So if you make a typo there will be no capital letter and you will know that the name is wrong. Then you can write: shBalance.Select Methods and Properties of the Worksheet When a sheet is selected in the VBAProject window you can see 11 properties of the worksheet in the Properties window of the VBE, properties for which you can set a default value to begin with and that you can modify through the VBA procedure whenever you whish. There are 3 properties that you will use frequently: the name (name within parentheses), the name (without parentheses) that is in fact the caption appearing on the sheet's tab in Excel and the visible property. As explained above you can change the (Name) if you are developing a workbook for others that might modify it in Excel. You cannot change the (Name) of a sheet programmatically. To change the caption you can either do it in the property window of the VBE or in Excel by right clicking on the tab then selecting " Rename" . Programmatically you can change the caption of a sheet with the following code: Sheets("Sheet1").Name= "Balance" You can programmatically hide or unhide a sheet with the following code: Sheets("Sheet1" ).Visible= True Sheets("Sheet1" ).Visible= False Remember that formulas in cells are calculated even if the sheet is hidden but that before you can do anything programmatically on the sheet you must unhide it: Sheets("Sheet1" ).Visible= True Sheets("Sheet1" ).Select Range("A1" ).Value=6 Sheets("Sheet1" ).Visible= False Remember that formulas in cells are calculated even if the sheet is very hidden but before you can do anything programmatically on the sheet you must unhide it: Sheets("Sheet1" ).Visible= True Sheets("Sheet1" ).Select Range("A1" ).Value=6 Sheets("Sheet1" ).Visible= True

Remember also that formulas on other sheets referring to cells of a hidden or very hidden sheet work even if the sheet is hidden or very hidden. If you want to hide many sheets at the same time you will use the following code: Sheets(Array("Sheet1" , "Sheet2" )).Select ActiveWindow.SelectedSheets.Visible = False Note that the names used in the array are the captions (the name on the tabs) and not the VBA name. You might want to delete sheets. Here is the code to do so: Sheets("Balance" ).Delete or shBalance.Delete You might also want to add one sheet. If you use the following code VBA will add a new sheet before the active worksheet. Sheets.Add Inserting one sheet after the sheet which caption is "Balance" and which name is shBalance: Sheets.Add before:=Sheets("Balance") or Sheets.Add before:=shBalance Inserting three sheets after the sheet which caption is "Balance" : Sheets.Add after:=Sheets(" Balance" ), Count:=3 Inserting one sheet at the beginning of the workbook. Notice the absence of double quotes when using the rank of the sheet: Sheets.Add before:=Sheets(1) Inserting one sheet at the end of the workbook: Sheets.Add after:=Sheets(Sheets.Count) Sometimes you want to send a single worksheet from a workbook to somebody but you don't want all the formulas to follow. Here is the code to copy a sheet out of a workbook into a new workbook, replace the formulas by values and save the new workbook: Sheets("Sheet3" ).Select Sheets("Sheet3" ).Copy Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False ActiveWorkbook.SaveAs " newBook.xls"

See the section on workbooks to manage the directory (path) when using the " SaveAs" method. ActiveSheet The ActiveSheet is the worksheet that has last been selected. So you can write: ActiveSheet.Visible=True ActiveSheet.Copy Remember that when you have copied a cell or a group of cells or any other object from a sheet you ALWAYS paste it to the ActiveSheet: ActiveSheet.Paste unless you are doing a PasteSpecial in which case the object is " Selection" : Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False If you want to check it the autofilters are on you need to use the object ActiveSheet also like in the following procedure where I check if the autofilters are on to remove them or exit the procedure: Range(" A2" ).Select If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilter Else Exit Sub End If VBA Chapter 15 of 24: VBA for Excel for the Cells, Rows and Columns Here is some code to move around and work with the components (rows, columns, cells and their values and formulas) of a worksheet. Selection and ActiveCell The object Selection comprises what is selected. It can be a single cell, many cells, a column, a row or many of these. For example: Range(" A1:A30" ).Select Selection.ClearContents will remove the content (values or formulas) of the cells A1 to A30.. The ActiveCell is the selected cell or the first cell of the range that you have selected. Try this in Excel: - select column A and see that cell A1 is not highlighted as the other cells. It is the ActiveCell.

- select row 3 and see that A3 is the ActiveCell. - select cells D3 to G13 starting with D3 and see that D3 is the ActiveCell - now select the same range but start with G13. So select G13 to D3 and see that G13 is the ActiveCell The ActiveCell is a very important concept that you will need to remember as you start developing more complex procedures. Delete or ClearContents BEWARE: If you write Range("a2").Delete the cell A2 is destroyed and cell A3 becomes cell A2 and all the formulas that refer to cell A2 are scrapped. If you use Range("a2").ClearContents only the value of cell A2 is removed. In VBA Delete is a big word use it with moderation and only when you really mean Delete. Cells and ClearContents To select all cells you will use Cells.Select And the to clear them all you will write: Selection.ClearContents or without seleting all the cells you can stil clear them alll with: Cells.ClearContents Range, Select To select a cell you will write: Range(" A1" ).Select To select a set of contiguous cells you will write: Range(" A1:A5" ).Select To select a set of non contiguous cells you will write: Range(" A1,A5,B4" ).Select Columns, Rows, Select, EntireRow, EntireColumn To select a column you will write: Columns("A" ).Select To select a set of contiguous columns you will write: Columns("A:B" ).Select To select a set of non contiguous columns you will write: Range("A:A,C:C,E:E" ).Select

To select a row you will write: Rows("1" ).Select To select a set of contiguous columns you will write: Range("1:1,3:3,6:6" ).Select To select a set of non contiguous columns you will write: Rows("1:13" ).Select You can also select the column or the row with this: ActiveCell.EntireColumn.Select ActiveCell.EntireRow.Select Range("A1" ).EntireColumn.Select Range("A1" ).EntireRow.Select If more than one cell is selected the following code will select all rows and columns covered by the selection: Selection.EntireColumn.Select Selection.EntireRow.Select Offset The Offset method is the one that you will use the most. It allows you to move right, left, up and down. For example if you want to move 3 cells to the right, you will write: Activecell.Offset(0,3).Select If you want to move 3 cells to the left, Activecell.Offset(0,-3).Select Beware though if you are in column A this line will return an error message If you want to move three cells down: Activecell.Offset(3,0).Select If you want to move three cells up: Activecell.Offset(-3,0).Select Here is a piece of code that you will use very frequently. If you want to select one cell and three more down: Range(Activecell,Activecell.Offset(3,0)).Select Range("A1" ,Range("A1" ).Offset(3,0)).Select Value

When you want to enter a numerical value in a cell you will write: Range("A1" ).Select Selection.Value = 32 Note that you don't need to select a cell to enter a value in it, from anywhere on the sheet you can write: Range(" A1" ).Value = 32 You can even change the value of cells on another sheet with: Sheets("SoAndSo" ).Range(" A1" ).Value = 32 BEWARE: When you send a value to a cell without selecting it the cursor doesn't move and the Activecell remains te same it doesn't become the cell in which you have just entered a value. So if you do Activecell.Offset(1,0).Select you will not move one cell down from the cell in wich you have just entered a value but from the last cell that was selected. You can also enter the same value in many cells with: Range("A1:B32" ).Value = 32 If you want to enter a text in a cell you need to use the double quotes like: Range("A1" ).Value = " Peter" If you want to enter a text within double quotes showing in a cell you need to triple the double quotes like: Range("A1" ).Value = """ Peter""" Formula When you want to enter a formula in a cell you will write: Range(" A1" ).Select Selection.Formula = " =C8+C9" Note the two equal signs (=) including the one within the double quotes like if you were entering it manually. Again you don't need to select a cell to enter a formula in it, from anywhere on the sheet you can write: Range(" A1" ).Formula = " =C8+C9" If you write the following: Range(" A1:A8" ).Formula = " =C8+C9" The formula in A1 will be =C8+C9, the formula in A2 will be =C9+C10 and so on. If you want to have the exact formula =C8+C9 in all the cells, you need to write: Range(" A1:A8" ).Formula = " =$C$8+$C$9" Month, Day, Year

If you have a date in cell A1 like January, 3 2007 Range("A2)".Value = Month(Range("A1")).Value the value entered in A2 will be 1 Range("A2)".Value = Day(Range("A1")).Value the value entered in A2 will be 3 Range("A2)".Value = Year(Range("A1")).Value the value entered in A2 will be 2007 VBA Chapter 16 of 24: Message Boxes (MsgBox) and Input Boxes in Excel VBA for Excel Code for Message Boxes In VBA for Excel the message box (MsgBox) is the primary tool to interact with the user. You can use it to inform, alert him or ask him to choose a certain path (Yes/No). The code in VBA for Excel to generate the following basic message box is: MsgBox "Your message here"

When you develop a procedure that takes time to run, you end your code with a basic message box telling the user that the procedure has been executed. MsgBox "The report is ready" or MsgBox "The procedure has been executed you may now go to sheet " " Cover" " " or MsgBox "The procedure has been executed you may now go to cell ""A1""" Notice that if you want part of your message to be between quotation marks you have to double the quotation marks within the message. When you develop a BA procedure that will do something important like delete all data always start the code with a message box that asks the user if he is certain that he wants the procedure to run. In this case you will use a "Yes/No" message box. When you create a basic message box (first example above), you don't need to use parentheses. If you develop a more complex message box you will see that when you start writing the line of code (example above) varAnswer = MsgBox(

right after the parenthesis Excel shows you what it is expecting as arguments:

1- The prompt is not optional, it is the message that you want your user to read in the message box and it must be between quotation marks. 2- When you enter the comma after the prompt Excel offers you a drop-down list of all the types of message box that you can create.

Choose your message box and enter a comma. 3- The third argument is the title of the message box that will appear in the blue band at the top of the message box. Submit a title between quotation marks. The fourth and fifth arguments are rarely used, they are optional and you don't need to enter anything else...but the closing parenthesis. Message Boxes used during testing When you are testing a long procedure you might want to stop the execution early. Use these 2 lines of code to do so: MsgBox "OK" Exit Sub The VBA procedure will run up to the messsage box and whe you click on "OK" the procedure is stopped.

VBA for Excel Code for Input Boxes Input boxes are used to require a SINGLE value from the user but it has many limits that will drive you to use VBA userforms instead.

The basic code for an input box is as follow. With the following procedure whatever value submitted by the user is entered in cell " A1" of a sheet named " Intro" . Sub proInput() Sheets("Intro" ).Range("A1" ).Value = InputBox("For what year do you need this report?" ) End Sub You can also use a different header instead of " Microsoft Excel" for the input box like " Please answer this question." in this example: Sheets("Intro" ).Range("A1" ).Value = InputBox("For what year do you need this report?" , "Please answer this question." )

Before we go on I must tell you that it is not possible to impose the password character (*) in the input box. To achieve this you must work with a userform and a text box. See how in the section on userforms and controls. VBA Chapter 17 of 24: VBA Excel to work with Databases To really get the most out of VBA working with databases you must

master these functionalities in Excel. Visit the website on Excel and study the sections on databases and database functionalities. When you work in an Excel database you must first make sure that all filters are off. To this end you will start your procedure with these two " IF" statements. First select any cell within the database. Range(" A3" ).Select If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilter End If If ActiveSheet.FilterMode = True Then ActiveSheet.ShowAllData End If Knowing that a database in a set of connected rows and columns you can select it all with: Range(" A3" ).Select Selection.CurrentRegion.Select Once this is done, you can count the number of rows (records) and the number of columns (fields) with the following code: varNbRows=Selection.Rows.Count varNbColumns=Selection.Columns.Count In fact the number of records is the number of rows minus one (the title row) and here is the code: varNbRecords=Selection.Rows.Count - 1 I never write the code for filtering (advanced or autofilter) a set of data I use the macro recorder and then modify the code.

...Discover More in the Downloadable Tutorial


Sorting Data with VBA Creare a set of data with 5 fields (columns) and a few records (5). On row 1 are the title cells and row 2 is the first many records. The MC is only filtering the first record because I have not selected them all.

To correct this situation, I modify the code to this There is one piece of code that the Macro Recorder can write for you to sort the data. The MR will write something like this: Range(" C3" ).Select Range(" A1:E2" ).Sort Key1:=Range(" C2" ), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal Range(" C3" ).Select Selection.CurrentRegion.Sort Key1:=Range(" C3" ), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal I could also change the sorting order and I alwys remove the argument DataOption1:=xlSortNormal including the preceding comma because older versions of Excel will bug on it. So the final code is: Range(" C3" ).Select Selection.CurrentRegion.Sort Key1:=Range(" C3" ), Order1:=xlDescending, Header:= _ xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom Note: The Space/Underscore allows you to break a long sentence and continue it on the next line. VBA Chapter 18 of 24: VBA for Excel Variables A variable is an object that you create and in which you can store text (STRING), dates (DATE), numbers (INTEGER, LONG, SIMPLE, DOUBLE) or almost anything else (VARIANT). Why should you use variable? The first good reason is to make your code dynamic, to avoid hard coding some values. Hard Coding vs Dynamic Coding You are hard coding when you write:

Workbooks.Open " MyFile.xls" You are dynamically coding when you enter the name of the file in a cell (A1) and you write. varWorkbook=Range(" A1" ).Value Workbooks.Open varWorkbook At this point you or the user can change the value in cell A1 and open any workbook. Counters You will create variables that you will use as counters. In a new workbook enter a value in cell B1, Copy/Paste the following procedure in a module and try it (Go to " Tools/Macro/Macros" select " proTest" and click on " Run" ): Sub proTest() Dim varCounter as Double Dim varNumber as Double varNumber = Range(" B1" ).Value Range(" A1" ).Select For varCounter = 1 to 25 Selection.Value=varNumber * 3 varNumber =Selection.Value Selection.Offset(1,0).Select Next End Sub Try different values in cell B1. You will create variables to count the number of rows, store the result in a variable and then do something as many time as there are rows. In a new workbook enter values in cells A1 to A10 then Copy/Paste the following procedure in a module and try it (Go to " Tools/Macro/Macros" select " proTest" and click on " Run" ): Sub proTest() Dim varCounter as Double Dim varNbRows as Double Range(" A1" ).select

varNbRows=Selection.currentRegion.Rows.Count For varCounter = 1 to varNbRows Selection.value=Selection.Value*2 Selection.Offset(1,0).select Next End Sub Enter numbers in cells A1 to A:1000 or A1 to whatever row and try it again I use variables in 99% of my procedures and you will do the same to reduce the number of hard coded values. Maintenance becomes so much simpler. Option Explicit When you open a module in the Visual Basic Editor the sentence " Option Explicit" is usually the first line of code at the top. This means that you MUST declare all the variables that you use in the procedure. This can be changed. In the Visual Basic Editor go to " Tools/Options/General" and uncheck " Require Variable Declaration" and from then on the " Option Explicit" sentence will not appear and you will not have to declare your variables. But BEWARE. There are many advantages to this " Option Explicit" and many disadvantages if you don't use it: Why should this option be activated? Let's say your are using a variable named " varMyVar" and that you write vayNyVar = 32. You have misspelled the name of the variable and you will go on thinking that " varMyVar" 's value is 32 but IT IS NOT 32 is the value of " vayNyVar" and it is of no use to you. If the Option Explicit is activated the VBE will tell you that your are using a non-existent variable " varNyVar" . You will hence avoid a lot of possible errors. If you write Rage(" A1" ) instead of Range(" A1" ) VBE will think that Rage(" A1" ) is some variable that you are using and will not tell you about the typo. So check the " Require Variable Declaration" in the options and rest in peace. Variable Declaration

Declaring variables is quite a simple thing. At the beginning of you procedure you tell VBA that some words will define your variables like: Dim varThisVariable as String means that varThisVariable is one of your variables and that it will contain text. Declare all your variables at the beginning of your procedures. Always use the prefix " var" and give a name to your variables that means something. It will make your code easier to read (I hate when I see " i" or " j" or any other meaningless names used as variables). Use one or many upper case letters within the name: Dim varThisVariable as String. When you key in your procedure don't capitalise any letter. If you have spelled the name right VBE will add the capital letters. So if you declare a variable named varCounter and you key in varconter VBE will not capitalise the " C" telling you that the name is misspelled. Data Types Because I want to keep things simple, I essentially use four types of variables. You can use the " VARIANT" type for all your variables and put anything it them but it is the " anything" that might give you problems later. STRING Text, up to 65,000 characters. I use it for text but also file names, path, worksheet names, workbook names, cells' addresses. I could also use " Bytes" , Integer" , " Long" and " Single" but they all have limits.

DOUBLE

Number with or without decimals

DATE

If you use the STRING type for dates, you won't be able to perform calculations on them so use the " Date" type. In this type of variable you can store anything, pictures, objects and even entire sheets

VARIANT

Public and Private Variables When you declare a variable with the code Dim varMyVar as String

it can only be used in the procedure from where it is declared. When the procedure ends, the variable disappear. If you want to use a variable in many procedures you must declare it at the top of a module this way: Public varMyVar as String You must remember that this variable disappears only when the workbook is closed and that until then it carries the last value that you have stored in it. To clear the value you must use the code: varMyVar = " " or varMyVar = Empty I feel uncomfortable making a variable public so I use other approaches to carry the value of a variable from one procedure to the other. Carrying the value of a variable from one procedure to the other The easy way to carry the value of a variable from one procedure to the other is by storing this value in any cell of the workbook: in the first procedure: Range(" A3456" ).Value=Variable1 in the second procedure: Variable1=Range(" A3456" ).Value You can also carry the variable itself from one procedure to the another procedure that you call from the first one. For example: Sub proTest1() Dim varMyVar As Double varMyVar = 3 Call proTest2(varMyVar) End Sub Sub proTest2(varMyVar) varMyVar = varMyVar * 2 Range(" A1" ).Value = varMyVar End Sub The variable is declared only in the first procedure. A value is stored in it (3). A second procedure is then called with the variable as argument. After the execution of the two procedures, the value of range A1 should be 6.

Array Variables An ARRAY VARIABLE is a multidimensional variable that you can size to your liking: myVariable(3) is a variable consisting of 4 different values, myVariable(5,10) is a variable consisting in 66 values, 6 rows and 11 columns and myVariable(5,10,10) can carry 726 values, etc... When you declare an array variable, the first element bears the number " 0" . The Variable varMyVariable(3) includes 4 elements from " 0" to " 3" . If like me you are uncomfortable with a variable varMyVariable(0), you can impose that the first element bears the number " 1" . In the general declaration (where you find the Option Explicit), write: Option Base 1 In this situation, myVariable(3) contains only three elements. You can carry the value of a variable from one procedure to the other by stocking this value in any cell in the workbook. ex: in the first procedure: Range(" A3456" ).Value=Variable1 in the second procedure: Variable2=Range(" A3456" ).Value VBA Chapter 19 of 24: VBA for Excel (If, Then, ElseIf, For, Next, Do, Loop) IF If..Then...End If When there is only one condition and one action, you will use the simple statement: If Selection.Value > 10 Then Selection.Offset(1,0).Value = 100 End If In plain English: if the value of the selected cell is greater than 10 then the value of the cell below is 100 if not do nothing. Note: Tests on strings are case sensitive so when you test a string of characters and you don't know if the user will use upper case or lower case letters, use the function LCase function within your test and write the strings in your code in lower case letters: If LCase(Selection.Value).Value= " yes" then... With this approach, your test will be valid whatever case your client

uses (Yes, YES or any other combination of cases). If..Then...End If (multiple tiers) When there are only two conditions that you want to check sequentially, you will use the statement: If Selection.Value > 10 Then If Selection.Value = 12 Then Selection.Offset(1,0).Value = 100 End If End If In plain English: first check if the value of the selected cell is greater that 10. If it is not do nothing. If it is check if the value of the selected cell is equal to 12. If so set the value of the cell below at 100 else do nothing. If..Then...And...End If When there are two inclusive conditions, you will use the statement: If Selection.Value > = 10 And Selection.Offset(0,1).Value < 20 Then Selection.Offset(1,0).Value = 100 End If In plain English: if the value of the selected cell is greater or equal to 10 and smaller than 20 the value of the cell below is 100 otherwise do nothing. If..Then...Or...End If When there are two exclusive conditions and one action, you will use the statement: If Selection.Value = 10 Or Selection.Offset(0,1).Value = 20 Then Selection.Offset(1,0).Value = 100 End If In plain English: if the value of the selected cell is equal to 10 or equal to 20 then the value of the cell below is 100 otherwise do nothing. If..Then...Else...End If When there is only one condition but two actions, you will use the statement: If Selection.Value > 10 Then Selection.Offset(1,0).Value = 100 Else Selection.Offset(1,0).Value = 50

End If In plain English: if the value of the selected cell is greater than 10 then the value of the cell below is 100 else the value of the cell below is 50. If..Then..ElseIf...End If When there are more than one condition linking each to a different action you will use the statement: If Selection.Value = 1 Then Selection.Offset(1, 0).Value = 10 ElseIf Selection.Value = 2 Then Selection.Offset(1, 0).Value = 20 ElseIf Selection.Value = 3 Then Selection.Offset(1, 0).Value = 30 ElseIf Selection.Value = 4 Then Selection.Offset(1, 0).Value = 40 ElseIf Selection.Value = 5 Then Selection.Offset(1, 0).Value = 50 End If In plain English: If the value of the selected cell is 1 then the value of the cell below is 10 but if the value of the selected cell is 2 then the value of the cell below is 20 but if the value of the selected cell is 3 then the value of the cell below is 30 but if the value of the selected cell is 4 then the value of the cell below is 40 but if the value of the selected cell is 5 then the value of the cell below is 50 but then if the value of the selected cell is not 1, 2, 3, 4 or 5 do nothing. Do..Loop The Do...Loop statement does pretty much the same thing as the For..Next statement but you don't need to declare a counter because the Loop stops when it encounters a certain condition. Try the following procedure by first entering 1 in cells A1 to A7 or A27 or to as far down as you want to go. Sub proTest() Dim varCounter as Integer Range(" A1" ).Select Do Until Selection.Value = " " Selection.Value = Selection.Value + 1 Selection.Offset(1, 0).Select

Loop End Sub In plain English: starting in cell A1 add 1 to the value of the selected cell and move one cell down. Do this until the value of the selected cell is nothing. Variation on the statement: Do Until Selection.Value = " " Do until the selected cell is empty Do While Selection.Value < > " " Do as long as the selected cell is not empty Loop Until Selection.Value = " " Loop until the selected cell is empty Loop While Selection.Value < > " " Loop as long as the selected cell is not empty

With...End With In the old days when computer memory was rare and expensive and computers were not very powerful programmers would us a lot of With..End With statements because is was less requiring on the memory and the capacities of the computer. When you develop in VBA for Excel (very small programs) memory is not really an object and our personal computers are as powerful as the large computers of yesterday. The macro recorder uses a lot of With..End With statements but I personally don't. Anyway here how it works. Range(" A3" ).Select With Selection.Font .Name = " Arial" .Size = 24 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False

.Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With is the same as writing Range(" A3" ).Select Selection.Font.Name = " Arial" Selection.Font.Size = 24 Selection.Font.Strikethrough = False Selection.Font.Superscript = False Selection.Font.Subscript = False Selection.Font.OutlineFont = False Selection.Font.Shadow = False Selection.Font.Underline = xlUnderlineStyleNone Selection.Font.ColorIndex = xlAutomatic Both work it's your choice. For..Next The FOR...NEXT loop is the one I use the most. It allows you to repeat an action a certain number of times. Sub proTest() Range(" A1" ) = 10 Range(" A2" ).Select For varCounter = 1 To 10 Selection.Value = Selection.Offset(-1, 0).Value * 2 Selection.Offset(1, 0).Select Next End Sub In plain English: Set the value of cell A1 to 10 then select cell A2. While the counter is going from 1 to 10 (10 times in other words) the value of the selected cell is twice the value of the cell above...move one cell down. Resulting from this procedure, Cell A1=10, A2=20, A3=40.....A11=10,240. If you'd write: For varCounter = 1 To 10 Step 2

the deed would be performed only 5 times. Resulting from this procedure, cell A1=10, A2=20, A3=40.....A6=320. Your can also start at the bottom and go up For varCounter = 10 To 1 Step -1 When you use a For..Next statement on a set of data it is interesting to count the number of rows and have your counter move from 1 to the number of rows. For varCounter = 1 to varNbRows VBA Chapter 20 of 24: VBA for Excel Functions Three topics in this step: - using Excel functions within macros, - creating new Excel functions with VBA for Excel and, - using VBA functions within macros. Existing Excel Functions There are hundreds of functions available in VBA. Most of the functions that you find in Excel are available through macros in this form: Range(" C1" ).Value= Application.WorksheetFunction.Sum(Range(" A1:A32" )) this sentence sums the values of cell A1 to A32 and stores the total in cell C1. Using Excel functions through VBA reduces substantially the calculation time and creates spreadsheet without formulas that you can more easily send to others. New Excel Functions You can create new functions in Excel. For example the function created by the code below will simply multiply the value of a cell by 2. Function fctDouble(varInput) fctDouble = varInput * 2 End Function Once this code is in a module in your workbook you access the new function the same way that you access the other functions in Excel by clicking on the icon function on the tool bar or from the menu bar " Insert/Functions" . In the dialog box select the " User Defined"

category and select you new function (" fctDouble" in this example) and follow the instructions. VBA Functions Here are some VBA functions that you will use within my Excel macros: LCase, UCase The IF statements, the SELECT CASE and DO WHILE are all case sensitive. When you test a string of characters and you don't know if the user will enter upper case or lower case letters, use the LCase or UCase functions within your test and write the string in proper case: If LCase(Selection.Value)= " toto" then... or Select Case LCase(Selection.Value) or Do While LCase(Selection.Value)< > " toto" If UCase(Selection.Value)= " TOTO" then... or Select Case UCase(Selection.Value) or DO WHILE UCase(Selection.Value)< > " TOTO" NOW() NOW() is an Excel function but also a VBA function. With the following code the Excel formula NOW() is inserted in cell A1. The cell " A1" will show the date of the day and this date will change every time the workbook is opened: Range(" A1" ).Formula = " =Now()" With the following code, the cell " A1" will carry the date when the procedure is executed and will keep this value until you execute the procedure again. It won't change every time you open the workbook. Range(" A1" ).Value = Now() VBA Chapter 21 of 24: SQL in VBA for Excel SQL stands for Structured Query Language and is the language used to work in almost all databases. From Excel with SQL you can extract data from any database, text file and other environments.

In the example below I needed to develop a query to extract data from a CSV file with 200,000 lines. I cannot get all 200,000 lines in Excel but with the following code I extract the unique values (no double) from the file. To query data you essentially need 2 things a connection (varConn) and an SQL sentence (varSql). To get the connection use the Macro Recorder while going to " Data/Import External Data/New Database Query" . For the SQL sentence you can use the same approach (Macro Recorder) or use Access. Create a query in Access, copy the SQL sentence created by Access and paste it into your VBA for Excel code. In the following code the file that data are extracted from is named " Generic Call Detail Report Rev.csv" , the sheet where the data reside is " Generic Call Detail Report Rev" and the field from which I am extracting data is " Subaccount #:" Sub proQuery() Dim varConn As String Dim varSql As String Dim varQuery As QueryTable varConn = " ODBC DefaultDir=E:\Garson Driver={Microsoft TextTreiber (*.txt *.csv)} DriverId=27 FIL=text MaxBufferSize=2048 MaxScanRows=8 PageTimeout=5 SafeTransactions=0 Threads=3 UID=admin UserCommitSync=Yes " varSql = " SELECT DISTINCT `Generic Call Detail Report Rev`.`Subaccount #:` FROM `Generic Call Detail Report Rev.csv` `Generic Call Detail Report Rev`" Set varQuery = ActiveSheet.QueryTables.Add( Connection:=varConn, Destination:=Range(" a1" ), Sql:=varSql) varQuery.Refresh End Sub Once the query has been created here is the code to refresh the data. You set the curser anywhere in the database and execute this small piece of code. Sub proRefresh()

Selection.QueryTable.Refresh BackgroundQuery:=False End Sub VBA Chapter 22 of 24: API's in VBA for Excel API stands for Application Programming Interface and consists of a collection of functions that provide programmatic access to the features of the operating system (Windows). When you use API's within VBA for Excel not only do you control Excel but all other parts of your version of Windows. Here is how it works if you want to copy a file from a directory to the other. You must first declare the API function that you want to use in your code (Private Declare....CopyFile...). Then you call it from you procedure (Sub proCopyFile...). Notice that you must ALWAYS use a variable to receive the result of an API function (varCopy = CopyFile(" C:\amy.xls" , " E:\SCI\amy.xls" , 1)) Option Explicit Private Declare Function CopyFile Lib " kernel32.dll" Alias " CopyFileA" _ (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _ ByVal bFailIfExists As Long) As Long Sub proCopyFile() Dim varCopy As Long varCopy = CopyFile(" C:\amy.xls" , " E:\SCI\amy.xls" , 1) If varcopy< > 0 Then MsgBox " Copy succeeded." End Sub Here is an example of code to delete a file in a certain directory. Remember that when you use this API the file that you delete doesn't go into the Recycling Bin and is not retrievable. Option Explicit Private Declare Function DeleteFile Lib " kernel32.dll" Alias _ " DeleteFileA" (ByVal lpFileName As String) As Long ' Declaration Sub proDeleteFile() Dim varDelete As Long ' return value varDelete = DeleteFile(" C:\amy.xls" )

If varDelete = 1 Then MsgBox " File deleted." End Sub If you work on a network and the saving time of a file is too long you can use both functions. I have created an application that needed to save hundreds of files to a network and I created this procedure. The time saving was very significant. In this example the active workbooks file names are stored in a variable called varCreatedWorkbook. Option Explicit Private Declare Function CopyFile Lib " kernel32.dll" Alias " CopyFileA" _ (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _ ByVal bFailIfExists As Long) As Long Private Declare Function DeleteFile Lib " kernel32.dll" Alias _ " DeleteFileA" (ByVal lpFileName As String) As Long ' Declaration Sub proCopyFile() Dim varCopy As Long Dim varCreatedWorkbook as String

ActiveWorkbook. Save As " C:\" & varCreatedWorkbook varCopy = CopyFile(" C:\" & varCreatedWorkbook, " E:\" & varCreatedWorkbook, 1) varDelete = DeleteFile(" C:\" & varCreatedWorkbook) End Sub To delete the file you could also use the Kill statement that is not an API. Kill " C:\" & varCreatedWorkbook VBA Chapter 23 of 24: Forms (Userforms) in VBA for Excel The form or userForm is also known as a GUI (Graphical User Interface). The form is used to require values, parameters and information from the user to feed the VBA procedure. Different basic controls can be added to the userform they are called: Label, TextBox, ComboBox, ListBox, CheckBox, OptionButton, ToggleButton, Frame, CommandButton, TabStrip, MultiPage, ScrollBar, SpinButton and Calendar. You can find all kinds of other controls on the Internet but the problem is that if you send your workbook to other people and the new control is not installed on their computer it wont work. This problem might even occur with the calendar. To learn more about all the controls see section 24. Before you start developing a userform to enter data in an Excel database see the data entry form that Excel offers you. Creating a userForm in VBA for Excel Creating a form in VBA is quite easy. In the VBE (Visual Basic Editor) you right click anywhere on your project in the Project Window and select " Insert/UserForm" and here is what you see:

In the list of components of your project the userform name appears . Anytime you want to work on your userform you double click on its name in the Project window. You can close the toolbox by clicking on its " X" and call it back by clicking on the toolbox icon on the toolbar Testing the Userform Anytime that you want to see your userform (finished product or work in progress) at work or when you want to test it select the userform by clicking in ay part of it that is empty (and not one of the controls), click on the " Run" button on the toolbar . Your userform will show with Excel in the background. To return to the VB Editor just click on the " X" of the userform. Adding Controls The toolbox offers you 15 controls. They are (starting with the most used): Label, TextBox, CommandButton, ComboBox, ListBox, CheckBox, OptionButton, ToggleButton, Frame, TabStrip, MultiPage, ScrollBar, Image, RefEdit and the calendar. To add controls to the form you left click on whatever control that you want in the Toolbox , hold the button down and drag the control on the form. You can now expand the control to the desired size. Once all your controls are on the form you left click on them and in the properties window you can change the properties of the selected control. You can also right click on them and select " Properties" . Tab Order

Once you have added the controls here is an important feature. The user can use the mouse to move from one control to the other but he should be able to move from one control to the other by entering a value in one and clicking " Enter" or " Tab" . To make sure that the user moves from a control to the next one you need to set the tab order. To do so, right click on the form itself and select the " Tab Order" item. Follow the instructions. The first control in the list will be the one that is active (flashing cursor) when the form is activated. Bring the controls that are not to be used by the user (labels) at the end of the list. In section 24 see how to " tab deactivate" single controls. Managing Controls You can move the controls by clicking on them holding and moving them around. You can resize them by selecting them and using the different handles around them. You can copy or cut them by right clicking on them and choosing the right menu item. Once you have added your controls you might want to align a few of them or resize a few so that they are all the same size. To do so you first need to select many controls at the same time. To do so left click on the form near one of the controls that you want to select. Hold and drag drawing a frame that includes many controls.

When you let go of the button all the controls that are touched by the frame are selected.

Right click on any of the selected controls and this contextual menu appears:

You can then align the controls (7th menu item) or make them the same size (8th menu item). VBA Chapter 23A of 24: Userforms Properties and VBA Code The UserForm Properties As you have seen in section 3 on the Visual Basic Editor you double click on the userform's name in the Project window and its properties appear in the Properties window:

In the Properties window of the VBE you MUST change the name " (Name)" of the form, its caption (the name in the blue band at the top of the UserForm) and you can also modify the default setting of any of the 32 other properties of the form. When you name a form always use the prefix " frm" like in " frmDatabase" and be as descriptive as you can be so that your code will be easy to read. Always use one or more upper case letters in the name. When you write " frmdatabase.show" in lower case letters Excel will capitalize some letters " frmDatabase.Show" letting you know that the name is spelled correctly. The caption is what your users will se at the top of the userform. Be as informative as possible and complete the information with a label if necessary.

Else than the Name and Caption there are just a few properties that you might want to modify. You can select a different color for the background with the property " BackColor" . By default the userform appears in the center of the screen. If you want it to show somewhere else set the " Start" property to " 0-Manual" and use the " Top" and " Left" properties to set a new position. The Code Opening and Closing the Userform The first thing to do is to create code to call your userform. Here is a basic line of code doing so: frmCity.Show See Section 9 on Events to learn how to assign macros to a text box or other button that you place on the sheet. The line of code to close the userform is: frmCity.Hide or Me.Hide " Me" being the general name of the active form The " Hide" sentence is usually part of the code of a command button on the form. A user clicks on a " GO" , " SEND, " SUBMIT" or " CANCEL" button and part of what must happen is that the userform disappears from the screen. Resetting the controls When you enter values in the controls of a userform (text boxes, option buttons, etc.) they don't go away when you close the form so they are back when you reopen it. When the form opens or closes the value of some controls might need to be reset or simply set. You can do this within the " Activate" event of the userform or also within the " Deactivate" event. But if you chose to do it at closing you must

also do it within the " Terminate" event when the user clicks on the " X" at the top right of the form. So let's do it at the opening. Double click on the gray part of the form in the code window. Select the event " Activate" in the drop down list at the top right of the code window. VBA creates the first and last line of the procedure. Add your code like: tbxSoAndSo.Value=" " or tbxSoAndSo.Value=0 cbxSoAndSo.value=" Choose a value" or cbxSoAndSo.value=" " If you get into more complex programming you can make certain controls invisible at opening: tbxSoAndSo.Vsible= False You will make them come back when the user makes a certain selection in another control by adding a line of code within the " Change" event of that control. tbxSoAndSo.Vsible= True You can add code within any of the 22 events of the userform but usually the " Activate" event is the only one that you will use. VBA Chapter 24 of 24: VBA Excel Command Buttons, Text Boxes , Combo Boxes and other Controls Now that you have designed you userform and added different controls It is time to manage the properties of these controls and to develop VBA code linked to them. When you single click on a control on the userform in the VBE its properties are shown in the Properties window. The properties and the number of properties differ depending on the type of controls that you have selected. These properties can be changed in the Properties window of the VB editor or can be changed programmatically in a VBA procedure. For all your controls set the " Name" property that you will refer to in your VBA procedures. Use prefixes and some upper case letters in these names (cbxCitx, txbCityName). Be as descriptive as possible to make your code clearer. The prefixes that I use are: command button (cmb), labels (lbl), combo boxes (cbx), text boxes (txb), list boxes (lbx), check boxes (ckb), radio button (rdb), toggle buttons (tbt), frames (fra), tab strips (tsp), multi pages (mpg), scroll bars (scb), spin buttons (spb). The controls that particularly need to be well named are the controls which eventual values you will be using in your procedures like the text boxes, the list boxes, the combo boxes, the option buttons and the check boxes. For example: Range(" A1" ).Value = txbCityName.Value

will take the value entered by the user in the text box named tbxCityName and enter in cell A1 of the active sheet. The " Caption" property contains the text shown on a label, a command button, a check box, an option button, a frame, a tab strip or a multi page. Most controls have a " TabIndex" property. Instead of setting this property for each control see the " Tab Order" of the userform in section 23. Then for most controls there are these general properties that allow you to set the font, the color of the font, the color of the background, the type of background, the type of border and other design features. In the following sub-sections yo will learn about the most important or usual controls. You will learn about their properties and the VBA code that can be developed within events related to them. There are 3 controls that are widely used and they are the command buttons, the labels and the text boxes. The command button is where most of the VBA code for the userform is developed. VBA Section 24A of 24: Properties and VBA code for Command Buttons VBA Section 24B of 24: Properties and VBA code for Labels VBA Section 24C of 24: Properties and VBA code for Text Boxes Then there are two little more complex and powerful controls the combo boxes and the list boxes. The difference between a combo boxes and the list boxes is that the combo box is a drop-down list and the user can submit a single value either one of the values from the drop-down list or any other value. The list box shows a certain number of values with or without a scroll bar and the user can select one or more values but not a value that is not in the list. Combo Box List Box

VBA Section 24D of 24: Properties and VBA code for Combo Boxes VBA Section 24E of 24: Properties and VBA code for List Boxes

A third group of controls appear often on userforms and they are the check boxes, the option buttons and the frames. VBA Section 24F of 24: Properties and VBA code for Check Boxes, Option Buttons and Frames Finally 5 other controls allow the development of fancier and more complex userforms: VBA Section 24G of 24: Properties and VBA code for Spin Buttons VBA Section 24H of 24: Properties and VBA code for Multi Pages VBA Chapter 24A of 24: The Command Buttons in VBA for Excel In the toolbox the command button has this icon . The command button is a very active control and there is always VBA code behind it. The command buttons are usually placed at the bottom of the form and serve to complete the transaction for which the form has been created. The caption of these buttons are usually " Go" , " Run" , " Submit" , " Cancel" , etc. Properties The other interesting properties of the command button are: - WordWrap to be able to write more that one line on a button, - ControlTipText which generates a small comment box when the user moves the mouse over the control. You can use this property to give explanations and instructions about the command button, For advanced users there are the: - Enabled and Visible properties that you can change programmatically to disable or render invisible a command button following a previous selection in another control of the userform. Code Name your command button before developing your code. VBA uses the name of the command button when it creates lines of code related to events. So if you don't name your command button VBA will create the private sub:: Private Sub CommandButton1_Click() as if you name the command Button " cmbSubmit" for example the private sub will start with: Private Sub cmbSubmit_Click()

If you name your command buttons after private subs have been created they won't work anymore. A very simple VBA procedure for the command button would look like this: Private Sub cmbSubmit_Click() Sheets(" Code" ).Range(" F1" ).Value = cbxInput.Value frmPassword.Hide End Sub The content of the combo box " cbxInput" is entered in cell " F1" of the sheet " Code" and the form (frmPassport) is closed. VBA Chapter 24B of 24: The Labels in VBA for Excel In the toolbox the label has this icon . The label is a passive control meaning that the user never really acts on it. It is there to inform the user and to label other controls like text boxes, combo boxes or list boxes. Properties The other interesting properties of the label are: - TabStop: To make the control invisible for the " Tab" and " Enter" keys (see Tab Order) set this property to " False" . - WordWrap: If you want to write more than one line of text in a label set this property to " True" . Code There is not much coding developed for the labels although there are 8 events related to the label. For example there is an event named " MouseMove" . If you develop code within this event it is executed when the mouse moves over the label. If the code is the following: MsgBox " Don't forget to..." a message box will appear when the user moves the mouse over the label. You can stack many labels one over the other and make their " Visible" property to " False" . You can then make any of the label visible from an event related to another control. For example if a user chooses a certain value in a combo box a certain label appears VBA Chapter 24C of 24: The Text Boxes in VBA for Excel In the toolbox the text box has this icon .

The text box is the simplest control to require an entry from the user. The user types something in it and this value can then be used in your VBA procedure. You will usually add a label to accompany the text box. For most controls including the VBA for Excel text box there are general properties that allow you to set the font, the color of the font, the color of the background, the type of background, the type of border and other design features. Using the 3 windows in the Visual Basic Editor you will see the following properties in the " Property" window when the text box is selected. Properties The other interesting properties of the text boxes are: - WordWrap to be able to write more that one line on a button, - ControlTipText which generates a small comment box when the user moves the mouse over the control. You can use this property to give explanations and instructions about the command button, - Enabled and Visible are properties that you can change programmatically to disable or render invisible a command button following a previous selection in another control of the userform, - TabIndex is a property that you change through the " Tab Order" functionality as shown in the UserForms section. - MaxLength to limit the number of characters entered by the user, - Value or Text which is the text show in the text box when the userform is activated (" Enter your Name" for example) To ask users to submit a password to run certain macros develop a userform with a text box and a command button. In the text box you can modify the " PasswordChar" property so that when the user enters the password nobody around can read it. Use an asterisk, an ampersand or any other character in it. Code The most important ting to remember is that a text box is what its name says it carries text. So if you want to send a numerical value from a text box to a cell you must use the " Val" thing: Range(" A1" ).Value=Val(tbxInput) The same applies when you want to use text box vale in mathematical operations: tbxInputTot= Val(tbxInput1) + Val(tbxInput2) One of the limitations of the text box is the format. If you want to show dates, numbers with decimals or with a coma for the thousands you need to program some procedure within the " Change" event of the text box

VBA Chapter 24D of 24: The Combo Boxes in VBA for Excel If you are looking for a drop-down list to use on a regular worksheet see the much easier and user friendly Excel drop-down lists. In the toolbox the combo box has this icon .

For most controls including the VBA for Excel combobox there are general properties that allow you to set the font, the color of the font, the color of the background, the type of background, the type of border and other design features. Using the 3 windows in the Visual Basic Editor you will see the following properties in the " Property" window when the combo box is selected. Properties The other interesting properties of the combobox in VBA for Excel are: - Name: Set the " Name" property of all the controls that you will refer to in your VBA procedures. I encourage you to use prefixes and some upper case letters in these names (cbxCity, txbCityName). Be as descriptive as possible to make your code clearer. The prefixes that I use are: command button (cmb), labels (lbl), combo boxes (cbx), text boxes (txb), list boxes (lbx), check boxes (ckb), radio button (rdb), toggle buttons (tbt), frames (fra), tab strips (tsp), multi pages (mpg), scroll bars (scb), spin buttons (spb), images (img) and ref edits (rfe). The controls that particularly need to be well named are the controls which eventual values you will be using in your procedures like the text boxes, the list boxes, the combo boxes, the option buttons and the check boxes. For example: Range(" A1" ).Value = cbxCityName.Value will take the value selected by the user in the combobox named cbxCityName and enter it in cell A1 of the active sheet. - RowSource: No complex programming to submit a list of values to feed the combo box. The values that should appear in the drop-down list of the combo box are submitted in the RowSource property. For example Sheet1!A1:A12 will feed the list with the values residing in cells A1 to A12 of the sheet with the Caption: " Sheet1" . The rules to submit the RowSource property is the caption of the sheet where the list resides followed by an exclamation point (!), the address of the first cell, a colon and the address of the last cell. IMPORTANT NOTE: if there is a space or a special character in the caption of the sheet where the list resides you must surround it with simple quotes like in 'This sheet'! A1:A12. - ListRows: is the number of values shown in the drop-down list. If you show less than the complete list a scroll bar is added automatically.

- MatchRequired: is by default set to false meaning that the user can submit any value in the combo box. If you want the user to be limited to the values in the list set this property to True. - Text: can contain the value shown in the combo box when the userform is activated (Select a City, for example). If there is no value set for this property the combo box is empty. You can tell the user what you want him to enter in it with a label above the combo box or with the ControlTipText property. - ControlTipText: which generates a small comment box when the user moves the mouse over the control. You can use this property to give explanations and instructions about the combo box. - ColumnCount: is the number of columns of values that you want shown in the drop-down list. For example if you want to show part number and part name in the list you will submit a RowSource like Sheet1!A1:B12 with the part numbers in column A and the part names in column B. In this situation the ColumnCount property is set at 2. - ColumnWidth: is the width of all the columns shown in the drop-down list of the combo box. - BoundColumn: is the column from which the value is drawn for the final value of the combo box. For example if the part number is in column A of the RowSource and the part name is in column B of the RowSource when the user select a value only column A or column B will become the final value of the combo box . So if you set the value of BoundColumn to 1 the part number becomes the final value. If you set BoundColumn to 2 the part number becomes the final value. VBA code As you have read earlier most of the VBA code concerning userforms and controls is linked to events related to the userform itself and the command buttons. There are 17 events related to the combo box (Double click on the userform name in the project window of the VBE and then double click on the combo box. You can develop code for each of these events. VBA Chapter 24E of 24: The List Boxes in VBA for Excel If you are looking for a drop-down list to use on a regular worksheet see the much easier and user friendly Excel drop-down list.

In the toolbox the list box has this icon Properties

- RowSource The values that should appear in the drop-down list of the combo box are submitted in the RowSource property. For example Sheet1!A1:A12 will feed the list with the values residing in cells A1 to A12 of the sheet with the Caption " Sheet1" . The rules to submit the RowSource property is the caption of the sheet where the list resides followed by an exclamation mark (!), the address of the first cell, a colon and the address if the last cell. IMPORTANT NOTE: if there is a space or a special character in the caption of the sheet where the list resides you must surround it with simple quotes like in 'This sheet'! A1:A12. - MultiSelect is set to 1 if you want the user to be able to select many values from the list. - Height The number of values shown in the list will depend on the height of the list box. You can set the height here or on the userform itself by stretching it. If the number of values in your RowSoOurce is greater than what can be shown in the list box a scroll bar is added automatically. - Text should contain the value shown in the combo box when the userform is activated (Select a City, for example). - ControlTipText which generates a small comment box when the user moves the mouse over the control. You can use this property to give explanations and instructions about the combo box. - ColumnCount is the number of columns of values that you want shown in the list box. For example if you want to show part number and part name in the list you will submit a RowSource like Sheet1!A1:B12 with the part numbers in column A and the part names in column B - ColumnWidth is the width of all the columns shown in the drop-down list of the combo box. - BoundColumn is the column from which the value is drawn for the final value of the combo box. For example if the part number is in column A of the RowSource and the part name is in column B of the RowSource when the user select a value only column A or column B will become the final value of the combo box . So if you set the value of BoundColumn to 1 the part number becomes the final value. If you set BoundColumn to 2 the part number becomes the final value. VBA Chapter 24F of 24: Option Buttons, Check Boxes and Frames In the toolbox the option button has this icon and, the frame this one . , the check box has this one

You don't need to add a label to accompany the check box or the option button because they come with their own. The check boxes and the option buttons are both used to offer the user a choice. The main difference between check boxes and option buttons is that if you have 5 of each on a form a user can check all 5 check boxes but can only select one of the option buttons. If you want to create two sets of option buttons read below on frames and option buttons. If you don't want to use frames to create groups of option buttons you will need to use the " GroupName" property of the option buttons. All option buttons with the same GroupName work together. Properties - WordWrap to be able to write more that one line in the caption, - ControlTipText which generates a small comment box when the user moves the mouse over the control. You can use this property to give explanations and instructions about the option button or the check box. - Enabled and Visible are properties that you can change programmatically to disable or render invisible an option button or a check box following a previous selection in another control of the userform. Frames Frames are also a passive control. Frames are used to improve the layout of the userform. You can use them around a group of controls that have something in common. Frames become more important to manage option buttons. If you have two sets of option buttons on a userform and you don't place them within a frame they all work together and you can choose only one. If you put each set within a frame you can choose one in each set. When you move a frame all its controls move with it. VBA Chapter 24G of 24: Spin Buttons In the toolbox the spin button has this icon .

You can ask a user to enter a value directly in a text box but you can make things a little fancier by using a text box and a spin button. The spin button is not really used by itself. Because the spin button doesn't show its value it is usually used with a text box. The text box

shows a number and by clicking on the arrows of the spin button the value in the text box is increased (or decreased) by 1, or 5 or 10...by whatever value set within the properties of the spin button. Properties The other interesting properties of the spin buttons are: - Min is the minimum value of the spin button. It can be negative - Max is the minimum value of the spin button. It can be negative - Small Change is the value of the change when the user clicks on the arrows - TabIndex is a property that you change through the " Tab Order" functionality as shown in the UserForms section. Code On the " Change" event of the spin button you will have code modifying the value of the text box that accompanies it: tbxInput=spbInput

...Discover More in the Downloadable Tutorial


What more in the Spreadsheets

See how this userform (pricing application) is built with detailed explanations in "vba-form-spin-button.xls"

VBA Chapter 24H of 24: Multi Pages In the toolbox the multi page has this icon .

The multi pages control is used to develop more elaborate userforms where the user can see a different set of controls on each of many pages. In the example below (developed by excel-vba.com for SCI as front end to the database created by HMIS) the user can choose a set of primary parameters on one page and a set of secondary parameters in the second page.

AppActivate "Microsoft Excel" or AppActivate "Microsoft Word" or AppActivate "Microsoft Internet Explorer" If you the want to shut it down: SendKeys "%{F4}", True Installation: Place the DLL file of your choice in any directory you choose (C:\Windows\System may be a good place). Name the file SKey.dll. Click Start, Run, and type:

regsvr32 C:\Windows\System\SKey.dll (or wherever you put the file) You should receive a message stating that registration of the DLL was successful. SKey.dll is an ActiveX DLL originally created simply to provide a method for Windows Scripting Host (WSH) scripts to send keystrokes to other applications. It was just an ActiveX wrapper for the Visual Basic SendKeys statement. Since the original development, the suggestion was made to add a method for setting the focus to a specific window, so I have added the ActivateApp method.

The ActivateApp method bSuccess = objKeys.ActivateApp "Microsoft Word" The ActivateApp method takes one parameter, a string, and activates the window whose title is the string provided. If no window matches exactly, the window whose title starts with the string provided will be activated. If multiple windows' titles start with the string provided, a random window will be activated. This method returns zero (0) if the method fails (no title matched or started with the provided string) and one (1) if the method succeeds.

The SendKeystrokes method objKeys.SendKeystrokes("The quick brown fox") The SendKeystrokes method takes one parameter, a string, and sends the keystrokes provided in that string to the active window. The syntax for these keystrokes is provided in the Visual Basic documentation. The following information is taken from Microsoft's Visual Basic documentation. Copyright belongs to Microsoft, and I take no credit for the information presented here. The original document appears at: http://msdn.microsoft.com/library/devprods/vs6/vb/html/vastmSendKeys.htm Each key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string.

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify brace characters, use {{} and {}}. To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes shown below: Key
BACKSPACE BREAK CAPS LOCK DEL or DELETE DOWN ARROW END ENTER ESC HELP HOME INS or INSERT LEFT ARROW NUM LOCK PAGE DOWN PAGE UP PRINT SCREEN RIGHT ARROW SCROLL LOCK TAB UP ARROW F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12

Code
{BACKSPACE}, {BS}, or {BKSP} {BREAK} {CAPSLOCK} {DELETE} or {DEL} {DOWN} {END} {ENTER} or ~ {ESC} {HELP} {HOME} {INSERT} or {INS} {LEFT} {NUMLOCK} {PGDN} {PGUP} {PRTSC} {RIGHT} {SCROLLLOCK} {TAB} {UP} {F1} {F2} {F3} {F4} {F5} {F6} {F7} {F8} {F9} {F10} {F11} {F12}

F13 F14 F15 F16

{F13} {F14} {F15} {F16}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes: Key
SHIFT CTRL ALT + ^ %

Code

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC". To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times. Note You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows. Sendkeys also can't send the PRINT SCREEN key {PRTSC} to any application. 1998 Microsoft Corporation. All rights reserved. Example using VBScript: Dim objKeys Dim bSuccess Set objKeys = CreateObject("SKey.SKeys") bSuccess = objKeys.ActivateApp "Microsoft Word" If bSuccess Then objKeys.SendKeystrokes("The quick brown fox") Set objKeys = Nothing The same example using Javascript: var objKeys = new ActiveXControl("SKey.SKeys"); var bSuccess = objKeys.ActivateApp("Microsoft Word"); if (bSuccess) {

objKeys.SendKeystrokes("The quick brown fox"); }


This utility is an Windows application of only one file, the SendKeys.exe, that can be executed with line command arguments to send keystrokes to an application window running on the Windows in an independent way and with the control of the start, the throughout and the end of the sending. Its interesting to be called from scripts that hasnt SendKeys statement like DOS BATs or from codes where available Sendkeys statement doesnt offer an adequate control to be called from determined positions of a procedure like in VBA

Features:
- Can be defined a wait in seconds before to start the keystroke sending - Can be defined a wait in seconds to give up, if the target windows aren't found - The target window can be the active windows or one with defined title - The syntax of the string to be sent is the same of the VBA Sendkeys statement with the following additional features not existent in VBA: {PAUSE} - Pauses 1 second before sending the following keys {PAUSE n} - Pauses n seconds before sending the following keys {PRTSC} - Sends 'Print Screen' key to capture all screen %{PRTSC} - Sends 'Alt + Print Screen' keys to capture only active window ^%{DEL} - Sends 'Ctrl + Alt + DEL' keys to POWER OFF the computer, IF POSSIBLE ^%{DEL 1} - Sends 'Ctrl + Alt + DEL' keys to POWER OFF the computer, FORCIBLY ^%{DEL 2} - Sends 'Ctrl + Alt + DEL' keys to RESTART the computer, IF POSSIBLE ^%{DEL 3} - Sends 'Ctrl + Alt + DEL' keys to RESTART the computer, FORCIBLY ^%{DEL 4} - Sends 'Ctrl + Alt + DEL' keys to LOGOFF the user, IF POSSIBLE ^%{DEL 5} - Sends 'Ctrl + Alt + DEL' keys to LOGOFF the user, FORCIBLY. - Runs in silence and, optionally, until without fail alert - Can register the execution end saving a log file (Sendkeys.log) in the disk that can be used for synchronization - Can be realized simultaneous instances and keys can be sent to more of one window simultaneously - Running SendKeys.exe without arguments, it shows a dialog with a quick help, a command to test and other command for more detail help. Tested with Microsoft Office for Windows, versions 97 (See Note), 2000, 2002(XP) and 2003. Download the English version here You can download too from this mirror site Last update: 08/25/2007 - 69.6kb Freeware based in this License Agreement

Sintaxe:
Path\SendKeys.exe Arg1 Arg2 Arg3 Arg4 [Arg5] Arguments are separated by spaces and Arg5 is optional. Arg1 - Wait in seconds before start the sending, after activate Arg2 - Wait in seconds to give up, if the target windows weren't found Arg3 - String of target window title (See Note1 and 2) Arg4 - String with keystroke to be sent (See Note1 and 3 )

Arg5 - Optional: 1 - Run in silence without fail alert; 2 - Save a log file (Sendkesy.log) on the disk to mark the execution end. To synchronize, a script in DOS BAT or in VBA can kill this log file, send the keys and pause until it will be created again. Note1: Quotations marks are necessary, if there is space in the string. Note2: In place of the window title you can use too the number returned by Shell function. Empty title (Quotation marks without text) sends keys to the active window. Note3: To send special keys, see VBA help for the Sendkeys statement. The only one difference is the character (Alt+0254) that here has a special meaning; It generates a pause of 1 second before of the sending of the following keys and it can be repeated to generate larger pauses. See Complete Sytax of the String to Send. Examples: 1 - In DOS BAT or in the dialog box of Windows run command it can be something like: SendKeys.exe 1 10 ""Untitle - Notepad"" ""Hello!~After of 1s.~After of 3s.~" 2 In VBA you use the Shell function as in the below test code: Dim Ret Shell "Notepad.exe", vbNormalFocus Ret = Shell("Path\SendKeys.exe 1 10 ""Untitle - Notepad"" ""Hello!~After of 1s.~After of 3s.~"", vbNormalFocus)

Complete Sytax of the String to Send:


Each key is represented by one or more characters. To specify a single keyboard character, use the character itself. For example, to represent the letter A, use "A" for string. To represent more than one character, append each additional character to the one preceding it. To represent the letters A, B, and C, use "ABC" for string. The character (Alt+0254) has a special meaning. It generate a pause of 1 second before next keystroke sending and it can be repeated to generate larger pauses. The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses ( ) have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use {+}. Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that may be significant when dynamic data exchange (DDE) occurs. To specify brace characters, use {{} and {}}. To specify characters that aren't displayed when you press a key, such as ENTER or TAB, and keys that represent actions rather than characters, use the codes shown below: Key BACKSPACE BREAK CAPS LOCK DEL or DELETE DOWN ARROW END ENTER ESC HELP HOME INS or INSERT {BREAK} {CAPSLOCK} {DELETE} or {DEL} {DOWN} {END} {ENTER}or ~ {ESC} {HELP} {HOME} {INSERT} or {INS} Code {BACKSPACE}, {BS}, or {BKSP}

LEFT ARROW NUM LOCK PAGE DOWN PAGE UP RIGHT ARROW SCROLL LOCK TAB UP ARROW F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16

{LEFT} {NUMLOCK} {PGDN} {PGUP} {RIGHT} {SCROLLLOCK} {TAB} {UP} {F1} {F2} {F3} {F4} {F5} {F6} {F7} {F8} {F9} {F10} {F11} {F12} {F13} {F14} {F15} {F16}

These, although they dont exist in VBA, are supported here too: Key Print Screen Pause 1 second Pause n seconds Code {PRTSC} {PAUSE} {PAUSE n}

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes: Key SHIFT CTRL ALT Code + ^ %

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC". To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times. Note You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows

Controlling Calculation from VBA.


For information on what the calculation methods do see Calculation Methods. VBA allows you to control Calculation methods, properties and events:

Calculation Methods:

F9 - Recalculate
Application.Calculate

CTRL/ALT/F9 Full Calculation


In Excel 2000, 2002 and 2003:
Application.CalculateFull

In Excel 97 there are two possible methods using either SendKeys or EnableCalculation: You can use SendKeys to send the CTRL/ALT/F9 key sequence. This can be tricky because Sendkeys just sends the keystrokes into the keyboard buffer, and they are not normally processed until VBA has ended and you cannot guarantee that they will be processed by the correct window/application etc. With Windows 95/98/ME:
SendKeys "%^{F9}", True

Use Sendkeys rather than Application SendKeys. The True argument causes VBA to wait for the calculation to proceed without interruption. This is required unless the Sendkeys statement is that last VBA statement to be executed other than End. The True argument does not work with Application.SendKeys

With Windows NT/2000/XP:

Application.SendKeys "%^{F9}" DoEvents

If the VBA procedure containing the Sendkeys statement is called directly or indirectly from a command button the True argument does not work, so you have to use DoEvents to get Win Xp and Excel97 to process them. Another method uses the worksheet.enablecalculation property. When this property is changed all the formulae on the worksheet are flagged as uncalculated, so toggling the property to false and then back to true for all sheets in all open workbooks will cause the next calculation to be a full calculation.
Dim oSht as worksheet Application.Calculation=xlCalculationManual for each oSht in Worksheets oSht.enablecalculation=false osht.enablecalculation=true next osht Application.calculate

You can also use this method for a single worksheet to do a full sheet calculate.

Full Calculation with Dependency Rebuild


In Excel 2002/2003 only: Application.CalculateFullRebuild In prior versions of Excel you can achieve the same effect by switching to manual, replacing all equal signs in formulae with equal signs and then either switching back to automatic or doing a manual full calculation (Ctrl/Alt/F9)

Shift F9 Sheet Recalculate


Worksheets(SheetName).Calculate

Range Calculate: see Calculation Methods for details and limitations


Range($a$3:$Z$9).Calculate

Evaluate Method:
You can use the Evaluate method of the Application, Worksheet or Chartobject to return the result of calculating a string containing Names, Ranges and/or a Formulae to VBA without altering anything on a worksheet. Evaluate also enables you to get the results of a single-cell or multi-cell array formula passed to Evaluate as a string. There are two

different syntaxes for Application.Evaluate, for example as Evaluate("SUM(A1:A20") or as [SUM(A1:A20)] The difference between using Application.Evaluate and Worksheet.Evaluate is not spelled out in Excel's help. Application.Evaluate the string as though it was on the active sheet, but Worksheet.evaluate evaluates the string as though it was on the referenced sheet: If Sheet1!A1 contains 'fred' and Sheet2!A1 contains 'Joe', and Sheet 1 is the active sheet then Evaluate("A1") returns 'fred' but Worksheets("Sheet2").Evaluate("A1") returns 'Joe'

Evaluate Method limitations:


The string must be less than 256 characters. All references in the string must be in A1 format rather than R1C1 format. Relative references in the string are treated as absolute, unless they are contained in defined names in which case the defined name is evaluated with respect to cell A1. Dates should be in USA format (Month-Day-Year). Evaluate always treats string formulae as array formulae. Evaluate will return an error value if the string formulae contains external references to closed workbooks or XLM functions. If the string formula contains a reference to an UDF it seems to be evaluated twice. if the string formula contains a reference to both a UDF and a name it will fail with an error 2029 if the name reference occurs AFTER the UDF reference: o If fred is a named range and xyz() is a user defined VBA function then this statement returns error 2029: application.Evaluate("=xyz(b1)+fred") o This statement returns the correct value:
application.Evaluate("=fred+xyz(b1)") o

Microsoft KB article 823604 identifies this problem but does not correctly diagnose the circumstances that cause it.

You can bypass many of these limitations (at the cost of performance) by inserting the formula string into a worksheet cell and then reading the resulting cell value back into a VBA variable.

Evaluate error handling:


If Evaluate cannot evaluate the string it returns an error rather than raising an error, so to trap the error you need to assign the result to a variant and then check the variant using ISERROR.
Public Function EVAL(theInput As Variant) As Variant ' ' if UDF evaluate the input string as though it was on this sheet ' else evaluate for activesheet ' Dim vEval As Variant Application.Volatile On Error GoTo funcfail

If not IsEmpty(theInput) then If TypeOf Application.Caller.Parent Is Worksheet Then vEval = Application.Caller.Parent.Evaluate(Cstr(theInput)) Else vEval = Application.Evaluate(cstr(theInput)) End If If IsError(vEval) Then EVAL = CVErr(xlErrValue) Else EVAL = vEval End If End If Exit Function funcfail: EVAL = CVErr(xlErrNA) End Function

The EVAL function can be called either as a UDF or from VBA. If the input string resolves to an array formula then EVAL will return the corresponding array of results. EVAL must be volatile because Excel cannot detect which cells the input string argument refers to, and hence does not know when the function needs to be recalculated. EVAL returns #Value if the string cannot be evaluated, and #N/A if the function encounters any other error.

Properties controlling Calculation Options:

Application.Calculation
Can be set to xlCalculationAutomatic, xlCalculationManual or xlCalculationSemiAutomatic, (in Excel95 these were xlAutomatic etc). Resetting calculation to xlCalculationAutomatic will trigger a recalculation. Setting these properties sometimes fails with a 1004 error when the code is called from the event code of a Control Toolbox control. You can bypass this problem by setting the TakeFocusonClick property of the control button to false, or for other controls by preceding it by Activesheet.Activate.

Excel's Initial Calculation Setting


FastExcel V2 allows you to control Excels initial calculation sequence. Excel sets the initial calculation mode from the first non-template, non-addin workbook opened, or created and calculated. This means that the calculation mode setting in subsequently opened workbooks will be ignored.

If you need to override the way Excel initially sets the calculation mode you can set it yourself by creating a module in ThisWorkbook (doubleclick ThisWorkbook in the Project Explorer window in the VBE), and adding this code. This example sets calculation to Manual.
Private Sub Workbook_Open() Application.Calculation = xlCalculationManual End Sub

Unfortunately if calculation is set to Automatic when a workbook containing this code is opened, Excel will start the recalculation process before the Open event is executed. The only way I know of to avoid this is to open a dummy workbook with a Workbook open event which sets calculation to manual and then opens the real workbook.

Application.CalculateBeforeSave
True or False. If calculation is Manual and CalculateBeforeSave is true when you Save the workbook it will be recalculated.

Application.Iteration, MaxIterations and MaxChange


If Application.Iteration is true and the workbook contains circular references, then calculation will iterate until either the number of iterations reaches MaxIterations or the largest change in cell value in the latest iteration is less than Maxchange.

Workbook.PrecisionAsDisplayed
If True the workbook will be calculated using the number of decimal places in the formatting.

Workbook.Date1904
If true date calculations in the workbook will be based on 1904.

Workbook.AcceptLabelsInFormulas
If true Excel will use "Natural Language" labels in formulae. For anything other than simple models this should be turned off.

Worksheet.EnableCalculation; Preventing Specific Worksheets being Calculated


FastExcel Version 2 gives you an improved implementation of this facility, called Mixed Mode sheets, which allows you to control which sheets will be recalculated by which type of calculation event, and saves your settings with the workbook.

Setting the Worksheet property EnableCalculation to False will prevent Excel from including the worksheet in a recalculation. This will stop the sheet being recalculated by Automatic Recalculation, F9, Ctrl/Alt/F9 and by Sheet.Calculate, Application.Calculate, Application.CalculateFull and Application.CalculateFullRebuild. The EnableCalculation property has no effect on Range.Calculate. If you use Range.Calculate on a formula on a worksheet that has EnableCalculation set to false then at the next recalculation Excel will recalculate any formulae on other sheets that are dependent on that formula. Setting enablecalculation to false and then back to true will flag all the formulae on the worksheet as uncalculated. If the calculation mode is Automatic a recalculation will be triggered. All calculation methods, including Sheet.Calculate but excluding Range.Calculate, will then calculate all the formulae on the sheet. You can use this method as a way of simulating Calculatefull at worksheet rather than workbook level. Note that Sheet.Calculate will not reset the uncalculated formulae flags, so two Sheet.Calculates in succession after toggling the EnableCalculation property will both do a Full Sheet Calculate. The property is reset to true when a workbook is opened.

Calculation Events:

Application or Workbook SheetCalculate


This event occurs after any worksheet is recalculated or changed chart data is replotted.

Worksheet or Chart Calculate


This event occurs after the Worksheet is recalculated or the chart's changed data is replotted.

Excel 2002/2003 Only


Excel 2002/2003 considerably enhances your ability to control calculation from VBA:

Adding specified cells to the calculation list


You can use Range.Dirty to add the specified cells to the list of cells requiring calculation at the next recalculation. There is a bug in Range.Dirty. It always acts on the currently active worksheet rather than the worksheet the Range object actually refers to.

Checking Calculation Status


The Application.CalculationState property allows you to check if calculation has completed ( xlDone ), is pending ( xlPending) , or is in process ( xlCalculating ). The Pending state seems to correspond to the message Calculate in the statusbar: for workbooks with more than 65536 dependencies the CalculationState is always xlPending or xlCalculating.

Interrupting Calculation
You can control the users ability to interrupt calculation using
Application.CalculationInterruptKey= XlAnyKey | XLEscKey | XlNokey

Application.CheckAbort
According to Help Application.Checkabort is supposed to stop recalculation except for a specified range. It does not do this. Apparently it throws a runtime error if there are pending abort messages in the app message queue (mouse clicks, esc key down etc). The parameter specifies whether to eat the message or leave it in the queue. The objective is to allow a a long-running VBA calculation to be interrupted in the same way as an Excel calculation.

S-ar putea să vă placă și