Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.

Slides:



Advertisements
Similar presentations
1B40: Data analysis 1 Objects Visual Basic is an (OO) object-oriented language. Performing a task in Visual Basic (VB) or.
Advertisements

ISOM3230 Business Applications Programming
PROGRAMMING IN VISUAL BASIC PART 1
Microsoft Office XP Microsoft Excel
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
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.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Some computer fundamentals and jargon Memory: Basic element is a bit – value = 0 or 1 Collection of “n” bits is a “byte” Collection of several bytes is.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
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
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 2 1 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
1 Computing for Todays Lecture 8 Yumei Huo Spring 2006.
VB .NET Programming Fundamentals
Adding Automated Functionality to Office Applications.
FIRST COURSE Excel Lecture. XP 2 Introducing Excel Microsoft Office Excel 2007 (or Excel) is a computer program used to enter, analyze, and present quantitative.
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.
XP New Perspectives on Microsoft Excel 2002 Tutorial 2 1 Microsoft Excel 2002 Tutorial 2 – Working With Formulas and Functions.
Microsoft Excel Diane M. Coyle Spring 2009 CS 105.
XP Abdul Hameed 1 Microsoft Office Excel 2013 Tutorial 2 – Working With Formulas and Functions.
IE 212: Computational Methods for Industrial Engineering
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Object Variables Visual Basic for Applications 3.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
Creating Macros Using VBA. Assigning a Macro to a Button Display the Forms toolbar. Click the Button icon. Click and drag the mouse pointer to specify.
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
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.
OV Copyright © 2011 Element K Content LLC. All rights reserved.  Insert Text  Format Text  Sort Data  Duplicate Data  Generate a Report Formatting.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 9 Macros And Visual Basic For Applications.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic  Is the language for programming in Word, Access,& Excel  Is the “Environment” in which the programming is done (called the Integrated Development.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
XP 1 Microsoft Office Excel 2003 Working With Formulas and Functions.
XP 1 ﴀ New Perspectives on Microsoft Office 2003, Premium Edition Excel Tutorial 2 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Microsoft Excel Prepared by the Academic Faculty Members of IT.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
VBA Programming for Excel Review Excel Objects Excel Methods Identifying Specific Cells Review Functions for Excel Custom Menus.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Excel Tutorial 8 Developing an Excel Application
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Exploring Microsoft Excel
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Activate a range and manipulating activated range
Introduction to Computer Programming IT-104
Presentation transcript:

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.

Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something

Visual Basic Range (“A3”).select Range is an object select is a method (“A3”) is a modifier for the object Note that object separated from method by a.

Activesheet.name Activesheet is an object name is a property Visual Basic The result is the name of the active sheet

Procedures Sub procedures return no values Function procedures return a value

Function Procedures Function Grade (exam1,exam2,exam3) as String Sum = exam1+exam2+exam3 If sum > 95 then Grade = “A” Else if sum > 80 Grade = “B” Else Grade = “C” End if End Function The function name must be assigned the value to be returned!

Arguments to a function must be in the order specified in the function description: Function Grade (exam1,exam2,exam3) as String = Grade (90, 80, C5) Note the “as String” to declare the type of value returned by the function.

Sub Procedures Sub Gc() ‘ Lines beginning with ‘ are comments ' Gc Macro ' Puts gc in active cell & units in adjacent cell to right ' Keyboard Shortcut: Ctrl+g ' ActiveCell.FormulaR1C1 = "32.174" ActiveCell.Offset(0, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "ft-lbm/lbf-s^2" End Sub This was a recorded macro – macros are “Sub” procedures.

Review of some Excel Basics Cell references of two types: A1 Columns are alphabetic, rows are numeric R1C1 R number Column number B2 and R2C2 refer to the same cell Can be set by the Tools / Options menus (Note that the two methods are transposed – A1 – column first, then row R1C1 – row first, then column )

Formulae in Excel (A1 style) A1 is a relative address --- it changes when the formula is copy /pasted $A$1 is an absolute address --- it does not change via copy / paste And can use mixed mode: A$1 – A is relative, $1 is absolute $A1 – A is absolute, 1 is relative

Formulae in Excel (R1C1 style) R1C1 is an absolute address – it does not change under copy / paste R[1]C[1] is a relative address – it does change under copy /paste And can use mixed mode: R1C[1] – R1 is absolute, C[1] is relative

In VBA, can use either or both A1 and R1C1 styles A1 style tends to be absolute A1 style used with the “Range” property Range(“A4”) Can refer to individual cells with the “Cells” Property, which uses an R1C1 style Cells(4,1)

In Excel, cells & ranges can be named Insert / name menu These names can be used in Formulae VBA

VBA Variable Types String A sequence of bytes containing characters Integer 2 byte integer Long 4 byte integer Single 4 byte real number Double 8 byte real number VariantCan hold anything (but “expensive” to use) “Object”A class of data types used by Excel/VBA

Characters & Stings For Excel, anything that is not a number or a formula is a string. If want a set of digits as a string, need to enclose in quote or quotation marks. For VBA, need to define variable that will hold strings as string data types Good programming practice is to declare all variables and their types via the “Dim” statement.

Visual Basic Sub NameIt() Dim newname as String newname = InputBox(“Enter a name for the worksheet”) ActiveSheet.Name=newname End Sub

“Object” data type Many of the objects in Excel have their own data type: Dim r as Range Dim q as Worksheet Dim z as Chart About the only one we will use is the “Range” data type.

Option Explicit Forces you to ‘Dim’ all variables Helps prevent typos Can set this as the default through the Tools/ Options menu. Require variable declaration check box)

Visual Basic All objects have properties Most objects have methods Will work with only a few of the many objects, methods and properties To get a list of objects and properties, invoke the Object Browser

Visual Basic Some of the objects Workbook Worksheet ActiveSheet Range Form Chart Note --- a “Cell” is not an object, but is a type of range object

Visual Basic And to further confuse the issue objects can have objects as properties And objects can be grouped into collections Workbook = collection of worksheets

Visual Basic Object Browser --- Just for a quick look In VBA, press F2 or “View / Browser” Select Excel from library list Enter an object in the second list box Use “F1” to get help on an object

Program module (Sub or Function) is made up of a series of steps to accomplish a task. Five major categories of steps: Input /Output operations (Which are really calls to other modules) Read 5, A Assignment statementsA = 5 Conditional statements If ( A > 5) then …. Calls to other modulesA = sqrt (12) Iteration StatementsFor I = 1 to 6 … Next i

Expression evaluation inVBA Operators in priority order Left to right Operator Priority List ( ) ^ - (unary minus) * / + - Comparison Operators (>, < …) Logical Operators (NOT, AND, OR…)

Test = ^2 > 5 AND (3-7)*2 > 6 (3-7)-4 3^2 9 -4* > 5True -8 > 6False True AND FalseFalse Thus, Test has the value “False”

Conditional Expressions If / then / else end if Select case …. End Case

If (expression) then One or more expressions else One or more expressions end if If (expression ) then (expression) Simple form

Select Case testexpression [Case expressionlist-n [statements-n]]... [Case Else [elsestatements] ] End Select Select Case Statement Like a complex If / Then / ElseIf … / EndIf We will not use it

Iteration Statements Used to execute a number of statements repeatedly Two major types --- For / Next Do / Loop

For counter to upperlimt step size Statements Next counter For Each cell In Selection Statements cell.value = expression Next cell Note that “value” is a property of the “cell” object and is the contents of the cell

Do while expression_is_true Statements Loop Do Statements Loop while expression_is_true

Do Until expression_is_true Statements Loop Do Statements Loop Until expression_is_true

To add develop a function (or sub) First determine what function (or sub ) is to do, the steps to do it, and the needed arguments. In VB, Insert Module Insert Procedure Select desired type Type name Add arguments to argument list

To add develop a function (or sub) Dim all variables Write the code Return to Excel & test

Some Simple Examples  Compute the factorial of number  Compute the sine of an angle from the series expansion

Public Function NFact(N) As Double ‘ Assumes N is an integer > 1 Dim I As Integer NFact = 1 I = 1 Do While I <= N NFact = NFact * I I = I + 1 Loop End Function Simple function to compute n!. There is a worksheet function to do this (Fact(n), but calling it is complicated.

Function to compute the sine of an angle (in radians) Use a Taylor series expansion about 0 radians Sine = Function will take two argument – the angle and n, then number Of terms in the series. It will return the approximate value of the sine. (In theory, n should be ∞ )

Public Function MySine(angle As Single, nterms As Integer) As Double ‘ Declare the variables. Dim J As Integer ‘ Initialize the value to zero MySine = 0.0 ‘ Compute the value by evaluating each term in the sum J = 1 Do While J <= nterms MySine = MySine + ((-1) ^ (J + 1)) * (angle ^ (2 * J - 1)) / (NFact(2 * J - 1)) J = J + 1 Loop End Function