Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
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
VBA Modules, Functions, Variables, and Constants
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
IE 212: Computational Methods for Industrial Engineering
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Chapter 3 Programming Fundamentals Writing Code 3 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
ENGR 112 Decision Structures.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
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.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Visual Basic Programming I 56:150 Information System Design.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Introduction to Programming Lecture 2 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
Controlling Program Flow with Looping Structures
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
VBA Programming Functions and Decisions. If-Then-Else Structure used to consider alternatives If then --or-- If then Else End If.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Chapter 6 Controlling Program Flow with Looping Structures.
Introduction to Programming Lecture 2
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
Spreadsheet-Based Decision Support Systems
2. Understanding VB Variables
Microsoft Office Illustrated
Repeating Program Instructions
CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Chapter 3 Programming Fundamentals
Presentation transcript:

Overview of VBA Programming & Syntax

Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color, name, value, visible) –Methods: things an object can do (e.g., calculate, additem, show, hide.) –Events: things that can happen to an object (e.g., click, activate, change, scroll.) u Excel’s Object Model u The VBA Editor

Properties: attributes or characteristics of an object.

Methods: things an object can do.

Events: things that can happen to an object.

Variables u Variable - a named location in the computer’s Random Access Memory (RAM) in which we can store data (i.e., text or numbers). u RAM is volatile so data stored there is not permanent. u Examples of variables... X = 7 Lname = “Major” X 7 Lname Major VB: RAM:

Do You Get It? Sub Test( ) X = 7 Y = 2 Z = X * Y Msgbox Z End Sub Note: Variables are usually zero or null until assigned a value.

The MsgBox Statement u MsgBox prompt [, buttons] [, title] u Example msg = “I like VBA” MsgBox msg,,“My Message”

Types of Variables Data typeDescriptionRange Byte 1-byte binary data0 to 255. Integer 2-byte integer< | 32,768 | Long 4-byte integer< |2,147,483,648| Single4-byte floating-pt.-1.4E – 45 to 3.4E38 Double 8-byte floating-pt. – 1.8E308 to 1.79E308 Currency 8-byte, 4 decimals –9E-14 to 9.2E14 String String of characters 0 to two billion chars. Variant 16-byte (chameleon)depends Boolean 2 bytesTrue or False. Date8-byte date/time 1/1/0100 to 12/31/9999. Object 4 bytesAny object reference.

Declaring Variables u Use Dim or Static within a Sub or Function Examples… Sub Test( ) Dim X As Integer Dim Y As Single Dim Z As Boolean Dim A, B, C As Currency ‘ A & B are Variant! Static D As Long‘ D will retain its value …….. End Sub Note: All variables in this example are local to this Sub

Declaring Variables u Use Public or Private at the Module Level Examples… Private Term As Integer Public Payment As Currency u Public variables in code modules are ‘global’ u Public variables in sheet modules are basically user- defined properties of the sheet

Arrays u An array is an indexed set of memory locations. u Each element of the array has a unique index. Example... Dim A(1 to 3) As Single A(1) = 7 A(2) = 19 A(3) = 5 Sum = A(1)+A(2)+A(3) VB: RAM: A(i) 31 Sum

Arrays cont’d u Arrays can also be multi-dimensional Example... Dim A(1 to 3, 1 to 2) As Single A(1,1) = 10 A(3,2) = VB: A(i,j) RAM:

Procedures u Two types: Sub(routines) & Functions u They can be either Private or Public (default) u A function always returns a value! u Sub Example… Public Sub Test( ) Dim x As String x = “BIT 5474” Msgbox “This course is ” & x End Sub

Procedures u Function Example… Private Function AddThree( x as Single) As Single AddThree = x + 3 End Function u We might use (or call) this function as follows Z = AddThree( 7 )

Procedure Arguments u Arguments may be passed to Subs and Functions either ByVal or ByRef Example... Sub MyExample ( ) Dim A as Integer, B as Integer A = 5 : B = 7 TestIt A, B ‘ Here, A becomes 25, B is still 7 End Sub Sub TestIt(ByRef X As Integer, ByVal Y As Integer) X = X^2 : Y = Y^2 End Sub

The InputBox Function u InputBox(prompt [, title] [, default] [, xpos] [, ypos] ) u Example msg = “How much would you like to borrow?” x = InputBox(msg, “Amount Financed”) u If the user clicks OK or hits Enter, the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string (“”).

The MsgBox Function u MsgBox( prompt [, buttons] [, title] ) u Example msg = “Would you like to continue?” If MsgBox(msg, vbYesNo, “Terminate Job?”)=vbYes Then ‘ blah, blah, blah Else ‘ blah, blah, blah End If

Decision Structures u If...Then u If...Then...Else u If...Then...ElseIf u Select Case

If…Then Examples… If X 2 Then Y = X*Z If Q>Z Then X=5*Q : Y = 2*Z If X > 5 Then Y = X + 25 X = 0 End If

If…Then…Else If age < 16 Then prompt = "You are not old enough for a license." MsgBox prompt Else prompt = "You can be tested for a license." MsgBox prompt End If

If…Then…ElseIf If jobClass = 1 Then Bonus = salary * 0.1 * rating / 10 ElseIf jobClass = 2 Then Bonus = salary * 0.09 * rating / 10 ElseIf jobClass = 3 Then Bonus = salary * 0.07 * rating / 10 Else Bonus = 0 End If

Select Case Select Case jobClass Case 1 Bonus = salary * 0.1 * rating / 10 Case 3 Bonus = salary * 0.07 * rating / 10 Case 2, 4, 5 'The expression list can contain several values... Bonus = salary * 0.05 * rating / 5 Case 6 To 8 '...or be a range of values Bonus = 150 Case Is > 8 '...or be compared to other values Bonus = 100 Case Else Bonus = 0 End Select

Looping Structures u Do While...Loop u Do Until...Loop u Do...Loop While u Do...Loop Until u For...Next u For Each...Next

Do While…Loop Do While Not( rs.Eof ) ' rs.Eof returns True/False Stocklist.Additem rs(“ticker”) rs.MoveNext Loop

Do Until…Loop Response = MsgBox("Do you want more data?", vbYesNo) Do Until Response = vbNo ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop

Do…Loop While Do ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop While Response = vbYes

Do…Loop Until Do ProcessUserData 'Call procedure to process data Response = MsgBox("Do you want more data?", vbYesNo) Loop Until Response = vbNo

For…Next For i = 1 to 3 Step 1 Sum = Sum + A(i) Next i For i =LBound(A) To Ubound(A) Sum = Sum + A(i) Next i

For Each…Next For Each x In Worksheets If x.Name = “Grades” Then X.Delete Next x Note: This is very useful for looping through collections of objects.