Introduction on VBA Lab 05 ins.Tahani Al_dweesh
Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using Excel Functions Checking for conditions Loops Object ins.Tahani Al_dweesh
Introduction Visual Basic for Applications Used to customize many Microsoft applications (Excel, Word, Access, Power Point) host applications. ins.Tahani Al_dweesh
Introduction : Cont Visual Basic for Applications, Excel’s: Powerful built-in programming language, permits you to easily incorporate user-written functions into a spreadsheet. ins.Tahani Al_dweesh
Programming Languages Three groups: Low-level languages are used to manipulate the computer at a basic level: assembly language. High-level languages are used to create stand-alone applications: C, C++, Fortran, Java, Visual Basic. Application level languages are used to manipulate an application program: VBA. ins.Tahani Al_dweesh
Visual Basic Editor ins.Tahani Al_dweesh To open VBA Click on Alt+F11 To open VBA Click on Alt+F11
Creating a simple function ins.Tahani Al_dweesh
The Steps: 1- Open a blank workbook using File | New. 2- Select Developer | Visual Basic Editor (Alt+F11) 3- Within the VBA editor, select Insert | Module from the menu. ins.Tahani Al_dweesh
4- Within the newly-created macro module, type the function statement. ins.Tahani Al_dweesh Function function_name (arg1,arg2,…) Function structure End Function Function function_name (arg1,arg2,…) Function structure End Function
Example ins.Tahani Al_dweesh Function addtwo(x, y) addtwo = x + y End Function Function addtwo(x, y) addtwo = x + y End Function 5- Return to Excel. In cell A1 enter. 6- Hit, to see the result. = addtwo(3 ; 5)
USE Functions The function you typed it appears in the function wizard, just as if it were a built- in Excel function. ins.Tahani Al_dweesh
cont ins.Tahani Al_dweesh To see this: in the “function category” scroll to and highlight “User Defined” Note that “Addtwo” appears.
Subroutine A subroutine ( called a “sub” by VBA ) subroutine, which is a set of statements which execute when invoked. ins.Tahani Al_dweesh Sub Subroutine_name() statement End Sub Sub Subroutine_name() statement End Sub
A Simple Example of a Subroutine: run the subroutine by using Developer | Macro Or (Alt+F8) ins.Tahani Al_dweesh Sub displaybox() MsgBox(”Welcome”) End Sub Sub displaybox() MsgBox(”Welcome”) End Sub Subroutine
ins.Tahani Al_dweesh -then double-clicking on “displaybox” out of the list. -Or Running.
Create Button to Invoke a Subroutine Creating a Button to Invoke a Subroutine : - creating a button in the spreadsheet is a way to create a shortcut to the subroutine. ins.Tahani Al_dweesh
Create Button to Invoke a Subroutine Move the mouse to the spreadsheet,hold down the left mouse button, and drag to create a rectangle. dialog box will pop up and one of the choices will be “displaybox”. Double click on it. ins.Tahani Al_dweesh
Create Button to Invoke a Subroutine ins.Tahani Al_dweesh When you are click on the button” show Message”, the message will be appears When you are click on the button” show Message”, the message will be appears
Differences Between Functions and Subroutines: SubroutinesFunctions Invoked by buttonyesno Insert into cell no yes Change the displayyesno ins.Tahani Al_dweesh
Object Model Includes such objects as: workbooks, worksheets, cells, rows, columns, ranges, charts and pivot tables ins.Tahani Al_dweesh
Objects An object: -is something that is identified by its properties (what it is) and methods (what it can do). For example: -Range is an Excel VBA object and one of its properties is value. -We connect an object to its property by a period (a dot or full stop). ins.Tahani Al_dweesh Range("A1").Value = 10
Using Ranges A Range object represents a cell, a row, a column, a rectangular block of cells, or the union of many rectangular blocks (a non- contiguous range). ins.Tahani Al_dweesh
Storing and Retrieving Variables in a Worksheet There are three ways to read and write to cells: “Range(“ “)” give cells a name, “Range(“ ”).Activate” lets you easily get at cells by using traditional cell addresses (e.g. “A1”). “Cell(, )” lets you address cells using a row and column numbering scheme. ins.Tahani Al_dweesh
1. “Range” give cells a name -Using a named range to read and write numbers from the spreadsheet: 1- created a named range : - to create a named range : ins.Tahani Al_dweesh
1. “Range” give cells a name ins.Tahani Al_dweesh 2- in the Module Sub ReadVariable() x = Range(”test ”).Value MsgBox (Str(x)) End Sub Sub ReadVariable() x = Range(”test ”).Value MsgBox (Str(x)) End Sub
1. “Range” give cells a name 3 -Enter the value “ 4 ” in the cell you just named “ test ”. 4- run the subroutine by using : Developer |Macro Or (Alt+F8) ins.Tahani Al_dweesh
The number on cell
2. traditional cell addresses -Reading from Cells Which are not Named: 1-first have to “activate” the worksheet containing the cell. notice that when you have finished calling this function, the cursor has moved to cell A1 in Sheet2 ins.Tahani Al_dweesh Sub ReadVariable2() Worksheets(”Sheet2”).Activate X=Range(”A1”).Value MsgBox (Str(x)) End Sub Sub ReadVariable2() Worksheets(”Sheet2”).Activate X=Range(”A1”).Value MsgBox (Str(x)) End Sub
2. traditional cell addresses -Writing to Cells Which are not Named: get number of cell (A1) then write this number in cell (B2) ins.Tahani Al_dweesh Sub WriteVariable2() ‘Make ”Sheet2” the active sheet Worksheets(”Sheet2”).Activate ‘Make ”A1” the active cell X=Range(”A1”).Value ‘Make ”B1” the active cell Range(”B1”).Value =X End Sub Sub WriteVariable2() ‘Make ”Sheet2” the active sheet Worksheets(”Sheet2”).Activate ‘Make ”A1” the active cell X=Range(”A1”).Value ‘Make ”B1” the active cell Range(”B1”).Value =X End Sub
Example of unnamed range: ins.Tahani Al_dweesh ‘places text "AB" in range A1:B5, on Sheet1 Worksheets("Sheet1").Range("A1:B5") = "AB" ‘places text "AB" in range A1:B5, on Sheet1 Worksheets("Sheet1").Range("A1:B5") = "AB" Another Way ‘places text "AB" in range A1:B5, on Sheet1 Worksheets(“Sheet1").Activate Range ("A1:B5") = “AB” ‘places text "AB" in range A1:B5, on Sheet1 Worksheets(“Sheet1").Activate Range ("A1:B5") = “AB”
ins.Tahani Al_dweesh
3.using a row and column numbering scheme -Using the “Cells” Function to Read and Write to Cells: put number (1) in cell (A3) Then read this number and write it in cell (C3) 1-first have to “activate” the worksheet containing the cell. ins.Tahani Al_dweesh Sub CellsExample() ’ Make ”Sheet2” the active sheet Worksheets(”Sheet2”).Activate ’ The first entry is the row, the second is the column ’ Write the number 1 into cell A3 Cells (3, 1) = 1 ’ Copy the number from cell A3 into cell C3 Cells (3, 3) = Cells (3, 1) End Sub Sub CellsExample() ’ Make ”Sheet2” the active sheet Worksheets(”Sheet2”).Activate ’ The first entry is the row, the second is the column ’ Write the number 1 into cell A3 Cells (3, 1) = 1 ’ Copy the number from cell A3 into cell C3 Cells (3, 3) = Cells (3, 1) End Sub
Checking for Conditions -To effectively control the VB program flow, we shall use If...Then...Else statement together with the conditional operators and logical operators. - The general format for the if...then...else statement is If...Then...Else ins.Tahani Al_dweesh
Example ins.Tahani Al_dweesh Function check( x ) If x > 0 Then MsgBox (“Positive Number") Else MsgBox (“Negative Number") End If End Function Function check( x ) If x > 0 Then MsgBox (“Positive Number") Else MsgBox (“Negative Number") End If End Function
Use the Function ins.Tahani Al_dweesh
Select Case: Select Case Case Do something Case Else Do something else End Select Select Case Case Do something Case Else Do something else End Select ins.Tahani Al_dweesh Sub selectTest() Select Case Range("A1").Value Case 100 To 400 Range("B1").Value = "it's between" Case Else Range("B1").Value = "it's not between" End Select End Sub Sub selectTest() Select Case Range("A1").Value Case 100 To 400 Range("B1").Value = "it's between" Case Else Range("B1").Value = "it's not between" End Select End Sub
Use the Case Select ins.Tahani Al_dweesh
Loops A simple for loop : For....Next Loop The format is: ins.Tahani Al_dweesh For counter=start to end (Step increment) One or more VB statements Next For counter=start to end (Step increment) One or more VB statements Next
- counter : A numeric variable used as the loop counter. - start and end : The initial and final values of the counter. - increment : The amount the counter is changed each time through the loop. #Notice that increment can be negative ins.Tahani Al_dweesh
Example ins.Tahani Al_dweesh Sub for_loop() Dim Sum As Integer Dim I As Integer Sum= 0 ‘ i, 0,1,2 For i = 0 To 2 Sum = Sum + i Next i MsgBox (Str(Sum)) End Sub Sub for_loop() Dim Sum As Integer Dim I As Integer Sum= 0 ‘ i, 0,1,2 For i = 0 To 2 Sum = Sum + i Next i MsgBox (Str(Sum)) End Sub Result
Loops Do While (condition true) Loop Runs while condition is true. Do... Loop While (condition true) Runs at least one time, and while the condition is true subsequently. ins.Tahani Al_dweesh
The formats are: a) Do While condition Block of one or more VB statements Loop ins.Tahani Al_dweesh Count= 0 Do while count<100 Count=count+1 Loop Count= 0 Do while count<100 Count=count+1 Loop
b) Do Block of one or more VB statements Loop While condition ins.Tahani Al_dweesh Do Count=count+1 Loop while count<100 Do Count=count+1 Loop while count<100
Any Question? ins.Tahani Al_dweesh
الجميع يفكر في تغيير العالم, ولكن لا أحد يفكر في تغيير نفسه Everyone thinks of changing the world, but no one thinks of changing himself