Visual Basic Review

Visual Basic Review

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 arithmetic 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 ¡ ^ exponentiation 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 ¡ Multiplication and Division ¡ * and / multiply and divide ¡ Addition and Subtraction ¡ + 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 relational operator 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us