Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.

Similar presentations


Presentation on theme: "Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a."— Presentation transcript:

1 Chapter 4 Getting Started with VBA

2 Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a macro. A sub is any set of code that performs a particular task. Sub can contain one line of code or hundreds. Subs can be used to break up a lengthy program into smaller more manageable parts. You can have a main sub that calls other subs or functions. Program is a collection of subs that achieve an overall goal. Each sub has a name which can be a mixture of letters, numbers, and the underscore character. All subs must start with the key work Sub and end with End Sub. Remember, all reserved words are colored Blue in VBA. The parenthesis next to a sub’s name are for arguments and are generally used with private subs that are not the main sub. It is common practice to place several related subs in the same macro or module. You can execute the macro using the Run button in the VBA editor, use the F5 key, use the Run/Run Sub/UserForm menu item, or create a button.

3 Understanding and Declaring Variables Variable are temporary storage areas. They can store values, True/False, and labels. Variable types: –String can store text –Integer stores whole numbers between +- 32,000 –Long stores whole numbers between +- 2 billion –Boolean is a logical variable stores True or False –Single stores real numbers up to seven decimals long –Double stores real numbers up to fourteen decimals long –Currency stores monetary values –Variant can store any other variable type –Dim is the reserved word used to create variables Option Explicit forces all variables used in the macro to declared with the Dim statement Option Explicit should be the first statement entered into the code window Object variable points to an object. This could allow you to use a range name for a several adjacent cells. –Example: Dim SRange As Range – Set SRange = ActiveWorkbook.Worksheets(“Data”).Range(“Scores”) –From that point on you can refer to the range as SRange –When defining an object use the Set statement on the left side of an assignment statement

4 Input Boxes,Message Boxes, Concatenation, Comments, and Strings The Input Box function takes at least one argument: a prompt. A second argument that is often used is the title that appears at the top of the dialog box. The generic dialog box has OK and Cancel buttons, a title, a prompt, and a text box for the user’s input. The MsgBox function takes at least one argument: a message that you want to display. Two other optional arguments often used are a button indication and a title. Message boxes with Yes and No buttons use the vbYesNo MsgBox InputBox(“Type your name.”,”User’s name”), vbInformation, “User’s Name” will make message box the result of the InputBox function. The underscore character allows you to continue statements on more than one line. Whether to use parentheses depends on whether or not you wish to capture the results from the message function. Parentheses are required when the result is captured in a variable or used in some way. Parentheses are optional when no result is being captured or used in someway. Concatenation character (ampersand &) will bind together two variables, strings, or some combination. Comments are used to document your work and should be used when appropriate. They are denoted by an apostrophe and have the color green. They are non-executable statements and the computer will ignore then. Strings are labels or text mixed with numbers and symbols. When assigning text or labels to strings use quotation marks around the text or label.

5 Displaying Text and Values Text can be displayed using a message box or by writing the contents of a string variable or literal to a particular cell. Values can be formatted using: Format$(variable,”number of decimals”) Currency formated using FormatCurrency function The Format function can be used for customized formats. String functions Right, Left, Mid, and Len can be used to extract certain parts of the string.

6 Specifying Objects, Properties, and Methods Specifying a member of a collection use the plural name of the collection, with the particular member specified in parentheses and enclosed inside quotes. Specifying objects down a hierarchy separate them with a period, with objects farther down the hierarchy to the right. An object to the right is qualified by any objects listed to its left. Specifying a property of an object list the property name to the right of the object separated by a period. Specifying a method for an object list the method name to the right of the object separated by a period. With/End With is a shortcut that can be used with objects an their properties and methods. You must use the End With and indent when appropriate. VBA tips: –Screen Updating: Application.ScreenUpdating = False –Display Alerts: Application.DisplayAlerts = False –Timer function: Allow you to see how long it takes the program to execute –StartTime = Timer –Statements –ElapsedTime = Timer – StartTime –MsgBox “This section took “ & ElapsedTime & “ seconds to run.”


Download ppt "Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a."

Similar presentations


Ads by Google