Programming 5

advertisement
23
Programming 5
In this section we learn about altering axes ranges on any graph. Any of you that have tried to
alter the maximum and minimum axes values on both axes will know that it can be somewhat
long winded and the results cannot be seen until the Axes Dialogue Box has been fully closed.
The following method uses “Spin Buttons” embedded in the worksheet to interactively alter axes
ranges.
Start Excel, centre justify all Cells and save the new file on your floppy disc as “Prog5a”
In Cell B10 type x and type – 40 in Cell B11. Use Edit  Fill  Series…, etc, to fill down to
+ 40 in unit increments. Name this range x.
In Cell C10 type y and type = (x^2/4 – x + 3)*(SIN(x/4))^2 – 13 , into C11 and press return.
Cell C11 should now show 118 to 3 s.f. Double click the fill handle to copy down this formula.
Highlight the corresponding y values and give this range the name y.
Make sure that no Cells are highlighted before clicking on the Chart Wizard Icon in the Standard
Toolbar. Choose an XY (Scatter) chart type, smooth and without points visible, before clicking
the Next button. In the Data range textbox type x,y and press the Finish button.
Resize the graph as follows: Hold down the “Alt” Key and with the left mouse button held down
drag the top left anchor point on the graph border to the top left of Cell H4. The purpose of
holding down the “Alt” button is to make the resized graph (or any object) “Snap to Grids”. In a
similar manner drag the bottom right anchor point of the graph border to give the largest graph
for your screen. Click once on the Chart Legend to highlight it and press the Delete Key on the
keyboard. With the mouse in the Plot Area click on the right mouse button and select “Clear”
from the drop down menu. In a similar manner repeat the above with any gridline. Thus the
graph should have no background colour or gridlines. (The reason being that it refreshes or
redraws quicker and without flicker). Save your work.
In Cell F4 type “Axes”, in F5 type 11, E6 type –14, F7 type –15 and G6 type
16. Now highlight the numbers 5, 6 and 7 from the left hand row indicator
column as shown.
Whilst these are highlighted click the right mouse button and select “Row
Height”. Type in 20 and click OK.
Now press and hold down the Ctrl button on the keyboard, then at the same
time click the four Cells F5, E6, F7 and G6. Whilst the mouse pointer
remains over one of these Cells press the right mouse button and choose
“Format Cells”. Select the “Alignment” Tab and alter the options as shown
below. Click OK.
24
If not already on display, open up the Visual Basic Toolbar (View  Toolbars  Visual Basic).
Cilck open the Control Toolbox and select the “Spin Button” Control
Whilst holding down the “Alt” key drag the Spin Button over the Cell F6, it should “Snap” to the
Cell size automatically. Repeat with three further Spin Buttons put into the Cells E7, F8 and G7.
It will look a little messy now, something like this:
spnyMax
spnxMin
spnyMin
spnxMax
Get into Design Mode if not already activated and right mouse click on top of a Spin Button,
choose “Properties” and modify each of the four in the same manner bar their individual names
as shown in the picture above. The modifications are given below:
Name:
Height:
Linked Cell:
Max:
Min:
Orientation:
Either of the above Spin Button names, respectively
6.75
F5 for spnyMax, G6 for spnxMax, F7 for spnyMin or E6 for spnxMin
500
-500
1 – fmOrientationHorizonal
One of the effects of this is to make the spin buttons like quite thin rectangles. Earlier we ‘top
aligned’ the four cells F5, G6, F7 and E6, the reason for this was so that we could squeeze in
underneath each number the associated Spin Button. By far the most accurate way to now move
each spin button is to highlight it then hold down the “Ctrl” key as you press either of the
keyboard cursor (arrow) keys. You should end up with the following picture – I have included a
simple border too.
We now give the Cells F5, G6, F7 and E6 the names “ymax”, “xmax”,
“ymin” and “xmin” respectively using the Name Box for speed (but
don’t forget to press Return each time or the name will not be entered)
Next, in Design Mode, right mouse click on the spnyMax Spin Button
and choose “View Code”. Type in the following code:
Private Sub spnyMax_Change()
Range("ymax").Activate
ActiveCell.Value = spnyMax.Value
Call axes1
End Sub
Alter this accordingly with each of the other Spin Buttons respectively.
Now, either stay in the VBA Editor or if in Excel press Alt+F11. Then select Insert  Procedure,
and fill out the Add Procedure Dialogue Box as shown below giving it the name “axes1”. After
pressing OK next type in the code for “axes1” as shown below.
25
Notice that this Procedure is expecting your graph to be called “Chart 1” – if it is not then it will
not work. After checking to see what your graph is actually called (Hold down the “Ctrl” key
whilst clicking on the graph and look in the Name Box) you can remedy this by either:
1. Changing the name of the graph in the Name Box, or
2. changing the name in the code of the “axes1” Procedure to match that of your graph on
the worksheet.
Now go back to the Excel worksheet and click on the Spin Button arrows, what happens? Notice
also that you can directly alter axes sizes by clicking in either of the Cells F5, F7, E6 or G6
respectively, typing in any particular value and then pressing Return. Save your work!
Notice also that, unlike in the previous exercise (Programming 4), this range of x and y values
are static. Hence, there is little point in altering the maximum and minimum values of x beyond
the values 40 and – 40, respectively.
26
We finish with a general statement that should be read as applicable to many of our previous
exercises, namely:
To make this particular exercise more transferable to other work delete all of your x and y
range and your graph but leave your Spin Buttons and their numbers. Save this file as Axes
and then use this as a template or starting point for any other work that will need a graph to
be drawn as part of it.
Therefore, once you have a Subroutine or Module that works – SAVE IT somewhere either to be
imported or ‘cut and pasted’ at a later time into other projects.
A screen shot is shown below, wherein the worksheet’s Gridlines have been removed for clarity
by selecting Tools  Options  View Tab, and unchecking the Gridlines Box in the ‘Windows
options’ section.
Download