Download presentation
Presentation is loading. Please wait.
Published byBeatrice Sanders Modified over 9 years ago
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 developed by CS professors: Cynthia Brown & Robert Martin
2
2 Syllabus Excel Objects And Names Excel Objects And Names The WorkBook The WorkBook Common Built-In Names Common Built-In Names Cell B 12 Highlighted Cell B 12 Highlighted Cell Names in Formulas Cell Names in Formulas Relative Names Relative Names Built-in Functions Built-in Functions Name Manager Name Manager Scope Scope
3
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 spreadsheets); it can also contain charts, macros (Excel VBA programs) and other objectsA workbook is divided into worksheets (individual spreadsheets); it can also contain charts, macros (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
4 The WorkBook Whether your WorkBook has 1 WorkSheet or many, all activity starts with opening the WorkbookWhether your WorkBook has 1 WorkSheet or many, all activity starts with opening the Workbook This is an “event”This is an “event” To associate “initialization work” with a VBA project, place all initialization actions into a sub that is activated automatically, when the WorkBook opensTo associate “initialization work” with a VBA project, place all initialization actions into a sub that is activated automatically, when the WorkBook opens That is the special, and predefined:That is the special, and predefined: sub Workbook_Open() A similar function for user forms:A similar function for user forms: Sub UserForm_Initialize()
5
5 The WorkBook Sub Workbook_Open() Cells( 1, 1 ).Value = “Hello:”... ‘ other cells being populated ' now have columns be width-adjusted: Cells.Columns.AutoFit Cells.Columns.AutoFit End Sub
6
6 Common Built-in Names In Excel, columns are named with letters such as A, B, etc; if you need more than 26 columns then you get names like AA, AB, and so onIn Excel, columns are named with letters such as A, B, etc; if you need more than 26 columns then you get names like AA, AB, and so on Rows are numberedRows are numbered The default way of referring to a cell is by its column and row, B12 for exampleThe default way of referring to a cell is by its column and row, B12 for example A sampling of pre-defined names, some you saw earlier:A sampling of pre-defined names, some you saw earlier: Workbook_Open() UserForm_Initialize() Cells() Cells.Columns.AutoFit ActiveCell
7
7 Common Built-in Names Other common, predefined names in VBA: ActiveCellcell of current cursor positionActiveCellcell of current cursor position ActiveCell.Interiorproperty of cell at current cursorActiveCell.Interiorproperty of cell at current cursor ActiveCell.Interior.colorobviousActiveCell.Interior.colorobvious lstBx.AddItem( “text” )print text on next line of list boxlstBx.AddItem( “text” )print text on next line of list box Cells()2d grid of all Excel cellsCells()2d grid of all Excel cells Cells.Columns.AutoFitadjust cell width to content widthCells.Columns.AutoFitadjust cell width to content width cInt( “text” )convert text to integer valuecInt( “text” )convert text to integer value Format( double, “cmd” )print double-prec. according to cmdFormat( double, “cmd” )print double-prec. according to cmd Option Explicitcommand avoids default declarationOption Explicitcommand avoids default declaration ThisWorkbook.Workbook_Openmain macro at startThisWorkbook.Workbook_Openmain macro at start UserForm_Initialize()macro, event at user form opening vbRedcolor value, just 1 sample vbCrcarriage return line feed character X.Captiona property of label ‘x’ X.Texttype of data in text box ‘x’ X.Visiblea property of label ‘x’, T/F Workbook_Open()macro, event at program loadingWorkbook_Open()macro, event at program loading
8
8 Cell B12 Highlighted
9
9 Cell Names in Formulas The most common reason to use cell names is in creating formulasThe most common reason to use cell names is in creating formulas Cell names can be relative or absoluteCell names can be relative or absolute Use a relative name when you want copies of the formula to adjust depending on what row or column they’re inUse a relative name when you want copies of the formula to adjust depending on what row or column they’re in Use an absolute name when you want to always refer to the same cell in every copy of the formula, regardless of future row and column addition (or deletion)Use an absolute name when you want to always refer to the same cell in every copy of the formula, regardless of future row and column addition (or deletion)
10
10 Formula Using a Relative Name
11
11 Writing a Relative Name Name A1 is relative: this is the default choiceName A1 is relative: this is the default choice Formula =A1+3 says to add 3 to A1. Since it is in cell B1, that is where the answer will show upFormula =A1+3 says to add 3 to A1. Since it is in cell B1, that is where the answer will show up
12
12 After pushing the enter key…
13
13 Copy Formula, Paste down Column
14
14 Each copy refers to its own row
15
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 AVERAGEThe next slide shows an example using the function AVERAGE Note the use of A1:A8 to name the range of cells in column A from position 1 to 8 inclusiveNote the use of A1:A8 to name the range of cells in column A from position 1 to 8 inclusive
16
16 A10 is the Average of A1 to A8
17
17 Subtract the Average from A1: Note $ to create an absolute name
18
18 Pasting down the column: relative references change, not absolutes!
19
19 Copying a Copy… Next sample uses a relative formula to add three to each element in A, and store the result in BNext sample uses a relative formula to add three to each 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 relative reference is to use the column to the left of the current columnNote that it uses the values in B; the idea of the relative reference is to use the column to the left of the current column
20
20 Formula for Column B
21
21 Same formula copied to C
22
22 Fixing just the Column Suppose I want the formula to change to the current row, but keep using column ASuppose I want the formula to change to the current row, but keep using column A Accomplished by just putting a $ in front of the A: instead of $A$1, use $A1Accomplished by just putting a $ in front of the A: instead of $A$1, use $A1
23
23 Column Absolute, Row Relative
24
24 Absolute and Relative Show macros rel_abs in class!
25
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 colAvg, then use that name in a formulaWe name cell A10 as colAvg, then use that name in a formula Note Dr. Brown actually used colAavg due to a bona- fide typoNote Dr. Brown actually used colAavg due to a bona- fide typo
26
26 Type Name, Press enter
27
27 The formula using the name
28
28 Name Manager Example
29
29 Sorting out Names Each worksheet has a cell named A1Each worksheet has a cell named A1 That is each worksheet of a workbook, hence there may be numerous A1 cellsThat is each worksheet of a workbook, hence there may be numerous A1 cells Within a worksheet, the name is unique and there is no problemWithin a worksheet, the name is unique and there is no problem To refer to cell A1 on another sheet, say Sheet2, use the name of the sheet as in Sheet2!A1To refer to cell A1 on another sheet, say Sheet2, use the name of the sheet as in Sheet2!A1
30
30 An Analogy You can think of cell names on a worksheet as being like names in a familyYou can think of cell names on a worksheet as being like names in a family In a regular family each person usually has a unique name that everyone usesIn a regular family each person usually has a unique name that everyone uses But if we consider a class in school, there are likely to be several people with the same given name. In that case the teacher uses family names as wellBut if we consider a class in school, there are likely to be several people with the same given name. In that case the teacher uses family names as well The sheet name is like the family name: it makes it clear exactly which A1 we mean, as in Sheet1!A1. But within a sheet, like within a family, there is no need for the family nameThe sheet name is like the family name: it makes it clear exactly which A1 we mean, as in Sheet1!A1. But within a sheet, like within a family, there is no need for the family name
31
31 Scope Scope is the range of text, over which a name is uniquely known and validScope is the range of text, over which a name is uniquely known and valid The scope of a cell ID is just the worksheetThe scope of a cell ID is just the worksheet The scope of a name we give to an object is the whole workbook; you do not need to use the sheet name when referring to it on another sheet, and the name can only be defined one time in a workbookThe scope of a name we give to an object is the whole workbook; you do not need to use the sheet name when referring to it on another sheet, and the name can only be defined one time in a workbook
32
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 panel; click on Define NameGo to the Formulas tab and find the Define Names panel; click on Define Name Type the name you want to use in the window that comes upType the name you want to use in the window that comes up
33
33 Click on Define Name
34
34 Put the name here and click OK
35
35 Use it in a formula
36
36 New View of Name Manager Notice how the defined names are absolute references
37
37 Finding Definitions on the Mac In the Windows version (Excel 2010) there is a Name ManagerIn the Windows version (Excel 2010) there is a Name Manager Finding definitions in the Mac version (Excel 2011) is a little trickierFinding definitions in the Mac version (Excel 2011) is a little trickier We’ll start by defining a couple of named ranges, and then show how to find the namesWe’ll start by defining a couple of named ranges, and then show how to find the names
38
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
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
40 Create a named range “powers” I selected the range and typed the name “powers” in the name box
41
41 To find the list of definitions… Using the Insert menu, follow the entries Insert -> Name -> Define (see the next slide)
42
42 The resulting definition list… I selected “powers” in the list box and it is showing me the definition of powers: (note the use of absolute addresses) =Sheet1!$B$1:$B$11
43
43 Why Use Defined Names? It can make your spreadsheets much more readable and less error-proneIt can make your spreadsheets much more readable and less error-prone This is especially true if you use lots of formulasThis is especially true if you use lots of formulas
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.