# 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute.

Slides:



Advertisements
Similar presentations
Microsoft Office XP Microsoft Excel
Advertisements

Review for Excel Chapter 1
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.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Tutorial 7: Developing an Excel Application
Developing an Excel Application
Tutorial 8: Developing an Excel Application
© 2002 ComputerPREP, Inc. All rights reserved. Excel 2000: Customizing Excel and Using Macros.
Copyright © 2008 Pearson Prentice Hall. All rights reserved Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
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.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
Lecture Excel: Multiple Worksheets. Workbook and Worksheets Multiple worksheets in a single workbook. When saved, only a single workbook (XLS) is saved.
Adding Automated Functionality to Office Applications.
Using Visual Basic for Applications (VBA) Jake Blanchard University of Wisconsin Spring 2010.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Macros and VBA A macro is a set of instructions that tells Excel which commands to execute.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Week 1.  Kate Watson  Checked at least every other day. Queries and general comments welcome.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #3: Manipulating Excel Objects IE 212: Computational Methods for Industrial Engineering.
Macros n Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become.
12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm.
Formulas and Advanced Features R003. AO1: Use Formulas & features in your spreadsheet Invoice sheet Absolute cell reference Macros Conditional Formatting.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
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
Lecture Excel: Macros & Pivot Tables. Macros A macro is a series of commands that are stored and can be run whenever you need to perform the task.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
1 Performing Spreadsheet What-If Analysis Applications of Spreadsheets.
2. Recording a Macro. Macro Recording Select Record Macro from the Macro file menu and the dialog box opposite will appear Your macro will require a name.
Visual Basic for Applications Macro Programming For Microsoft Office.
Visual Basic for Applications (VBA) An object-oriented programming language –Instructions for the manipulation of objects –A structured way to provide.
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.
Chapter 9 Macros And Visual Basic For Applications.
Access Lesson 11 Creating and Running Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
Excel: Formulas and Functions 1. 2 Objectives  Name a cell or range  Locate and use Excel’s predefined functions  Debug formulas that contain errors.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
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.
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
Excel Spreadsheets Formatting and Functions ICS100 – Spring 2007 D. Pai.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Excel Tutorial 8 Developing an Excel Application
Developing an Excel Applications
IE 8580 Module 4: DIY Monte Carlo Simulation
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Excel: Macros & Pivot Tables
Microsoft Access 2003 Illustrated Complete
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Exploring Microsoft Excel
IS-171 Computing With Spreadsheets
Lesson 1 - Automating Tasks
Jeroo Code 7-Sep-19.
Presentation transcript:

# 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 2# 2 CS 105 Spring 2010 Macro Statements A Macro always begins a Sub statement A Macro ends with an End Sub statement

# 3# 3 CS 105 Spring 2010 A Macro To perform multiple actions on the same object Macros use –With –End With

# 4# 4 CS 105 Spring 2010 Select and Do—the pattern Note how first the Macro selects the object, then executes the code…

# 5# 5 CS 105 Spring 2010 More about Macros… Step Into command helps you debug a macro Opening a file with a macro will prompt a question about viruses

# 6# 6 Recording a Macro in Office 2007 Go to Developer, click on Record Macro CS 105 Spring 2010

# 7# 7 Record Macro Comments begin with an apostrophe (‘) Comments help the programmer, others to read code

# 8# 8 CS 105 Spring 2010 Calling Macros You can use macros easily because they are on a Module sheet, that makes them available to everything, so all you do is write the macro name

# 9# 9 CS 105 Spring 2010 Finding the Stop Recording button in Office 2003 We lost our little macro recorder button If you lose yours, go to View/Toolbars/Stop Recording, and it will appear on your spreadsheet or on a toolbar.

# 10 Stop Recording in Office 2007 Developer, then click on Stop Recording CS 105 Spring 2010

# 11 CS 105 Spring 2010 Cell References in Macros Absolute references— When we specify exactly what cells are affected Absolute cell address are constant

# 12 CS 105 Spring 2010 An absolute Macro Range selects the starting or active cell/cells—the references below are absolute—whenever the Macro runs, these exact cells are selected First, the cell is selected, then a number is placed in it

# 13 CS 105 Spring 2010 ActiveCell.FormulaR1C1 R1C1 is a reference style (you can set this under Tools/Options/General) Don’t worry, we won’t be using this in class. We want you to know where it came from.

# 14 CS 105 Spring 2010 ActiveCell.FormulaR1C1 = "Income" All that you need to know is that for the active cell, the content is "Income" "FormulaR1C1" refers to the contents of the active cell You find this notation in Macros that you create

# 15 CS 105 Spring 2010 FormulaR1C1 vs. Value Range("D5").Select ActiveCell.FormulaR1C1 = "8" Range("D5").Select ActiveCell.Value = "8" These two code fragments produce the same results.

# 16 CS 105 Spring 2010 What if you want to vary the cells?

# 17 CS 105 Spring 2010 Relative references We don’t always want the event to happen in the same place Cell addresses change Visual Basic uses Offset to indicate space away from active cell

# 18 CS 105 Spring 2010 Recording a Macro with Relative References Before you start recording your Macro, click on the Relative Reference button

# 19 In Office 2007 First, click on Use Relative References, then click Record Macro CS 105 Spring 2010

# 20 CS 105 Spring 2010 Relative Cell References –ActiveCell.Offset(2, 0).Range("A1").Select –Means the cell two rows below the active cell, in the same column

# 21 CS 105 Spring 2010 ActiveCell.Offset(2, 0).Range("A1").Select What does ActiveCell.Offset(2, 0).Range("A1").Select mean? How to I make the cell next to the active cell become the new active cell?

# 22 CS 105 Spring 2010 Range("A1").Select This part of the code tells you how many cells have been selected. ActiveCell.Offset(2, 0).Range("A1").Select means 1 cell is selected ActiveCell.Offset(2, 0).Range("A1:C1").Select means 3 cells in a row have been selected (if you are in cell B2, then B2, C2, and D2 are selected)

# 23 CS 105 Spring 2010 Activate or Select? Range("B3").Select Selection.Interior.ColorIndex = 6 Range("B3").Activate Selection.Interior.ColorIndex = 6 Both code fragments have the same result

# 24 CS 105 Spring 2010 This Relative Macro Causes a Crash

# 25 CS 105 Spring 2010 Run it line-by-line to find bug Code runs OK until it reaches the line under the highlighted line. You can turn your attention to the problem line of code!

# 26 CS 105 Spring 2010 To Summarize What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute