VBA (continued) DSC340 Mike Pangburn. Consider a quite different example  You need to compute someone’s exact age.  The math is actually somewhat complicated.

Slides:



Advertisements
Similar presentations
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
Advertisements

String Variables Visual Basic for Applications 4.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Introduction to JavaScript for Python Programmers
5.05 Apply Looping Structures
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Financial Information Management Managing Financial Information Critical Thinking Business Process Modeling WINIT Control Structures Homework.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Scripting Languages.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Intrinsic Functions Pre-coded Functions Used to improve developer productivity Broad Range of Activities Math calculations Time/Date functions String.
Mr C Johnston ICT Teacher BTEC IT Unit 06 - Lesson 05 Learning To Program.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.
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.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
22/11/ Selection If selection construct.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Week 6.  Assessment 2  Do Loops  Custom Functions.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
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.
Macro’s Within excel. Most functionality can be driven from VBA VBA is the programming language that runs inside of excel. It uses visual basic as the.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming control structures, events, objects.
ME 142 Engineering Computation I Using Subroutines Effectively.
A Macro to Exchange the Values of Arbitrary Cells.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
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.
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.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
DATA TYPES, VARIABLES AND CONSTANTS. LEARNING OBJECTIVES  Be able to identify and explain the difference between data and information  Be able to identify,
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
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.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 6 Controlling Program Flow with Looping Structures.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Selection Using IF THEN ELSE CASE Introducing Loops.
IE 8580 Module 4: DIY Monte Carlo Simulation
Spreadsheet-Based Decision Support Systems
البرمجة بلغة الفيجول بيسك ستوديو
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to TouchDevelop
Control Structures Part B - Message and Input Boxes
Selection Statements.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Programming Concepts and Database
VB.Net Programming Console Application
Data Types and Maths Programming Guides.
GCSE Computing:: Selection (IF statements)
Presentation transcript:

VBA (continued) DSC340 Mike Pangburn

Consider a quite different example  You need to compute someone’s exact age.  The math is actually somewhat complicated and we don’t have time/energy/experience to figure it out, so we perform a Google search on VBA code and calculating someone’s age.  Here’s what I found…

VBA age calculator from WWW ‘ Determines time in Years (Y), Months (M), and Days (D) between Date1 and Date2 Dim Temp1 As Date Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1)) Y = Year(Date2) - Year(Date1) + (Temp1 > Date2) M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2)) D = Day(Date2) - Day(Date1) If D < 0 Then M = M - 1 D = Day(DateSerial(Year(Date2), Month(Date2) + 1, 0)) + D + 1 End If

Store Y, M, D info in an object  I’d like a “date object” to store the 3 date parts (Y, M, and D)  There is no such built-in object type (i.e., a “class”) in VBA  So, I can create one, which I decided to call timeInYMD

Using my new class  Assuming I have used the WWW code I found to calculate Y, M, and D, I want to create an object from my new class  Here is what that code would look like in VBA syntax Dim result As timeInYMD ‘ declares the new variable named result Set result = New timeInYMD ‘ sets the var. equal to a new timeInYMD object result.Years = Y result.Months = M result.Days = D

My complete Age() function Function Age(Date1 As Date, Date2 As Date) As timeInYMD Dim Y, M, D As Integer Dim Temp1 As Date ' Math/code found on WWW Google search Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1)) Y = Year(Date2) - Year(Date1) + (Temp1 > Date2) M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2)) D = Day(Date2) - Day(Date1) If D < 0 Then M = M - 1 D = Day(DateSerial(Year(Date2), Month(Date2) + 1, 0)) + D + 1 End If ' End of found code computing age in Y, M, D Dim result As timeInYMD Set result = New timeInYMD result.Years = Y result.Months = M result.Days = D Set Age = result End Function

How do we get the user’s DOB?  We could get the user’s DOB (date of birth) by having it typed into a worksheet cell  Let’s see how we can do it with a dialog box  Again, we can do a Google WWW search to find some example VBA code for an input box window. Here’s what I found: Dim DOB As Date DOB = InputBox ( Prompt :="Your DOB please", Title :="ENTER DOB", Default :="01/01/2001")

Once we have their DOB, call Age()  After the user types their DOB into the dialog box, we compute their age via our Age() function, which returns a timeInYMD object  So we can hold that result in our program using a timeInYMD object variable Dim yourAge As timeInYMD Set yourAge = Age(DOB, Date)

Report info to user in a MsgBox  This code will report the results back to the user Dim messageStart As String messageStart = "That would suggest you are " & yourAge.Years & " years, " & yourAge.Months & " months, and ” & yourAge.Days & " days old." MsgBox messageStart, vbInformation, "Age Calculator"

My complete subroutine Sub ProcessUserDOB() Dim DOB As Date DOB = InputBox(Prompt:="Your DOB please (e.g., 01/15/1988)", Title:="ENTER YOUR DOB", Default:="01/01/2001") Dim yourAge As timeInYMD Set yourAge = Age(DOB, Date) Dim messageStart As String messageStart = "That would suggest you are " & yourAge.Years & " years, " & yourAge.Months & " months, and ” & yourAge.Days & " days old.” MsgBox messageStart, vbInformation, "Age Calculator” End Sub

Let’s call another sub to give voting/drinking info.  Let’s write another sub()  We will call it at the bottom of the prior sub by adding the line: Call ReportDrinkingVotingStatus(yourAge)  This new sub will take the person’s age and determine:  Is the person of legal drinking age?  Is the person of legal voting age?

Figuring out the voting/drinking Qs  Is the age eligible for voting or drinking? Dim canDrink, canVote As Boolean If yourAge.Years >= 21 Then canDrink = True Else: canDrink = False End If If yourAge.Years >= 18 Then canVote = True Else: canVote = False End If

Report the results using a MsgBox If canDrink And canVote Then messageEnd = “Good news, that would mean you can drink and vote." Else: If canVote Then messageEnd = " You can't drink, but you can vote." Else: messageEnd = " Bummer, you can't drink or vote." End If MsgBox messageEnd, vbInformation, "Age Calculator"

The complete voting/drinking sub Sub ReportDrinkingVotingStatus(yourAge As timeInYMD) Dim messageEnd As String Dim canDrink, canVote As Boolean If yourAge.Years >= 21 Then canDrink = True Else: canDrink = False End If If yourAge.Years >= 18 Then canVote = True Else: canVote = False End If If canDrink And canVote Then messageEnd = " Good news, that would mean you can drink and vote." Else: If canVote Then messageEnd = " You can't drink, but you can vote." Else: messageEnd = " Bummer, you can't drink or vote." End If MsgBox messageEnd, vbInformation, "Age Calculator" End Sub