The goal
Let's write code that extracts sales above 250. The worksheet starts out as:
When the button is clicked, the program extracts the values above 250 to another part of the worksheet. It also shows the size of the new data set:
How does the program work? Basically, it runs through the original data. When it finds a value that's OK, it copies that value to the new data set.
Finding the end
The amount of sales data could change from day to day. Let's start with the usual code for finding the end of a data set:
This uses look-ahead. It keeps looping until it finds an empty cell. If you need a reminder, check out Finding the end.
The index variable has a different name from before. The reason will become clear in a moment.
Recall that an index variable refers to a location where data is stored. In Cells(row, 1)
, row
is an index variable. It says which row to use.
Testing for high sales
Now, let's add code to test whether each value should be copied to the new location.
The data is the third column is an extract of the data in the first column.
Cells(??, 3) = Cells(??, 1)
Take something from column 1 (right of the =), and put it in column 3 (left of the =).
It's
If Cells(rowSource, 1) > 250 Then
Cells(rowSource, 1)
. That's where the data is coming from.
What cell will the first piece of copied data go into?
Cells(??, 3) = Cells(rowSource, 1)
Cell(1, 3)
, right?
The new index variable will be for the destination of the copied value.
There are now two index variables in the code. The first one, rowSource
, runs over the original data set, the data source. rowSource
goes up by one every time through the Do
loop (line 13).
The second index variable, rowDestination
, tracks where new data is copied to. The destination. It goes up by 1, as well (line 10). But it only goes up when sales are more than 250.
Showing the count
One more thing we need to do: show the number of data elements copied:
That's the program! So, to extract data to the worksheet:
- Pick where the extracted data should go. That's the destination.
- Create an index variable for the destination.
- Each time there's a value to copy, copy it to the destination, and increment (add one to) the new index variable.
Exercises
Start:
Sample output:
Start with this workbook, and add your own code.
Upload your workbook. The usual coding standards apply, as always.
In the text box on the submission form, please answer this question: What did you learn from this exercise?
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
The worksheet starts like this:
When the user clicks Run, your program extracts data. Data from days where the price increased by 2 or more is copied into the "Large increase" section. Data from days where the price fell by 2 or more is copied into the "Large decrease" section. Here's some sample output:
Download this worksheet and add the code to it.
Hints:
- The first rows don't contain data. You'll need to adjust for that.
- You'll need three counters. One for the input rows, and one each for the output regions.
Upload your solution. The usual coding standards apply.
When you submit, please answer this question: What did you learn from this exercise? Type in the text area of the solution form.
(If you were logged in as a student, you could submit an exercise solution, and get some feedback.)
Summary
Start with the usual code for finding the end of a data set. Add code to test whether each value should be copied to the new location.
There are two index variables in the code. The first one runs over the original data set, the data source. The second one tracks where new data is copied to, the destination.