Excel VBA Basics
Excel VBA Basics
Outline
Cell intersection
.of row and column
In the example the
ID of the cell: B4
Column - characters
uniquely designate
. each column
=5+3, =45*7-3
Excel Spreadsheet
Example
Solution I
We have inserted
absolute
constants and
invoked AVERAGE
excel function
Select a. 1
cell to be
updated
Go with. 4
mouse to the
first argument
(here
Number1)
See how we
refer to a
!range
Finally. 6
click OK
,Finally
Using If Expression in
Excel
=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
Using Sin/Cos/Tan
Expression in Excel
Formatting Cells
Adding Graphs/Charts
Outline
Using Macros
!The Output
Outline
VB example Hello
!World
The Output
Using Toolbox
This is a
label
This is a
button
Using the
Toolbox select a
GUI element and
by mouse-click
place it on the
frame
Do you
remember
?the code
!!The Output
Using Combo-Box
Add Source of
range for the
combo-box
Select
The ComboBox
The output
after user
makes
combo box
selection
Outline
The procedure
places the current
time inside cell C1
Sub ShowSum()
MsgBox _
Module1.sumNo(3,5
)
End Sub
Function sumNo(x, y)
sumNo = x + y
End Function
Passing Arguments by
Value or by Reference
Functions/Procedure Scope
VBA Variables
Sub NoVariable()
Range("A1").Value = _
Range("B2").Value
Range("A2").Value = _
Range("B2").Value * 2
Range("A3").Value = _
Range("B2").Value * 4
Range("B2").Value = _
Range("B2").Value * 5
End Sub
Sub WithVariable()
Dim _ iValue as Integer
iValue = _
Range("B2").Value
Range("A1").Value = _
iValue
Range("A2").Value = _
iValue * 2
Range("A3").Value = _
iValue * 4
Range("B2").Value = _
iValue * 5
End Sub
Using Variables
Declaring Variables
Variables Assignment
Variables Assignment
cont.
Sub ParseValue()
Dim sWord as String
Dim iNumber as Integer
Dim rCell as Range
The scope & lifecycle of a variable defines the code where the
variable can be accessed and time when the stored data is kept
inside the variable
Procedure-Level
Module-Level
Sub scopeExample()
Procedure level variables
Dim x as Integer
x=5
End Sub
Module level
Dim y as Integer
variables
all the module procedures are here
Project level variables
Public z as Integer
all the module procedures are here
A Cell
A Range
E2:E5
A current
Worksheet
Excel Containers
Sub Test1()
Worksheets("Sheet1").Range("A10", "B12")
= "Hello
Worksheets(1).Range("A13,B14") = "World!"
End Sub
Two equal ways to
refer Sheet1
The range of
two
cells
The Output
Which Workbook
was
?Used
!The Output
Referencing Cells
ActiveSheet.Range.Cells(1,1)
Range.Cells(1,1)
Cells(1,1)
See how we
calculate cell
12
In the given
!range
Few methods/properties of
Excel Classes
VBA Arrays
Dim LoanBooks(3)
LoanBooks(1) = Winnie The Pooh
LoanBooks(2) = Adventures of Huckleberry
Finn
LoanBook(3) = Frankenstein
Multidimensional Arrays
Dim WeekTasks(7,2)
WeekTasks(1,1) = To buy milk
WeekTasks(7,1) = To dance
What will the
?code print
If Age >= 18
Then MsgBox "You can vote"
ElseIf Age >=22 and Age < 62
Then MsgBox You can
drive
End If
= "A"
= "B"
= "C"
= "D"
= E"
Do
Cells(i, 1) = i
i = i + 1
Loop While i < 11
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
References
http://www.usd.edu/trio/tut/excel/13.html
http://www.anthony-vba.kefra.com/
index_011.htm
Tutorial on Excel
http://msdn.microsoft.com/en-us/library/aa2
24506(office.11).aspx
Assignment #1
The data:
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 solutions
You should submit Excel file, and three VBA modules (only hardcopy)