You are on page 1of 7

*

Programming 1
User Defined Functions (UDFs) - one glitch and one inconvenience! Anyone who has experimented with UDFs in any version of Excel later than Excel 5 will have noticed two annoying features. These will e loo!ed at first before we do any more advanced wor!. The Glitch "pen Excel and centre align all #ells. $n #ell %& type 'x and from %( to %)* fill with a range of integers from +, to *-. .ighlight #ells %& to %)* and select $nsert /ame #reate0 etc0 to name the range 'x. $n #ell A, type h and in #ell A1 type -0 then highlight #ells A, to A1 and select $nsert /ame #reate0 etc0 to name the #ell 'h. /ext0 select File 2aveAs0 and type 3glitch*4. 2ave to your floppy dis!. $n #ells #& to 5& type in 'a to 'e0 one to each #ell. $n #( type 6 x7, + 18x7) 9 1:;x 9 5< 0 and press =eturn. This should give a #ell output of > &*. /ext0 dou le clic! on the fill handle utton of #ell #( to automatically fill the 'a column. /ote that0 if you instead have a column of > &*s then your wor!sheet will still e in the '?anual #alculation ?ode. $f you wish to reset to 'Automatic #alculation ?ode then select Tools "ptions #alculation Ta 0 and then clic! in the 3Automatic4 option followed y "@. $f you do not wish to change ac! then simply press the 2pecial Function @ey FA once + ut $ suggest that you do change ac!. /ow0 select File 2aveAs0 and type 3glitch*a4. 2ave to your floppy dis!. /ext0 press Alt92pecial Function @ey F**0 that is0 Alt9F**. This opens up the Bisual %asic for Applications ;B%A< window into which we are now going to write the previous function. 2elect $nsert ?odule0 this opens up a window into which we type the following codeC ;=ecall that the editor will fill much of this code in automatically + provided we press either the =eturn or Ta uttons at the appropriate moment.< Function f;x As 2ingle< As 2ingle f 6 x 7 , > 1 8 x 7 ) 9 1 : ;x 9 5< End Function #lic! on the Excel $con in the 2tandard Tool ar to return to the Excel wor!sheet and now in #ell D( type in 6 f;x< 0 and press =eturn. Dhat will e shown is the error message 3EBAFUE4. =egardless of this error0 dou le clic! on the fill handle of the #ell to fill down the formula. /ext0 in #ell %1 type 6 f;h< 0 and press =eturn. This time you should have the value -.( in the #ell ;$f you once again have 3EBAFUE4 then it may e ecause you didnt alter the argument to the function f; < from 'x to 'h< =epeat this previous operation ut instead in #ell #)1. $t should again give -.(. Thus0 single /amed #ells experience no pro lems with User Defined Functions ;UDFs<

) First 'Fixit $n #ell E( type in 6f;BAFUE;x<< 0 and press =eturn ;Upper case is not necessary< and dou le clic! on the fill handle of the #ell to fill down the formula. This wor!s0 ut it is neither particularly succinct nor mathematically clear. 2econd 'Fixit $n #ell F( type in 6 f;x9-< 0 and press =eturn and dou le clic! on the fill handle of the #ell to fill down the formula. This wor!s ut again it could e mathematical confusing. Third 'Fixit $n #ell 5( type in 6 f;x 9 h< 0 and press =eturn and dou le clic! on the fill handle of the #ell to fill down the formula. This wor!s and gives the correct answer to what we expect of f;x< ; ecause at present h is set eGual to Hero<. /ow this may not e exactly what we were after %UT the fact that the com ination of a /amed Range of values with a /amed Cell put into the argument of our UDF will have much use later on0 and will then have greater mathematical meaning. =ecall setting up the #entral Derivative ?odule from page ** in the 3/amed =anges ,4 exercise. $n that case the UDF was acting on a single #ell and not a Range of #ells. De now !now that such a UDF can also return values from a Range of #ells. /otice that f;*8x<0 f;)8x<0 f;x:5<0 etc0 or f;p8x<0 f;x:G<0 etc0 would also return full range values for appropriate choices of p and G + useful for transformation graphs. Therefore0 at present $ suggest that you actually type in the function in terms of x as you have done in column # and then copy this formula down. Then a clic! on any #ell in this range will show you the mathematical form of the function in the formula ar0 in this case 6 x7,>18x7)91:;x95<. /ext0 open the B%A editor with Alt9F** and define the same function in a module0 and then this will e what you use in later wor! in the wor!sheet ;Irovided that you will e using an argument to the function other than a sole 'x + as has een discussed in depth a ove<. =ecall again that for a single0 /amed #ell rather than a /amed Range of #ells then the straight forward #ell entry of 6 f;x< will evaluate the function correctly without either of the a ove 'fixes The nconvenience "pen Excel0 #entre align all #ells. 2elect File 2aveAs0 and type 3v aJfunc4. 2ave to your floppy dis!. $n #ell %1 type x0 and in %5 type in the value ). 5ive this data Cell the /ame x. $n #ell #5 type 6 A#osh;x< 9 x +,0 and press =eturn0 the cell should have the value -.,*&A5(. /ext0 press Alt92pecial Function @ey F**0 that is0 Alt9F**. This opens up the Bisual %asic for Applications ;B%A< window into which we are now going to write the previous function. 2elect $nsert ?odule0 this opens up a window into which we type the following codeC Function f;x As 2ingle< As 2ingle f 6 A#osh;x< 9 x >, End Function

, #lic! on the Excel $con in the 2tandard Tool ar to return to the Excel wor!sheet and now in #ell D5 type 6 f;x< 0 and press =eturn. Dhat will e shown is the following error message elow.

The cause of this pro lem is that the B%A language does not !now what the Arc#osh of x means0 unli!e the actual Excel wor!sheet. $n fact B%A has a surprisingly sparse collection of !nown mathematical functions 'pre>programmed in to it. To see a listing of availa le B%A mathematical functions first get into the B%A screen using Alt9F**0 then anywhere on an open ;module< window type v a.math. 0 and on typing in the last period ;full stopK< you will have the availa le functions displayed in a 'pop>up list. As you will see things are rather limited0 however0 where there is a will there is a wayK The listing on the next page gives many useful mathematical functions re>written in terms of some of the availa le B%A functions. /ote that in an expression such asC

Arccosh 6 Fog;x 9 2Gr;x 8 x > *<<0 $ have deli erately left out the argument of the actual function. This is due to how it must e implemented in the B%A ?odule code. For example0 consider the ?odule code given elow0 oth of which output the same value0 that of cosh * ;x< or arc cosh ; x< C
Function f;x As 2ingle< As 2ingle Arccosh 6 Fog;x 9 2Gr;x 8 x > *<< f 6 Arccosh 9 x + , End Function or Function f;x As 2ingle< As 2ingle f 6 Fog;x 9 2Gr;x 8 x > *<< 9 x > , End Function

$ thin! that the first is prefera le since we are reminded exactly which function we are using. /ow rewrite your previous module code to e either of the previous two and before returning ac! to the Excel wor!sheet either select =un =eset0 or simply clic! on the small lue sGuare on the tool ar. The purpose of this action is to enter your changes and stop the Editors De ug mode. "n returning to the wor!sheet0 ma!e sure that your answer is identical to that in #5. 2ave your file.

#onstruction of functions ased on B%A functions


2ec 6 * : #os;x< #osec 6 * : 2in;x< #otan 6 * : Tan;x< Arcsin 6 Atn;x : 2Gr;>x 8 x 9 *<< Arccos 6 Atn;>x : 2Gr;>x 8 x 9 *<< 9 ) 8 Atn;*< Arcsec 6 Atn;x : 2Gr;x 8 x > *<< 9 2gn;;x< > *< 8 ;) 8 Atn;*<< Arccosec 6 Atn;x : 2Gr;x 8 x > *<< 9 ;2gn;x< > *< 8 ;) 8 Atn;*<< Arccotan 6 Atn;x< 9 ) 8 Atn;*< 2inh 6 ;Exp;x< > Exp;>x<< : ) #osh 6 ;Exp;x< 9 Exp;>x<< : ) Tanh 6 ;Exp;x< > Exp;>x<< : ;Exp;x< 9 Exp;>x<< 2ech 6 ) : ;Exp;x< 9 Exp;>x<< #osech 6 ) : ;Exp;x< > Exp;>x<< #otanh 6 ;Exp;x< 9 Exp;>x<< : ;Exp;x< > Exp;>x<< Arcsinh 6 Fog;x 9 2Gr;x 8 x 9 *<< Arccosh 6 Fog;x 9 2Gr;x 8 x > *<< Arctanh 6 Fog;;* 9 x< : ;* > x<< : ) Arcsech 6 Fog;;2Gr;>x 8 x 9 *< 9 *< : x< Arccosech 6 Fog;;2gn;x< 8 2Gr;x 8 x 9 *< 9 *< : x< Arccotanh 6 Fog;;x 9 *< : ;x > *<< : ) Fog/ 6 Fog;x< : Fog;n<

/ote that not all of the a ove functions are availa le as standard functions in Excel either. To find out which are first change to the Excel wor! oo! and then clic! on any clear #ell and then select $nsert Function. This will display the 3Iaste Function4 Dialogue %ox0 as shown elow. Then choose 3?ath L Trig4 and scroll through the availa le functions. Those that arent in the list0 such as cosech >* ; x< 0 etc0 can either e inputted as a UDF0 as descri ed a ove0 or as the corresponding eGuivalent formC Fog;;2gn;x< 8 2Gr;x 8 x 9 *< 9 *< : x< %ut0 notice the following necessary alterations for use within the Excel wor! oo! rather than for B%A0 viHC Fog;;2ign;x< 8 2Grt;x 8 x 9 *< 9 *< : x< since the Excel wor! oo! has different 'calls for the same function + confused yetM Iersonally $ would go for a UDF to clarify the wor!ing.

The !""lication#$or%sheetFunction

5 %efore you egin to thin! that having to !now all and more of the a ove formulae constructions is completely eyond you Excel has a very simple0 al eit wordy way of 'gra ing any functions found in the Excel Dor!sheet and putting them into B%A code. The a ove module code for the Arc#osh function would instead need to e writtenC Function f;x As Dou le< As Dou le f 6 Application.Dor!sheetFunction.Acosh ;x< End Function /otice now that the wor!sheet function 'Acosh must have its argument set as a dou le>precision0 floating>point num er. The importance of this seemingly innocuous example cannot e overstatedN the Application.Dor!sheetFunction in your code should almost always e used. The reasoning eing that your own attempts at designing a suita le function0 though admira le0 will almost always cause your code to run slower and may also contain mista!es. /otice that when you are typing the phrase 3Application.Dor!sheetFunction.Acosh;x<4 then you will actually only need to type the final ';x< elieve it or notK + see the stages elowK

;*< =ight clic! after the '6 sign. 'Application #lic! on either 3Fist Iroperties:?ethods4

;)<

2croll down to the word


and either select it and press

the 'Ta or 3#omplete Dord4 utton or dou le clic! it.

&

;,< /ext type a period 3.4 0 this will open another drop>down menu. /ow either scroll down or Oust type 'w + this will automatically scroll you down to the start of the options that egin with the letter 'w. Again0 either select the reGuired optionC 'Dor!sheetFunction and then press the 'Ta utton or dou le clic! the reGuired option.

;1< Again type a period 3.4 0 this will open

another drop>down menu. 2imilarly0 either select the reGuired optionC 'Acosh and then press the 'Ta utton or dou le clic! the reGuired option.

;5< /ow type the opening rac!et and notice the information ox that appears. $t gives

you important information regarding how many arguments the function can accept and what type of arguments must e used.

;&<

Finally type in your functions dummy argument + it need not e x + and close the rac!et. Thus only ;x< needed to e typedKK

Finally0 e aware that there will e times when you have to write your own custom function0 possi ly ecause it is neither present in the Excel Dor!sheet or the B%A function lists. A simple mathematical example is the .yper olic 2ecant. .ere then0 the Ta le on Iage 1 a ove is of use.

You might also like