You are on page 1of 19

CENG200

VBA
LECTURE 2
General note about using VBA for the
first time
• VBA stand for Visual Basic for Application
• To use the VBA code(generate micro) in excel you
will need the Developer tab in the Ribbon, then
choose visual basic command then insert new
module
• To save your excel file with the VBA code you
should save it as .XLSM type
• When you open your .xlsm file you may have
warning message about enabling the micro(vba
code). You should accept that to keep or edit your
existing code
The structure of VBA sub program
• Sub program name ()
Statements….
.....
End Sub
• program name : starting with alphabet, space
and special character not allowed except the
underscore.
• Statements: instructions to achieve the
requirement (our goal for the given problem)
To solve the problem you may need to
determine the following:
• Inputs : the information that should be
provided to find the result (to achieve the goal)
• Process: the logical or arithmetic steps that
should be performed on the inputs to get the
result
• Output: view or display the result to the user
The forms of inputs in VBA
• Constant value given in the question, you can
define variable and keep the value in, example:
G=9.81
• Data stored in the excel sheet (in the cells), you
can reach those data by using :
– Range object, example:
Range("c20") = Range("c3") + 1 getting value from c3
– Cell object, example:
Cells(20, 3) = Cells(3, 3) + 1 getting value from c3
• Input box (accept the input from the user),
example:
L=val(inputbox(“enter the length of rectangle”))
The forms of outputs in VBA
• Display the data in the excel sheet (in the cells),
you can display those data by using :
– Range object, example:
Range("c20") = Range("c3") + 1 setting value to c20
– Cell object, example:
Cells(20, 3) = Cells(3, 3) + 1 setting value to c20
• Message box , example:
R= Cells(3, 3) + 1
Msgbox(R) display R value in a message box
The forms of Process in VBA
We may use one or more from the following to
solve the given problem :
• equation /statement
Like: Find area of room, find volume of cone, convert
meter to centimeter
• Make Decision
Like: Verify if the given number is odd or even, Find the
absolute value for the given number
• Repeating
Like: Find Maximum grade between the given students
grades, find the average of test1 and test2 for each
student in the class
Equation/statement
Problem1: Find the area of a room in c2, where
the length and width values placed in A2 and B2
respectively.

Sub problem1()
Range("c2") = Range("a2") * Range("b2")
End Sub
Equation/statement
Problem2: use the input box to accept value in
meter then convert it to centimeter unit. Show
the result in massage box.
Sub problem2()
m = Val(InputBox("enter the value in meter"))
c = m * 100
MsgBox (c & " cm")
End Sub
Equation/statement
Problem3: Store 50 in A1, store “VBA” in A3
Sub problem3()
Range("a1") = 50 or cells(1,1)=50
Range("a3") = ”VBA"
End Sub
Repeating (Loop statement)
Problem1: view “VBA” in the A1:A10 range.
*You can solve this problem with and without loop:
Sub problem1()
Range("a1:a10") = "VBA"
End Sub
or
Sub problem1()
For i = 1 To 10
Cells(i, 1) = "VBA"
Next i
End Sub
Repeating (Loop statement)
Problem2: Set the numbers from 1 to 100 in D1
to D100 range.
Sub problem2()
For i = 1 To 100
Cells(i, 4) = i
Next i
End Sub
Repeating (Loop statement)
Problem3: Set the odd numbers from 1 to 30 in
column B starting from B3.
Sub problem2()
For i = 1 To 30 step 2
Cells(i, 2) = i
Next i
End Sub
What will happen here??
Repeating (Loop statement)
Problem3: Set the odd numbers from 1 to 30 in
column B starting from B3. try this
Sub problem2()
row = 3 'starting from third row b3
For Odd = 1 To 30 Step 2 'odd numbers 1-30
Cells(row, 2) = Odd 'set the odd number in the cell
row = row+ 1 'increment by 1 to go for the next cell
Next Odd 'next odd number
End Sub
Repeating (Loop statement)
Problem4: Set the numbers from 20 to 1 in column C
starting from C1.
Sub problem2()
row = 1 'starting from first row c1
For N = 20 To 1 Step -1 ‘ numbers 20-1
Cells(row, 3) = N 'set the number in the cell
row = row+ 1 'increment by 1 to go for the next cell
Next N 'next number
End Sub
Repeating (Loop statement)
Problem5: Find the total of the numbers in A1:A10
Sub problem5()
Range("a11") = "=sum(a1:a10) ”
End Sub
OR
Sub problem5()
sum = 0 ’initialize the summation counter
For i = 1 To 10 ‘ row number from a1:a10
sum= sum+ Cells(i, 1) 'set the number in the cell
Next i 'next number
Range("a11")= sum
End Sub Try if from A1:H1
Repeating (Loop statement)
Problem6: Find the total of the even numbers 1-50
Sub problem5()
sum = 0 ’initialize the summation counter
For i = 2 To 50 step 2 ‘even numbers from 1-50
sum=sum+i ’add the current number to the counter
Next i 'next number
Range("a1")= sum
End Sub
Repeating (Loop statement)
Problem6: Find w

Sub problem6()
sum = 0
j=50
For i = 2 To 18 step 2
sum=sum+ i/j
j=j-5
Next i
Range("a1")= sum
End Sub
Repeating (Loop statement)
H.W. : Find the 8th term from the following:
1, ½, ¼, ….

You might also like