Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.

Slides:



Advertisements
Similar presentations
Introduction to Excel This class is “HANDS-ON” you will need to open up an excel spreadsheet and do examples as you go along. Students will be able to.
Advertisements

(MICROSOFT EXCEL). Is a spreadsheet application designed to take advantage of the windows graphical interface MICROSOFT EXCEL.
Microsoft Office XP Microsoft Excel
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Essence of programming  Branching  Repetitions.
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Introduction to Excel Chapter 2 Excel Fundamentals Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Programming Excel Macros, Visual Basic, and Solving Your Problem.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
L13_1 Microsoft Excel - Building a Worksheet * Excel * Starting Excel and the Excel Window * Entering Text and Numbers * Calculating a Sum * Using the.
Microsoft Excel 2007 Introduction to Spreadsheet Programs
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
Lecture Roger Sutton CO357 Computing for Business and Accounting 13: Automation and Control (Macros and Form controls ) 1.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
11 10/10/2005 BAE Spreadsheet Macros (Visual Basic for Applications) Slides to accompany an in-class demo of spreadsheet technique: Macro Programming.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Microsoft Excel Macros & Excel Solver (IENG490)
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
 Starting Excel 2003  Using Help  Workbook Management  Cursor Management  Manipulating Data  Using Formulae and Functions  Formatting Spreadsheet.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Excel Terms Worksheet: a grid of rows and columns Columns – labeled A, B, C, then AA, BB, etc. – 256 columns Rows – numbered 1, 2, 3 through 65,536 Cell.
Visual Basic for Applications Macro Programming For Microsoft Office.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Key Applications Module Lesson 21 — Access Essentials
1 Microsoft Excel An Introduction to Spreadsheets Lecture 18.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 9 Macros And Visual Basic For Applications.
Spreadsheets the basics. Readings n As per Module 5.
Intermacs Form Download Excel Tutorial Pivot Tables, Graphic Tools, Macros By: Devin Koehl.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
PERFORMING CALCULATIONS Microsoft Excel. Excel Formulas A formula is a set of mathematical instructions that can be used in Excel to perform calculations.
The Excel model for information processing The Excel model is a grid of cells in which items of information are stored and processed. Any information that.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Excel Spreadsheets Formatting and Functions ICS100 – Spring 2007 D. Pai.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
An electronic document that stores various types of data.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
© Stefano Grazioli - Ask for permission for using/quoting: Source: Excel VBA Programming by John Walkenbach.
Macros in Excel Using VBA Time Required – 5 hours.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Institute of Management Studies
Excel Tutorial 8 Developing an Excel Application
Microsoft Excel This class is “HANDS-ON” you will need to open up an excel spreadsheet and do examples as you go along. Students will be able to follow.
Microsoft Excel.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Microsoft Access Illustrated
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introducing VBA Macros
Introduction to VBA IE 469 Spring 2018.
Exploring Microsoft Excel
Microsoft Excel 2007 Introduction to Spreadsheet Programs
Intro to Excel CSCI-150.
Presentation transcript:

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