Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
78 views

Programming 3: Dynamic Named Ranges and Automatic Graph Updating - The Power of The Macro Recorder

The document describes using dynamic named ranges and the macro recorder to automatically update graphs as data changes. 1) Dynamic named ranges are ranges that resize as data is added or removed. Setting up dynamic named ranges allows a graph to update automatically when the underlying data changes. 2) The macro recorder is used to capture formatting actions like centering and decimal place settings. These formatting steps are added to VBA code to ensure graphs are formatted consistently as data is recalculated. 3) Together, dynamic named ranges and macros recorded with the macro recorder allow graphs to dynamically update their data range and formatting anytime the underlying data is changed through the click of a button.

Uploaded by

Dascalu Ovidiu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Programming 3: Dynamic Named Ranges and Automatic Graph Updating - The Power of The Macro Recorder

The document describes using dynamic named ranges and the macro recorder to automatically update graphs as data changes. 1) Dynamic named ranges are ranges that resize as data is added or removed. Setting up dynamic named ranges allows a graph to update automatically when the underlying data changes. 2) The macro recorder is used to capture formatting actions like centering and decimal place settings. These formatting steps are added to VBA code to ensure graphs are formatted consistently as data is recalculated. 3) Together, dynamic named ranges and macros recorded with the macro recorder allow graphs to dynamically update their data range and formatting anytime the underlying data is changed through the click of a button.

Uploaded by

Dascalu Ovidiu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

4-

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 accordingly with the number of Cells put into it. It can be resized in either sense, that is, made larger or smaller, as often as we choose. If a graph is drawn using two such ranges then it will be seen to update dynamically either when a new point (row of data , or se!uence of points, is added or subtracted. "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 the 3ize 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 these enlarged Cells and from the .ormatting /oolbar 3elect the ,order Icon to place e$ternal and internal 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:;,;<,=,=,C">N/A(3heet4:;,8;, ?4 Clic1 Add. 3imilarly for the y@s, select Insert Name Define7 In the 'Names in wor1boo18* te$t entry bo$, type in y. In the 'Refers to8* te$t entry bo$, type in 9 "..3#/(3heet4:;C;<,=,=,C">N/A(3heet4:;C8;C ?4 Clic1 Add.

In Cell ,< type A B and fill down to 4= in unit increments. In Cell C< type 9 $C- A <D$, and press Return. /he &alue in Cell C< should read <B, ne$t double clic1 the fill handle to copy down the formula. Clic1 on any empty Cell to remo&e the selection of the Ey@ column.

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

9 sheet4:$

9 sheet4:y

"n clic1ing the 'Add* button you will see the word '3eries4* appear in the te$t bo$, proceed to type in the shown G and H ranges and then clic1 on .inish. Hou should now ha&e your graph, select the '3eries4* Iegend ,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 the fill handle and with the left mouse button held down, drag the mouse down until the indicator says E4B@, i.e. $ is at 4B, and let go. Fhat should happen is that the graph automatically updates itself to the new range of $ and y. +ighlight Cells ,K to C-< and press Delete. "nce again 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 your floppy dis1. Automatic e!! "i!!ing In Cell #< type 2in $, .< type 2a$ $ and L< type 3tep. In Cell #B type A 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 that we do not name LB 'step* in order to pre&ent names and functions conflicting later in 0,A 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 Command,uttons. (lace these 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 the following8 Name# cmdCalc Acce!erator# C aption# Calculate

(Changes the name on the button face

4< In a similar manner but with the second button in its '(roperties* ,o$ change the following8 Name# cmdClear Acce!erator# r aption# Clear 3till in Design 2ode, double clic1 on the 'Calculate* ,utton to ta1e you into the cmdCalc 3ubroutine (associated with the 'Calculate* ,utton within the 0,A #ditor. In the 3ubroutine write 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(Nmin$N 3et 2a$$ 9 Range(Nma$$N 3et Inc 9 Range(NincN Count 9 2in$ CellNumber 9 Int(C3ng((2a$$ ? 2in$ O Inc .or i 9 4 /o CellNumber P 4 Cells(i P ), N,N .0alue 9 Count Cells(i P ), NCN .0alue 9 f(Count Count 9 Count P C3ng(Inc Ne$t i Range(NA4N .3elect #nd 3ub In a similar manner write the code for the 'Clear* ,utton as below8 (ri&ate 3ub cmdClearMClic1( Range(N$N, NyN .Clear #nd 3ub
Initially we shall 1eep the e!uation the same as before, f ( x ) = x < x . 3o staying in the 0,A #ditor 2ode we select Insert 2odule, and in the new 2odule window type in the code8

f(Count is actually the function f, defined as f($ 9 $C- A <D$ e&aluated at $ 9 Count. /his will be written into a 0,A 2odule later.

.unction f($ As 3ingle As 3ingle f9$C-?<D$ #nd .unction Now return to the #$cel wor1boo1 and sa&e your file. /hen once you are out of the Design 2ode you may change some or all of the &alues of 2in $, 2a$ $ or 3tep, and then clic1 on either of the buttons A though without further modifications to the code I suggest a!ways clic1ing on the 'Clear* ,utton before the 'Calculate* ,utton.

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

In this instance, in this file, simply clic1 on "6 and no problem will arise. /o attempt to pre&ent similar messages a much more thorough method of declaring Names, .unctions, 3ubroutines, .unction calls, 3ubroutine calls, Range Declarations, Cell References, 3heet References and so on 7 is needed. /his need for thoroughness is re!uired, in particular, when wor1ing across more than 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 learning until we are more competent with 0,A constructs, references to which may be found in the ,ibliography. 3a&e your wor1 again, then select .ile 3a&eAs, and this time sa&e the file as '(rog)c* on your floppy 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 y columns loo1 somewhat messy: that is to say that different Cells appear to be e&aluated to a different number of decimal places and they are not centre %ustified. Recall that the first thing that was done with the original file was to do e$actly that, centre %ustify all the wor1sheet. Fell what has happened is that your 0,A code is does not obey the formatting done to your #$cel wor1sheet, so it needs its own formatting to be done within the code. /o find out e$actly how to accomplish the abo&e formatting feats may ta1e some time loo1ing through 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 Dialogue ,o$

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.

45 After getting to this stage, whilst being recorded, clic1 on an empty Cell, then select from the 3tandard /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%ust it 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 AltP.Q, to get the following Dialogue ,o$8

Fith '2acro4* highlighted clic1 on '#dit* to &iew the code, which should loo1 something li1e this below8 +owe&er, all that we need from this entire 2acro are %ust the two lines

+erein lie the (ros and Cons of recorded 2acros8? /hey are an e$cellent learning tool and can do many useful tas1s but 7 7 they contain too much irrelevant information and they cannot compete with good 0,A programming nor can they do all the tas1s that are possible by programming. /o return to our original 0,A code in the cmdCalc 3ubroutine in order so that we may modify the column formatting we can either double clic1 on the E3heet4@ icon in the 0,A (ro%ect window or

4K 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 our original 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(Nmin$N 3et 2a$$ 9 Range(Nma$$N 3et Inc 9 Range(NincN Count 9 2in$ CellNumber 9 Int(C3ng((2a$$ ? 2in$ O Inc .or i 9 4 /o CellNumber P 4 Cells(i P ), N,N .0alue 9 Count Cells(i P ), NCN .0alue 9 f(Count Count 9 Count P C3ng(Inc Ne$t i Range(N$N, NyN .3elect Fith 3election .Number.ormat 9 N=.==N .+orizontalAlignment 9 $lCenter #nd Fith Range(NA4N .3elect #nd 3ub Now return to the #$cel wor1sheet, clic1 off the Design 2ode, press the 'Clear* ,utton and once again with '3tep* set to =.<, press the 'Calculate* ,utton. ,oth $ and y columns should now be centre aligned and set to - decimal places. Notice also that the a$es formatting on the graph has also changed accordingly. As an e$tension, how would you rewrite the 0,A code in the cmdCalc 3ubroutine to automatically place an outer border around both the '$* and 'y* columns of &aluesR A wor1sheet picture is shown below of the final stage. DD $%&N U'(NG T%(' "()& A)$A*' MA+& 'UR& T%AT T%& $,R+'%&&T A) U)AT(,N M,D& (' '&T T, AUT,MAT( AND N,T MANUA) ,T%&R$('& T%& GRAP% $()) N,T AUT,MAT( A))* UPDAT& ",R A N&$ (NPUT RANG& --

Additional code

Notice again the EAmerican@ spelling

4Q

You might also like