Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)

Slides:



Advertisements
Similar presentations
ISOM3230 Business Applications Programming
Advertisements

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.
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.
Week 10.  Protecting Code  Protecting Worksheets  Error Handling  Charts.
Using Macros and Visual Basic for Applications (VBA) with Excel
Week 5.  Recap – For Each..Next  Personal Macro File  Immediate window  Object Variables Working with worksheets and workbooks  For Loops.
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
Writing VBA Macros Record a new Macro, recording only the name, shortcut and description Stop recording Open macro for editing and enter VB code Test.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
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
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Object Variables Visual Basic for Applications 3.
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.
12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Visual Basic for Applications Macro Programming For Microsoft Office.
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.
OV Copyright © 2011 Element K Content LLC. All rights reserved.  Insert Text  Format Text  Sort Data  Duplicate Data  Generate a Report Formatting.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
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.
Basics 2. Write a macro that … … uses a popup message box to say Hello. (msgbox ”Hello”) … writes the contents of the active cell in a message box. (you.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 9 Macros And Visual Basic For Applications.
Week 6.  Assessment 2  Do Loops  Custom Functions.
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
31/01/ Selection If selection construct.
Controlling Program Flow with Looping Structures
Introduction to Macros in Excel Franco Musso. ActiveCell.Offset.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
1 Visual Basic for Applications - VBA IE What is VBA?  VBA comes with Office u Objects (e.g. of Excel) are exposed for use by VBA u Visual Basic.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
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.
© Stefano Grazioli - Ask for permission for using/quoting: Source: Excel VBA Programming by John Walkenbach.
Chapter 6 Controlling Program Flow with Looping Structures.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
IE 8580 Module 4: DIY Monte Carlo Simulation
VBA - Excel VBA is Visual Basic for Applications
Advanced Excel Topics – Loops
Spreadsheet-Based Decision Support Systems
Scripts & Functions Scripts and functions are contained in .m-files
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
CHAPTER FIVE Decision Structures.
Exploring Microsoft Excel
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Presentation transcript:

Week 4

 Recap – Ranges  For Each Loops

 Ranges Referencing Range Objects  Set (keyword)

1. Starting in the cell Current record a macro for each direction (up, down, left, right) 2. Input box – prompt the user to type l, r, u, d 3. You are going to need If..then..Else if..statements for each of the four options 4. The if statements need to take the form: 5. If (user selects direction caps OR lowercase) AND next cell in that direction is not empty THEN insert appropriate text from macro recorded earlier, ELSEIF

 Active cell is empty ActiveCell. Value = ""  Active cell is not empty ActiveCell.Value <> “” (use ActiveCell.Offset to check surrounding cells)  If (myVar = g OR myVar = G) And another condition Then brackets are needed because the AND is dealt with before the OR

 You can use the Immediate window to test references and / or debug snippets of VBA code. It’s quicker than having to write a Sub just to test one or two lines.  ? in the Immediate Window ‘means print the value of’  Whereas you can execute a statement by typing it and pressing Return eg ActiveCell=99 puts 99 into the active cell on the active worksheet in the active workbook

 Rather than repeat code to perform repeat tasks, you can sometimes create a loop structure to repeat the task in hand with one line of code. The most common loop structures are:  For Each … Next  For … Next  Do … While/Do … Until

Sub selection_test() Selection.CurrentRegion.Select Selection.Formula = 4 Selection.Interior.Color = vbYellow End Sub Sub range_formatandfour() Dim r As Range Set r = Range("G10:h12") r.Formula = 4 r.Interior.Color = vbRed End Sub

 Used when you want to treat each cell in the range as an individual  Particularly when the original value is required (eg New value = old value*1.2)  Or when using an If statement based on the cell value (eg For each cell in range, if the value is less than 0 change the font to red) For Each object in selection -- do some action on each object -- Next object Object is often a range variable representing a single cell

For each Action Next Loop When last object processed, exit loop

 Here is an example: Sub selection_update() Dim mycell as Range ‘we assume that cells were selected before running macro ’if any cell contains non-numeric data macro will fail For Each myCell In Selection Mycell.Interior.Color = vbBlue myCell.Formula = myCell.Formula * 1.5 Next myCell End Sub

 Another example Sub looptest() For Each myCell In Selection If myCell.Formula > 25 Then myCell.Formula = myCell.Formula * 1.5 myCell.Interior.Color = vbBlue Else myCell.Formula = myCell.Formula * 1.2 myCell.Interior.Color = vbRed End if Next myCell End Sub

 Examples: Increment_individual_values Find_max_value  Looping worksheets/ books See code in module c

Sub loop_cells() Dim myRange As Range For Each myRange In Selection MsgBox myRange.Address myRange.Value = "macro passing through" Next myRange End Sub

Sub increment_individual_values() 'NOTE: assumes that some numeric cells are selected, will fail if a cell contains non-numeric data 'PURPOSE: multiply each individual value by 1.5 Dim acell As Range For Each acell In Selection acell.Font.Bold = True acell.Value = acell.Value * 1.5 acell.Font.Bold = False acell.Font.Italic = True Next End Sub

Sub find_max_value() Dim mx As Double Dim mycell As Range mx = Selection.Cells(1).Value For Each mycell In Selection If mycell.Value > mx Then mx = mycell.Value End If Next mycell MsgBox "Maximum value is " & mx End Sub

 Text Colour  Auto Number  For Each increment

 Using Object Variables

Sub MoveAbout() Dim direction As String direction = InputBox("Which way?") If (direction = "l" Or direction = "L") And ActiveCell.Offset(0, -1) <>_ "" Then Selection.End(xlToLeft).Select ' brackets are necessary because And is evaluated before Or ElseIf (direction = "r" Or direction = "R") And _ ActiveCell.Offset(0, 1) <> "" Then Selection.End(xlToRight).Select ElseIf (direction = "b" Or direction = "B") And _ ActiveCell.Offset(1, 0) <> "" Then Selection.End(xlDown).Select ElseIf (direction = "t" Or direction = "T") And _ ActiveCell.Offset(-1, 0) <> "" Then Selection.End(xlUp).Select End If End Sub