<<

Fundamentals

l We build up instructions from three types of materials ¡ Constants ¡ Variables ¡ Expressions

Fundamentals Fundamentals l Constants are just that, they are values l Variables are names assigned to that don’t change as our macros are locations in computer memory where we executing want to store things that may change are the macro executes l Think of them as named mailboxes

1 Fundamentals Fundamentals l Expressions or statements are the l As a rule in this class, you will always start your instructions that tell the computer or code with your name, the class, the date, and EXCEL just what to do the assignment number l For example

Fundamentals – Fundamentals – Variables Variables l There really aren’t that l Boolean many kinds of things that ¡ This is a data type that is use we deal with so keeping to store LOGICAL values them straight shouldn’t be ¡ True or False much of a problem ¡ Nothing else l Is you get confused, check the help menu for data types

2 Fundamentals – Fundamentals – Variables Variables l Integer l Integer ¡ This one is important and ¡ There are no decimal places used lots in an integer number and ¡ An integer number is just a with integers can whole number lead to some interesting ¡ No decimal places results because of the lack of decimal places ¡ Notice the range of values, if you go beyond that range, you have to use another type ¡ -32,768 to 32,767

Fundamentals – Fundamentals – Variables Variables l Long l Single ¡ If you need an integer to go ¡ This is the first of the real beyond the range provided number types by the INTEGER type, you ¡ It includes decimal fractions can use the LONG type and will store ranges from +/- ¡ This is still an integer but it 3.4 x 1038 down to +/- 1.2 x has a range of + or – over 2 10-45 Billion

3 Fundamentals – Fundamentals – Variables Variables l Double l String ¡ If you need more precision ¡ The only other data type we than what is provided by the are going to worry about right SINGLE type, you can use now is the STRING data type the DOUBLE type ¡ This is used to hold strings ¡ It ranges out from 10308 to made up of letters, blanks, 10-308 numbers, and symbols

Fundamentals – Fundamentals – Constants Variables l Constants come in each type l The syntax of the statement is l A number or string that won’t change in the ¡ Dim variablename as type program is a constant l variablename is the name of the location where you want to store the information

4 Fundamentals – Fundamentals – Variables Variables l The syntax of the statement is l So the statement ¡ Dim variablename as type ¡ Dim Radius As Single l type is the type of information that you want to l Sets us a storage location named Radius to store in the named location hold a single precision real number

Fundamentals – Fundamentals – Variables Variables l VBA allows us to dimension (type) multiple l Each variable name must be typed variables on the same line but you have to be independently and separated by a comma from careful here the other types

5 Fundamentals – Fundamentals – Variables Variables l Three common mistakes l Every cell (as well as groups of cells, charts, ¡ Dim Radius, Height, Volume as Single and other things) is actually an object in ¡ Dim Radius as Single, Dim Height as Single EXCEL and so can be treated as an object in ¡ Dim as Single, Radius, Height, Volume VBA

Fundamentals – Fundamentals – Variables Variables l In this case, we are l We do the selection using what is known by using the .Select as a Range object Method l We are selecting the l Methods are just cells from A1 to F17 special ways of inclusive as our handling objects and object their properties

6 Fundamentals – Fundamentals – Variables Assignment statements l We are able to set up variables to hold objects l An assignment statement reads by typing the variable according to what type of ¡ Place what the right side of the expression evaluates object the variable will hold to into the location on the left side of the expression l For example to set up a range object we would ¡ The left side and right side are separated by an equals sign (=) use the specification statement ¡ Note that the right side is evaluated and then placed into the left side l Dim WhatCells as Range ¡ The left side is just a location

Fundamentals – Fundamentals – Assignment statements Assignment statements l For example the assignment statement l Since we have an evaluation, then a ¡ X = 1. replacement, a statement like l Would evaluate the right side ¡ X = X + 1. ¡ A constant evaluates to itself l Is completely proper and meaningful l Then since there is nothing else to evaluate it l The computer looks at the right side first would store the result in the memory location ¡ It gets the value stored in X labeled X ¡ Adds 1 to it ¡ Store the result in X

7 Fundamentals – Fundamentals – Assignment statements Order of Operations l If X had 21 originally stored in it and the l For ARITHMETIC evaluations, VBA has a expression number of operators ¡ X = X + 1 ¡ - (uniary minus) l Was executed, after the execution of the ¡ ^ expression, X would contain 22 ¡ * and / multiply and divide l Since X is a variable, it can change values ¡ + and – add and subtract during the execution of the program

Fundamentals – Fundamentals – Order of Operations Order of Operations l All of the operators beside the first take two l The order in which arithmetic expression are arguments evaluated are l The uniary minus only take one argument ¡ Uniary Minus ¡ - (uniary minus) ¡ Exponentiation ¡ ^ exponentiation ¡ and ¡ * and / multiply and divide ¡ and ¡ + and – add and subtract

8 Fundamentals – Fundamentals – Order of Operations Order of Operations l For operators of equal precedence, they are l We have a way to circumvent the normal evaluated in left to right order arithmetic order of operations is we choose to ¡ Uniary Minus do so ¡ Exponentiation l Anything that we enclose in parentheses will be ¡ Multiplication and Division evaluated first ¡ Addition and Subtraction l This order goes from inside to out

Fundamentals – Building a macro Order of Operations l Parentheses allow us to control the order of And finally we write the modified value back in to the evaluation of an arithmetic expression call A1 l Parentheses are evaluated from the inside out when they are nested l For every open parenthesis ( there must be a closed parenthesis )

9 The Next Step Making a Decision

l For example, if we wanted to look at what We need a way to make a decision based on what the value we just was in a cell and decide how to treat it stored in checkVar based on if it was even or odd (integers only) ¡ If the number is even, divide it by 2 and display the result in red ¡ If the number is odd, add 1 to it then divide by 2 and display the result in blue

Making a Decision Making a Decision

Logical expression evaluation to either true or false, not to a number. There are actually two types of operators that are used in logical expressions: This is critical to know the difference because Visual Basic sometimes tries to make up for your mistakes with tragic results. Relational Operators Logical Operators

10 Making a Decision Making a Decision

Relational operators are based on the relation ship of one argument In both cases, we could answer true or false to the proposed rel ation. to another If X = 1, Y = 3, and Z = 5 then

Is X larger than Y? Is X larger than Y? à False Is 1.2 not equal to Z? Is 1.2 not equal to Z? à True

Making a Decision Making A Decision

While we can store the evaluation of a relational expression int o a We do this by using what is known as an IF- THEN- ELSE- ENDIF Boolean variable, what we usually do is use the results of the construct. evaluation of the expression to control execution of the program. Logical Condition

Do this if the Do this is the condition is condition is true false

11 Making A Decision Making A Decision

The diagram is known as a flow chart and represents what action the You can think of it as moving along program takes as it “flows” through execution. one way streets, from top to bottom, as the program executes. Logical Logical Condition Condition In this case we get to the decision, based on the decision we can either

Do this if the Do this is the go down the right or left path, and Do this if the Do this is the condition is condition is condition is condition is true false then eventually they converge and true false we go on.

Making A Decision Making A Decision

In our case, the logical condition will The relational operators are be the evaluation of a relational comparison operators and always expression evaluate to a logical value (TRUE or Logical Logical Condition FALSE) Condition Relation expressions are made up of two arguments connected by a The relational operators are very similar to Do this if the Do this is the Do this if the Do this is the condition is condition is = equal to condition is condition is arithmetic expressions which have true false true false an arithmetic operator and two <> Unequal to arguments < Less than > Greater than <= Less than or equal to >= Greater than or equal to

12 Making A Decision Making A Decision

Here is where confusion can creep Previously, we utilized it as a an in. Now we are using the equals sign assignment to replace the as a relational operator which is left side with the evaluation of the Logical Logical comparing the value of two Condition right side Condition arguments. Here we use it to give the logical

= equal to Do this if the Do this is the value based on the equality of its Do this if the Do this is the condition is condition is condition is condition is true false arguments true false <> Unequal to = equal to < Less than <> Unequal to > Greater than < Less than > Greater than <= Less than or equal to <= Less than or equal to >= Greater than or equal to >= Greater than or equal to

Making A Decision Making A Decision

Since we almost always use these In our problem, we need to decide if relational operators within the a number is even or not context of process control, the Logical An even number is evenly divisible Logical context will determine which equal Condition Condition by 2 so we need to incorporate that sign meaning we have into our programming

It isn’t as bad as it seems if you are Do this if the Do this is the Do this if the Do this is the condition is condition is We can do this by taking advantage condition is condition is careful true false true false of integer arithmetic = equal to <> Unequal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to

13 Making A Decision Making A Decision

Remember that integers have no Luckily, Visual Basic has a function decimal part and that we actually set which allows us to divide two up places in memory, variables, to integers by each other and gives us specifically hold integers the remainder If we took 3. and divided it by 2 we The function is know as MOD and would get 1.5 takes two arguments If we then tried to store the 1.5 into Notice that we use it is the same an integer, we would store a result way we used binary arithmetic that we are not sure of operators If we run this code, we get 2 in our Argument Operator Argument selected cell

Making A Decision Making A Decision

Also, like other arithmetic operators, If you look for the Mod operator in it evaluates to a number the help section you will get the result shown here so we will know In this case, it is the remainder from how to use the operator in our the division of one integer divided by expression another Any even number divided by 2 will You may have noticed that one of have 0 as a remainder so all we have the arguments isn’t an integer to test is what the Mod operator Visual Basic made it one for this returns from our number and 2 expression Argument Operator Argument

14 Making A Decision Making A Decision

No we can begin to put all of this What we have done is to build the together paths we saw earlier First we need to put in the decision We start with If then an expression structure that evaluates to a logical value (True or False) IF- THEN- ENDIF We then provide instructions to execute if the logical expression evaluates to true and another set of instruction to evaluate if the logical expression is false It will always be one or the other, never both

Making A Decision Making A Decision

The form of the statement is In this code we have done a number important of things I indent to know just where I am •We check to see if the value we and to show that I know that there captured from the cell is evenly are two branches divisible by 2 Now we can add the code to do just •We make a calculation based on what we originally set out to do. that check and we change the property of the selected cell •We display the results of our calculations in the cell

15 Homework lThe current calendar, call the Gregorian calendar, was introduced in 1582. Every year divisible by 4 was declared to be a leap year, with the exception of years ending in 00 and not divisible by 400. Write a macro that gets the year in four digits from a cell and in the adjacent cell (to the right) writes the number of days in that year. - DUE WED 21 January 2004

16