Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.

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.
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.
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
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.
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.
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.
Using the Visual Basic Editor Visual Basic for Applications 1.
Lecture Excel: Multiple Worksheets. Workbook and Worksheets Multiple worksheets in a single workbook. When saved, only a single workbook (XLS) is saved.
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.
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.
Week 1.  Kate Watson  Checked at least every other day. Queries and general comments welcome.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
Spreadsheets in Finance and Forecasting Presentation 11 Visual Basic.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
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 (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
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.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
IN THE NAME OF ALLAH UserForms on VBA Lab 06 Tahani Al_dweesh.
Microsoft Excel Macros & Excel Solver (IENG490)
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Week 3.  Assessed Exercise 1  Working with ranges.
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.
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.
# 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.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 9 Macros And Visual Basic For Applications.
Creating Macros in Excel Adding Automated Functionality to Excel & Office Applications.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
Macros in Excel Using VBA Time Required – 5 hours.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
VBA Excel Macro 1.Create a Macro: To create a macro in Excel VBA, Create a Macro: To create a macro in Excel VBA,Create a Macro: To create a macro.
Excel Tutorial 8 Developing an Excel Application
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Outline In this module, the following topics will be covered:
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Excel: Macros & Pivot Tables
Created by Stacey Wilson
Spreadsheet-Based Decision Support Systems
Microsoft Excel.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Excel VBA Day 3 of 3 Tom Vorves.
Introducing VBA Macros
Introduction to VBA IE 469 Spring 2018.
Creating Macros in Excel
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Microsoft Excel 2007 – Level 2
Presentation transcript:

Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji

What is a Macro? Macros are programs used to perform operations using excel functions and business process knowledge in an automated fashion Macros can be created using Record Macro Option or by using direct Visual Basic Programming depending on your skill set and interest Macro in Excel is a recording of each command and action you perform to complete a task Whenever you need to carry out that task in your spreadsheets, you just run the macro instead Macros can be activate by a couple of keystrokes or by a worksheet button so they are easy to execute If recorded correctly, they will always carry out the same steps in the same order with no chance for operator error

Recording a Macro Lets record a simple Macro of same background color of cell Step 1: Click on CTRL+F11 to launch a worksheet Macro 1 Step 2: Go to Developer and click on Record Macro Step 3: Go to Home and Click on Color fill and select Yellow Step 4: Go to a new sheet and select a range of cells and then run the macro to see the range become yellow background colored

Creating “Hello World” through VB From an open Excel sheet press Alt+F11 to launch the VB Editor The above left panel appears Now go to Insert> Module to launch the empty Editor Type in the following – Sub showmessage() MsgBox ("Hello World") End Sub Now go to Run and click on Run Sub/User Form

Modules and Procedures Procedure is a unit of code enclosed between Sub and End Sub statement or Function and End Function statements Module is the container of Procedures Procedures can have either private or public scope Procedures with private scope are only accessible to the other procedures in the same module; Procedures with public scope are accessible to all procedures in every module in the workbook in which the procedure is declared, and also in all workbooks that contain a reference to that workbook By default, procedures has public scope HW: 1) Name some spreadsheet programs that existed before Excel 2) How was Excel able to outwit them?

Private and Public Lets use the Now() function provided by Excel to show the current time through a macro. We will have one Macro created as public and the other as private Public Sub ShowTime()     Range("C1") = Now() End Sub Private Sub ShowTime()     Range("C1") = Now() End Sub

Objects and Collections Object is a special type of variable that contains both data and codes Collection is a group of objects of the same class Each object contains its own methods or properties Most used Excel objects Workbook, Worksheet, Sheet, and Range Worksheet object represents a worksheet, while the Sheet object represents a worksheet or a chartsheet

Methods and Property Property is a built-in or user-defined characteristic of object Method is an action performed with the object Method and property for Workbook Object: Workbooks.Close - Close method close the active workbook Workbooks.Count - Count property returns the number of workbooks that are currently opened Some objects have default properties. Example, Range whose default property is Value and so it can be written as   Range("A1") = 1    or     Range("A1").Value = 1

Offset and Relative Reference Range has an Offset property that is used to move the active Cell around. Here we use Offset property to move cursor when current cell is E5 ActiveCell.Offset(1,0) = 1   - Puts "1" a row under on E6 ActiveCell.Offset(0,1) = 1   - Puts "1" a column to right on F5 ActiveCell.Offset(0,-3) = 1  - Puts "1" three columns to left on B5 Relative Reference uses instead of B9, relative reference to the Active cell. If Active cell is E5. then B9 will be R[4]C[-3]. This can be used only in the formula and not in Range

Creating Repetitive Data Step 1: Record Macro of Putting AB in an active cell with a relevant name Step 2: Now open the Macro in VB and add the following code: Selection.Copy ActiveCell.Offset(1, 0).Range("A2, A4,A6").Select ActiveSheet.Paste Application.CutCopyMode = False See the examples HW: 1) What was worksheet limitation in previous version of Excel say 2003

Functions and Subroutines Functions and Subroutines are both used within modules to perform specific action Function returns a value while a Subroutine needs to be called to get a result set Examples are Sub ShowSum()     msgbox sumNo(3,5) End Sub Function sumNo(x, y)     sumNo = x + y End Function

Creating an Expense Form Sub Expense_form() ‘' Expense_form Macro ‘ ActiveCell.FormulaR1C1 = "Date of Expense" ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select ActiveCell.Offset(0, -1).Columns("A:A").EntireColumn.EntireColumn.AutoFit ActiveCell.Offset(0, 1).Range("A1").Select ActiveCell.FormulaR1C1 = "Description" ActiveCell.Columns("A:A").EntireColumn.Select ActiveCell.Columns("A:A").EntireColumn.EntireColumn.AutoFit ActiveCell.Offset(1, 2).Range("A1").Select ActiveCell.FormulaR1C1 = "Type" ActiveCell.FormulaR1C1 = "Subtype" ActiveCell.FormulaR1C1 = "Amount" ActiveCell.Offset(10, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)" ActiveCell.Offset(0, -4).Range("A1").Select ActiveCell.FormulaR1C1 = "Total" ActiveCell.Offset(-10, 2).Range("A1").Select End Sub

Conditional Statements There are 2 types of conditional statements – if-then-else or Case Example If-then-else Case

Loop Statements Loops are of the following types – For Next Do While Loop Do Until Loop Do Loop While Do Loop Until

Creating a Pivot Table

Create a Chart using the Recording Step 1: Highlight the range of cells from A2 to D5, which includes the column titles and the row headings Step 2: Choose Insert > Column > 3-d Clustered Column. Step 3: Choose Style 3 from Design Tab Step 4: Add a Chart title "2003 - 05 Income Summary“ Step 5: Add Axes titles X-axis: “Years” Y-axis: “Summary”

Error Handling in Macro Excel macro is written in the Visual Basic for Applications (VBA) programming language Unless you know VBA, re - recording a macro that does not work right is usually the best option To keep all Macros centrally, create a personal.xls file and hide it, so it will be available to all workbooks Every time you record a Macro save it in this file Read On

Here is a Operational Report using Excel Example Here is a Operational Report using Excel

Thank You