Macros in Excel Using VBA Time Required – 5 hours.

Slides:



Advertisements
Similar presentations
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Advertisements

Using Basic FormulasUsing Basic Formulas Lesson 4 © 2014, John Wiley & Sons, Inc.Microsoft Official Academic Course, Microsoft Word Microsoft Excel.
WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
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.
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
For Deborah Cottle Brown Elementary ITS. Excel PowerPoint us/training/default.aspx (Office Online Training in Outlook)
Copyright © 2008 Pearson Prentice Hall. All rights reserved Copyright © 2008 Prentice-Hall. All rights reserved. Committed to Shaping the Next.
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
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.
Excel Tutorial 1 Getting Started with Excel
Microsoft Office Excel 2013 Core Microsoft Office Excel 2013 Core Courseware # 3253 Lesson 8: Macros, Importing and Exporting Data.
MACROS CS1100 Computer Science and its Applications CS11001.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Objectives 1.Identify the functions of a spreadsheet 2.Identify how spreadsheets can be used. 3.Explain the difference in columns and rows. 4.Locate specific.
Customizing Word Microsoft Office Word 2007 Illustrated Complete.
Microsoft Excel 2003 Illustrated Complete Excel and Advanced Worksheet Management Customizing.
Adding Automated Functionality to Office Applications.
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.
Excel Lesson 14 Creating and Using Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
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.
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.
Word Lesson 16 Working with Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
Microsoft Office Word 2013 Core Microsoft Office Word 2013 Core Courseware # 3250 Lesson 8: Using Productivity Tools.
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.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
1 CA201 Word Application Increasing Efficiency Week # 13 By Tariq Ibn Aziz Dammam Community college.
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-
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Microsoft Excel Macros & Excel Solver (IENG490)
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
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.
® Microsoft Office 2010 Excel Tutorial 1: Getting Started with Excel.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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.
Chapter 9 Macros And Visual Basic For Applications.
Intermacs Form Download Excel Tutorial Pivot Tables, Graphic Tools, Macros By: Devin Koehl.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
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.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Introduction to Microsoft Excel Macros COE 201- Computer Proficiency.
Intermacs Form Download Excel Tutorial Pivot Tables, Graphic Tools, Macros By: Devin Koehl.
Excel part 5 Working with Excel Tables, PivotTables, and PivotCharts.
XP 1 Workshop Overview Goal Participants will leave the workshop with some basic Excel skills and the ability to locate and use online resources to continue.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
An electronic document that stores various types of data.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
CONDITIONAL FORMATTING AND CUSTOM NUMBER FORMATS LEC 5 1.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
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.
Microsoft Excel 2007 Noris Bt. Ismail Faculty of Information and Communication Technology Tel : (Ext 8408) BCOMP0101.
Software Development Languages and Environments. Computer Languages Just as there are many human languages, there are many computer programming languages.
Excel Tutorial 8 Developing an Excel Application
Developing an Excel Application
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Advanced Microsoft Excel
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Microsoft Office Excel 2003
Microsoft Excel 2007 – Level 2
Presentation transcript:

Macros in Excel Using VBA Time Required – 5 hours

Outline-Lab 1  Why Excel? Why Macros  Use of Developer Tab -How to access it and use it-why  Why use Macros  How and where to type a macro  Run a macro  Save the file

 Powerful tool that you can use to manipulate, analyze, and present data  Easier way to perform repetitive tasks  Excel have Visual Basic for Applications (VBA), a programming language that gives you the ability to extend those applications-tasks  VBA works by running macros, step-by-step procedures written in Visual Basic  You can also use VBA to build new capabilities into Excel (for example, you could develop new algorithms to analyze your data, then use the charting capabilities in Excel to display the results)  Macros: A macro is a recording of each command and action you perform to complete a task. Then, whenever you need to carry out that task in a spreadsheet, you just run the macro instead.macro Why Excel-VBA

Bringing up the necessary tabs Developer Tab To enable the Developer tab…  On the File tab, choose Options to open the Excel Options dialog box.  Click Customize Ribbon on the left side of the dialog box.  Under Choose commands from on the left side of the dialog box,  select Popular Commands.  Under Customize the ribbon on the right side of the dialog box, select  Main tabs, and then select the Developer check box.  Click OK.  After Excel displays the Developer tab, note the location of the  Visual Basic, Macros, and Macro Security buttons on the tab. Note: Students do it As the teacher explains

NOTE:  Security Warning: Macros have been disabled bar appears between the ribbon and the worksheet when you open a workbook that contains a macro, you can click the Enable Content button to enable the macros.  Also, as a security measure, you cannot save a macro in the default Excel file format (.xlsx); instead, you must save the macro in a file with a special extension,.xlsm.

Visual Basic Editor This following procedure shows you how to create a new blank workbook in which to store your macros. Save the workbook in the.xlsm format. To create a new blank workbook Click the Macros button on the Developer tab. In the Macro dialog box that appears, type, Hello under Macro Name. Click the Create button to open the Visual Basic Editor with the outlines of a new macro already typed in.

The Visual Basic Editor contains the following code. Sub Hello() End Sub Sub stands for Subroutine, which you can define for now as "macro". Running the Hello macro runs any code that is between Sub Hello() and End Sub. Now edit the macro so that it looks similar to the following code. Sub Hello() MsgBox ("Hello, world!") End Sub

 Go back to the Developer tab in Excel and click the Macros button again.  Select the Hello macro in the list that appears and then click Run to display a small message box that contains the text, "Hello, world!"  You just created and implemented custom VBA code in Excel. Click OK in the message box to close it and finish running the macro.

A Real-World Example  Suppose that you have a workbook that contains lists on a large number of worksheets and that you want to change the name of each worksheet to match the heading of the list on that worksheet.  Not every worksheet has a list on it, but if it does, the heading is in cell B1, and if it does not, cell B1 is blank.  The names of worksheets without lists should be left alone.  Ordinarily, this could be a complex task that involves looking at each worksheet to see if it has a list, copying the name if it does, clicking the worksheet tab, and then pasting in the new name.  Instead of performing all of those steps manually, use Excel VBA to rename the sheets automatically.

To record a macro that renames a worksheet  Click Record Macro on the Developer tab.  Name the macro RenameWorksheets,  Rename Sheet1 to New Name, and click Stop Recording.  Go to the Developer or View tab, click the Macros button, and choose Edit to open the Visual Basic Editor. The code in the Visual Basic Editor should look similar to the following. Sub RenameWorksheets() ' ' RenameWorksheets Macro ' ' Sheets("Sheet1").Select Sheets("Sheet1").Name = "New Name" End Sub

Comments:  Any line that begins with an apostrophe is a comment and  has no effect on what the macro does.  The main uses for comments are the following:  To make the code easier to understand, not just for you,  but for anyone else who might need to modify the code later.  To temporarily disable a line of code (called commenting it out). The next line uses the Select method to select the Sheet1 member of the Sheets collection object. The last line of the recorded macro modifies the Name Property of the Sheet1 member of the Sheets collection. This is the line to keep. After you make your changes, the recorded code should now look like the following.

Sub RenameWorksheets() Sheets("Sheet1").Name = "New Name" End Sub Observe -Manually change the sheet called New Name back to Sheet1, then run the macro. The name should change back to New Name. End Of Lab 1

Assignment 1 (100 points) Students: Please write a code- Macro to : 1) Display your name, 2) Name of the school and its 3) Address of the school Run the macro and show it to the teacher. 30 points for each task and 10 points for something that you come up with on your own. Lab 2 Review What is a Macro, How to bring up a developer tab, How do you save a file (format)

Another way to Record Macros

If you have assigned a keyboard shortcut to a macro then you can run it by holding down the CTRL key and pressing the associated letter.

Review of Excel. How to use Developer tab and how to create Macros. What is another way to record a Macro Loops : Why and what are conditional statements and loops – students tell /explain-review -A conditional statement has a condition and if the condition is true only then the following statement is executed. -A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop)is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.indefinitely Looping One limitation of the code up to this point is that it only makes a change to one worksheet. You could add another line for each worksheet that you want to rename, but what if you do not know how many worksheets there are, or what their current names are? You need a way to apply some rule for each sheet in the workbook.

VBA has a construction called a For Each loop that is ideal. The For Each loop examines each item in a collection object such as Worksheets and can be used to take an action (like change a name) to some or all of those items. ***For more information about the For Each loop, see the VBA Language Reference. Click "Visual Basic Conceptual Topics", then "Using For Each...Next Statements". Also, be aware that the VBA Language Reference, like the Object Model Reference, will amply repay the time that you spend browsing it, and is an excellent place to look for ideas if you get stuck working on code.VBA Language Reference Using the third example in the "Using For Each...Next Statements" topic, edit the macro so that it looks similar to the following code.

Sub RenameWorksheets() For Each myWorksheet In Worksheets myWorksheet.Name = "New Name" Next End Sub myWorksheet is a variable; that is, what it represents varies. In this case, the myWorksheet variable successively represents each worksheet in the Worksheets collection. You do not have to use myWorksheet; you could use "x", "ws", "WorksheetToRenameAfterTheContentsOfCellB1", or (with a few restrictions) almost any name that you want. myWorksheet.Name = "New Name" To correct the line so that you can verify that the For Each loop works, change the line to the following. myWorksheet.Name = myWorksheet.Name & "-changed" Instead of trying to give each worksheet the same name, this line changes the current name of each worksheet (myWorksheet.Name) to the current name with "-changed" appended to it.

To test the macro, rename the worksheets back to Sheet1, Sheet2, and Sheet3 and delete the contents of cell B1 on one or more of the worksheets. Run the macro to verify that it renames the worksheets that have text in cell B1 and leaves the other worksheets alone. The macro works for any number of worksheets, with any combination of populated and empty B1 cells. Assignment 2: (100 points) Hand the handout too create a table of names of persons and corresponding heights. The job is to find the height of the tallest person.

Question: Check if the table is created correctly Ask them how would they calculate the average height of all students Where/which formula will they use? (Ask them to show you ) ( 30 points) LAB # 3 contd…

Expected Ans: Use the average function to take in the range of cells and divide by the total number of people  Ask them to open the Macros or the developer tab-  give handouts  Ask them what steps would they use logically …to find the tallest person For tallest person:Ask questions: Expected ans: Compare the height of the first with the second and store the value of whoever is the tallest. Then proceed to the next pair. Compare the value store with the new values. If another big value is found replace the stored value. The tallest height and corresponding name is the tallest. For added bonus (10 points they should tell the name of the person who is the tallest)

Teach the students how to write it using Macros- Discuss, explain the terms and what they mean and write the code with the help of questions and answers on the board… Sub Tall_person() Dim tallest As Integer, tall As Integer, i As Integer tallest = 0 For i = 2 To 11 'ignore the two code lines below, they are only added to illustrate the loop Cells(i, 4).Select MsgBox "i = " & i If Cells(i, 4).Value > Cells(i + 1, 4) Then tallest = Cells(i, 4) Else tallest = Cells(i + 1, 4) Next i MsgBox "The tall person is at row " & tallest End Sub

Walk about and check whether the students are doing it corrrectly Help/explain how to them how to run the code Review Questions: 1)Ask students how to open the developer tab 2)How to record a Macro 3)What is an integer 4)How do you identify a cell 5)Students continue to work on the previous lab. 6)Teacher walks around helping/explaining.

Lab # 4 Assignment # 3: Create an excel spreadsheet with the names of 10 students in a class. Each student has 3 subjects Calculate the average for each student. (30 points) According to the average.. -if the average is greater than or equal to 90 the student gets grade A -if the average is greater than or equal to 80 and less than 90 the student gets grade B -if the average is greater than or equal to 70 and less than 80 the student gets grade C -if the average is greater than or equal to 60 and less than 70 the student gets grade D -if the average is less than 60 the student gets an F (60 points) Display the names of students with grade A. (10 points)

Bibliography:  us/library/ee aspx#odc_Office14_ta_GettingStarte dWithVBAInExcel2010_MacrosAndTheVisualBasicEdit or us/library/ee aspx#odc_Office14_ta_GettingStarte dWithVBAInExcel2010_MacrosAndTheVisualBasicEdit or  ttp:// create-a-simple-macro-in-excel-2010/  macro-tutorial.htm macro-tutorial.htm 