Excel VBA Basics
Excel VBA Basics
Cell – intersection of
row and column.
In the example the
ID of the cell: B4
We have inserted
absolute constants
and invoked
AVERAGE excel
function
2. Click this
button
1. Select a
cell to be
updated
6. Finally
Ing. John Suarez click “OK”
[email protected] -
twitter: @johnsuarezr -
997205589
Finally,
=If(A2>1,”Yes”,”No”)
If it is true that the value in the cell A2
is greater then 1, then the value of
current cell is “Yes”
Otherwise (else), the value is “No”
This is a label
This is a button
Select
The Combo-
Add Source of Box
range for the
combo-box
Sub NoVariable()
statement in few lines
use “_” at the end of line! Sub WithVariable() Dim _
Range("A1").Value = _ iValue as Integer
Range("B2").Value iValue = _
Range("A2").Value = _ Range("B2").Value
Range("B2").Value * 2 Range("A1").Value = _
Range("A3").Value = _ iValue
Range("B2").Value * 4 Range("A2").Value = _
Range("B2").Value = _ iValue * 2
Range("B2").Value * 5 Range("A3").Value = _
End Sub iValue * 4
Range("B2").Value = _
iValue * 5
End Sub
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
Using Variables
Declaring Variables
Format: Dim varibaleName AS dataType
Examples:
Dim myText As String
Dim myNum As Integer
Dim myObj As Range
The default value of
any numeric variable is zero
any string variable – “” (empty string)
an Object variable – is nothing (still
Ing. John the declaration will store
Suarez
[email protected] -
space for the object!!!) twitter: @johnsuarezr -
997205589
Variant “Data Type”
In VB you don‟t have to declare variable before its
usage
Then, VB will by itself declare such variable as “Variant”
You can also declare variable as “Variant”
Dim myVar as Variant
Variant – means that the variable may contain any
data type
The price is very high!!! – any time VB access such
variable, it will spend time Ing.on
John“deciding”
Suarez what is its
[email protected] -
“current” type! twitter: @johnsuarezr -
997205589
Variables Assignment
To assign a value to a Numeric or String
type Variable, you simply use your
Variable name, followed by the equals
sign (=) and then the String or Numeric
x=5
End Sub
Dim y as Integer Module level variables
A Cell
A Range
E2:E5
A current
Worksheet
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
Excel Containers
Workbooks: a collection of objects of
class “Workbook”
Worksheets: a collection of objects of
class “Worksheet”
Sheets: a collection of Sheet objects
Range: a range of objects of class Cell
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
Referencing the Objects -
Examples This will take the
whole square
between the two
cells
Sub Test1()
Worksheets("Sheet1").Range("A10", "B12") =
"Hello“
Worksheets(1).Range("A13,B14") = "World!"
End Sub
Two equal ways to The range of two
refer Sheet1 cells
This is the
currently active
cell
The assignment
result
If Age >= 18
Then MsgBox "You can vote
ElseIf Age >=22 and Age < 62
Then MsgBox “You can drive”
End If
i=
Do While i =< 10
Cells(i, 1) = i
i=i+1
Loop
i=1
Do
Cells(i, 1) = i
i =i+1
Loop While i < 11
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
Test yourself!
What does the procedure do?
Sub CellsExample
For i = 1 To 5
For j = 1 To 5
Cells(i, j) = "Row " & i & " Col " & j
Next j
Next i
End Sub
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
Ing. John Suarez
[email protected] -
twitter: @johnsuarezr -
997205589
References
http://www.usd.edu/trio/tut/excel/13.html
Tutorial on Excel
http://www.anthony-
vba.kefra.com/index_011.htm
Great place to learn VBA basics!
http://msdn.microsoft.com/en-
us/library/aa224506(office.11).aspx
MSDN online help, a good place to learn about Excel
classes (their data and functions set) -
Ing. John Suarez
[email protected]
twitter: @johnsuarezr -
997205589
Assignment #1
Create Excel file with grades
The data:
There are 4 students with ids & names
There are 4 assignments and two exams
Each student has grades for each one of the assignments and exams, the grades are
from 20 to 100
Some cell in the worksheet keeps factor of 10
Create VBA module that will calculate final grade for every student and
places it in the new column allocated to keep the final grade
20% for the assignments average and 80% - for the maximal grade of the two exams
plus factor
If the grade becomes higher than 100 – it should be 100
Create VBA that accepts a column name from user and sorts the whole file
according to the given column
Create VBA that adds additional column with grades translated to A, B, C,
D, E, F.
Next week in class I will collect your
Ing.solutions
John Suarez
You should submit Excel file,[email protected]
and three VBA modules (only
- hardcopy)
twitter: @johnsuarezr -
997205589