CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.

Slides:



Advertisements
Similar presentations
Integrated Business Applications with Databases (D3) Jenny Pedler
Advertisements

ISOM3230 Business Applications Programming
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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
Customisation The GUI in most GIS applications is sufficient for most needs. However, situations arise where you want either to: –Modify the interface,
VBA Modules, Functions, Variables, and Constants
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Control Statements Making Decision in Your Program.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Guide To UNIX Using Linux Third Edition
Adding Automated Functionality to Office Applications.
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.
Python quick start guide
IE 212: Computational Methods for Industrial Engineering
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.
Tutorial 11 Using and Writing Visual Basic for Applications Code
The animation is already done for you; just copy and paste the slide into your existing presentation. Dony Pranadiyanta, ST.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Using Visual Basic for Applications in Microsoft Project Sean Vogel.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
I Power Higher Computing Software Development High Level Language Constructs.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
ME 142 Engineering Computation I Using Subroutines Effectively.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Visual Basic Programming I 56:150 Information System Design.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Language Find the latest version of this document at
Understanding Visual Basic Fundamentals CHAPTER 13 Understanding Visual Basic Fundamentals.
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.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
IE 8580 Module 4: DIY Monte Carlo Simulation
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
Excel VBA Day 3 of 3 Tom Vorves.
Exploring Microsoft Excel
T. Jumana Abu Shmais – AOU - Riyadh
CS285 Introduction - Visual Basic
Tonga Institute of Higher Education
Presentation transcript:

CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures and Functions  File and data management (I/O)  Using the Object Browser  First look on the IDE Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures and Functions  File and data management (I/O)  Using the Object Browser  First look on the IDE

2/38 Constants (remark)  Convention: use BLOCK letters to name constants  e.g. Dim MYCONST = 100  easily recognised  Convention: use BLOCK letters to name constants  e.g. Dim MYCONST = 100  easily recognised

3/38 ArraysArrays  Problem: individual values that should be stored together  data of the same type  e.g. names of people in a class  Solutions: 1. User-defined type (looser) 2. Collection (even more looser) 3. Array (tight) – a special collection  Problem: individual values that should be stored together  data of the same type  e.g. names of people in a class  Solutions: 1. User-defined type (looser) 2. Collection (even more looser) 3. Array (tight) – a special collection

4/38 Types of Arrays Like by variables:  Integer, Long -- usual  Single, Double  String  Boolean –- rare  Variant -- very rare = types of values stored in the array  must be of the same type Like by variables:  Integer, Long -- usual  Single, Double  String  Boolean –- rare  Variant -- very rare = types of values stored in the array  must be of the same type

5/38 Dimensions – 1D, 2D,...  1D array of 100: [1.00;2.82;3.63;4.11;..;100.75]  2D array of (3;5): [ ] = 3 rows and 5 columns --> 3x5 matrix  3D array: a cube  4D, 5D, 6D... (rare)  1D array of 100: [1.00;2.82;3.63;4.11;..;100.75]  2D array of (3;5): [ ] = 3 rows and 5 columns --> 3x5 matrix  3D array: a cube  4D, 5D, 6D... (rare)

6/38 DeclarationDeclaration  Syntax: Dim MyArray(100) As Single Dim My2DArray(3,5) As Integer  and also: Dim MyArray(1 to 25) As Single Dim My2DArray(3, 8 to 12) As Integer  UBound(), LBound()  Option Base 1  Syntax: Dim MyArray(100) As Single Dim My2DArray(3,5) As Integer  and also: Dim MyArray(1 to 25) As Single Dim My2DArray(3, 8 to 12) As Integer  UBound(), LBound()  Option Base 1

7/38 ElementsElements  just like individual variables  identified by an index e.g. ReadElement = MyArray(12)  ReadElement must be a variable of the right type  MyArray must have an index of 12  ‘Subscript out of range’  just like individual variables  identified by an index e.g. ReadElement = MyArray(12)  ReadElement must be a variable of the right type  MyArray must have an index of 12  ‘Subscript out of range’

8/38 Value assignment  I want the element of index 5 to be 8.314: MyArray(5) =  with a string array: MyStringArray(5) = ”8.314”  filling up an array: with cycles (loops)  use For.. Next  I want the element of index 5 to be 8.314: MyArray(5) =  with a string array: MyStringArray(5) = ”8.314”  filling up an array: with cycles (loops)  use For.. Next

9/38 Dinamic Arrays Problem: I don’t know the size of the array I need in design-time (depends on user input) Solution: redimensioning Syntax: first:Dim MyDinArray() later:ReDim MyDinArray(Rows, Cols) where Rows & Cols are variables (!) = an elastic bag :) Problem: I don’t know the size of the array I need in design-time (depends on user input) Solution: redimensioning Syntax: first:Dim MyDinArray() later:ReDim MyDinArray(Rows, Cols) where Rows & Cols are variables (!) = an elastic bag :)

10/38 LoopsLoops  Use-case: a block of statements must be repeated many times  Loop types:  For.. Next --> I have a fix number (e.g. 25)  Do.. Loop --> I have a limiting condition  Use-case: a block of statements must be repeated many times  Loop types:  For.. Next --> I have a fix number (e.g. 25)  Do.. Loop --> I have a limiting condition

11/38 For.. Next  to run a block of statements a definite number of times e.g. 25  e.g. to fill up an array For CurrentIndex = 1 To 32 MyArray(CurrentIndex) = CurrentIndex – 1 Next CurrentIndex  to run a block of statements a definite number of times e.g. 25  e.g. to fill up an array For CurrentIndex = 1 To 32 MyArray(CurrentIndex) = CurrentIndex – 1 Next CurrentIndex

12/38 For.. Next  Syntax example: For Count = 1 To 25 [statement1] [statement2]... [statementN] Next Count  Syntax example: For Count = 1 To 25 [statement1] [statement2]... [statementN] Next Count

13/38 For.. Next  Stepping up (Step 2) or down (Step -2): For Count = 1 To 30 Step 2 ’run 15 times [statement1] [statement2]... [statementN] Next Count  Stepping up (Step 2) or down (Step -2): For Count = 1 To 30 Step 2 ’run 15 times [statement1] [statement2]... [statementN] Next Count

14/38 Do.. Loop  to run a block of statements an indefinite number of times  evaluates a condition to determine whether or not to continue running  types: 1.evaluates at the start  While / Until 2.evaluates at the end  While / Until  to run a block of statements an indefinite number of times  evaluates a condition to determine whether or not to continue running  types: 1.evaluates at the start  While / Until 2.evaluates at the end  While / Until

15/38 Do.. Loop (1.)  Syntax: (! logically opposites !) Do While [Condition=True] [block of statements] Loop Do Until [Condition=True] [block of statements] Loop  Syntax: (! logically opposites !) Do While [Condition=True] [block of statements] Loop Do Until [Condition=True] [block of statements] Loop

16/38 Do.. Loop (2.)  Syntax: (! logically opposites !) Do [block of statements] Loop While [Condition=True] Do [block of statements] Loop Until [Condition=True]  Syntax: (! logically opposites !) Do [block of statements] Loop While [Condition=True] Do [block of statements] Loop Until [Condition=True]

17/38 Nested loops  Two or more loops in one another: Do Until Error < [statement1] For Count = 1 To 100 [block] Next Count [more statements] Loop  Two or more loops in one another: Do Until Error < [statement1] For Count = 1 To 100 [block] Next Count [more statements] Loop Do not nest in more than 3-4 levels depth

18/38 Dealing with conditions  evaluating a condition: True/False or Cases  branching: 2, 3, 4... N  selection structure:  2 branches (T/F) --> If..Then..Else  3-4 branches --> If..Then..ElseIf..Else  more branches --> Select Case  evaluating a condition: True/False or Cases  branching: 2, 3, 4... N  selection structure:  2 branches (T/F) --> If..Then..Else  3-4 branches --> If..Then..ElseIf..Else  more branches --> Select Case

19/38 If..Then..ElseIf..Then..Else  Syntax: If [Condition=True] Then [block of statements] Else ‘this happens if Condition=False [another block of statements] End If  Syntax: If [Condition=True] Then [block of statements] Else ‘this happens if Condition=False [another block of statements] End If

20/38 If..Then..ElseIf..ElseIf..Then..ElseIf..Else  Syntax: If [Condition1 = True] Then [block of statements] 1 ElseIf [Condition2 = True] Then [block of statements] 2 ElseIf [Condition3 = True] Then [block of statements] 3 Else ‘this happens if all Conditions are False [block of statements] 4 End If  Syntax: If [Condition1 = True] Then [block of statements] 1 ElseIf [Condition2 = True] Then [block of statements] 2 ElseIf [Condition3 = True] Then [block of statements] 3 Else ‘this happens if all Conditions are False [block of statements] 4 End If

21/38 Select Case  Syntax: Select Case [MyVariable] Case [Value1] [block of statements] 1 Case [Value2] [block of statements] 2 … Case [ValueN] [block of statements] N Case Else [else-block of statements] End Select  Syntax: Select Case [MyVariable] Case [Value1] [block of statements] 1 Case [Value2] [block of statements] 2 … Case [ValueN] [block of statements] N Case Else [else-block of statements] End Select

22/38 In one row  in simple cases: If [Condition=True] Then [statement] 1 Else [statement] 2  in simple cases: If [Condition=True] Then [statement] 1 Else [statement] 2

23/38 Nested If  If..Then..(ElseIf..)Else structures can be nested into each other, but consider the extent of this: If [Condition1 = True] Then [block of statements] 1 If [Condition2 = True] Then [block of statements] 2 End If [block of statements] 3 Else [block of statements] 4 End If  If..Then..(ElseIf..)Else structures can be nested into each other, but consider the extent of this: If [Condition1 = True] Then [block of statements] 1 If [Condition2 = True] Then [block of statements] 2 End If [block of statements] 3 Else [block of statements] 4 End If

24/38 Organization of code  Scope (Subs and variables)  Private or Public (Global)  Return value  no return value --> Procedure (Sub)  yes, of a specified type --> Function  Modules and Forms  WorkBook Object > WorkSheet Object  Scope (Subs and variables)  Private or Public (Global)  Return value  no return value --> Procedure (Sub)  yes, of a specified type --> Function  Modules and Forms  WorkBook Object > WorkSheet Object

25/38 ScopeScope  Private  seen within the current Sub  Public (default)  seen within the current Module  Global  only for variables  seen from anywhere  hazard! --> rare  Private  seen within the current Sub  Public (default)  seen within the current Module  Global  only for variables  seen from anywhere  hazard! --> rare

26/38 ProcedureProcedure  no return value (usually)  General form: Public Sub SubName([Arg1], [Arg2]) [statements] End Sub  no return value (usually)  General form: Public Sub SubName([Arg1], [Arg2]) [statements] End Sub

27/38 FunctionFunction  Syntax: Public Function MyFunction(MyArg As Integer) As Integer [statements] End Function  Value: reached under the name MyFunction  e.g. MyFunction = 5 * MyArg  Syntax: Public Function MyFunction(MyArg As Integer) As Integer [statements] End Function  Value: reached under the name MyFunction  e.g. MyFunction = 5 * MyArg

28/38 Handling events  Event Watching in Windows (auto)  Event --> warns all programs running  Handled event: procedure call  e.g. Private Sub Workbook_Activate() … End Sub  Event Watching in Windows (auto)  Event --> warns all programs running  Handled event: procedure call  e.g. Private Sub Workbook_Activate() … End Sub

29/38 File management  open files  read from files  write to files  we will use only text files (txt)  open files  read from files  write to files  we will use only text files (txt)

30/38 File management FileNum = FreeFile ‘Integer, Open ”MyText.txt” As #FileNum Access Read [do something with data] Close #FileNum ‘Never forget!! FileNum = FreeFile ‘Integer, Open ”MyText.txt” As #FileNum Access Read [do something with data] Close #FileNum ‘Never forget!!

31/38 Data output (in xls) Cells(Row,Col)=”Message”

32/38 VB for Excel IDE  Project Explorer  Properties Window  Working area  Immediate Window  ToolBox (on the WorkSheet)  Toolbars: Standard, Debug, Edit, UserForm  Object Browser  Project Explorer  Properties Window  Working area  Immediate Window  ToolBox (on the WorkSheet)  Toolbars: Standard, Debug, Edit, UserForm  Object Browser

33/38 Object Browser  tree structure  functions and parameters  IsNumeric(), Left(), Right(), Mid(), Trim(), CStr(), Cint(), …  return types  to use a function in a branch: MyCosValue = VBA.Math.Cos(MyDoubleVar)... use dots between levels  tree structure  functions and parameters  IsNumeric(), Left(), Right(), Mid(), Trim(), CStr(), Cint(), …  return types  to use a function in a branch: MyCosValue = VBA.Math.Cos(MyDoubleVar)... use dots between levels

34/38 InputBox()InputBox()  Built-in function  Syntax: MyNewValue = InputBox(Prompt, Title, Default) String variableQuestion Def. value  Built-in function  Syntax: MyNewValue = InputBox(Prompt, Title, Default) String variableQuestion Def. value

35/38 Readable code  End of statement: auto  Begin each statement in a new row  e.g. 2 statements in a row: [statement1] : [statement2]  Groups: statments with a similar function  e.g. declarations, output statements, value assignments etc.  Loops: start lines with a Tab inside  End of statement: auto  Begin each statement in a new row  e.g. 2 statements in a row: [statement1] : [statement2]  Groups: statments with a similar function  e.g. declarations, output statements, value assignments etc.  Loops: start lines with a Tab inside

36/38 Literature online You can find the links on our site, just do not forget to do look it up;).  VisualBasic.about.com  MSDN  VB eBook  VBA Tutorial (with example) + The 10 Commandments of VB You can find the links on our site, just do not forget to do look it up;).  VisualBasic.about.com  MSDN  VB eBook  VBA Tutorial (with example) + The 10 Commandments of VB

See you next week:)