Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean makes a box that can store TRUE or FALSE Dim Test As Boolean Test=(5>3) Test TRUE Dim A(-2 to 2, 3 to 5) As Single makes an ARRAY of boxes indexed as This is box A(-1,5) The Statement A(-1,5)=7.3 Puts the value 7.3 in the Computer Box Labeled A(-1,5) 5.7 Arrays can contain any type
Number Variables in VBA Dim N as Integer N Makes a box in the Computer storage called N – Can only store Integers to Dim N as Long will allow for integers up to 2 billion Dim A as Single A Makes a box in the Computer storage called A – Uses 32 bits precision Dim N as Double give 64 bits precision Always declare variables in a VBA program; Declare Variables with the Dim statement When you declared a variable imagine the labeling of a box in the computer storaage Rules The statement: A = 7.6 puts the value 7.6 into the box A7.6 The statement: N = 5 puts the value 5 into the box N The statement: B = 5.3 puts the value 5.3 into the box B 5.3 Assuming that B has been declared as Dim B as Single 5 The statement: N = 5.7 puts the value 6 into box N 6 The statement: A = B puts the value 5.3 into the box A Leaves box B 5.3 With the statement: A = A+1 The computer calculates the RHS FIRST--- Takes Current value of A(5.3) Adds 1 (6.3) drops result in box A— This overwrites the value in box A 6.3 A The old value of A can not be Retrieved.
For Loops For I =1 to 3 P=I+1 Next I P=2 P=3 P=4 For I =1 to 5 Step 2 P=I+1 Next I P=2 P=4 P=6 For I =5 to 1 Step -2 P=I+1 Next I P=6 P=4 P=2
For I =1 to For J = I to 2*I P=I*J Next J Next I P=1 P=2 P=6 P=4 P=8 P=12 P=9 P=15 P=18