Week 6.  Assessment 2  Do Loops  Custom Functions.

Slides:



Advertisements
Similar presentations
TUTORIAL 1 Getting Started with Excel
Advertisements

© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
1 Alt + ‘ Ctrl + Shift + ~ Ctrl + Shift + $ Ctrl + Shift + % Ctrl + Shift + ! Ctrl + Shift + & Ctrl + Shift + _ Ctrl + b Ctrl + i Ctrl + u Ctrl + 9 Ctrl.
USING EXCEL FOR KEEPING STUDENT RECORDS. WHAT THIS PRESENTATION COVERS What Excel looks like Cells, rows and columns Renaming sheets Minimising the ribbon.
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Microsoft Office XP Microsoft Excel
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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.
WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
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.
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.
Loading Excel Double click the Excel icon on the desktop (if you have this) OR Click on Start All Programs Microsoft Office Microsoft Office Excel 2003.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 2 1 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
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.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
1 Computing for Todays Lecture 8 Yumei Huo Spring 2006.
FIRST COURSE Excel Lecture. XP 2 Introducing Excel Microsoft Office Excel 2007 (or Excel) is a computer program used to enter, analyze, and present quantitative.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Exploring Formulas.
Managing Business Data Lecture 8. Summary of Previous Lecture File Systems  Purpose and Limitations Database systems  Definition, advantages over file.
Microsoft Excel Diane M. Coyle Spring 2009 CS 105.
XP Excel Tutorial 3 Working with Formulas and Functions.
XP Abdul Hameed 1 Microsoft Office Excel 2013 Tutorial 2 – Working With Formulas and Functions.
Objectives Describe the advantages of spreadsheets List several applications for spreadsheets Explain the underlying principles of electronic spreadsheet.
IE 212: Computational Methods for Industrial Engineering
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. 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.
Using Excel for A – Z Analysis: ‘To Present’ items Jack Weinbender, Milligan College.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
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
 What is a formula in Excel?  A formula is statement written by the user to be calculated. Formulas can be as simple or as complex as the user wants.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Excel CREATING A WORKSHEET AND CHART. Personal Budget Worksheet We will create a personal budget worksheet that shows you income each month and your expenses.
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.
Microsoft ® Office Excel 2007 Working with Charts.
Lesson 7 Using Logical and Financial Functions
Week 4.  Recap – Ranges  For Each Loops  Ranges Referencing Range Objects  Set (keyword)
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 9 Macros And Visual Basic For Applications.
ME 142 Engineering Computation I Using Subroutines Effectively.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
XP 1 Microsoft Office Excel 2003 Working With Formulas and Functions.
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
XP 1 ﴀ New Perspectives on Microsoft Office 2003, Premium Edition Excel Tutorial 2 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
VBA Navigation, Conditionals, and Boxes. VBA Navigation.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
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.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
CONDITIONAL FORMATTING AND CUSTOM NUMBER FORMATS LEC 5 1.
Macros in Excel Using VBA Time Required – 5 hours.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Computer Fundamentals Muhammadamin Daneshwar And Masoud Aras Computer Engineering Department Soran University Lecture 6.
Excel Tutorial 8 Developing an Excel Application
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Spreadsheet-Based Decision Support Systems
MS-Excel Part 1.
Analyzing Data Using Formulas
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Microsoft Excel 2007 – Level 1
Presentation transcript:

Week 6

 Assessment 2  Do Loops  Custom Functions

Sub SheetCounter() MsgBox ("There are " & Worksheets.Count & " worksheets in this _ workbook") End Sub Sub CreateStaffList() Dim r As Worksheet Dim s As Worksheet Worksheets.Add.Name = "StaffList" Set r = Worksheets("net_pay") Set s = Worksheets("StaffList") r.Range("G2:G10").Copy s.Range("A1:A10") End Sub

Produce a macro that, if run on a new workbook will: Ask the user how many stores and how many products they wish to monitor Create new worksheets so that there are enough for one for each week (4?) and a summary sheet Name each work sheet appropriately Ensure each sheet is set up ready for the user to input data, with formulas to calculate the row and column totals

 Add formulas in the summary sheet to “pull through” the data from the previous sheets  Extend the summary sheet to hold other useful calculations (eg. best performing store / product)  Allow the user to input the number of weeks the workbook will be used for  Ask the user which month/ year the workbook is intended for and save the workbook using month/year as a name

 There are four varieties of the Do loop: Do While... Loop Do... Loop While Do Until... Loop Do... Loop Until

Sub DWL() Dim x As Integer x = 0 Do While x < 5 Debug.Print x x = x + 1 Loop MsgBox "bye" End Sub x < 5 is the condition which initially evaluates to True The key word “Loop” returns control to the Do While statement which re- evaluates the condition; there should be a statement somewhere before the statement which changes the condition otherwise the loop will run ‘forever’  (Note: Ctrl + Break should terminate most runaway loops; if not Ctl Alt + Delete and end Excel - and lose unsaved work!)

Sub MarkEmptyCells() Do While IsEmpty(ActiveCell) With ActiveCell.Value = "This cell is blank".Font.Bold = True.Offset(1, 0).Activate End With Loop MsgBox "We are out of the loop. That's all folks" End Sub

Sub DLW() Dim x As Integer x = 0 Do Debug.Print x x = x + 1 Loop While x < 5 End Sub There is no condition at this stage so the statements to be executed happen at least once. The Statement returns control to the Do statement only if the condition evaluates to true.

Sub DLU() Dim x As Integer x = 0 Do Debug.Print x x = x + 1 Loop Until x > 5 End Sub Sub DUL() Dim x As Integer x = 0 Do Until x > 5 Debug.Print x x = x + 1 Loop End Sub

Sub markEmptyCells_until() Do Until not IsEmpty(ActiveCell) ActiveCell.Value = "Default value" ' whatever ActiveCell.Font.Bold = True 'makes it easy to see ActiveCell.Offset(1, 0).Select Loop MsgBox "We are out of the loop. That's all folks" End Sub

 All the following programs select the first empty cell in the same column below the active cell, four illustrating the use of Do loops; the fifth uses the statement that is recorded when you press Ctrl and the Down Arrow key

Sub one() Do While Not IsEmpty(ActiveCell) ActiveCell.Offset(1, 0).Select Loop End Sub Sub two() Do ActiveCell.Offset(1, 0).Select Loop While Not IsEmpty(ActiveCell) End Sub Sub three() Do Until IsEmpty(ActiveCell) ActiveCell.Offset(1, 0).Select Loop End Sub Sub four() Do ActiveCell.Offset(1, 0).Select Loop Until IsEmpty(ActiveCell) End Sub Sub five() Selection.End(xlDown).Select ActiveCell.Offset(1, 0).Select End Sub

 Loops can contain control statements such as If Then or other loops  If statements can contain loops  You can break out of a loop if necessary by using the Exit Do keyword

 Do Loop  Leap Years (part 1)

 You are probably familiar with using built-in functions like SUM, AVERAGE and IF. With VBA, you can create your own functions, uniquely tailored to your needs. function functionname() -- function code goes here -- end function

 Let's say you want to create a function that calculates how much your Net Pay is after deductions. The function would involve these values:  GrossPay: how much you earn before deductions  Income Tax as a percentage  National Insurance as a percentage  Pension Fund as a percentage

 NetPay is how much you take home after IncomeTax, National Insurance and Pension Fund contributions have been deducted.  The maths would look like this:  Deductions =  (GrossPay * IncomeTax) + (GrossPay * NI) + (GrossPay * Pension)  NetPay = GrossPay - Deductions

Function NetPay _ (GrossPay As Double, IncomeTax _ As Double, NI As Double, Pension As_ Double) As Double Dim Deductions As Double Deductions = (GrossPay * IncomeTax) +_ (GrossPay * NI) +(GrossPay * Pension) NetPay = GrossPay - deductions End Function

 You can use functions in the same way as you use Excel functions. Your VBA functions will appear in the “User Defined Functions” category

 A function called myFV, (so it doesn’t conflict with the inbuilt Excel function FV) which works out the value of an investment after compound interest has been added, i.e. its future value. Function myFV(pr As Single, rate As Single, nper As Integer) As_ Single myFV = pr * (1 + rate) ^ nper End Function  On the other hand you might also want a function that works out just the compound interest i.e. minus the principal. Function Compound(pr As Single, rate As Single, nper As Integer) as Single Dim fv As Single fv = (pr * (1 + rate) ^ nper) compound = fv - pr End Function

LCase() returns lower case version of a string LCase(“ABCD”) UCase() returns upper case version of a string UCase(“abcd”) Len() returns number of characters in a string Len(“ABCDE”) Trim() removes leading and trailing spaces from a string Trim(“ ABC “) LTrim() removes leading spaces from a string LTrim(“ ABC”) RTrim() removes trailing spaces from a string RTrim(“ABC “) Left() returns leftmost characters of a string Left(“ABCDEF”,3) Right() returns rightmost characters of a string Right(“ABCDEF,3”) StrComp() compares two strings for equivalence and returns the integer result of comparison: 0 if strings are equal, -1 if strings are different StrComp(“ABC,”abc”) Val() returns numeric value of a string in data type appropriate to the format of the argument. Val(“123.45”)

 You can create a macro to call the FunctionWizard or Insert Function dialog – which would save a few seconds each time you run it:  ActiveCell.FunctionWizard  You would then choose which function to use …

 You can call functions with the following code: Application.WorksheetFun ction.FunctionName(arg1,arg2)

 Error Handling  Conversion Functions