You are on page 1of 7

.

Programming 2
Control Buttons on the Worksheet Open Excel, select File SaveAs, and call the new file Prog2a on your floppy disc Select !iew "ool#ars !isual $asic "his puts the !isual $asic %cons in the "ool#ar, na&ely'

Opens the !$A Editor +Sa&e as Alt;F//, Opens the !$A (ontrol "ool#ox, where (ontrol $uttons are stored

Puts the Excel Sheet into 7esign 8ode 6 this allows editing of #uttons in wor)sheet

(lic) on the (ontrol "ool#ox, this gives a further &enu which &ay #e displayed with the other tool#ars or &ay #e free*floating on the wor)#oo)

(o&&and $utton (lic) on the (o&&and $utton %con and &ove the &ouse cursor +which should loo) li)e a fine cross, to the &iddle of the (ell A2 -ext hold down the left &ouse #utton and drag diagonally down to the &iddle of the (ell (. and let go "his will create for you a co&&and #utton within the Excel wor)sheet %t should #e called (o&&and$utton/ -ext, in (ell $0 type 1a2 and in $3 type 4 5ighlight (ells $0 and $3 and select %nsert -a&e (reate, etc, to give $3 the -a&e 1a2 -otice that entering values into these (ells have greyed out the !$A and (ontrol "ool#ox "ool#ars 6 they will return as soon as no (ell entries are #eing &ade and you will still #e in design &ode -ow, whilst still in 7esign 8ode, dou#le clic) on (o&&and$utton/ "his ta)es you to the !$A Editor screen, and you should see so&ething along the lines of the Pro9ect and Properties $oxes on one side of your screen as shown on the following page Our concern is with the Properties that we want the new #utton to have See the picture of the Properties $ox #elow for details, #ut on the top line * the 1+-a&e,2 line * change (o&&and$utton/ to 1c&d"est2 Si&ilarly, on the 1Accelerator2 line type in 1"2 and on the 1(aption2 line type in 1"est2 "o enter this last alteration, press :eturn 6 always do this

(hange to 1c&d"est2 "ype in 1"2

"ype in 1"est2

Note that if ever you go into the !$A editor and cannot see on the screen either of the Pro9ect or Properties windows shown a#ove, then whilst in the !$A Editor screen select !iew fro& the tool#ar and the clic) on #oth the 1Pro9ect Explorer2 and the 1Properties <indow2 options

> %n the Editor window of the screen you &ay have the following su#routine definition displayed

(lic) on this list arrow in the O#9ect $ox and select c&d"est (lic) on the list arrow in the O#9ect $ox and select c&d"est fro& the drop down list "his will auto&atically open up the c&d"est Su#routine that is associated with a (lic) of the $utton on the Excel wor)sheet "herefore, any code that is typed into this Su#routine will #e executed each ti&e we clic) on the $utton now called "est, which has #een e&#edded into the wor)sheet "ype the following code into the Su#routine

"he 7i& "rans/ As :ange tells !$A that we are defining 1"rans/2 to #e a 1:ange2 varia#le type, that is it can accept values fro& single or &ultiple wor)sheet (ells 7i& i As %nteger tells !$A

0 that we are defining 1i2 to #e an 1%nteger2 varia#le, which in this instance, is for use later on in the progra& "he four lines Set "rans/ ? :ange+@$3@, Set "rans2 ? A$3B Set "rans4 ? :ange+@a@, Set "ransC ? AaB are four possi#le ways in which the progra& can 1read2 the value in the (ell $3 +which also has the na&e 1a2 associated with it, fro& the Excel wor)sheet which can then #e used and &anipulated within the progra& "he 1:ange2 function can accept &any types of argu&ent, not 9ust single cell ones For exa&ple, other such accepta#le argu&ents could #e $3,$/2 or A4'E/3 %n particular see the Su#routine c&d(lear on the next page "he lines (ells+/4 * i, C ; i, !alue ? "rans/ D 4 E C, and (ells+/4 ; i, @$@, !alue ? "rans/, show two ways in which values fro& the progra& +possi#ly altered, &ay #e passed #ac) to the wor)sheet -otice that the first argu&ent in the 1(ells2 function is the Row coordinate and the second is the Column coordinate, which &ay #e either a nu&#er or letter :eturn to the Excel sheet and exit 7esign &ode, we will now see how to clear the wor)sheet of previous results (lic) on the (ontrol "ool#ox and clic) on the (o&&and $utton %con and &ove the &ouse cursor to the &iddle of the (ell (2 -ext hold down the left &ouse #utton and drag diagonally down to the &iddle of the (ell E. and let go "his will create for you a second co&&and #utton within the Excel wor)sheet %t should #e called (o&&and$utton2, dou#le clic) on this #utton to open the !$A Editor Once again, fro& the Properties $ox, on the 1+-a&e,2 line * change (o&&and$utton2 to 1c&d(lear2 Si&ilarly, on the 1Accelerator2 line type in 1(2 and on the 1(aption2 line type in 1(lear2 (lic) on the list arrow in the O#9ect $ox and select c&d(lear fro& the drop down list "his will auto&atically open up the c&d(lear Su#routine %n the c&d(lear Su#routine type the following code' Private Su# c&d(learF(lic)+, :ange+@$/C'$/>,E/2,F//,G/H,53@, (lear End Su# :eturn to the Excel sheet, exit 7esign &ode and test your wor) #y altering the value of a in (ell $3 and then save it Also SaveAs the file Prog2# on your floppy dis) %n this next file, Prog2#, we will introduce :ando& nu&#ers %nitially, resiIe your "est and (lear #uttons #y first entering the 7esign 8ode on the !isual $asic "ool#ar 6 &a)e each #utton only one (ell wide and two (ells deep so that the pair of the entirely cover (ells $2 to (4 (reate another (o&&and$utton and place it over (ells 72 and 74 %t should #e called (o&&and$utton4 (lic) onto (ell $. and enter the word Jpper, clic) onto (ell $= and enter the word Kower Enter /. into (ell (. and 6 > into (= (lic) onto (. and then clic) into the -a&e $ox "ype 1J2 into the -a&e $ox and press :eturn Si&ilarly, clic) onto (= and then into the -a&e $ox "ype 1K2 into the -a&e $ox and press :eturn -ext, if you are still in 7esign 8ode, dou#le clic) on this #utton to open the !$A Editor, and fro& the Properties $ox, on the 1+-a&e,2 line * change (o&&and$utton4 to 1c&d:ando&2 Si&ilarly, on the 1Accelerator2 line type in 1:2 and on the 1(aption2 line type in 1:ando&2 (lic) on the list arrow in the O#9ect $ox and select c&d:ando& fro& the drop down list "his will auto&atically open up the c&d:ando& Su#routine %n the c&d:ando& Su#routine type the following code'

3 Private Su# c&d:andF(lic)+, 7i& :and!alue As %nteger 7i& Jpper As :ange, Kower As :ange Set Jpper ? AJB Set Kower ? AKB :ando&iIe :and!alue ? %nt++Jpper * Kower ; /, L :nd ; Kower, (ells+3, $, !alue ? :and!alue End Su# -otice that we &ust type in the wor) :ando&iIe #efore any code as this selects a rando& 1seed2 for the :ando& -u&#er Generator Function, :nd "he code :and!alue ? %nt++Jpper * Kower ; /, L :nd ; Kower, :eturns an integer value in the range given #y the values in (ells (. and (= :eturn to the Excel sheet, exit 7esign &ode and test your wor) #y altering the value of a via the 1:ando&2 #utton Save your wor), and then select File SaveAs Prog2c on your floppy dis) %n the new file Prog2c, &odify the code in the loop, as shown #elow, fro& the Su#routine c&d"est6 what happensM For i ? / "o C %f i ? / "hen (ells+/2 * i, C ; i, !alue ? @a D 4 E C@ (ells+/4 * i, C ; i, !alue ? "rans/ D 4 E C Else%f i ? 2 "hen (ells+/2 * i, C ; i, !alue ? @a;2@ (ells+/4 * i, C ; i, !alue ? 2 ; "rans2 Else%f i ? 4 "hen (ells+/2 * i, C ; i, !alue ? @4La@ (ells+/4 * i, C ; i, !alue ? 4 L "rans4 Else (ells+/2 * i, C ; i, !alue ? @a*4@ (ells+/4 * i, C ; i, !alue ? "ransC * 4 End %f -otice the 1A&erican2 spelling

Add these extra lines into c&d"est

"est the file and save your wor) Also select File SaveAs Prog2d on your floppy dis) For the penulti&ate exercise in file Prog2d, create two &ore (o&&and$uttons and place the& on top of (ells N2 to O4 %n (ell N. type in = and press :eturn, now clic) #ac) into the (ell N. and then clic) into the -a&e $ox Give this (ell the na&e Seed 5ighlight the e&pty (ells N= to K0 and clic) into the -a&e $ox againP give this #loc) of (ells the na&e $loc) 7ou#le clic) on the #utton over the (ells N2 to N4 dou#le clic) on this #utton to open the !$A Editor, and fro& the Properties $ox, on the 1+-a&e,2 line * change to 1c&dFill2 Si&ilarly, on the 1Accelerator2 line type in 1F2 and on the 1(aption2 line type in 1Fill (ells2 "ype in the following code

/H Private Su# c&dFillF(lic)+, 7i& i As %nteger, 9 As %nteger, count As %nteger 7i& Start!alue As :ange count ? H Set Start!alue ? :ange+@Seed@, For i ? /H "o /2 For 9 ? = "o 0 (ells+9, i, !alue ? Start!alue Start!alue ? Start!alue ; / count ? count * / -ext 9 -ext i (ells+., @N@, !alue ? Start!alue * A#s+count, End Su# :eturn to the Excel wor)sheet now and in 7esign 8ode dou#le clic) on the #utton over the (ells O2 to O4 to open the !$A Editor, and fro& the Properties $ox, on the 1+-a&e,2 line * change to 1c&d(lear$loc)2 Si&ilarly, on the 1Accelerator2 line type in 1(2 and on the 1(aption2 line type in 1(lear2 "ype in the following code Private Su# c&d(lear$loc)F(lic)+, :ange+@$loc)@, (lear End Su# "est the wor)sheet and save your wor) <hat happens if the line (ells+., @N@, !alue ? Start!alue * A#s+count, is &issing fro& the code in the Su#routine c&dFill M "est the file and save your wor) Also select File SaveAs Prog2e on your floppy dis) For the final exercise, using file Prog2e, create two &ore (o&&and$uttons and place the& ontop of (ells %/C to %/. and %/0 to %/3 -ext highlight the two (ells %/= to %/>, and with the &ouse pointer so&ewhere in this selected range clic) on the right &ouse #utton -ext, choose For&at (ellsQ Align&ent "a# fro& the drop down &enu, and chec) the 8erge cells #ox Also fro& the Align&ent "a# change #oth !ertical and 5oriIontal text #oxes to (enter Fro& the Font "a# change the SiIe to 2H points (lic) OO 7ou#le clic) on the #utton over the (ells %/C to %/. dou#le clic) on this #utton to open the !$A Editor, and fro& the Properties $ox, on the 1+-a&e,2 line * change to 1c&dJp2 Si&ilarly, on the 1Accelerator2 line type in 1J2 and on the 1(aption2 line type in 1Jp2 "ype in the following code Private Su# c&dJpF(lic)+, :ange+@%/=@, Activate Active(ell !alue ? Active(ell !alue ; / End Su#

// Si&ilarly, dou#le clic) on the #utton over the (ells %/= to %/> dou#le clic) on this #utton to open the !$A Editor, and fro& the Properties $ox, on the 1+-a&e,2 line * change to 1c&d7own2 Si&ilarly, on the 1Accelerator2 line type in 172 and on the 1(aption2 line type in 17own2 "ype in the following code Private Su# c&d7ownF(lic)+, :ange+@%/=@, Activate Active(ell !alue ? Active(ell !alue * / End Su# :eturn to the Excel wor)sheet and, once off 7esign 8ode, clic) on either #utton +or hold down the Alt #utton and press either 1J2 or 172, to see what happens 5ow would you &odify this code to change the incre&ental step to any other a&ount that can #e altered fro& within the Excel wor)sheetM Rour wor)sheet should rese&#le the picture shown #elow'

You might also like