IENG 490 LAB FIND/FINDNEXT AND BYREF/BYVAL

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Introduction to Visual Basic for Applications programming in Excel.
“Going loopy with Visual Basic!”
Sub and Function Procedures
ISOM3230 Business Applications Programming
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Pre-defined System Functions Simple IF & VLOOKUP.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Sub Programs To Solve a Problem, First Make It Simpler.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Modeling using VBA. Using Toolbox This is a label This is a button Using the Toolbox select a GUI element and by mouse-click place it on the frame This.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Apply Sub Procedures/Methods and User Defined Functions
IE 212: Computational Methods for Industrial Engineering
Why to Create a Procedure
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
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
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
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 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
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.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
VBA (continued) DSC340 Mike Pangburn. Consider a quite different example  You need to compute someone’s exact age.  The math is actually somewhat complicated.
Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
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.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
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.
# 1# 1 Using Procedures and Functions What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function? CS.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
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.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Macros in Excel Using VBA Time Required – 5 hours.
VBA Programming for Excel Review Excel Objects Excel Methods Identifying Specific Cells Review Functions for Excel Custom Menus.
Sub Procedures And Functions
VBA - Excel VBA is Visual Basic for Applications
Spreadsheet-Based Decision Support Systems
Functions Chapter 6-Part 2.
2. Understanding VB Variables
Final Exam Review Part 4 - VBA
Procedures and Functions
VBScript Session 7 Dani Vainstein.
Introduction to Visual Programming
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Procedures: Functions and Subroutines
Activate a range and manipulating activated range
VBA Programming for Excel
Presentation transcript:

IENG 490 LAB FIND/FINDNEXT AND BYREF/BYVAL RESEARCH ASSISTANT: ADEWALE ADESINA

Find functions Generally, each of the function is expressed as .Find( What , After , LookIn , LookAt , SearchOrder , SearchDirection , MatchCase , MatchByte , SearchFormat ) Take note of the “dot” before “Find”, the meaning of this is that you will have to find something from somewhere.

That “Somewhere” is written before the “dot”. Ofcourse “Somewhere” to find a string or any Excel data type must be a collection of strings or other data types which is or must be definate. That is, it should have a specified boundaries (starting point and end point). Obviously, find function anticipates to look (in, at), after, search (order or with direction or certain format), match data (by case or character, byte) from a range of string of data type. Hence a range of data source must be specified with find function and a Range object represents the first cell where that information is found.

Hence a range of data source must be specified with find function and a Range object represents the first cell where that information is found. The decleration is Range.Find(Requiredfunction:=string/datatype,Optionalfunction:=excelValues)

The FindAll function simplifies the use of the Find method. Function FindAll(What, Optional SearchWhat As Variant, _         Optional LookIn, _         Optional LookAt, _         Optional SearchOrder, _         Optional SearchDirection As XlSearchDirection = xlNext, _         Optional MatchCase As Boolean = False, _         Optional MatchByte, _         Optional SearchFormat) As Range The function returns a range consisting of all the cells that match the specified criteria.  The table below presents the list of the required and optional functions

Sub Find( ) Dim SalaryWkSht As Worksheet Dim LastRow As Long, i As Long, c As Long Dim myCell As Range Set SalaryWkSht = Sheets("Sheet1") LastRow = SalaryWkSht.Range("A" & Rows.Count).End(xlUp).Row c = 12000 Set myCell = SalaryWkSht.Range("B1:B" & LastRow).Find(What:=c, LookIn:=xlValues) If Not myCell Is Nothing Then MsgBox "Value found in cell " & myCell.Address End If Exit Sub End Sub

Assumming the value to be found appears more than once in the cell, we need .FindNext function to find them within the cell. Sub FindNext() Dim c As Range On Error Resume Next With Worksheets(1).Range("B1:B500") Set c = .Find(10000, LookIn:=xlValues) If Not c Is Nothing Then firstaddress = c.Address Do MsgBox "Value found in cell " & c.Address Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstaddress End If End With End Sub

ByVal and ByRef Function A previously declared argument can by passed over or copied to another argument(s) by using By Value (ByVal) or By Reference (ByRef) Subfunction. ByRef passes a pointer to the variable, so any changes are reflected everywhere that variable is used. ByVal means that you are passing a copy of a variable to your Subroutine. Changes are made to the copy but the original will not be altered.

This implies that , in the case of objects, they are always passed as references (pointers) but if you pass an object ByVal you pass a copy of the pointer to the object rather than the original pointer (note: not a copy of the object) ByVal Public Function functionByVal(ByVal x) variant/datatype functionByVal = variant/datatype End Function   ByRef Public Function functionByRef(ByRef x) functionByRef = variant/datatype

Default argument Public Function functionByDefault(x) variant/datatype functionByDefault= variant/datatype End Function   It should be noted that whenever, the default argument is used, ByRef is preset by the Excel VBA.

Example: Datatype Sub ieng490( ) Dim i As Long i = 1 Call Change_ByRef(i) ' i is changed by the Change_ByRef procedure and that change IS reflected here too because it was passed by reference MsgBox "i is now: " & i End Sub Sub Change_ByRef(ByRef lInput As Long) lInput = 14 End Sub  

Sub ieng490ByVal ( ) Dim i As Long i = 1 Call Change_ByVal(i) ' i is changed only within the Change_ByVal procedure because a COPY of it was passed MsgBox "i is still: " & i End Sub Sub Change_ByVal(ByVal lInput As Long) lInput = 21 End Sub  

Example: Object Sub Object_ieng490ByRef ( ) Dim j As Range Set j = Range("B1") Call Change_object_ByRef(j) ' j is changed to a different cell by the Change_ByRef procedure and ' that change IS reflected here too because it was passed by reference MsgBox "j is now: " & j.Address End Sub Sub Change_object_ByRef(ByRef jInput As Range) ' change the range variable to one row down Set jInput = jInput.Offset(1)  

Sub Object_ieng490ByVal ( ) Dim j As Range Set j = Range("B1") Call Change_object_ByVal(j) ' r is changed to a different cell by the Change_ByVal procedure but ' that change IS NOT reflected here because it was passed by value. MsgBox "j is still: " & j.Address End Sub Sub Change_object_ByVal(ByVal jInput As Range) ' change the range variable to one row down Set jInput = jInput.Offset(1) End Sub  

Data ByRef Sub MyHome ( ) Dim x As Double x = 5.005 MsgBox AddUp(x) End Sub Function AddUp(ByRef x As Double) As Double x = 10 + x End Function

Example 2: Data ByVal Sub MyHome( ) Dim x As Double x = 5.005 MsgBox AddUp(x) MsgBox x End Sub Function AddUp(ByVal x As Double) As Double AddUp = 10 + x End Function

CIRCLE RADIUS, AREA AND CIRCUMFERENCE Sub CalRadiusByRef() Dim r As Double r = 2.5 Radius = r MsgBox "Radius of the circle is " & r MsgBox "Area of the circle is " & Area(r) MsgBox "Circumference of the circle is " & Circumference(r) End Sub Function Area(Radius As Double) As Double Radius = Radius ^ 2 Area = WorksheetFunction.Pi * Radius End Function Function Circumference(Radius As Double) As Double Circumference = 2 * WorksheetFunction.Pi * Radius End Function   

To correct this, ByVal can be added the Function Area code as You will notice that the value of the circumference of 39.2669 obtained by the ByRef function is wrong The actual value for the circumference of a circle with radius of 2.5 is 15.7 This is as a result of ByRef function that changed the value of the original radius to the new value of radius used for the calculation of the area at this line   Radius = Radius ^ 2 To correct this, ByVal can be added the Function Area code as “Function Area(ByVal Radius As Double) As Double”  

Sub CalRadius() Dim r As Double r = 2.5 MsgBox "Radius of the circle is " & r MsgBox "Area of the circle is " & Area(r) MsgBox "Circumference of the circle is " & Circumference(r)   End Sub Function Area(ByVal Radius As Double) As Double Radius = Radius ^ 2 Area = WorksheetFunction.Pi * Radius End Function Function Circumference(Radius As Double) As Double Circumference = 2 * WorksheetFunction.Pi * Radius