Week 10.  Protecting Code  Protecting Worksheets  Error Handling  Charts.

Slides:



Advertisements
Similar presentations
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 6 BACKNEXTEND 6-1 LINKS TO OBJECTIVES Workbook properties Workbook properties Workbook Sharing.
1 Linking & Consolidating Worksheets Applications of Spreadsheets.
Protecting cells in the worksheet from being changed. June 21, 2012.
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.
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Templates and Styles Excel Advanced. Templates are pre- designed and formatted spreadsheets –They provide consistency of layout/structure –They.
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
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.
Working with Charts ISYS 562. Chart Locations As an embedded object on a worksheet: –Worksheet ChartObjects –ChartObject: A container for a chart »Chart.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Microsoft Excel 2003 Illustrated Complete Excel and Advanced Worksheet Management Customizing.
Macros in Excel Intro to lab 1. Macroinstructions Macro is recorded in VBA module sequence of Excel operations Macros can automate tasks in Excel Macro.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
LOGO Chapter V Formattings 1. LOGO Overview  Conditional formatting  Working with tables  Filtering  Sorting  Freeze panes  Pivot tables  How to.
Using the Select Case Statement and the MsgBox Function (Unit 8)
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.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
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.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Excel Spreadsheet basics. Excel Sheets and Books  Spreadsheet: tool to analyze, chart and manage data for personal, business and financial use Worksheet:
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.
Excel Ch 4 Review.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 5 Recording Macros. How to Record a Macro Use Tools/Macro/Record New Marco Give the following information: macro name; provide a description of.
Chapter 9 Macros And Visual Basic For Applications.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
Introduction to Microsoft Excel Macros COE 201- Computer Proficiency.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Microsoft Excel Prepared by the Academic Faculty Members of IT.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
© Stefano Grazioli - Ask for permission for using/quoting: Source: Excel VBA Programming by John Walkenbach.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Lecture 6 – Working with VBA Sub Procedures Dr Joanna Wyrobek 1.
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
VBA - Excel VBA is Visual Basic for Applications
Formatting Worksheet Elements
Data Validation and Protecting Workbook
Developing an Excel Application
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Visual Basic for Applications in Microsoft Excel (1)
Exploring Microsoft Excel
Microsoft Office Excel 2003
Microsoft Excel 2007 – Level 2
Presentation transcript:

Week 10

 Protecting Code  Protecting Worksheets  Error Handling  Charts

 Using form tools  Changing the QAT  Revision Exercises

 Run the Add Customer form from the button on the Data Sheet  Features: Auto number Customer ID Accommodates other ranges in the worksheet  Design Issues: Uses Named Range to place details on the sheet Code attached to Initialize event generates an autonumber The text box’s Enabled property is set to False to disallow editing Need to update the named range after input some data below the named range which we don’t want to overwrite

 Locking the project for viewing means that the macros will still run but that users will need a password to access the code in the editor.  Close the file and reopen it.  To access the code of a protected project, from the Project Window, double click on the appropriate project and enter the password at the prompt.

 You can password protect sheets using the Review tab in excel where you can specify the limits of the protection  You can exclude selected cells from the protection by highlighting and using the protect cells tab of the format cells dialog

 A line of code to protect the current sheet might look like this. ActiveSheet.Protect "password" True, True, True  Where ActiveSheet is the object and Protect the method.  Protect has many arguments.  The first is the password string, followed by a series of True/False arguments resembling the tickbox list found in Excel.  As with the Excel interface, by default most options are set to True (protected) whilst a few are false (unprotected)

 Instead of always protecting the active sheet, you might want the user to choose which sheet to unprotect Sub protect_choose() Dim mySheet As Worksheet Dim sheetName As String sheetName = InputBox("Please type Sheet name", "non-robust sheet selector", "sheet1") Set mySheet = Worksheets(sheetName) mySheet.protect “password" End Sub

 Adapt previous macro to protect each sheet in turn Sub ProtectSheets() Dim mySheet As Worksheet mySheet.Protect “password" End Sub

 If you try to run a sub to add a worksheet named “Results” where a worksheet named “Results” already exisits, Sub will stop running A message box containing an error code will appear Error handling seeks to write code that deals with the issue and moves on to the next line

 Use On Error Resume Next to allow the macro to continue when an error is encountered.  Use Err.Number to find out if there is an error or not. If the number property of the Err object is 0, then no error occurred. For example: If err.number <> 0 Then -- do something to cope with eventuality -- End if

 Function SheetExists This function uses the Err object to see if an error occurs when a sheet is referenced, i.e. it doesn’t exist. If an error occurs, then the Resume command clears the error and allows the program to continue. Can be used within another sub to check for a sheet name before trying to use the name for another sheet

Sub make_report() Dim mysheet As Worksheet Dim myBase As String Dim mySuffix As Integer Set mysheet = Worksheets.Add myBase = "Report" mySuffix = 1 On Error Resume Next mysheet.Name = myBase & mySuffix Do Until Err.Number = 0 Err.Number = 0 mySuffix = mySuffix + 1 mysheet.Name = myBase & mySuffix Loop End Sub

 Record a macro to create a chart (start with a simple column chart) in a new sheet for the exam results on the exams sheet  Good example of instance where recorded code and reference to the object map can help

Sub MakeChart() Selection.CurrentRegion.Select Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Sheets("Exams").Range("A1:B10")_, PlotBy:= xlColumns ActiveChart.Location Where:=xlLocationAsObject, Name:="Exams" With ActiveChart.HasTitle = True.ChartTitle.Characters.Text = "Exam Marks".Axes(xlCategory, xlPrimary).HasTitle = False.Axes(xlValue, xlPrimary).HasTitle = False End With ActiveChart.HasLegend = False End Sub

 Adapt your code to produce a pie chart  Rename the chart  Label the axis  Allow the user to select the range of the chart data

 Module level variables Use module level variables only when two or more subs need access to the same variable; otherwise it’s better practice to use variables local to the procedure they are in. To declare a module level variable, you declare it in the Declarations section (where the Option statements are), before any subs.  You can use Dim as previously, but it is recommended you use the keywords Private or Public to determine the scope of the variable. Private variables are accessible only to the subs in the same module, Public variables are accessible to all the subs in the workbook.

 An example of a longer application  Uses Module level variables  Has three main Subs which call on combinations or smaller subs  Uses Workbook_Open event handler to reset workbook