Cell references can be expressions
You've seen code like this:
weight = Cells(3, 2)
Excel takes the value in row 3, column 2, and puts into the variable weight
.
3 and 2 are constants. But in fact, you can use numeric expressions for row and column. For example:
column = 2
row = column + 1
weight = Cells(row, column)
row
, and column
are indexes. Indexes are values that let you look up other values. An index isn't the value you want, but it lets you find the value you want.
When you have a large data set, it's impractical to have a variable for each value. But you can access any value you want, by using indexes.
Example: Daily sales
Let's say we have a spreadsheet of sales data, with one cell each day of the year. Like this:
There's a row for each month. Row 10 is sales for January, row 11 is for February, etc.
There's a column for each day. Column 1 is for the first day of the month, column 2 for the second day, etc.
Sales for March 9 is in row 12, column 9. Sales for July 12 is in row 16, column 9.
Suppose we want a program that will lookup sales for a given month and day. For example:
Here's the code:
This code…
sales = Cells(month + 9, day)
… doesn't compute sales, not really. It computes which cell has the sales.
That's the essence of indexes. They let you find the data you want in a large data set, by using expressions that give the data's location.
Sales change
Let's add something. As well as showing sales for the day, the program will show how much the sales changed from the previous day. That is, the day's sales minus the previous day's sales.
Here's the code:
day
is the index of the sales day we want, so day - 1
is the index of the previous day. Doing computations on an index lets us grab related data.
Example: Animal data
More lookup. Here's some data about animals. Number of legs, niceness (5 to 1), and whether we eat it.
The data is for dogs, cats, chickens, and cows. The dog data starts at row 10, the cat data at row 13, etc.
Let's write a program that looks up data about an animal. Users type the animal's name, click a button, and the data is shown:
Some notes:
- Input validation. If the program doesn't know the animal, it will tell the user that.
- Input flexibility. The user can type "DOG", "doG", "Dog", " dog ", and the program will work.
- Output formatting. The value for "Eat it?" will start with an uppercase character. For example, "Yes", not "yes".
Here's the main program:
getAnimalType
returns a flag, showing whether the user gave good data or not.
Here's the input routine:
Other programs have initialized the dataOK
flag to True
, and then flipped it if the code detected an error. This code inits the flag to False
, and then flips it if the data is good. Do whatever is more convenient. In this program, only a few data values are OK, so this approach makes sense. But, whatevs, do what makes the most sense to you.
Here's the processing code:
The data format repeats. It's always legs, niceness, then eating. The code uses the animal type to figure out which row the data starts in, and computes the individual cell positions from there. Again, it's using a variable to figure out where the data it needs is stored. Then it grabs the data from that location.
This is what indexes are all about. They tell you where data is.
Finally, the output.
Look at eatIt
, in line 11. Strip off the first character [ Left(eatIt, 1)
]. Make it uppercase [ UCase(Left(eatIt, 1))
]. Append to it the data starting at the second character [ Mid(eatIt, 2)
].
This program relies on the data for each animal having a regular structure. For every animal, it's the same three data elements, in the same order. Legs, niceness, eating, legs, niceness, eating, legs, niceness, eating, etc. Repeat for as many animals as there are.
Most business data is structured. For example, each sale has a date, a customer number, a total, etc. Each customer has a first name, last name, address, etc.
Here's more code that uses indexes.
Example: Tic tac toe
Here's a program that lets two people play tic tac toe. They type in row, column, and symbol, and the program updates the board.
Code:
Now let's add code that detects whether the person who just played has won. There are three ways you can win. First, you can complete one of the three rows:
Let's say the player has just placed an X on a cell. How would you check whether the player had won by completing a row?
- If the player put an X on row 1, you can check (row 1, cell 1), (row 1, cell 2), and (row 1, cell 3). If they are all Xs, winner!
- If the player put an X on row 2, you can check (row 2, cell 1), (row 2, cell 2), and (row 2, cell 3). If they are all Xs, winner!
- If the player put an X on row 3, you can check (row 3, cell 1), (row 3, cell 2), and (row 3, cell 3). If they are all Xs, winner!
Let's generalize:
- If the player put an X on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Xs, winner!
No matter what row the player used, check all the cells in that row for an X.
What about the other player, using Os? Let's add a rule:
- If the player put an X on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Xs, winner!
- If the player put an O on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Os, winner!
Let's generalize:
- If the player put Symbol on row Row, you can check (row Row, cell 1), (row Row, cell 2), and (row Row, cell 3). If they are all Symbols, winner!
Take the row and the symbol the player used. Check all the cells in that row for that symbol.
"Check for a winner" sounds like a nice chunk of code, so let's rewrite to this:
The If
statement implements the rule: Check all the cells in the row where the player just placed a symbol. If all of the cells contain the symbol, winner!
winner
is a flag. You saw flags earlier, with input validation. But the flag pattern has many uses.
Exercises
Here's a sample:
The user enters the time of the transport, kilos, and whether the destination is Earth or Mars. The program computes the price. The PPK (price per kilo) table is shown.
You can assume that users don't make any typing mistakes.
Hint: Time is entered as hour, then a space, then minutes. Use the InStr
function to find the space. The characters to the left of that give you the hour.
Upload your spreadsheet.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Upload your Excel file.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Summary
Indexes are values that let you look up other values. An index isn't the value you want, but it lets you find the value you want.
When you have a large data set, it's impractical to have a variable for each value. But you can access any value you want, by using indexes.
Most business data has a regular structure. For example, each sale has a date, a customer number, a total, etc. Each customer has a first name, last name, address, etc.