Programming 3

download Programming 3

of 7

Transcript of Programming 3

  • 7/27/2019 Programming 3

    1/7

    Programming 3

    Dynamic Named Ranges and Automatic Graph Updating The Power of the Macro

    Recorder.

    A Dynamic Named Range is one in which the size has not been set but rather alters accordinglywith the number of Cells put into it. It can be resized in either sense, that is, made larger orsmaller, as often as we choose. If a graph is drawn using two such ranges then it will be seen toupdate dynamically either when a new point (row of data, or se!uence of points, is added orsubtracted.

    "pen #$cel, centre %ustify all Cells and sa&e file as 'rog)a* on your floppy disc.+ighlight Cells - to ) and select ormat Cells Alignment /ag, and then Center align te$t

    both +orizontal and 0ertical and also chec1 the 2erge cells o$. rom the ont /ab change the3ize to 45, then press "6. +ighlight Cells C- to C) and repeat the process.

    In Cell - () has been absorbed into - now type $, and into Cell C- type y. +ighlight theseenlarged Cells and from the ormatting /oolbar 3elect the order Icon to place e$ternal andinternal borders on the Cells.

    To set up the dynamic Named Range

    3elect Insert Name Define7 In the 'Names in wor1boo18* te$t entry bo$, type in $. In the'Refers to8* te$t entry bo$, type in

    9 "3#/(3heet4:;;

  • 7/27/2019 Programming 3

    2/7

    Clic1 on the Chart Fizard from the 3tandard /oolbar and choose GH (3catter, smooth andwithout points and clic1 Ne$t. 3elect the 3eries /ab in the -ndChart Fizard window and clic1 onthe 'Add* button.

    "n clic1ing the 'Add* button you will see the word '3eries4* appear in the te$t bo$, proceed totype in the shown G and H ranges and then clic1 on inish. Hou should now ha&e your graph,select the '3eries4* egend o$ and press Delete.

    Now highlight the bottom row of your $ and y data columns, it should be Cells 4J to C4J. clic1

    on top of thefill handleand with the left mouse button held down, drag the mouse down until theindicator says E4B@, i.e. $ is at 4B, and let go. Fhat should happen is that the graph automaticallyupdates itself to the new range of $ and y. +ighlight Cells K to C-< and press Delete. "nceagain the graph should update itself.

    3a&e your wor1 and also select ile 3a&eAs, and this time sa&e the file as 'rog)b* on yourfloppy dis1.

    Automatic e!! "i!!ing

    In Cell #< type 2in $, < type 2a$ $ and L< type 3tep. In Cell #B type 4=, B type 4B and LB

    type 4. >se the Name o$ to name #B, B and LB min$, ma$$ and inc, respecti&ely (Notice thatwe do not name LB 'step* in order to pre&ent names and functions conflicting later in 0A code.Remember to press Return after writing new names in the Name o$ or they will not be

    permanently entered into the wor1sheet.

    ut the wor1sheet into Design 2ode since we now need to create two Commanduttons. lacethese on top of Cells #- to ) (halfway through it and - (halfway through it to L).

    Ne$t, select the first button with a clic1 the left mouse button, then whilst still on top of the button,clic1 with the right mouse button.

    Ne$t, from the Drop Down 2enu select roperties and in the 'roperties* o$ change thefollowing8

    Name# cmdCalcAcce!erator# Caption# Calculate (Changes the name on the button face

    4)

    9 sheet4:$ 9 sheet4:y

  • 7/27/2019 Programming 3

    3/7

    In a similar manner but with the second button in its 'roperties* o$ change the following8

    Name# cmdClearAcce!erator# raption# Clear

    3till in Design 2ode, double clic1 on the 'Calculate* utton to ta1e you into the cmdCalc3ubroutine (associated with the 'Calculate* utton within the 0A #ditor. In the 3ubroutinewrite the following code8

    ri&ate 3ub cmdCalcMClic1(

    Dim i As 3ingle, Count As 3ingle, CellNumber As 3ingle Dim 2in$ As Range, 2a$$ As Range, Inc As Range

    3et 2in$ 9 Range(min$ 3et 2a$$ 9 Range(ma$$

    3et Inc 9 Range(inc

    Count 9 2in$ CellNumber 9 Int(C3ng((2a$$ ? 2in$ O Inc

    or i 9 4 /o CellNumber P 4 Cells(i P ), .0alue 9 Count Cells(i P ), C.0alue 9 f(Count Count 9 Count P C3ng(Inc Ne$t i

    Range(A4.3elect#nd 3ub

    In a similar manner write the code for the 'Clear* utton as below8

    ri&ate 3ub cmdClearMClic1( Range($, y.Clear#nd 3ub

    Initially we shall 1eep the e!uation the same as before, ( )

    -

  • 7/27/2019 Programming 3

    4/7

    ecause the code herein has been written !uite sparsely to aid speed and hopefully its clarity,attempting to run 0A enhanced #$cel files will sometimes yield error messages. "ne suchmessage is

    In this instance, in this file, simply clic1 on "6 and no problem will arise. /o attempt to pre&entsimilar messages a much more thorough method of declaring Names, unctions, 3ubroutines,unction calls, 3ubroutine calls, Range Declarations, Cell References, 3heet References and so on7 is needed. /his need for thoroughness is re!uired, in particular, when wor1ing across morethan one 3heet at a time in the same file, or e&en in a different file, which may or may not be open:

    +owe&er, for our purposes here, in the Eearly learning@ stages, bre&ity will hopefully aid learninguntil we are more competent with 0A constructs, references to which may be found in theibliography.

    3a&e your wor1 again, then select ile 3a&eAs, and this time sa&e the file as 'rog)c* on yourfloppy disc.

    Using the Macro Recorder

    In file 'rog)c* choose whiche&er 2in $ and 2a$ $ you wish but change 3tep to =.< before

    Clearing and Calculating the new graph. Fhat you will probably notice is that the $ and ycolumns loo1 somewhat messy: that is to say that different Cells appear to be e&aluated to adifferent number of decimal places and they are not centre %ustified. Recall that the first thing thatwas done with the original file was to do e$actly that, centre %ustify allthe wor1sheet. Fell whathas happened is that your 0A code is does not obey the formatting done to your #$celwor1sheet, so it needs its own formatting to be done withinthe code.

    /o find out e$actly how to accomplish the abo&e formatting feats may ta1e some time loo1ingthrough boo1s or through the help screen. +owe&er, help is at hand with the 2acro Recorder.

    /he 2acro Recorder can be acti&ated by selecting from the 3tandard /oolbar, /ools 2acro

    Record New 2acro. Could you do this now and then you will see the Record 2acro Dialogueo$

    In general it is useful to choose meaningful names for 2acros but lea&e this as 2acro4 and clic1"6. Hou will then see the loating 2enu and from this point on e&ery mouse clic1,drag, whate&er: will be recorded until the small blue s!uare on the 2enu is clic1ed.

    4B

  • 7/27/2019 Programming 3

    5/7

    After getting to this stage, whilst being recorded, clic1 on an empty Cell, then select from the3tandard /oolbar, ormat Cells Number /ab, and then in the 'Category* list bo$ choose'Number*. #&en if we are already set on - decimal places ad%ust it so that we are not, then ad%ustit bac1 to - decimal places (3o that an action has ta1en place, then clic1 on the Alignment /ab.#&en if we are already set on Center Alignment in the '+orizontal /e$t alignment* bo$ ad%ust it so

    that we are not, then ad%ust it bac1 it so that we are horizontally center aligned. Clic1 on "6./hen clic1 on the small blue s!uare on the loating 2enu to stop the recording.

    Now select /ools 2acro 2acros, or clic1 AltPQ, to get the following Dialogue o$8

    Fith '2acro4* highlighted clic1 on '#dit* to &iew the code, which should loo1 something li1ethis below8

    +owe&er, all that we need fromthis entire 2acro are %ust thetwo lines

    +erein lie the ros and

    Cons of recorded 2acros8?

    /hey are an e$cellent learningtool and can do many usefultas1s but 77 they contain too muchirrelevantinformation and theycannot compete with good0A programming nor canthey do all the tas1s that are

    possible by programming.

    /o return to our original 0A code in the cmdCalc 3ubroutine in order so that we may modify thecolumn formatting we can either double clic1 on the E3heet4@ icon in the 0A ro%ect window or

    45

  • 7/27/2019 Programming 3

    6/7

    else return to the #$cel wor1sheet, select Design 2ode and then double clic1 on the embedded'Calculate* utton.

    /hus transferring these, pre&iously identified, rele&ant two lines ? only slightly modified ? to ouroriginal cmdCalc 3ubroutine gi&es us the new code8

    ri&ate 3ub cmdCalcMClic1( Dim i As 3ingle, Count As 3ingle, CellNumber As 3ingle Dim 2in$ As Range, 2a$$ As Range, Inc As Range

    3et 2in$ 9 Range(min$ 3et 2a$$ 9 Range(ma$$ 3et Inc 9 Range(inc

    Count 9 2in$ CellNumber 9 Int(C3ng((2a$$ ? 2in$ O Inc

    or i 9 4 /o CellNumber P 4 Cells(i P ), .0alue 9 Count Cells(i P ), C.0alue 9 f(Count Count 9 Count P C3ng(Inc Ne$t i

    Range($, y.3elect Fith 3election .Numberormat 9 =.== .+orizontalAlignment 9 $lCenter

    #nd Fith

    Range(A4.3elect

    #nd 3ub

    Now return to the #$cel wor1sheet, clic1 off the Design 2ode, press the 'Clear* utton and onceagain with '3tep* set to =.

  • 7/27/2019 Programming 3

    7/7

    4Q