1 cs 106 computing fundamentals ii chapter 16 “naming in excel” herbert g. mayer, psu cs status...
TRANSCRIPT
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
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
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
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()
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
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
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
8
Cell B12 Highlighted
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)
10
Formula Using a Relative Name
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
12
After pushing the enter key…
13
Copy Formula, Paste down Column
14
Each copy refers to its own row
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
16
A10 is the Average of A1 to A8
17
Subtract the Average from A1: Note $ to create an absolute name
18
Pasting down the column: relative references change, not absolutes!
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
20
Formula for Column B
21
Same formula copied to C
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
23
Column Absolute, Row Relative
24
Absolute and Relative
Show macros Show macros rel_absrel_abs in class! in class!
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
26
Type Name, Press enter
27
The formula using the name
28
Name Manager Example
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
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
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
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
33
Click on Define Name
34
Put the name here and click OK
35
Use it in a formula
36
New View of Name Manager
Notice how the defined names are absolute references
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
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
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
40
Create a named range “powers”
I selected the range and typed the name “powers” in the name box
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)
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
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