Saeed Ghanbartehrani Summer 2015 Lecture Notes #3: Manipulating Excel Objects IE 212: Computational Methods for Industrial Engineering.

Slides:



Advertisements
Similar presentations
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Excel Project 4 Financial Functions, Data Tables, Amortization Schedules, and Hyperlinks.
Advertisements

Microsoft Office XP Microsoft Excel
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.
Using Basic FormulasUsing Basic Formulas Lesson 4 © 2014, John Wiley & Sons, Inc.Microsoft Official Academic Course, Microsoft Word Microsoft Excel.
Chapter 13: More on Objects Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Exploring Microsoft Excel 2002 Chapter 1 Chapter 1 Introduction to Microsoft Excel: What is a Spreadsheet? By Robert T. Grauer Maryann Barber Exploring.
Exploring Office Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 1 – Introduction to Excel: What is a Spreadsheet?
Tutorial 3 Calculating Data 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.
1 Committed to Shaping the Next Generation of IT Experts. Chapter 4: Spreadsheets in Decision Making: What If? Robert Grauer and Maryann Barber Exploring.
1 CS110: Lecture 2 Spreadsheets Prepared by Fred Annexstein University of Cincinnati CC Some rights reserved Today’s Topics Basics of Excel Spreadsheets.
Adding Automated Functionality to Office Applications.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #2: The Visual Basic Editor The Visual Basic for Applications Programming Language IE 212: Computational.
Exploring Excel 2003 Revised - Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 1 – Introduction to Excel: What is a.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
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.
Key Applications Module Lesson 16 — Excel Essentials Computer Literacy BASICS.
Microsoft Excel By: Dr. K.V. Vishwanath Professor, Dept. of C.S.E,
Chapter 11: Introduction to the Visual Basic Environment Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Week 3.  Assessed Exercise 1  Working with ranges.
CHAPTER 13 Creating a Workbook Part 2. Learning Objectives Work with cells and ranges Work with formulas and functions Preview and print a workbook 2.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Excel Project 1 Creating a Worksheet and an Embedded Chart.
1 Performing Spreadsheet What-If Analysis Applications of Spreadsheets.
Exploring Office 2003 Vol 1 2/e - Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 1 – Introduction to Excel: What is.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
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.
The Object Model Visual Basic for Applications 2.
Chapter 6.  If a cell style will be used over and over again it can be modified in the cell styles gallery  Home ⇒ Cell Styles ⇒ right-click a style.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
1 Microsoft Excel An Introduction to Spreadsheets Lecture 18.
IC 3 BASICS, Internet and Computing Core Certification Key Applications Lesson 11 Organizing the Worksheet.
Chapter 1 Creating a Worksheet and a Chart
© 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 15 Advanced Tables.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 13: More on Objects Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
PERFORMING CALCULATIONS Microsoft Excel. Excel Formulas A formula is a set of mathematical instructions that can be used in Excel to perform calculations.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Microsoft Office 2013 ®® Calculating Data with Formulas and Functions.
Chapter 3: Referencing and Names Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 4 Decision Making. Agenda Function Goal Seek command AutoFilter command Worksheet operation Printing worksheet.
Microsoft ® Excel ® 2013 Enhanced Excel Tutorial 3 Calculating Data with Formulas and Functions.
Excel Introduction to computers. Excel 2007 Starting the Excel program.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Microsoft Excel 2007 Noris Bt. Ismail Faculty of Information and Communication Technology Tel : (Ext 8408) BCOMP0101.
Resource Review Excel formula basics Demonstrate how to enter manual formulas Examine some of the available functions and their usage Discuss the.
COMPUTER LITERACY NOTES MS-EXCEL. SPREADSHEETS A spreadsheet is a computer equivalent of a paper ledger sheet. Excel allows you to create spreadsheets.
Excel Chapter 1 Creating a Worksheet and an Embedded Chart
Exploring Office Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 1 – Introduction to Excel: What is a Spreadsheet?
Microsoft Excel.
Outline In this module, the following topics will be covered:
Microsoft Excel.
Spreadsheet-Based Decision Support Systems
Data Validation and Protecting Workbook
Shelly Cashman: Microsoft Excel 2016
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Creating a Workbook Part 2
Microsoft Excel All editions of Microsoft office.
Exploring Microsoft Excel
Intro to Excel CSCI-150.
Activate a range and manipulating activated range
Key Applications Module Lesson 16 — Excel Essentials
Lab 08 Introduction to Spreadsheets MS Excel
Exploring Microsoft Excel 2003
Presentation transcript:

Saeed Ghanbartehrani Summer 2015 Lecture Notes #3: Manipulating Excel Objects IE 212: Computational Methods for Industrial Engineering

2 Objects, Properties, and Methods  In this module, various properties and methods for commonly manipulated objects will be demonstrated –Ranges –Workbooks –Worksheets  The following aspects of programming in VBA will also be discussed –The With…End construct –Cell naming and referencing –Excel formulas and functions

3 Objects, Properties, and Methods (cont.)  VBA code can be used to manipulate Excel objects to more advanced degree than using just Excel functionality  Knowing how to find object properties and methods with the Object Browser and VBE tools is fundamental

4 The Range Object  The Range object is arguably the most used object in Excel VBA –A Range object can be a single cell, a rectangular block of cells, or the union of many rectangular blocks (i.e., a non-continuous range)  The Range object has several properties and methods, e.g., –Properties  Color  Border  Values  Font –Methods  Clearing  Copy  PasteSpecial

5 Range – Color Format  The Interior property is used to change the color of the cell(s) within a range  There are a few sub properties that can be used –ColorIndex –Color –Pattern

6 Range – Color Format (cont.)  ColorIndex property –Value = numerical color index

7 Range – Color Format (cont.)  Color property –Value = VB Constant or RGB Function

8 Range – Color Format (cont.)  Color property –Value = VB Constant or RGB Function

9 Range – Color Format (cont.)  Pattern property –Value = XL Constant

10 Range – Color Format (cont.)  In-Class Exercise –Open a new workbook in Excel –Name this file “Objects.xlsm” –Change the range “A1:F12” in Sheet1 so that the interior color is BLUE, as shown in the figure below  Use all the properties just explained –Also experiment with the property Pattern  Apply xlPatternGray50 and xlPatternSolid to the same cell range

11 Range – Border Format  There is one main property and one main method to format the border of a range  The Borders property has several sub properties –LineStyle  Value = xlDashed, xlSolid, … –Weight  Value = xlThick, xlThin, … –Color  Value = VB Constant, RGB Function –XL Constants  xlInsideHorizontal, xlEdgeBottom, …

12 Range – Border Format (cont.)  Values for sub properties LineStyle and Weight

13 Range – Border Format (cont.)  The BorderAround method has several possible arguments –LineSytle:= xlDash, xlSolid, … –Weight:= xlThick, xlThin, … –Color:= VB Constant, RGB Function  Notice that a combination of a colon and an equal sign are used to define the argument of the method (:=) –This is different than the values of properties that are set using just an equal sign (= ) Range("A1:F12").BorderAround LineStyle:=xlContinuous, Weight:=xlThick, Color:=vbBlack

14 Range – Border Format (cont.)  In-Class Exercise –Use the Excel file “Objects.xlsm” created previously –Change the borders of the range “A1:F12” in Sheet2 so that it looks like the figure shown below  Use both the Borders property and the BorderAround method

15 Range – Values  The Value property is used in VBA to assign values to ranges  The value of a cell or a group of cells can be –Text –Numerical –Formula –Reference –Variable

16 Range – Values (cont.)  In-Class Exercise –Write VBA code to enter several different values into Sheet3, as shown below  Add Sheet3 to your workbook, if necessary

17 Range – Font Format  The Font property is used in Excel VBA to format fonts of ranges  There are several sub properties to use with the Font property –Bold  Value = True or False –Size  Value = number –Color  Value = VB Constant, RGB Function –ColorIndex  Value = number –FontStyle  Value = “style”

18 Range – Font Format (cont.)  In-Class Exercise –Write VBA code to format the font of the values you entered in Sheet3 so that the look as shown in the figure below

19 Range – Clearing Contents  There are three methods commonly used to clear a range of cells –Clear  Clears everything –ClearContents  Only clears values or formulas –ClearFormats  Only clears formats  It is important to know which method is most appropriate for your worksheet

20 Range – Clearing Contents (cont.)  In Class Exercise –Write VBA code to produce the effects shown below in Sheet3

21 The Application Object  The Application object is useful when executing VBA code that copies and pastes large amounts of data  This object has two main properties –ScreenUpdating  Value = True or False –CutCopyMode  Value = True or False  There is also one main method –Wait  Arguments = Now, TimeValue

22 The Application Object (cont.)  The ScreenUpdating property –Helps the VBA code run more efficiently since the Excel screen does not need to be updated after every action in the code  Eliminates the flickering of the screen  The CutCopyMode property –Prevents a flashing box from remaining around the range which has been copied after a macro has been executed

23 The Application Object (cont.)  In Class Exercise –Insert a new worksheet into workbook “Objects.xlsm”  This will be Sheet4  Enter the values shown below  Write VBA code to understand the effect of the properties CutCopyMode and ScreenUpdating

24 The Wait Method  The Wait method is used frequently when Excel is used to perform simulations  Wait pauses the macro while it is being run until a specified time is reached –The Now argument  Calculates the current time –The TimeValue argument  Gives an integer-valued time amount to add to the current time  The macro will play again once Now plus TimeValue time is reached

25 The Wait Method (cont.)  In Class Exercise –Use the Wait method to obtain the results shown below in Sheet4

26 The With Construct  The With construct is used to set several properties of one object in an enclosed statement  For example, compare these two sets of code  Use Sheet4 to practice the application of the With construct Range(“A1:C8”).Interior.Color = vbRed Range(“A1:C8”).Font.Bold = True Range(“A1:C8”).Font.Name = “Arial” Range(“A1:C8”).Borders(xlEdgeBottom).LineStyle = xlDash With Range(“A1:C8”).Interior.Color = vbRed.Font.Bold = True.Font.Name = “Arial”.Borders(xlEdgeBottom). LineStyle = xlDash End With

27 Referencing Ranges in VBA  There are several ways to reference ranges and cells using VBA –Offset –Cells –Rows –Columns –EntireRow –EntireColumn –End

28 Offset vs. Cells  The Offset property –Considers the upper left-most cell in the range (could be a named range) to be in the 0 th row and 0 th column –It offsets the range selection downward by a certain row count (if positive) or upward (if negative) of the range –It offsets the range selection to the right by a certain column count (if positive) or to the left (if negative) of the range

29 Offset vs. Cells (cont.)  The Cells property –Considers the upper left-most cell in the range (could be a named range) to be in the 1 st row and 1 st column –It then finds the cell in the x th row position below (if the position change is positive) or above (if the position change is negative) and the y th column position to the right (if the position change is positive) or left (if the position change is negative) of the range

30 Offset vs. Cells (cont.)  In Class Exercise –Use the Offset and Cells properties to achieve the results shown below in Sheet5

31 Columns and Rows  Columns and Rows, reference columns and rows in a named range, respectively –Both properties take a numerical index value to find the numbered column within the named range –Both properties consider the first column or row in the range to be indexed as 1

32 EntireColumn and EntireRow  EntireColumn and EntireRow, are used to modify every column or row in the range for the length of the column or row of the entire worksheet  The EntireColumn property will affect every column in the range and the EntireRow property will affect every row in the range for their entire respective length

33 End  End is a very useful property as it can help you find the end of a row or a column of any range of data  The End property can take four values –For columns  xlDown and xlUp –For rows  xlToRight and xlToLeft  You do not need to name an entire data range to use this property, just one cell in the data range is fine

34 Naming Ranges  The most common way to assign object names is by using the Name property  When you name a cell in Excel using Formulas > Define Name > Define Name…, the name appears in the name window whenever the corresponding range is selected –The same occurs after naming a range in VBA

35 Naming Ranges (cont.)  In Class Exercise –Practice using the Name property using the data entered in Sheet5

36 Formulas with the Range Object  Two main properties can be used with the Range object –Formula  Value = reference by column letter and row number –FormulaR1C1  Value = reference by R1C1 Notation  A cell or an entire range of cells can be used with these properties  There is also one method we can use with the Range object concerning formulas –AutoFill  Arguments = Destination, Type

37 Formulas with the Range Object (cont.)  In Class Exercise –We will calculate sums and averages using the data entered in Sheet5 –We also use the AutoFill method to copy and paste formulas

38 Formulas with the Application Object  The Application object uses the WorksheetFunction property to set a function for a cell or range of cells  The WorksheetFunction property has several sub properties for almost all of the Excel functions –Max –Min –Average –Sum –Count –VLookup

39 Formulas with the Range Object (cont.)  In Class Exercise –We will use the WorksheetFunction property to perform calculations using the data entered in Sheet5

40 Conditional Formatting  Also associated with formatting the Range object is the FormatConditions object, which places conditional formatting on a specified range of cells  There are three main methods and several properties for this object –Add –Modify –Delete

41 Conditional Formatting (cont.)  In Class Exercise –Add another sheet to the file “Objects.xlsm” (Sheet6) and enter the values shown below in the table labeled “Original data” –Write VBA code to place a conditional format on the range of cells so that any cell with a value less than 10 has a yellow fill and bold font Original data Result

42 Workbooks and Worksheets  Workbooks and Worksheets objects will not be manipulated as often as Range objects  However, there is one important method that both Workbooks and Worksheets use –Activate  Argument = (none)  There is one important property that Worksheets often use –Visible  Value = True or False

43 Workbooks and Worksheets (cont.)  Activate method –We want to take some values from workbook Workbook1 and transfer them for analysis into workbook Workbook2 Workbook1 Workbook2

44 Workbooks and Worksheets (cont.)  Activate method –We need to write VBA code in a module in workbook Workbook2  Open both workbooks in Excel  Create a module in Workbook2 –The code added to the module needs to do the following  Activate workbook Workbook1  Use the Copy method of the Range object to copy the data  Activate workbook Workbook2  Paste the data

45 Workbooks and Worksheets (cont.)  Visible property –We will use this property of the Worksheets objects to hide and show worksheets as we navigate the user through our Excel-based applications –The code to accomplish this effect should be written in a sub procedure associated with the Open event of the Workbook object

46 Workbooks and Worksheets (cont.)  In-Class Exercise –Use the Excel file “Objects.xlsm” created previously –Go to the VBE editor  Inside the Project Explorer window, locate the object ThisWorkbook  Double click on the object –Write code in the Open event so that Sheet2 and Sheet3 are hidden the next time the workbook is opened –Save and close your workbook –Re-open the workbook and see the resulting effect

47 Summary  Several methods and properties were covered for the following objects –Workbooks and Worksheets –Ranges –Application  There are several properties of the Range object to reference ranges and cells  The With construct can help reduce code when modifying several properties of one object  Formulas can be created in VBA by using properties of the Range object and the Application object