Working With Variables
Working With Variables
WORKING WITH
VARIABLES
ELF © 2013
Excel VBA
Concept of Variables
2
Variable Names
The following are the rules when naming the variables in VBA
It must be less than 255 characters
No spacing is allowed
It must not begin with a number
Period is not permitted
Concept of Variables
3
Declaring Variables
Needs to declare the variables before using them by
assigning names and data types.
Format syntax:
Dim variable_name As data_type
Data Types
The manner in which a program stores data in memory.
The type of data a variable is classified as
User-Defined Types
createyour own data type.
a new data type by combining the built-in data types
User-Defined Types
Example:
Dim group As Student
group.StuName = "James"
Type Student
group.StuID = "1007"
StuName As String
group.StuYear = "Final Year"
StuID As String
group.StuFee = "$20,000"
StuFee As Currency
Cells(2, 2) = group.StuName
StuYear As String
Cells(3, 2) = group.StuID
End Type
Cells(4, 2) = group.StuYear
Cells(5, 2) = group.StuFee
Variables & Data Types
PRACTICAL LEARNING
10
Example
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim BirthDay As Date
Sub Variables()
Dim Shirts As Byte
Dim Pants As Byte
Dim OtherItems As Byte
Dim TotalItems As Byte
Shirts = 6
Pants = 4
OtherItems = 2
TotalItems = Shirts + Pants + OtherItems
ActiveCell.FormulaR1C1 = TotalItems
End Sub
Variables & Data Types
PRACTICAL LEARNING
13
Sub Variables()
Dim Side As Double
Dim Perimeter As Double Side = 32.75
Perimeter = Side * 4
ActiveCell.FormulaR1C1 = Perimeter
End Sub
Variables & Data Types
PRACTICAL LEARNING
14
Using a String
Sub Variables()
Dim CustomerName As String
ActiveCell.FormulaR1C1 = CustomerName
End Sub
Variables & Data Types
PRACTICAL LEARNING
15
Sub Variables()
Dim NumberOfShirts As Byte
Dim PriceOneShirt As Currency
Dim TotalPriceShirts As Currency
NumberOfShirts = 5
PriceOneShirt = 1.25
TotalPriceShirts = NumberOfShirts * PriceOneShirt
ActiveCell.FormulaR1C1 = TotalPriceShirts
End Sub
Variables & Data Types
PRACTICAL LEARNING
16
Using a Date
Sub Variables()
Dim DepositDate As Date
DepositDate = #2/5/2008#
ActiveCell.FormulaR1C1 = DepositDate
End Sub
Use of Option Explicit
17
Example
Option Explicit
Private Sub CommandButton1_Click()
Dim YourName As String, password As
String
YourName = "John"
password = 12345
Cells(1, 2) = YourNam
Cells(1, 3) = password
End Sub
Assigning Values to the Variables
19
Value Assignment
Use the assignment operator (=) followed by the value
you need to store
The general format of an assignment is
Variable=Expression
Example
Dim Value
Value = 100
Arithmetic Operations
20
^ Exponential 2^4=16
* Multiplication 4*3=12
/ Division 12/4=3
Mod Modulus (return the remainder 15 Mod 4=3
from an
integer division)
\ Integer Division (discards the 19\4=4
decimal places)
+ or & String concatenation "Visual"&"Basic"="Visual
Basic"
Arithmetic Operations
PRACTICAL LEARNING
21
Option Explicit
Private Sub CommandButton1_Click ()
Dim number1, number2, number3 as Single
Dim total, average as Double
number1=Cells (1, 1).Value
number2=Cells (2, 1).Value
number3= Cells (3, 1).Value
Total=number1+number2+number3
Average=Total/3
Cells (5, 1) =Total
Cells (6, 1) =Average
End Sub
Arithmetic Operations
PRACTICAL LEARNING
22
Option Explicit
Private Sub CommandButton1_Click()
Dim secondName As String, yourName As String
firstName = Cells(1,1).Value
secondName = Cells(2,1).Value
yourName = firstName + " " + secondName
Cells(3,1) = yourName
End Sub
23 Using Message box and Input box
Excel VBA
MsgBox ( ) Function
24
Example:
yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
1 vbOk Ok button
2 vbCancel Cancel button
3 vbAbort Abort button
4 vbRetry Retry button
5 vbIgnore Ignore button
6 vbYes Yes button
7 vbNo No button
MsgBox ( ) Function
PRACTICAL LEARNING
27
Excel Macro
Sub ADD_Macro()
Dim X As Integer
Dim Y As Integer
X = Sheet2.Range("A1")
Y = Sheet2.Range("A2")
Sheet2.Range("B1") = X + Y
End Sub
Simple Addition Example
PRACTICAL LEARNING
32
X = Val(NUM1.Text)
Y = Val(NUM2.Text)
SUM.Text = Str(X + Y)
End Sub
Exercises
33