Excel and Visual Basic Chapter I – The Visual Basic Language Before we start Computers store information in memory. The basic memory element is the byte (a set of 8 bits, each bit can have the value 0 or 1). With a byte you can have bits combination from binary - 00000000 (decimal 0) to binary – 11111111 (decimal 255), 256 combinations in all (having 8 bits you have 2 8 = 256 combinations). In memory you can only have numbers (in a byte you have a integer number from 0 to 255). So how can you store information in memory if that information is not an integer number? The answer is easy, you just store an integer number that represents (codes) that information. If you want to save 256 different colors in a byte you just code the colors (0 – white, 1 – black, 2 – yellow, 3 – red, …). You don’t store the color but just the number that represents it. It’s the same with letters, computers don’t store the letter just a number that represents it. So in memory you have only numbers. Information is the way we interpret those numbers. 1 – Fundamental Data types A type is a class that represents information with the same characteristics. We group information with common characteristics in classes. We give a name to each one of those classes. In a programming language we store information in variables (well, information is stored in computer memory so variables represent one or more bytes in memory). When we say : X is an integer variable - we mean X can store values with the characteristics of the integer numbers. Types have two fundamental characteristics: - The set of values information can take - Operations you can use with those values 1.1 The integer type In VB an integer variable can store integer numbers (set of values) from –32768 to 32767 Where this number comes from? Vb uses two bytes to store integer numbers. We have in all 16 bits so we can have numbers from binary - 00000000 00000000 (decimal 0) to binary – 11111111 11111111 (decimal 65535 --»»216 -1). Integer number can be negative too, so we must have a way to represent those numbers. Computers use one of the bits (the one at the left) to say if the integer number is positive or negative. If that bit is 0 the number is positive, if the bit is 1 the number is negative. So the biggest positive integer is 0111 1111 1111 1111 (first bit must be 0 to be positive). This is decimal 32767. If we add 1 to this number we will have binary – 1000 0000 0000 000 (decimal 32768). If we look closer we see that first bit is 1 so this must be a negative number. It is the negative number –32768. Valid operations: (operations between integers that also return an integer value) + addition - subtraction * multiplication \ integer division Expression 7 \ 3 results 2 MOD remainder after integer division Expression 7 MOD 3 results 1 ^ power Expression 2^3 results 8 You can also use normal division (/) between integers but result is no longer an integer but a floating-point value. Expression 4 / 2 results 2.0 Some VB functions that return integer values Function Sintax Returns Expression example Returns Int(number) Integer part of number * Fi x(number) Integer part of number * Round(expression [,numdecimalplaces]) A number rounded to a specified number of decimal places A number rep resented by string A number converted fro m string or expression Int(8.7) Int(-8.7) Fi x(8.7) Fi x(-8.7) Round(17.267) Round(17.6) Round(17.267, 2) Val(“123”) 8 -9 8 -8 17 18 17.27 123 Cint(17.6) Cint(“17.6”) Asc(“A”) 18 176 65 Asc(“Apple”) 65 Abs(-5) 5 Val(string) Cint (string or numeric expression ) Asc(string) Abs(number) Integer representing the char code of the first letter o f string Absolute value of number *The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. 1.2 The long type Longs are just big integers. VB uses 4 bytes to store long numbers. We can now represent values from – 2147483648 to 2147483647. Everything else said for integer type is valid for longs, the only exception is the Cint function replaced by the Clng function. 1.3 The single type We can use singles to store single-precision floating-point numbers. Singles range in value from -3.402823E38 to -1.401298E-45 for negative values and from 1.401298E-45 to 3.402823E38 for positive values. (3.402823E38 means 3.402823 x 10 38 ) Single-precision numbers can have up to 7 digits precision mantissa (part to the left of the E in a floating-point number) Valid operations: + addition - subtraction * multiplication / division ^ power 1.4 The double type We can use doubles to store double-precision floating-point numbers. Doubles range in value from -1.79769313486231E308 to -4.94065645841247E-324 for negative values and from 4.94065645841247E-324 to 1.79769313486232E308 for positive values. Double-precision numbers can have up to 15 digits precision mantissa (the part to the left of the E in a floating-point number) Valid operations: The same as the single type Some VB functions that return single or double values Function Sintax Returns Expression example Returns Cos(angle) a Double specify ing the cosine of angle* a Double specify ing the sine of angle* a Double specify ing the tangent of angle* a Double specify ing the natural logarith m of number (base e logarith m) a Double specify ing the square root of a number a single random value less than 1 but greater than or equal to zero A number rep resented by string Absolute value of number Cos(0) 1.0 Sin(0) 0.0 Tan(0) 0.0 Log(1) 0.0 Sqr(4) 2.0 Int(1 +10*rnd) Val(“123.17”) Integer between 1 and 10 123.17 Abs(-5.0) 5.0 Sin(angle) Tan(angle) Log(number) Sqr(number) Rnd() Val(string) Abs(number) * angle is expressed in radians 1.5 The Boolean type A variable of type Boolean can only take the values true or false. Valid operations: AND logical conjunction OR logical disjunction NOT logical negation XOR logical exclusion Ope ration results True AND True True AND False False AND True False AND False True False False False True OR True True OR False False OR True False OR False True True True False True XOR True True XOR False False XOR True False XOR False False True True False NOT True NOT False False True Examples : (5>2) AND (3=2) (5>2) OR (3=2) (5>2) XOR (3=2) NOT (5>2) results results results results False True True False Note : The AND, OR and XOR operators also perform a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result according to the following table: Bit in expression1 Bit in expression2 AND OR XOR Corres ponding bi t in result 1 1 0 0 1 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 For example 5 AND 3 results 1 ----» 101 AND 011 = 001 (in binary) The Not operator inverts the bit values of any nume ric expression and sets the corresponding bit in result accordingly. Some VB functions that return boolean values Functi on Sintax EOF(filenumber) Returns Expression example True when the end of a file opened for Random or sequential Input has been reached, False otherwise Is Empty(expression) True if the variable is uninitialized, or is explicitly set to empty; otherwise, it returns False . False is always returned if expression contains more than one variable. IsEmpty only returns meaningful information for variants True if expression is Null; otherwise, IsNull returns False.The Null value indicates that the Variant contains no valid data. Dim InputData Open "MYFILE" For Input As #1 'Open file for input Do While Not EOF(1) 'Check for end of file Line Input #1, InputData 'Read line of data. Debug.Print InputData 'Print to the Immediate windo w. Loop Close #1 ' Close file. Dim MyVar, MyCheck MyCheck = IsEmpty(MyVar) IsNull(expression) Is Error(expression) Boolean value indicating whether an expression is an error value IsNumeric(expression) True if the entire expression is recognized as a number; otherwise, it returns False True if the expression is a date or is recognizable as a valid date; otherwise, it returns False IsDate(expression) Returns cells(1,1).=”Hello World” TRUE FALS E cells(1,1).ClearContents TRUE Dim MyVar, MyCheck MyCheck = IsNull(MyVar) My Var =”” MyCheck = IsNull(MyVar) My Var =null MyCheck = IsNull(MyVar) IsDate(“8-10-2006”) FALS E FALS E TRUE TRUE FALS E FALS E TRUE TRUE IsDate(“32-12-2006”) FALS E MyCheck = IsEmpty(cells(1,1)) MyCheck = IsEmpty(cells(1,1)) IsError (evaluate(“5 / 0”)) IsError (evaluate(“5 / 2”)) IsNumeric(“Hello World”) IsNumeric(“732”) 1.6 The string type We can use strings to store one or more characters. Characters include uppercase and lowercase letters, digits and symbols. All the characters are saved in memory using a numeric (0 – 255) code, the ASCII code for that character. As there are 256 ASCII codes, each string occupies a byte in memory for each character. The first 128 characters (0–127) of the character set correspond to the letters and symbols on a standard U.S. keyboard. These first 128 characters are the same as those defined by the ASCII character set. The second 128 characters (128–255) represent special characters, such as letters in international alphabets, accents, currency symbols, and fractions. Codes 0 to 31 are control codes such TAB, BACKSPACE, CARRIAGE RETURN. We can have two kinds of strings in VB - Fixed length strings (Length cannot change during program execution) A fixed-length string can contain 1 to approximately 64K (2^16) characters. Declaration of a fixed 8 char length string DIM sbyte as string*8 - Variable length strings (Length can change during program execution) A variable-length string can contain up to approximately 2 billion (2^31) characters Declaration of a variable length string Dim name as string Literal value strings are always placed within quotation marks “” Example: name = “Shakespeare” Valid operations: & Concatenation Expression “HI ” & “THERE” results “HI THERE” Some Subprograms that work with STRINGS Functi on Sintax Returns Expression example Returns Len(string) a long containing the number of characters in string Len(“ Shakespeare”) 11 Lef t(string,lenght) a string containing lenght number of characters fro m the left side of string. Lef t(“ Shakespeare”,5) “Shake” Right(string,lenght) a string containing lenght number of characters fro m the right side of string. Right(“ Shakespeare”,5) “peare” Mid( string, start[, length]) a string containing length number of characters fro m string starting at start. If length is omitted all characters fro m the start position to the end of string are returned. Mid(“ Shakespeare”,2,4) “hake” Mid(“ Shakespeare”,6) “speare” a long specifying the position of the first occurrence of string2 within string1. start is the starting position for e ach search. I f o mitted, search begins at the first character position. If search fails function returns 0. a long specifying the position of the last occurrence of string2 within string1. start is the starting position for e ach search. I f o mitted, search begins at the last character position. If search fails function returns 0. InStr(“ Shakespeare” ,”spe”) 6 InStr(“ Shakespeare” ,”rat”) 0 InStr(4,“ Shakespeare” ,”a”) 9 InStrRev(“ This is VBA”,”i”) 6 InStrRev(“ This is VBA”,”i”,4) 3 InStr([start, ]string1, string2) InStrRev(string1, string2[,start]) [] Optional parameters
© Copyright 2026 Paperzz