We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12
COMP1022Q
Introduction to Computing with Excel VBA
Different Types of Variable in VBA
David Rossiter, Gibson Lam and Oz Lam
Outcomes • After completing this presentation, you are expected to be able to: 1. Understand the use of five common types of variables and their limitations
COMP1022Q Different Types of Variable in VBA Page 2
We have seen Making and Using a Variable this before
• For example, you can make a variable called Name
which holds a string (a piece of text) using this code: Dim Name As String • You can use the variable to hold some text using this code: David Name = "David" Name
• You can then show the content of
the variable using a message box: MsgBox Name Different Types of Variable • In addition to the String variable which stores text, there are many other types of variable for storing other things • In this presentation, we look at these types of variable: 1. Integer for storing (small) integer numbers 2. Long for storing (large) integer numbers 3. Single for storing (less accurate) decimal numbers 4. Double for storing (more accurate) decimal numbers 5. Boolean for storing the True or False values • These are not the only types of variable, we may look at other types later Integer Variables • If you want to store an integer number in VBA, you can use an Integer variable, i.e. Dim Money As Integer Money = InputBox("How much do you have?") MsgBox "You have $" & Money
COMP1022Q Different Types of Variable in VBA Page 5
Range of an Integer Variable • An integer variable can only handle a number in the range -32,768 to 32,767 • If you try to put a number that is outside that range into an Integer variable, the code will show an error
COMP1022Q Different Types of Variable in VBA Page 6
Long Variables • If you want to store a number larger than 32,767 or smaller than -32,768, use a Long variable • A Long variable can store a number in the range -2,147,483,648 to 2,147,483,647 Dim Money As Long Money = InputBox("How much do you have?") MsgBox "You have $" & Money Using a Number Which Has a Decimal Place • If you try to enter a number which has a decimal place (such as 13.4) in the previous two examples, the decimal place is dumped and the number is automatically rounded up or down i.e.
• If you want to keep the decimal place, you need to use a
variable type which can handle it, see next slide Single and Double Variables 1/2 • Single and Double variables can both handle a decimal place, but Double has more accuracy Dim Pi As Single Pi = InputBox("What is the value of Pi?") MsgBox "The value of Pi is " & Pi
COMP1022Q Different Types of Variable in VBA Page 9
Single and Double Variables 2/2 Dim Pi As Double Pi = InputBox("What is the value of Pi?") MsgBox "The value of Pi is " & Pi
COMP1022Q Different Types of Variable in VBA Page 10
Boolean Variables • A Boolean variable is used to store one of the two values only: True or False • For example, the comparison of two numbers can be put in a variable like this: Dim Number1 As Double Dim Number2 As Double Dim Comparison As Boolean
Number1 = InputBox("What is the first number?")
Number2 = InputBox("What is the second number?") Comparison = (Number1 = Number2)
MsgBox "Are the two numbers the same? " & _
Comparison • When the two numbers • When the two numbers are different are the same