You are on page 1of 11

VISUAL BASIC PROGRAMMING

PART 3
Communicating with excel Worksheet

 In Excel, when writing a formula,


– We reference a cell by using its cell address (column
and row coordinates)
• A1 or C2 or D3
– If the cell is on another worksheet, we preface the cell
address with name of that worksheet and an !
• Optimistic!A1 or Judy!C2 or Case1!D3
Communicating with excel Worksheet

 In Visual Basic
– We reference a cell by using the cells function,
Cells(row,column)
• Cells(1, 1) for A1, Cells(2, 3) for C2, Cells(3, 4) for D3
• Both the row and column coordinates are expressed as
numbers
• The row coordinate precedes the column coordinate

– We reference the worksheet by using the statement,


Worksheets(“Name”).Activate, in our program
• Worksheets(“Optimistic”).Activate,
Worksheets(“Judy”).Activate,
• Worksheets(“Case1”).Activate
Communicating with excel Worksheet

 Read the value from the excel cell


– Variable = Cells(row, col)
• We are assigning the value in the worksheet cell to a variable
in our program. (Cells function is right of the = sign)
X = Cells(5, 9)
• Assigns the value in the worksheet cell (row 5, column 9) to
the variable X
 Write a value to the excel cell
– Cells(row, col) = expression
• We are assigning the value of the expression in our program to
a cell on a worksheet. (Cells function is left of the = sign)
Cells(5,9) = X
• Assigns the value in X to the worksheet cell (row 5, column 9)
Write a VB program to read the student’s name and mark from
the keyboard and save the information into the excel worksheet
named "student"
Write a VB program that places the sum of NUM1 and NUM2
in each row into the Addition column in that row. Assume the
name of the worksheet is "Stats "
Write a VB program such that: Result is 1 if either number is greater than
5, Result is 2, if both numbers are greater than 5, Result is zero otherwise.
Assume Worksheet name is "Check".
Write a VB program to read the student’s name and mark from the keyboard,
and save the name, mark and grade into the excel worksheet named "Mark".
The grade should be assigned according to the chart below.
Write a VB program to compute the salary bonus for each employee , The
employee will get 9% if he has more than 15 years of working expedience ,
7.5% if he has more than10 years and 6% otherwise.

You might also like