Download presentation
Presentation is loading. Please wait.
Published byLene Kjærgaard Modified over 6 years ago
1
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji
2
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
3
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
4
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
5
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?
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
Conditional Statements
There are 2 types of conditional statements – if-then-else or Case Example If-then-else Case
14
Loop Statements Loops are of the following types – For Next
Do While Loop Do Until Loop Do Loop While Do Loop Until
15
Creating a Pivot Table
16
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 " Income Summary“ Step 5: Add Axes titles X-axis: “Years” Y-axis: “Summary”
17
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
18
Here is a Operational Report using Excel
Example Here is a Operational Report using Excel
19
Thank You
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.