1 cs 106 computing fundamentals ii chapter 16 “naming in excel” herbert g. mayer, psu cs status...

43
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS Status 7/6/2013 Status 7/6/2013 Initial content copied verbatim from Initial content copied verbatim from CS 106 material developed by CS 106 material developed by CS professors: Cynthia Brown & Robert Martin CS professors: Cynthia Brown & Robert Martin

Upload: beatrice-sanders

Post on 25-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

1

CS 106Computing Fundamentals II

Chapter 16“Naming In Excel”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSStatus 7/6/2013Status 7/6/2013

Initial content copied verbatim fromInitial content copied verbatim fromCS 106 material developed byCS 106 material developed by

CS professors: Cynthia Brown & Robert MartinCS professors: Cynthia Brown & Robert Martin

Page 2: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

2

Syllabus Excel Objects And NamesExcel Objects And Names

The WorkBookThe WorkBook

Common Built-In NamesCommon Built-In Names

Cell B 12 HighlightedCell B 12 Highlighted

Cell Names in FormulasCell Names in Formulas

Relative NamesRelative Names

Built-in FunctionsBuilt-in Functions

Name ManagerName Manager

ScopeScope

Page 3: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

3

Excel Objects and Names

• Every Excel object is contained in a workbookEvery Excel object is contained in a workbook

• A workbook is divided into worksheets (individual A workbook is divided into worksheets (individual spreadsheets); it can also contain charts, macros spreadsheets); it can also contain charts, macros (Excel VBA programs) and other objects(Excel VBA programs) and other objects

• Each object has to be identifiable by a nameEach object has to be identifiable by a name

• We’ll start by looking at cellsWe’ll start by looking at cells

Page 4: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

4

The WorkBook• Whether your WorkBook has 1 WorkSheet or many, all Whether your WorkBook has 1 WorkSheet or many, all

activity starts with opening the Workbookactivity starts with opening the Workbook

• This is an “event”This is an “event”

• To associate “initialization work” with a VBA project, To associate “initialization work” with a VBA project, place all initialization actions into a sub that is place all initialization actions into a sub that is activated automatically, activated automatically, when the when the WorkBook opensWorkBook opens

• That is the special, and predefined: That is the special, and predefined:

sub Workbook_Open()sub Workbook_Open()

• A similar function for A similar function for user formsuser forms::

Sub UserForm_Initialize() Sub UserForm_Initialize()

Page 5: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

5

The WorkBook

Sub Workbook_Open()Sub Workbook_Open()

Cells( 1, 1 ).Value = “Hello:”Cells( 1, 1 ).Value = “Hello:”

. . . ‘ other cells being populated. . . ‘ other cells being populated

' now have columns be width-adjusted:' now have columns be width-adjusted:

Cells.Columns.AutoFitCells.Columns.AutoFit

End SubEnd Sub

Page 6: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

6

Common Built-in Names• In Excel, columns are named with letters such as A, B, In Excel, columns are named with letters such as A, B,

etc; if you need more than 26 columns then you get etc; if you need more than 26 columns then you get names like AA, AB, and so onnames like AA, AB, and so on

• Rows are numberedRows are numbered• The default way of referring to a cell is by its column The default way of referring to a cell is by its column

and row, and row, B12B12 for example for example• A sampling of pre-defined names, some you saw A sampling of pre-defined names, some you saw

earlier:earlier:• Workbook_Open()

• UserForm_Initialize()

• Cells()

• Cells.Columns.AutoFit

• ActiveCell

Page 7: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

7

Common Built-in NamesOther common, predefined names in VBA:Other common, predefined names in VBA:•ActiveCellActiveCell cell of current cursor positioncell of current cursor position•ActiveCell.InteriorActiveCell.Interior property of cell at current cursorproperty of cell at current cursor•ActiveCell.Interior.colorActiveCell.Interior.color obvious obvious •lstBx.AddItem( “text” )lstBx.AddItem( “text” ) print text on next line of list boxprint text on next line of list box•Cells()Cells() 2d grid of all Excel cells2d grid of all Excel cells•Cells.Columns.AutoFitCells.Columns.AutoFit adjust cell width to content widthadjust cell width to content width•cInt( “text” )cInt( “text” ) convert text to integer valueconvert text to integer value•Format( double, “cmd” )Format( double, “cmd” ) print double-prec. according to cmdprint double-prec. according to cmd•Option ExplicitOption Explicit command avoids default declarationcommand avoids default declaration•ThisWorkbook.Workbook_OpenThisWorkbook.Workbook_Open main macro at startmain macro at start• UserForm_Initialize() macro, event at user form opening• vbRed color value, just 1 sample• vbCr carriage return line feed character• X.Caption a property of label ‘x’• X.Text type of data in text box ‘x’• X.Visible a property of label ‘x’, T/F•Workbook_Open()Workbook_Open() macro, event at program loadingmacro, event at program loading

Page 8: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

8

Cell B12 Highlighted

Page 9: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

9

Cell Names in Formulas

• The most common reason to use cell names is in The most common reason to use cell names is in creating creating formulasformulas

• Cell names can be Cell names can be relative relative or absoluteor absolute

• Use a Use a relative name relative name when you want copies of the when you want copies of the formula to adjust depending on what row or column formula to adjust depending on what row or column they’re inthey’re in

• Use an Use an absolute name absolute name when you want to always refer when you want to always refer to the same cell in every copy of the formula, to the same cell in every copy of the formula, regardless of future row and column addition (or regardless of future row and column addition (or deletion)deletion)

Page 10: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

10

Formula Using a Relative Name

Page 11: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

11

Writing a Relative Name

• Name Name A1A1 is relative: this is the default choice is relative: this is the default choice

• Formula Formula =A1+3 =A1+3 says to add says to add 33 to to A1A1. Since it is in cell . Since it is in cell B1B1, that is where the answer will show up, that is where the answer will show up

Page 12: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

12

After pushing the enter key…

Page 13: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

13

Copy Formula, Paste down Column

Page 14: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

14

Each copy refers to its own row

Page 15: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

15

Built-in Functions

• Excel comes with lots of useful functions built inExcel comes with lots of useful functions built in

• The next slide shows an example using the function The next slide shows an example using the function AVERAGEAVERAGE

• Note the use of Note the use of A1:A8 A1:A8 to name the to name the range of cells range of cells in in column A from position 1 to 8 inclusivecolumn A from position 1 to 8 inclusive

Page 16: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

16

A10 is the Average of A1 to A8

Page 17: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

17

Subtract the Average from A1: Note $ to create an absolute name

Page 18: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

18

Pasting down the column: relative references change, not absolutes!

Page 19: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

19

Copying a Copy…

• Next sample uses a relative formula to add three to Next sample uses a relative formula to add three to each element in A, and store the result in Beach element in A, and store the result in B

• We copy the same formula into column CWe copy the same formula into column C

• Note that it uses the values in B; the idea of the Note that it uses the values in B; the idea of the relative reference is to use the column to the left of the relative reference is to use the column to the left of the current columncurrent column

Page 20: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

20

Formula for Column B

Page 21: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

21

Same formula copied to C

Page 22: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

22

Fixing just the Column

• Suppose I want the formula to change to the current Suppose I want the formula to change to the current row, but keep using column Arow, but keep using column A

• Accomplished by just putting a $ in front of the A: Accomplished by just putting a $ in front of the A: instead of $A$1, use $A1instead of $A$1, use $A1

Page 23: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

23

Column Absolute, Row Relative

Page 24: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

24

Absolute and Relative

Show macros Show macros rel_absrel_abs in class! in class!

Page 25: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

25

Meaningful Names

• User can give a meaningful name to a cellUser can give a meaningful name to a cell

• But user-defined names are always absoluteBut user-defined names are always absolute

• We name cell A10 as We name cell A10 as colAvgcolAvg, then use that name in a , then use that name in a formulaformula

• Note Dr. Brown actually used Note Dr. Brown actually used colAavgcolAavg due to a bona- due to a bona-fide typofide typo

Page 26: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

26

Type Name, Press enter

Page 27: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

27

The formula using the name

Page 28: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

28

Name Manager Example

Page 29: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

29

Sorting out Names

• Each worksheet has a cell named Each worksheet has a cell named A1A1

• That is each worksheet of a workbook, hence there That is each worksheet of a workbook, hence there may be numerous may be numerous A1 A1 cellscells

• Within a worksheet, the name is unique and there is Within a worksheet, the name is unique and there is no problemno problem

• To refer to cell To refer to cell A1A1 on another sheet, say on another sheet, say Sheet2Sheet2, use , use the name of the sheet as in the name of the sheet as in Sheet2!A1Sheet2!A1

Page 30: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

30

An Analogy

• You can think of cell names on a worksheet as being You can think of cell names on a worksheet as being like names in a familylike names in a family

• In a regular family each person usually has a unique In a regular family each person usually has a unique name that everyone usesname that everyone uses

• But if we consider a class in school, there are likely to But if we consider a class in school, there are likely to be several people with the same given name. In that be several people with the same given name. In that case the teacher uses family names as wellcase the teacher uses family names as well

• The sheet name is like the family name: it makes it The sheet name is like the family name: it makes it clear exactly which A1 we mean, as in clear exactly which A1 we mean, as in Sheet1!A1Sheet1!A1. But . But within a sheet, like within a family, there is no need within a sheet, like within a family, there is no need for the family namefor the family name

Page 31: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

31

Scope• Scope Scope is the range of text, over which a name is is the range of text, over which a name is

uniquely known and validuniquely known and valid

• The scope of a The scope of a cell cell ID ID is just the worksheetis just the worksheet

• The scope of a The scope of a namename we give to an object is we give to an object is the whole the whole workbookworkbook; you do not need to use the sheet name ; you do not need to use the sheet name when referring to it on another sheet, and the name when referring to it on another sheet, and the name can only be defined one time in a workbookcan only be defined one time in a workbook

Page 32: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

32

Naming a Range

• Highlight the cells you want to nameHighlight the cells you want to name

• Go to the Formulas tab and find the Define Names Go to the Formulas tab and find the Define Names panel; click on Define Namepanel; click on Define Name

• Type the name you want to use in the window that Type the name you want to use in the window that comes upcomes up

Page 33: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

33

Click on Define Name

Page 34: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

34

Put the name here and click OK

Page 35: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

35

Use it in a formula

Page 36: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

36

New View of Name Manager

Notice how the defined names are absolute references

Page 37: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

37

Finding Definitions on the Mac

• In the Windows version (Excel 2010) there is a Name In the Windows version (Excel 2010) there is a Name ManagerManager

• Finding definitions in the Mac version (Excel 2011) is Finding definitions in the Mac version (Excel 2011) is a little trickiera little trickier

• We’ll start by defining a couple of named ranges, and We’ll start by defining a couple of named ranges, and then show how to find the namesthen show how to find the names

Page 38: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

38

I just named cell A1 “root”

Cell A1 is selected, and I typed the name “root” in the name box at the upper left, and hit return

Page 39: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

39

Create a list of powers of root…

The formula in cell B1 is “=root”The formula in B2 is “=B1*root”This formula is copied down the column to create the list of powers of root

Page 40: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

40

Create a named range “powers”

I selected the range and typed the name “powers” in the name box

Page 41: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

41

To find the list of definitions…

Using the Insert menu, follow the entries Insert -> Name Using the Insert menu, follow the entries Insert -> Name -> Define-> Define

(see the next slide)(see the next slide)

Page 42: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

42

The resulting definition list…

I selected “powers” in the list box and it isshowing me the definition of powers: (notethe use of absolute addresses)=Sheet1!$B$1:$B$11

Page 43: 1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material

43

Why Use Defined Names?

• It can make your spreadsheets much more readable It can make your spreadsheets much more readable and less error-proneand less error-prone

• This is especially true if you use lots of formulasThis is especially true if you use lots of formulas