Troubleshooting Simple Code Problems Project Management Help

What can go wrong? Visual Basic is expecting very specific kinds of information. If you forget, for example, to put – True for the Create property, VB will stop executing in the middle of your code arid open a message box:

Troubleshooting Simple Code Problems

Troubleshooting Simple Code Problems

Understanding Parts and Functions of VBA Code

To work with VB,you need to know more about the syntax of the language-the rules that govern how the language works. Just as most human languages have widely recognized parts (such as nouns, verbs, adjectives, and so on), programming languages break
down into a number of parts that you put together in statements: arrangements defined by the syntax of the language.

You put together statements by using combinations of keywords, objects, properties, methods, constants, and variables, with required arguments and optional arguments specifying information. In the next sections, we’ll look at what each of these terms mean and how you work with them.


keyword is a word defined as having a meaning in Visual Basic. Keywords include object names, properties, methods, and argument names. It’s possible to create variables, constants, and procedures that have the same names as Visual Basic keywords.’ This is called shadowing a keyword, and isn’t usually a good idea because it becomes easy to get confused. For example, there’s an object called Basel; neCost, so it’s a bad idea to create a variable, constant, or procedure named Base line Cost.

As we’ll see in a little while, you can name your variables, constants, and procedures pretty much anything that strikes your fancy, so there’s no real reason to shadow a keyword- but there are so many keywords in Project 2000 that it’s surprisingly easy to shadow a keyword accidentally


Groups of objects are organized into collections, which provide an easy way to access the objects. For example, the Resource Groups collection contains a collection of Group objects, each of which represents a “group” of resources. The collection Task Groups also contains a collection of Group objects, each of which represents a “group” of tasks.Usually, the name of a collection is the plural of the object, Calendars is the collection of Calendar. The Group collection objects example is an exception, however, where the same object can be collected into two different collections (confused yet?). Collections themselves are objects, too. To reduce confusion, from now on, we’ll refer to any collection as a collection rather than a collection object


A constant is an item in memory that keeps an unchanging piece of information while a program is executing. You can specify the appropriate constant in your code instead of the corresponding value, because it provides an easy way of handling complex information or information that may change from computer to computer, such as the location of a particular Project file. Project uses constants to signify frequently used information, such as tl!! month of the year (for example, the constant pjApri 1 in the pj Month group of constants represents April) and the options available while performing actions (for example, pj Date Format contains more ways to format the date on Project views and reports than you could possibly use), Each constant has a numeric value associated with it; for example, the constant pjDate_mm_dd-yyyy in the pj Date Format group has the numeric value 20 associated with it. When using VBA to automate Project procedures, you can use either the descnptive constants or the numeric values to specify the constant you wish to assign .


A variable is a location in memory that you set aside for storing a piece of information while a procedure is running. You can create as many variables as you need, and you can ‘give them any name that meets Visual Basic’s naming rules. As we mentioned before, it’s a bad idea to shadow a keyword by giving a variable the same name.

The rules for creating names for variables in Visual Basic and VBA are simple:
• A name can be up to 40 characters long.
• A name must begin with a letter.
• A name can include letters, numbers, and underscores.
•A name cannot include spaces or symbols other than the underscore.
• A name must be unique within its scope.

Scope is discussed in “Creating Variables” later



Data Types

Visual Basic and VBA include a range of variable types, called data types, designed for particular kinds of data. For example, you would store a string of text such as a word or phrase in a string variable, whereas you would store an integer (whole number) value in an integer variable. Table 24.1 lists the data types available in VBA,and the range of values available for each type. You can also create user-defined types by using the Type statement in VBA See the Visual Basic help files for more information on creating user defined data types.



The Variant data type is the default data type, so it is automatically used if you don’t specify a data type when you declare a constant, variable, or argument. Variables declared as the Variant data type can contain string, date, time, Boolean, or numeric values. Variant data types take up more storage space than other types, and should be avoided unless the. data type is unknown.





Creating a Variable

You can create a variable in two ways: by declaring it explicitly (listing-it at the beginning of a procedure) or by creating it implicitly (by simply using it in your code),

Declaring a Variable Explicitly

When you declare a variable explicitly, you can also set the variable’s scope, which determines where the variable will be available.

There are three types of scope:

• Procedure level: the variable is only used in the procedure.
• Private level: the variable can be used by any procedure in the same module .
• Public level: the procedure can be used by any project open at the same time as the module that contains the procedure.

To declare a variable explicitly, use a D;m statement, a Private statement, or a Pub 1; c statement. Variable declarations are usually listed at the top of a subroutine, immediately following the opening Sub line and any user documentation, so they’re available
when needed in the procedure.

To declare a variable with procedure-level scope, use a Dim statement within the procedure.

The following procedure asks the user for an estimate date. The second line of code declares a procedure, level variable called dteEst Date (a date variable). The third statement displays ail input box and assigns its result to the dteEst Date variable.

Private and Public scope variables are declared at the module level: code at the beginning of the module prior to any procedures. All private and public variables must be declared explicitly with module level code

To declare a variable with Private scope, use a Private statement prior to the procedure at the beginning of the module. The following statement creates an integer variable named intDaysPastEst with Private scope:

private int Days Past Est As Integer
To declare a variable with Public scope, use a Pub’ i c statement prior to the procedure at the beginning of the module:
Public objExcelChart As Object

Creating a Variable Implicitly

Instead of declaring a variable explicitly. you can create it implicitly by typing the variable’s name in code. The following statement implicitly creates the variable i by assigning the value 123 to it:

int My Value – 123
. When you create a variable implicitly, it has procedure-level scope only. and is assigned the Variant data type.

Declaring Variables

If you’re creating variables with procedural scope, it’s still a good idea to declare them explicitly.When you declare variables implicitly ,every typo becomes a new variable. It’s easy, for example, to mistype ; nt Expr Date as; nt Exp Date. With implicit variable creation, you now have two variables. You can spend a long time trying to determine why your code isn’t working as designed

There’s another reason to use explicit declaration: your code is easier to change or troubleshoot when you can easily see the variables used in a procedure at the top of the procedure’S code




Posted on November 26, 2015 in Automating Project 2000 with VBA

Share the Story

Back to Top

Pin It on Pinterest

Share This