Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.

Slides:



Advertisements
Similar presentations
MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Advertisements

ISOM3230 Business Applications Programming
259 Lecture 9 Spring 2013 Advanced Excel Topics – Visual Basic for Applications (VBA)
PROGRAMMING IN VISUAL BASIC PART 1
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.
Using Macros and Visual Basic for Applications (VBA) with Excel
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
Programming in Visual Basic
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
Tutorial 11 Using and Writing Visual Basic for Applications Code
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
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.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
A First Book of ANSI C Fourth Edition
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Visual Basic Programming
Chapter 9 Macros And Visual Basic For Applications.
Spreadsheet Data Tables Data Table 1 allows you to change one input variable’s value at a time and record the impact that the input assumption has on several.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
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.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
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.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Advanced Excel Topics – Visual Basic for Applications (VBA)
Chapter 3: Variables, Functions, Math, and Strings
Chapter 3: Variables, Functions, Math, and Strings
Introduction to Programming
Exploring Microsoft Excel
Introduction to Programming
3.1 Iteration Loops For … To … Next 18/01/2019.
Logical Operators and While Loops
Introduction to Programming
Introduction to Programming
Data Types and Maths Programming Guides.
Microsoft Excel 2007 – Level 2
Presentation transcript:

Class 3 Programming in Visual Basic

Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about Boolean operations

Addressing Spreadsheet Cells A B C D Rows Columns Cell C2 A1 Reference Style

Addressing Spreadsheet Cells A B C D Rows Cell (2, 3) R1C1 Reference Style row, column Columns

VBA Interaction with Excel Excel VBA Function 0, 1, 2, 3 … numbers 1 number Excel VBA Subroutine 0, 1, 2, 3 … numbers 1, 2, 3 … numbers Our focus today

Strings Sub string_demo() Cells(10, 1) = "First" Cells(10, 2) = "Second" Cells(10, 3) = "Third" End Sub Using the VBA editor, enter the following program… While in VBA editor, press F5 to run Subroutine Name Strings in double quotes No arguments passed

Results

Sample Subroutine with Calculation Sub sum_and_product_of_five_numbers() 'This program gives the sum and product of ' five numbers located in Cells B1 to B5 Dim row As Integer Dim sum, product As Double sum = 0 product = 1 For row = 1 To 5 sum = sum + Cells( row, 2) product = product * Cells( row, 2) Next row Cells(6, 1) = "sum" Cells(7, 1) = "product" Cells(6, 2) = sum Cells(7, 2) = product End Sub Subroutine name Comment Declare variables Initialize variables Start loop Input and calculate End loop Output End In VBA editor, enter this program.

Example (con’t) Enter five numbers in cells B1 to B5

Example (con’t) 1.Press Alt+F8 2.Select 3.Press Run

Result

An easier way to execute a subroutine  You can link a subroutine to a keyboard control character.  Assignment is made under Options in the Macro dialog box.

Using Macro Options  Under Options, fill in a letter next to “ctrl+” and the selected macro can be executed by pressing that key combination.  Be careful. If you choose a character that is a standard Windows function (e.g., ctrl+c), VBA will overwrite the Windows function with your macro.

Arrays Dim list (5) as integer Example Indices Default: Index starts with zero Numbers stored in array

Arrays Dim list (1 to 5) as integer Indices Override: Index starts with one Numbers stored in array Example 2

Arrays Dim list (1 to 5, 1 to 2) as integer Row Indices 1 2 Column Indices This is a two-dimensional array. Up to 60 dimensions are allowed. Example 3

Sample Program Sub array_demo() Dim list(1 To 5) As Integer Dim i As Integer Dim sum As Integer sum = 0 i = 1 list(1) = 1 list(2) = 4 list(3) = 9 list(4) = 16 list(5) = 25 Do While sum < 16 sum = sum + list( i) i = i + 1 Loop Cells(15, 1) = "sum =" Cells(15, 2) = sum End Sub Title Declare variables Initialize variables Start loop Calculate End loop Output End Enter and run this program.

Results Sub array_demo() Dim list(1 To 5) As Integer Dim i As Integer Dim sum As Integer sum = 0 i = 1 list(1) = 1 list(2) = 4 list(3) = 9 list(4) = 16 list(5) = 25 Do While sum < 16 sum = sum + list( i) i = i + 1 Loop Cells(15, 1) = "sum =" Cells(15, 2) = sum End Sub

Boolean Operations Truth Tables A B Y And A B Y Or A Y Not result = expression1 Or expression2 result = Not expression result = expression1 And expression2 0 = false 1 = true Y A B

Example Program Option Explicit Sub Boolean_test() Dim truth As Boolean Dim a, b As Integer a = 1 b = 2 truth = False If a = 1 And b = 2 Then truth = True End If Cells(1, 1) = truth End Sub Title Declare variables Initialize Boolean operation Assign Output End Enter and run this program. To see what happens, play with various Boolean operators, and different initial values of a and b.

Function Exercise  The value of  (the ratio of a circle’s circumference to its diameter) has been estimated for about 4000 years.  Several methods for calculating  are presented in the Math Supplement (pages 8-10).  Excel’s built in function for  is only accurate to 14 decimal places (van Rooman did better in 1593).  As a team, pick ~4 (1 per team member) of the various methods used to estimate  using infinite series (avoid Methods a, c, e and k).  Program your function to calculate the first 50 terms, or stop when the value does not change by more than

Subroutine Exercise  Convert your function into a subroutine.  The subroutine should title Column A as  and Column B as that is, the change in  from the previous iteration.  Then beginning in Cells A2 and B2 and going down, output the successive terms of .  Stop the iteration after the first 200 terms, or if an additional term does not change the value of  by more than  For each method selected by your teammates, compare the rate that they converge to an accurate value of 