You are on page 1of 477

Table of Content

1.1 Selecting Special Cells ..................................................................................... 1 1.2 Setting a Chart's Data Point Labels ................................................................ 2 1.3 Topics in Learning Excel Programming ......................................................... 4 Part I: The !" En#ironment ......................................................................................... 6 Chapter 2. Preliminaries.............................................................................................. 7 2.1 $hat Is a Programming Lang%age& ............................................................... 7 2.2 Programming St'le ............................................................................................ 8 Chapter 3. The is%al !asic E(itor) Part I.............................................................. 13 3.1 The Pro*ect $in(o+ ........................................................................................ 13 3.2 The Properties $in(o+ .................................................................................. 17 3.3 The Co(e $in(o+ ........................................................................................... 18 3., The Imme(iate $in(o+ .................................................................................. 20 3.- "rranging $in(o+s ......................................................................................... 21 Chapter ,. The is%al !asic E(itor) Part II ............................................................ 23 ,.1 .a#igating the IDE .......................................................................................... 23 ,.2 /etting 0elp...................................................................................................... 25 ,.3 Creating a Proce(%re ...................................................................................... 25 ,., 1%n Time) Design Time) an( !rea2 3o(e .................................................. 26 ,.- Errors ................................................................................................................. 27 ,.4 Deb%gging ........................................................................................................ 30 ,.5 3acros ............................................................................................................... 35 Part II: The !" Programming Lang%age.................................................................. 38 Chapter -. ariables) Data T'pes) an( Constants ............................................... 39 -.1 Comments......................................................................................................... 39 -.2 Line Contin%ation ............................................................................................. 39 -.3 Constants .......................................................................................................... 39 -., ariables an( Data T'pes .............................................................................. 42 -.- !" 6perators ................................................................................................. 57 Chapter 4. 7%nctions an( S%bro%tines ................................................................... 59 4.1 Calling 7%nctions ............................................................................................. 59 4.2 Calling S%bro%tines ......................................................................................... 60 4.3 Parameters an( "rg%ments ........................................................................... 61 4., Exiting a Proce(%re ......................................................................................... 65 4.- P%blic an( Pri#ate Proce(%res ...................................................................... 65 4.4 Pro*ect 1e8erences .......................................................................................... 65 Chapter 5. !%ilt9in 7%nctions an( Statements ....................................................... 67 5.1 The 3sg!ox 7%nction ..................................................................................... 68 5.2 The Inp%t!ox 7%nction.................................................................................... 69 5.3 !" String 7%nctions ...................................................................................... 70 5., 3iscellaneo%s 7%nctions an( Statements ................................................... 74 5.- 0an(ling Errors in Co(e ................................................................................. 77 Chapter :. Control Statements ................................................................................ 81 :.1 The I8...Then Statement .................................................................................. 81 :.2 The 7or Loop .................................................................................................... 81 :.3 The 7or Each Loop.......................................................................................... 83 :., The Do Loop ..................................................................................................... 84 :.- The Select Case Statement ........................................................................... 85 :.4 " 7inal .ote on !" ....................................................................................... 86 Part III: Excel "pplications an( the Excel 6b*ect 3o(el .......................................... 88 Chapter ;. 6b*ect 3o(els ......................................................................................... 89 ;.1 6b*ects) Properties) an( 3etho(s ................................................................ 89 ;.2 Collection 6b*ects............................................................................................ 90 ;.3 6b*ect 3o(el 0ierarchies ............................................................................... 92 ;., 6b*ect 3o(el S'ntax ....................................................................................... 93 ;.- 6b*ect ariables .............................................................................................. 94 Chapter 1<. Excel "pplications .............................................................................. 100 1<.1 Pro#i(ing "ccess to an "pplication's 7eat%res....................................... 100

1<.2 $here to Store an "pplication .................................................................. 103 1<.3 "n Example "((9In ..................................................................................... 110 Chapter 11. Excel E#ents ....................................................................................... 113 11.1 The EnableE#ents Propert' ....................................................................... 113 11.2 E#ents an( the Excel 6b*ect 3o(el ......................................................... 113 11.3 "ccessing an E#ent Proce(%re ................................................................. 113 11., $or2sheet E#ents ....................................................................................... 114 11.- $or2!oo2 E#ents ........................................................................................ 115 11.4 Chart E#ents ................................................................................................ 116 11.5 "pplication E#ents ....................................................................................... 116 11.: =%er'Table 1e8resh E#ents ...................................................................... 118 Chapter 12. C%stom 3en%s an( Toolbars ........................................................... 119 12.1 3en%s an( Toolbars: "n 6#er#ie+ .......................................................... 119 12.2 The Comman(!ars Collection .................................................................. 121 12.3 Creating a .e+ 3en% !ar or Toolbar ...................................................... 123 12., Comman(9!ar Controls.............................................................................. 124 12.- !%ilt9in Comman(9!ar9Control IDs ........................................................... 125 12.4 Example: Creating a 3en% ........................................................................ 128 12.5 Example: Creating a Toolbar ..................................................................... 129 12.: Example: "((ing an Item to an Existing 3en% ....................................... 131 12.; "%gmenting the S1>?tils "pplication ...................................................... 131 Chapter 13. !%ilt9In Dialog !oxes ......................................................................... 139 13.1 The Sho+ 3etho( ....................................................................................... 141 Chapter 1,. C%stom Dialog !oxes ........................................................................ 143 1,.1 $hat Is a ?ser7orm 6b*ect& ..................................................................... 143 1,.2 Creating a ?ser7orm 6b*ect...................................................................... 143 1,.3 "cti#e> Controls .......................................................................................... 144 1,., "((ing ?ser7orm Co(e.............................................................................. 145 1,.- Excel's Stan(ar( Controls .......................................................................... 146 1,.4 Example: The "cti#ateSheet ?tilit' .......................................................... 147 1,.5 "cti#e> Controls on $or2sheets .............................................................. 152 Chapter 1-. The Excel 6b*ect 3o(el .................................................................... 157 1-.1 " Perspecti#e on the Excel 6b*ect 3o(el ............................................... 157 1-.2 Excel En%ms ................................................................................................ 159 1-.3 The !" 6b*ect !ro+ser ........................................................................... 161 Chapter 14. The "pplication 6b*ect ...................................................................... 163 14.1 Properties an( 3etho(s o8 the "pplication 6b*ect ................................. 165 14.2 Chil(ren o8 the "pplication 6b*ect ............................................................ 189 Chapter 15. The $or2boo2 6b*ect ........................................................................ 194 15.1 The $or2boo2s Collection ......................................................................... 194 15.2 The $or2boo2 6b*ect ................................................................................. 199 15.3 Chil(ren o8 the $or2boo2 6b*ect .............................................................. 206 15., Example: Sorting Sheets in a $or2boo2 ................................................. 208 Chapter 1:. The $or2sheet 6b*ect ....................................................................... 211 1:.1 Properties an( 3etho(s o8 the $or2sheet 6b*ect ................................. 211 1:.2 Chil(ren o8 the $or2sheet 6b*ect ............................................................. 219 1:.3 Protection in Excel >P ................................................................................ 222 1:., Example: Printing Sheets ........................................................................... 224 Chapter 1;. The 1ange 6b*ect .............................................................................. 229 1;.1 The 1ange 6b*ect as a Collection ............................................................ 230 1;.2 De8ining a 1ange 6b*ect ............................................................................ 231 1;.3 "((itional 3embers o8 the 1ange 6b*ect................................................ 237 1;., Chil(ren o8 the 1ange 6b*ect .................................................................... 266 1;.- Example: /etting the ?se( 1ange ........................................................... 279 1;.4 Example: Selecting Special Cells ............................................................. 280 Chapter 2<. Pi#ot Tables ........................................................................................ 291 2<.1 Pi#ot Tables.................................................................................................. 291 2<.2 The Pi#otTable $i@ar( ............................................................................... 293 2<.3 The Pi#otTable$i@ar( 3etho( .................................................................. 296

2<., The Pi#otTable 6b*ect ................................................................................ 298 2<.- Properties an( 3etho(s o8 the Pi#otTable 6b*ect ................................. 303 2<.4 Chil(ren o8 the Pi#otTable 6b*ect ............................................................. 317 2<.5 The Pi#ot7iel( 6b*ect ................................................................................. 317 2<.: The Pi#otCache 6b*ect .............................................................................. 333 2<.; The Pi#otItem 6b*ect .................................................................................. 334 2<.1< Pi#otCell an( Pi#otItemList 6b*ects ....................................................... 338 2<.11 Calc%late( Items an( Calc%late( 7iel(s ................................................ 342 2<.12 Example: Printing Pi#ot Tables ............................................................... 345 Chapter 21. The Chart 6b*ect ................................................................................ 349 21.1 Chart 6b*ects an( Chart6b*ect 6b*ects .................................................. 349 21.2 Creating a Chart .......................................................................................... 350 21.3 Chart T'pes .................................................................................................. 356 21., Chil(ren o8 the Chart 6b*ect ...................................................................... 359 21.- The "xes Collection .................................................................................... 360 21.4 The "xis 6b*ect ........................................................................................... 363 21.5 The Chart"rea 6b*ect ................................................................................. 373 21.: The Chart/ro%p 6b*ect .............................................................................. 374

21.; The ChartTitle 6b*ect .................................................................................. 378 21.1< The DataTable 6b*ect .............................................................................. 378 21.11 The 7loor 6b*ect........................................................................................ 379 21.12 The Legen( 6b*ect ................................................................................... 379 21.13 The PageSet%p 6b*ect ............................................................................. 381 21.1, The Plot"rea 6b*ect ................................................................................. 381 21.1- The Series 6b*ect ..................................................................................... 382 21.14 Properties an( 3etho(s o8 the Chart 6b*ect ........................................ 388 21.15 Example: Scrolling Thro%gh Chart T'pes ............................................. 392 21.1: Example: Printing Embe((e( Charts ..................................................... 395 21.1; Example: Setting Data Series Labels .................................................... 399 Chapter 22. Smart Tags .......................................................................................... 407 22.1 $hat "re Smart Tags& ............................................................................... 407 22.2 SmartTag1ecogni@er 6b*ect ..................................................................... 408 22.3 SmartTag 6b*ect ......................................................................................... 408 22., SmartTag"ction 6b*ect .............................................................................. 409 22.- SmartTag6ptions 6b*ect............................................................................ 410 Part I : "ppen(ixes ..................................................................................................... 411 "ppen(ix ". The Shape 6b*ect.............................................................................. 412 ".1 $hat Is the Shape 6b*ect&.......................................................................... 412 ".2 A96r(er ........................................................................................................... 412 ".3 Creating Shapes............................................................................................ 413 "., Diagram) Diagram.o(e) an( Diagram.o(eChil(ren 6b*ects ............... 420 "ppen(ix !. /etting the Installe( Printers ........................................................... 423 "ppen(ix C. Comman( !ar Controls .................................................................... 426 C.1 !%ilt9in Comman(9!ar Controls .................................................................. 426 "ppen(ix D. 7ace IDs.............................................................................................. 444 "ppen(ix E. Programming Excel8rom "nother "pplication ............................... 450 E.1 Setting a 1e8erence to the Excel 6b*ect 3o(el ....................................... 450 E.2 /etting a 1e8erence to the Excel "pplication 6b*ect .............................. 450 "ppen(ix 7. 0igh9Le#el an( Lo+9Le#el Lang%ages ........................................... 454 7.1 !"SIC.............................................................................................................. 455 7.2 is%al !asic .................................................................................................... 456 7.3 C an( CBB ...................................................................................................... 457 7., is%al CBB ...................................................................................................... 458 7.- Pascal.............................................................................................................. 459 7.4 761T1". ...................................................................................................... 460 7.5 C6!6L............................................................................................................ 460 7.: LISP ................................................................................................................. 461 "ppen(ix /. .e+ 6b*ects in Excel >P ................................................................. 463 AllowEditRange Object .......................................................................................... 463 AutoRecove Object................................................................................................ 463 !alculated"e#be Object ...................................................................................... 464 !ell$o #at Object................................................................................................... 464 !u%to#& o'e t( Object........................................................................................... 465 )iag a#* )iag a#+ode and )iag a#+ode!,ild en Object% ................................ 465 E o Object ............................................................................................................ 466 E o !,ec-ingO'tion% Object ................................................................................ 468 . a',ic Object ........................................................................................................ 468 /R0)1e ve and /R0)2'dateEvent Object%.......................................................... 469

&ivot!ell and &ivot/te#3i%t Object% ...................................................................... 469 & otection Object .................................................................................................... 470 R0) Object ............................................................................................................. 470 1#a t0ag Related Object% ...................................................................................... 471 1'eec, Object ......................................................................................................... 471 1'ellingO'tion% Object ........................................................................................... 473 0ab Object............................................................................................................... 473 2%edObject% Object ................................................................................................ 473 2%e Acce%%3i%t and2%e Acce%% Object%................................................................. 474 4atc, Object .......................................................................................................... 474 Colophon ................................................................................................................... 476

vi

Preface
A% t,e title %ugge%t%* t,i% boo- i% 5o t,o%e w,o want to lea n ,ow to ' og a# "ic o%o5t E6cel 4e %,ould begin b( add e%%ing t,e 7ue%tion* 84,( would an(one want to ' og a# "ic o%o5t E6cel98 0,e an%we i% %i#'le: to get #o e 'owe out o5 t,i% 5o #idable a''lication. A% (ou will %ee* t,e e a e #an( t,ing% t,at (ou can do at t,e ' og a##ing level t,at (ou cannot do at t,e u%e ; inte 5ace level<t,at i%* wit, t,e #enu% and dialog bo6e% o5 E6cel. !,a'te 1 ' ovide% %o#e conc ete e6a#'le% o5 t,i%. 0,i% boo- ' ovide% an int oduction to ' og a##ing t,e E6cel object #odel u%ing =i%ual >a%ic 5o A''lication% ?=>A@. Aoweve * it i% not intended to be an enc(clo'edia o5 E6cel ' og a##ing. 0,e goal ,e e i% to ac7uaint (ou wit, t,e #ain 'oint% o5 E6cel ' og a##ing<enoug, %o t,at (ou can continue (ou education ?a% we all do@ on (ou own. 0,e goal i% t,at a5te eading t,i% boo(ou %,ould not need to el( on an( %ou ce ot,e t,an t,e E6cel =>A Ael' 5ile o a good E6cel =>A e5e ence boo- and a nice object b ow%e ?%uc, a% #( En,anced Object > ow%e * a cou'on 5o w,ic, i% included in t,e bac- o5 t,i% boo-@. /t ,a% been #( e6'e ience t,at int oducto ( ' og a##ing boo-% ?and* %adl(* #o%t t ade co#'ute boo-%@ tend to do a g eat deal o5 ,and,olding. 0,e( cove conce't% at a ve ( %low 'ace b( 'adding t,e# ,eavil( wit, ove blown e6a#'le% and i elevant anecdote% t,at onl( t,e aut,o could conceivabl( 5ind a#u%ing* #a-ing it di55icult to 5e et out t,e 5act%. $ an-l(* / 5ind %uc, un' o5e%%ionali%# inc edibl( in5u iating. /n #( o'inion* it doe% t,e eade a g eat di%%e vice to ta-e 'e ,a'% 400 'age% o5 in5o #ation and 'ad it wit, anot,e 600 'age% o5 jun-. 0,e e i% no doubt in #( #ind t,at we need #o e ' o5e%%ionali%# 5 o# ou aut,o %* but it i% not ea%( to 5ind w ite % w,o ,ave bot, t,e -nowledge to w ite about a %ubject and t,e t aining ?o talent@ to do %o in a 'edagogical #anne . ?/ %,ould ,a%ten to add t,at t,e e a e a nu#be o5 e6cellent aut,o % in t,i% a ea<itB% ju%t t,at t,e e a e not nea l( enoug, o5 t,e#.@ "o eove * 'ubli%,e % tend to encou age t,e c eation o5 1000;'lu% 'age to#e% becau%e o5 t,e gene al 5eeling a#ong t,e 'ubli%,e % t,at a boo- #u%t be ',(%icall( wide enoug, to %tand out on t,e boo-%,el5C / %,udde to t,in- t,at t,i% #ig,t* in 5act* be t ue. ?/ a# ,a''( to %a( t,at OBReill( ,a% not %uccu#bed to t,i% o'inion.@ >( cont a%t* Writing Excel Macros with VBA i% not a boo- in w,ic, (ou will 5ind #uc, ,and,olding ?no will (ou 5ind #uc, ,and,olding in an( o5 #( boo-%@. 0,e boo- ' oceed% at a elativel( a'id 'ace 5 o# a gene al int oduction to ' og a##ing t, oug, an e6a#ination o5 t,e =i%ual >a%ic 5o A''lication% ' og a##ing language to an ove view o5 t,e E6cel object #odel. .iven t,e eno #it( o5 t,e %ubject* not eve (t,ing i% cove ed* no %,ould it be. +eve t,ele%%* t,e e%%ential% o5 bot, t,e =>A language and t,e E6cel object #odel a e cove ed %o t,at* w,en (ou ,ave 5ini%,ed t,e boo-* (ou will -now enoug, about E6cel =>A to begin c eating e55ective wo -ing ' og a#%. / ,ave t ied to 'ut #( e6'e ience a% a ' o5e%%o ?about 20 (ea %@ and #( e6'e ience w iting boo-% ?about 30 o5 t,e#@ to wo - ,e e to c eate a t ue lea ning tool 5o #( eade %. Ao'e5ull(* t,i% i% a boo- t,at can be ead* 'e ,a'% #o e t,an once* and can al%o %e ve a% a u%e5ul e5e ence.

Preface to the Second Edition


viii

4it, t,e ecent elea%e o5 E6cel 10 ?al%o called E6cel D&@* it wa% nece%%a ( to u'date #( boo-. E6cel D& i% #o%tl( an evolutiona ( %te' 5o wa d 5 o# E6cel 2000* but doe% ,ave %o#e inte e%ting new 5eatu e% wo t, %'ecial attention* %uc, a% %u''o t 5o te6t;to;%'eed and %#a t tag%. 0,e E6cel object #odel ,a% 37 new object%* containing 266 new #e#be %. 0,e e a e al%o 180 new #e#be % o5 ' ee6i%ting object%. /n t,i% boo-* / cove #o%t o5 t,e cent al object%. $igu e &;1 %,ow% #o%t o5 t,e new object% in t,e E6cel D& object ,ie a c,( and w,e e t,e%e object% occu in t,e E6cel D& object #odel. ?0,i% 5igu e i% ta-en 5 o# #( ' og a# Object "odel > ow%e . $o #o e in5o #ation on t,i% ' og a#* 'lea%e vi%it #( web %ite at ,tt':EEwww. o#an' e%%.co#.@ Figure P-1. New objects in the Excel XP object hierarchy

i6

The Book's Audience


A% an int oduction to ' og a##ing in E6cel =>A* t,e boo- i% ' i#a il( add e%%ed to two g ou'% o5 eade %:
C

E6cel u%e % w,o a e not ' og a##e % but w,o would li-e to be. /5 (ou 5all into t,i% catego (* it i% ' obabl( becau%e (ou ,ave begun to a'' eciate t,e 'owe o5 E6cel and want to ta-e advantage o5 it% #o e advanced 5eatu e% o ju%t acco#'li%, ce tain ta%-% #o e ea%il(. E6cel u%e % w,o a e ' og a##e % ?in vi tuall( an( language<=i%ual >a%ic* =i%ual >a%ic 5o A''lication%* >A1/!* !* !FF* and %o on@ but w,o a e not 5a#ilia wit, t,e E6cel object #odel. /n t,i% ca%e* (ou can u%e Writing Excel Macros to b u%, u' on %o#e o5 t,e detail% o5 t,e =>A language and lea n about t,e E6cel object #odel and ,ow to ' og a# it.

Organization of This Book


Writing Excel Macros con%i%t% o5 21 c,a'te % t,at can in5o #all( be divided into 5ou 'a t% ?e6cluding t,e int oducto ( c,a'te @. /n addition* t,e e a e 5ive a''endi6e%. !,a'te 1 e6a#ine% w,( (ou #ig,t want to lea n ' og a##ing and ' ovide% a 5ew e6a#'le% o5 t,e -ind% o5 ' oble#% t,at can be%t be %olved t, oug, ' og a##ing. !,a'te 2 int oduce% ' og a##ing and t,e =i%ual >a%ic 5o A''lication% language. !,a'te 2 t, oug, !,a'te 4 5o # t,e 5i %t 'a t o5 t,e boo-. !,a'te 3 and !,a'te 4 e6a#ine t,e =i%ual >a%ic /nteg ated )evelo'#ent Envi on#ent ?/)E@* w,ic, i% t,e ' og a##ing envi on#ent u%ed to develo' E6cel =>A a''lication%. 0,e %econd 'a t o5 t,e boo- con%i%t% o5 !,a'te 5 t, oug, !,a'te 8* w,ic, 5o # an int oduction to t,e =>A language* t,e language co#'onent t,at i% co##on to "ic o%o5t =i%ual >a%ic and to #an( o5 "ic o%o5tB% #ajo a''lication%* including 4o d* E6cel* &owe &oint* and Acce%%* a% well a% to %o5twa e 5 o# %o#e ot,e 'ubli%,e %. /ndividual c,a'te % %u ve( =>AB% va iable%* data t('e%* and con%tant% ?!,a'te 5@* 5unction% and %ub outine% ?!,a'te 6@* int in%ic 5unction% and %tate#ent% ?!,a'te 7@* and cont ol %tate#ent% ?!,a'te 8@. 0,e t,i d 'a t o5 t,e boo- i% devoted to %o#e gene al to'ic% t,at a e needed to c eate u%able e6a#'le% o5 E6cel a''lication% and to t,e E6cel object #odel it%el5. 4e begin wit, a di%cu%%ion o5 object #odel% in gene al ?!,a'te 9@. 0,e %ucceeding c,a'te % di%cu%% w,at con%titute% an E6cel a''lication ?!,a'te 10@* E6cel event% ?!,a'te 11@* E6cel #enu% and toolba % ?!,a'te 12@* and E6cel dialog bo6e%* bot, built;in and cu%to# ?!,a'te 13 and !,a'te 14@. ?0,o%e w,o ,ave ead #( boo- Learning Word Programming #ig,t notice t,at t,e%e to'ic% ca#e at t,e end o5 t,at boo-. 4,ile / would ,ave ' e5e ed t,i% o ganiGation ,e e a% well* / could not con%t uct #eaning5ul E6cel e6a#'le% wit,out cove ing t,i% #ate ial before di%cu%%ing t,e E6cel object #odel.@ 0,e la%t c,a'te % o5 t,e boo- a e devoted to t,e E6cel object #odel it%el5. 0,i% #odel dete #ine% w,ic, ele#ent% o5 E6cel ?wo -boo-%* wo -%,eet%* c,a t%* cell%* and %o on@ a e acce%%ible t, oug, code and ,ow t,e( can be cont olled ' og a##aticall(. !,a'te 15 give% an ove view o5 t,e E6cel object #odel. 1ub%e7uent c,a'te % a e devoted to ta-ing a clo%e loo- at %o#e o5 t,e #ain object% in t,e E6cel object #odel* %uc, a% t,e A''lication object ?!,a'te 16@* w,ic, e' e%ent% t,e E6cel a''lication it%el5H t,e 4o -boo- object ?!,a'te 17@* w,ic, e' e%ent% an E6cel wo -boo-H t,e

4o -%,eet object ?!,a'te 18@* w,ic, e' e%ent% an E6cel wo -%,eetH t,e Range object ?!,a'te 19@* w,ic, e' e%ent a collection o5 cell% in a wo -boo-H t,e &ivot0able object ?!,a'te 20@H and t,e !,a t object ?!,a'te 21@. !,a'te 22 cove % 1#a t 0ag%. / ,ave t ied to include u%e5ul e6a#'le% at t,e end o5 #o%t o5 t,e%e c,a'te %. 0,e a''endi6e% ' ovide a dive %e collection o5 %u''le#enta ( #ate ial* including a di%cu%%ion o5 t,e 1,a'e object* w,ic, can be u%ed to add %o#e inte e%ting a two - to E6cel %,eet%* dete #ining w,at ' inte % a e available on a u%e B% %(%te# ?t,i% i% not 7uite a% ea%( a% (ou #ig,t t,in-@* and ,ow to ' og a# E6cel 5 o# ot,e a''lication% ?%uc, a% 4o d* Acce%%* o &owe &oint@. 0,e e i% al%o an a''endi6 containing a ve ( b ie5 ove view o5 ' og a##ing language% t,at i% de%igned to give (ou a 'e %'ective on w,e e =>A 5it% into t,e g eat %c,e#e o5 t,ing%.

The Book's Text and Sample Code


4,en eading t,i% boo-* (ou will encounte #an( %#all ' og a##ing e6a#'le% to illu%t ate t,e conce't%. / ' e5e to u%e %#all coding e6a#'le%* ,o'e5ull(* ju%t a 5ew line%* to illu%t ate a 'oint. &e %onall(* / %ee# to lea n #uc, #o e 7uic-l( and ea%il( b( tin-e ing wit, and t acing t, oug, %,o t ' og a# %eg#ent% t,an b( %tud(ing a long* detailed e6a#'le. 0,e di55icult( in tin-e ing wit, a long ' og a# i% t,at c,anging a 5ew line% can a55ect ot,e 'o tion% o5 t,e code* to t,e 'oint w,e e t,e ' og a# will no longe un. 0,en (ou ,ave to wa%te ti#e t (ing to 5igu e out w,( it wonBt un. / encou age (ou to 5ollow along wit, t,e code e6a#'le% b( t('ing t,e# in (ou %el5. ?+eve t,ele%%* i5 (ouBd at,e %ave (ou %el5 t,e t('ing* %a#'le ' og a#% a e available onlineH %ee 1ection &.7 late in t,i% & e5ace.@ Al%o* / encou age (ou to e6'e i#ent -- it i% de5initel( t,e be%t wa( to lea n. Aoweve * to ' otect (ou %el5* / strongly %ugge%t t,at (ou u%e a t, ow;awa( wo -boo- 5o (ou e6'e i#enting. One 5inal co##ent about t,e %a#'le code i% wo t, #a-ing* 'a ticula l( %ince t,i% boo- and it% coding e6a#'le% a e intended to teac, (ou ,ow to w ite =>A ' og a#% 5o "ic o%o5t E6cel. .ene all( %'ea-ing* t,e e i% %o#ew,at o5 a ,o %e;be5o e;t,e;ca t ' oble# in t (ing to w ite about a co#'licated object #odel* %ince it i% al#o%t i#'o%%ible to give e6a#'le% o5 one object and it% ' o'e tie% and #et,od% wit,out e5e ing to ot,e object% t,at #a( not (et ,ave been di%cu%%ed. $ an-l(* / donBt %ee an( wa( to avoid t,i% ' oble# co#'letel(* %o at,e t,an t ( to ea ange t,e #ate ial in an unnatu al wa(* it %ee#% bette to %i#'l( ' oceed in an o de l( 5a%,ion. Occa%ionall(* we will need to e5e to object% t,at we ,ave not (et di%cu%%ed* but t,i% %,ould not cau%e an( %e iou% ' oble#%* %ince #o%t o5 t,e%e 5o wa d e5e ence% a e 5ai l( obviou%.

About the Code


0,e code in t,i% boo- ,a% been ca e5ull( te%ted b( at lea%t t, ee individual%<#(%el5* #( edito Ron &et u%,a* and t,e tec,nical eviewe * "att !,ild%. /ndeed* / ,ave te%ted t,e code on #o e t,an one #ac,ine ?wit, di55e ent o'e ating %(%te#%@ and at #o e t,an one ti#e ?at lea%t du ing t,e w iting o5 t,e boo- and du ing t,e 5inal ' e'a ation 5o boo- ' oduction@. 2n5o tunatel(* all t, ee o5 u% ,ave un into %o#e deviation% 5 o# e6'ected be,avio ?t,at i%* t,e code doe%nBt %ee# to wo - a% adve ti%ed* o wo - at all@ a% well a% %o#e incon%i%tencie% in code

be,avio ?t,at i%* it wo -% di55e entl( on di55e ent %(%te#% o at di55e ent ti#e%@. /ndeed* t,e e ,ave been occa%ion% w,en one o5 u% did not get t,e %a#e e%ult% a% t,e ot,e % wit, t,e %a#e code and t,e %a#e data. "o eove * / ,ave 'e %onall( ,ad t ouble on occa%ion du'licating #( own e%ult% a5te a %igni5icant %'an o5 ti#eC / %u''o%e t,at t,i% %,ouldnBt be enti el( %u ' i%ing con%ide ing t,e co#'le6it( o5 a ' og a# li-e E6cel and t,e 5allibilit( o5 u% all* but t,e nu#be o5 %uc, 'eccadilloe% ,a% ' o#'ted #e to add t,i% caveat. O55,and* / can t,in- o5 two ea%on% 5o t,i% be,avio <w,et,e it be eal o ju%t a''a ent<neit,e o5 w,ic, i% b( an( #ean% an e6cu%e:
C

0,e %tate o5 docu#entation being w,at it i%* t,e e #a( be additional unmentioned e7ui e#ent% o e%t iction% 5o %o#e code to wo - ' o'e l(* o even at all. A% an e6a#'le* now,e e in t,e va%t docu#entation<at lea%t t,at / could 5ind<doe% it %a( t,at we cannot u%e t,e Aa%A6i% #et,od to 'ut an a6i% on a c,a t be5o e we ,ave %et t,e location o5 t,e data 5o t,at a6i%C ?0,i% %ee#% to #e to be 'utting t,e ca t be5o e t,e ,o %e* but t,at i% not t,e i%%ue.@ /5 we t ( to do %o* t,e e%ulting e o #e%%age %i#'l( %a(% 8"et,od BAa%A6i%B o5 object BI!,a tB ,a% 5ailed.8 0,i% i% not #uc, ,el' in 'in'ointing t,e ' oble#. O5 cou %e* wit,out being ' iv( to t,i% -ind o5 in5o #ation 5 o# t,e %ou ce* we #u%t e%o t to e6'e i#entation and gue%%wo -. /5 t,i% doe% not eveal t,e %ituation* it will appear t,at t,e code %i#'l( doe% not wo -. !o#'ute % a e not %tatic. 4,eneve we in%tall a new a''lication* w,et,e it be elated to E6cel o not* t,e e i% a c,ance t,at a )33 o ot,e %(%te# 5ile will be e'laced b( a newe 5ile. 1adl(* newe 5ile% a e not alwa(% bette . 0,i% could be t,e cau%e* but ce tainl( not t,e e6cu%e* 5o incon%i%tent be,avio ove ti#e.

0,e ea%on t,at / a# b inging t,i% u' i% to let (ou -now t,at (ou #a( un into %o#e incon%i%tencie% o deviation% 5 o# e6'ected be,avio a% well. / ,ave t ied to 'oint out %o#e o5 t,e%e ' oble#% w,en t,e( occu * but (ou #a( encounte ot,e %. O5 cou %e* one o5 ou bigge%t c,allenge% ?(ou % and #ine@ i% to dete #ine w,et,e it i% we w,o a e #a-ing t,e #i%ta-e and not t,e ' og a#. / will ,a%ten to add t,at w,en / encounte a ' oble# wit, code be,avio * / a# u%uall( ?but not alwa(%@ t,e one w,o i% at 5ault. /n 5act* %o#eti#e% / #u%t e#ind #(%el5 o5 #( %tudent%* w,o con%tantl( %a( to #e* 80,e e i% an e o in t,e an%we % in t,e bac- o5 t,e te6tboo-.8 / ,ave lea ned ove 20 (ea % o5 teac,ing t,at 99J o5 t,e ti#e ?but not 100J o5 t,e ti#e@* t,e e o i% not in t,e boo-C 4ould t,at t,e %o5twa e indu%t ( ,ad t,i% good a eco dC / ,o'e (ou enjo( t,i% boo-. &lea%e 5eel 5 ee to c,ec- out #( web %ite at ,tt':EEwww. o#an' e%%.co#.

Con entions in this Book


0, oug,out t,i% boo-* we ,ave u%ed t,e 5ollowing t('og a',ic convention%: Constant width indicate% a language con%t uct %uc, a% a language %tate#ent* a con%tant* o an e6' e%%ion. 3ine% o5 code al%o a''ea in con%tant widt,* a% do 5unction% and #et,od ' otot('e%. talic

e' e%ent% int in%ic and a''lication;de5ined 5unction%* t,e na#e% o5 %(%te# ele#ent% %uc, a% di ecto ie% and 5ile%* and /nte net e%ou ce% %uc, a% web docu#ent% and e#ail add e%%e%. +ew te #% a e al%o italiciGed w,en t,e( a e 5i %t int oduced.
Constant width italic

in ' otot('e% o co##and %(nta6 indicate% e'laceable 'a a#ete na#e%* and in bod( te6t indicate% va iable and 'a a#ete na#e%.

Obtaining the Sample Programs


0,e %a#'le ' og a#% ' e%ented in t,e boo- a e available online 5 o# t,e /nte net and can be 5 eel( downloaded 5 o# ou web %ite at ,tt':EEwww.o eill(.co#EcatalogEe6l#ac o2.

!o" to Contact #s
4e ,ave te%ted and ve i5ied all t,e in5o #ation in t,i% boo- to t,e be%t o5 ou abilit(* but (ou #a( 5ind t,at 5eatu e% ,ave c,anged ?o even t,at we ,ave #ade #i%ta-e%C@. &lea%e let u% -now about an( e o % (ou 5ind* a% well a% (ou %ugge%tion% 5o 5utu e edition%* b( w iting to: OBReill( K A%%ociate% 1005 . aven%tein Aig,wa( +o t, 1eba%to'ol* !A 95472 ?800@ 998;9938 ?in t,e 2.1. o !anada@ ?707@ 829;0515 ?inte nationalElocal@ ?707@ 829;0104 ?5a6@ 0,e e i% a web 'age 5o t,i% boo-* w,e e we li%t an( e ata* e6a#'le%* and additional in5o #ation. Lou can acce%% t,i% 'age at: ,tt':EEwww.o eill(.co#EcatalogEe6l#ac o2 0o a%- tec,nical 7ue%tion% o co##ent on t,e boo-* %end e#ail to: boo-7ue%tion%Mo eill(.co# $o #o e in5o #ation about ou boo-%* con5e ence%* %o5twa e* Re%ou ce !ente %* and t,e OBReill( +etwo -* %ee ou web %ite at: ,tt':EEwww.o eill(.co#

Ackno"ledgments
/ would li-e to e6' e%% #( %ince e%t t,an-% to Ron &et u%,a* #( edito at OBReill(. A% wit, #( ot,e boo-%* Ron ,a% been o5 con%ide able ,el'. Ae i% one o5 t,e be%t edito % t,at / ,ave wo -ed wit, ove t,e la%t 17 (ea % o5 boo- w iting. Al%o* / would li-e to t,an- "att !,ild% 5o doing an all;i#'o tant tec,nical eview o5 t,e boo-.

Chapter $% &ntroduction
"ic o%o5t E6cel i% an a''lication o5 eno #ou% 'owe and 5le6ibilit(. >ut de%'ite it% 'owe 5ul 5eatu e %et* t,e e i% a g eat deal t,at E6cel eit,e doe% not allow (ou to do o doe% not allow (ou to do ea%il( t, oug, it% u%e inte 5ace. /n t,e%e ca%e%* we #u%t tu n to E6cel ' og a##ing.

3et #e give (ou two e6a#'le% t,at ,ave co#e u' in #( con%ulting ' actice.

$%$ Selecting Special Cells


0,e E6cel u%e inte 5ace doe% not ,ave a built;in #et,od 5o %electing wo -%,eet cell% ba%ed on va iou% c ite ia. $o in%tance* t,e e i% no wa( to %elect all cell% w,o%e value i% between 0 and 100 o all cell% t,at contain a date late t,an Nanua ( 1* 1998. 0,e e i% al%o no wa( to %elect onl( t,o%e cell% in a given colu#n t,at a e di55e ent 5 o# t,ei i##ediate ' edece%%o %. 0,i% can be ve ( u%e5ul w,en (ou ,ave a %o ted colu#n and want to e6t act a %et o5 uni7ue value%* a% %,own in $igu e 1;1. Figure 1-1. Selecting unique values

/ ,ave been a%-ed #an( ti#e% b( client% i5 E6cel ' ovide% a wa( to #a-e %uc, %election%. A5te a 5ew %uc, 7ue%tion%* / decided to w ite an E6cel utilit( 5o t,i% 'u 'o%e. 0,e dialog 5o t,i% utilit( i% %,own in $igu e 1;2. 4it, t,i% utilit(* t,e u%e can %elect a #atc, t('e ?%uc, a% nu#be * date* o te6t@ and a #atc, c ite ion. /5 e7ui ed* t,e u%e %u''lie% one o two value% 5o t,e #atc,. 0,i% ,a% ' oven to be an e6t e#el( u%e5ul utilit(. Figure 1-2. he Select S!ecial utility

/n t,i% boo-* we will develo' a %i#'le ve %ion o5 t,i% utilit(* w,o%e dialog i% %,own in $igu e 1;3. 0,i% boo- will al%o %u''l( (ou wit, t,e nece%%a ( -nowledge to en,ance t,i% utilit( to %o#et,ing %i#ila to t,e utilit( %,own in $igu e 1;2. Figure 1-". Select S!ecial #ialog

$%' Setting a Chart's (ata Point )abels


A% (ou #a( -now* data label% can be edited individuall( b( clic-ing twice ?'au%ing in between clic-%@ on a data label. 0,i% 'lace% t,e label in edit #ode* a% %,own in $igu e 1;4. Once in edit #ode* we can c,ange t,e te6t o5 a data label ?w,ic, b ea-% an( lin-%@ o %et a new lin- to a wo -%,eet cell. Acco#'li%,ing t,e %a#e t,ing ' og a##aticall( i% al%o ve ( ea%(. $o in%tance* t,e code:

ActiveChart.SeriesCollection(1).DataLabels(2).Text = "=M ChartSheet!"12C2"

%et% t,e data label 5o t,e %econd data 'oint to t,e value o5 cell >12. +ote t,at t,e 5o #ula #u%t be in R1!1 notation. ?4e will e6'lain t,e code in !,a'te 21* %o donBt wo ( about t,e detail% now.@ Figure 1-$. % #ata label in e#it &o#e

2n5o tunatel(* ,oweve * E6cel doe% not ' ovide a %i#'le wa( to lin- all o5 t,e data label% 5o a data %e ie% wit, a wo -%,eet ange* be(ond doing t,i% one data label at a ti#e. /n !,a'te 21* we will c eate %uc, a utilit(* t,e dialog 5o w,ic, i% %,own in $igu e 1;5. 0,i% dialog ' ovide% a li%t o5 all t,e data %e ie% 5o t,e %elected c,a t. 0,e u%e can %elect a data %e ie% and t,en de5ine a ange to w,ic, t,e data label% will be lin-ed o 5 o# w,ic, t,e value% will be co'ied. /5 t,e cell value% a e co'ied* no lin- i% e%tabli%,ed* and %o c,ange% #ade to t,e ange a e not e5lected in t,e c,a t. 0,e e i% al%o an o'tion to cont ol w,et,e 5o #atting i% lin-ed o co'ied. Figure 1-'. Set (ata )abels #ialog

/ ,o'e t,at t,e%e illu%t ation% ,ave convinced (ou t,at E6cel ' og a##ing can at ti#e% be ve ( u%e5ul. O5 cou %e* (ou can do #uc, #o e #undane t,ing% wit, E6cel ' og a#%* %uc, a% auto#ating t,e ' inting o5 c,a t%* %o ting wo -%,eet% al',abeticall(* and %o on.

$%* Topics in )earning Excel Programming


/n gene al* t,e education o5 an E6cel ' og a##e b ea-% down into a 5ew #ain catego ie%* a% 5ollow%. !he Visual Basic Editor $i %t* (ou need to lea n a bit about t,e envi on#ent in w,ic, E6cel ' og a##ing i% done. 0,i% i% t,e %o;called Visual Basic Editor o E6cel =>A ntegrated "e#elopment En#ironment ?/)E 5o %,o t@. 4e ta-e ca e o5 t,i% in !,a'te 3 and !,a'te 4. !he Basics of Programming in VBA +e6t* (ou need to lea n a bit about t,e ba%ic% o5 t,e ' og a##ing language t,at E6cel u%e%. 0,i% language i% called Visual Basic for Applications ?=>A@. Actuall(* =>A i% u%ed not onl( b( "ic o%o5t E6cel* but al%o b( t,e ot,e #ajo co#'onent% in t,e "ic o%o5t O55ice a''lication %uite: Acce%%* 4o d* and &owe &oint. An( a''lication t,at u%e% =>A in t,i% wa( i% called a host application 5o =>A. ?0,e e a e al%o a nu#be o5 non; "ic o%o5t ' oduct% t,at u%e =>A a% t,ei unde l(ing ' og a##ing language. A#ong t,e #o%t notable i% =i%io* a vecto ;ba%ed d awing ' og a#.@ /t i% al%o u%ed b( t,e %tandalone ' og a##ing envi on#ent called =i%ual >a%ic ?=>@. 4e will di%cu%% t,e ba%ic% o5 t,e =>A ' og a##ing language in !,a'te 5 t, oug, !,a'te 8. $b%ect Models and the Excel $b%ect Model Eac, =>A ,o%t a''lication ?4o d* Acce%%* E6cel* &owe &oint* =i%ual >a%ic@ %u''le#ent% t,e ba%ic =>A language b( ' oviding an ob%ect model to deal wit, t,e object% t,at a e 'a ticula to t,at a''lication. $o in%tance* E6cel =>A include% t,e Excel ob%ect model* w,ic, deal% wit, %uc, object% a% wo -boo-%* wo -%,eet%* cell%* ow%* colu#n%* ange%* c,a t%* 'ivot table%* and %o on. On t,e ot,e ,and* t,e Word ob%ect model deal% wit, %uc, object% a% docu#ent%* te#'late%* 'a ag a',%* 5ont%* ,eade %* table%* and %o on. Acce%% =>A include% two object #odel%* t,e Access ob%ect model and t,e "A$ ob%ect model* t,at allow t,e ' og a##e to deal wit, %uc, object% a% databa%e table%* 7ue ie%* 5o #%* and e'o t%. ?0o lea n #o e about t,e 4o d* Acce%%* and )AO object #odel%* %ee #( boo-% Learning Word Programming and Access "atabase "esign and Programming* al%o 'ubli%,ed b( OBReill(.@ 0,u%* an E6cel ' og a##e #u%t be 5a#ilia wit, t,e gene al notion o5 an object #odel and wit, t,e E6cel object #odel in 'a ticula . 4e di%cu%% object #odel% in gene al in !,a'te 9* and ou di%cu%%ion o5 t,e E6cel object #odel ta-e% u' #o%t o5 t,e e#ainde o5 t,e boo-. /ncidentall(* t,e E6cel object #odel i% 7uite e6ten%ive<a clo%e %econd to t,e 4o d object #odel in %iGe and co#'le6it(* wit, al#o%t 200 di55e ent object%. 3e%t (ou be too di%cou aged b( t,e %iGe o5 t,e E6cel object #odel* / %,ould 'oint out t,at (ou onl( need to be 5a#ilia wit, a ,and5ul o5 object% to ' og a# #eaning5ull( in E6cel =>A. /n 5act* a% we will %ee* t,e va%t #ajo it( o5 t,e 8action8 i% elated to ju%t %even object%: A''lication* Range* 4o -%,eet$unction* 4o -boo-* 4o -%,eet* &ivot0able* and !,a t.

0o ,el' (ou get an ove all two;di#en%ional 'ictu e o5 t,e E6cel object #odel* a% well a% detailed local view%* / ,ave w itten %'ecial object b ow%e %o5twa e. ?0,e object b ow%e co#e% wit, ove a doGen ot,e object #odel% a% well.@ $o #o e in5o #ation* 'lea%e vi%it ,tt':EEwww. o#an' e%%.co#. 4,et,e (ou a e inte e%ted in E6cel ' og a##ing to be #o e e55icient in (ou own wo - o to #a-e #one( w iting E6cel ' og a#% 5o ot,e % to u%e* / t,in- (ou will enjo( t,e inc ea%ed %en%e o5 'owe t,at (ou get b( -nowing ,ow to #ani'ulate E6cel at t,e ' og a##ing level. And becau%e E6cel ' og a##ing involve% acce%%ing t,e E6cel object #odel b( u%ing t,e =i%ual >a%ic 5o A''lication% ' og a##ing language<t,e %a#e ' og a##ing language u%ed in "ic o%o5t 4o d* Acce%%* and &owe &oint<a5te eading t,i% boo-* (ou will be ,al5;wa( to being a 4o d* Acce%%* and &owe &oint ' og a##e a% wellC

Part &+ The ,BA En ironment


!,a'te 2 !,a'te 3 !,a'te 4

Chapter '% Preliminaries


4e begin wit, %o#e gene al 5act% elated to ' og a##ing and ' og a##ing language% t,at will ,el' to give t,e #ain %ubject #atte o5 t,i% boo- %o#e 'e %'ective. A5te all* =>A i% ju%t one o5 #an( ' og a##ing language%* and an(one w,o want% to be a =>A ' og a##e %,ould ,ave %o#e 'e %'ective on w,e e =>A 5it% into t,e g eate %c,e#e o5 t,ing%. Re%t a%%u ed* ,oweve * t,at we will not dwell on %ide i%%ue%. 0,e 'u 'o%e o5 t,i% c,a'te i% to give a ve ( b ie5 ove view o5 ' og a##ing and ' og a##ing language% t,at will be o5 inte e%t to eade % w,o ,ave not ,ad an( ' og a##ing e6'e ience* a% well a% to t,o%e w,o ,ave.

'%$ -hat &s a Programming )anguage.


1i#'l( 'ut* a ' og a##ing language i% a ve ( %'ecial and ve ( e%t icted language t,at i% unde %tood b( t,e co#'ute at %o#e level. 4e can oug,l( divide ' og a##ing language% into t, ee g ou'%* ba%ed on t,e 'u 'o%e o5 t,e language:
C C C

3anguage% de%igned to #ani'ulate t,e co#'ute at a low level* t,at i%* to #ani'ulate t,e o'e ating %(%te# ?4indow% o )O1@ o even t,e ,a dwa e it%el5* a e called low-le#el languages. An e6a#'le i% a%%e#bl( language. 3anguage% de%igned to c eate %tandalone a''lication%* %uc, a% "ic o%o5t E6cel* a e highle#el languages. E6a#'le% a e >A1/!* !O>O3* $OR0RA+* &a%cal* !* !FF* and =i%ual >a%ic. 3anguage% t,at a e de%igned to #ani'ulate an a''lication ' og a#* %uc, a% "ic o%o5t E6cel* a e application-le#el language%. E6a#'le% a e E6cel =>A* 4o d =>A* and &owe &oint =>A.

0,o%e te #% a e not %et in conc ete and #a( be u%ed di55e entl( b( ot,e %. Aoweve * no one would di%ag ee t,at %o#e language% a e intended to be u%ed at a lowe level t,an ot,e %. 0,e co#'ute wo ld i% 5ull o5 ' og a##ing language%<,und ed% o5 t,e#. /n %o#e ca%e%* language% a e develo'ed 5o %'eci5ic co#'ute %. /n ot,e ca%e%* language% a e develo'ed 5o %'eci5ic t('e% o5 a''lication%. 0able 2;1 give% %o#e e6a#'le% o5 ' og a##ing language% and t,ei gene al 'u 'o%e%. able 2-1. So&e Progra&&ing )anguages Language General Purpose A3.O3 An atte#'t to de%ign a unive %al language >A1/! A %i#'le* ea%(;to;lea n language de%igned 5o beginne % !* !FF A ve ( 'owe 5ul language% wit, e6cellent %'eed and cont ol ove t,e co#'ute !O>O3 A language 5o bu%ine%% ' og a##ing $OR0RA+ A language 5o %cienti5ic ' og a##ing and nu#be c unc,ing 3i%' A language 5o li%t ' oce%%ing ?u%ed in a ti5icial intelligence@ &a%cal A language to teac, %tudent% ,ow to ' og a# 8co ectl(8 1/"23A A language 5o %i#ulating ?o #odeling@ ',(%ical ',eno#ena 1#alltalA language 5o object;o iented ' og a##ing =i%ual >a%ic A ve %ion o5 >A1/! de%igned 5o c eating 4indow% a''lication% =i%ual !FF A ve %ion o5 !FF de%igned 5o c eating 4indow% a''lication%

Team79 7l'D

& og a##ing language% va ( 7uite a bit in t,ei %(nta6. 1o#e language% a e #uc, ea%ie to ead t,an ot,e % ?a% a e %'o-en language%@. A% a ve ( %i#'le e6a#'le* 0able 2;2 %,ow% %o#e wa(% t,at di55e ent ' og a##ing language% a%%ign a value ?in t,i% ca%e* 5@ to a va iable na#ed D. +otice t,e va iation even in t,i% %i#'le ta%-. able 2-2. %ssign&ent in *arious )anguages Assignment Statement # $% & L'T # = & or # = & & %( # # = &) M*+' & T* # # = & # =. & (S'T, # &) # -= & # = &

Language A&3 >A1/! >E0A !* !FF !O>O3 $OR0RA+ N 3/1& &a%cal =i%ual >a%ic

/5 (ouB e inte e%ted in ,ow =i%ual >a%ic co#'a e% wit, %o#e o5 t,e ot,e #ajo ' og a##ing language%* A''endi6 $ contain% a %,o t de%c i'tion o5 %eve al language%* along wit, %o#e ' og a##ing e6a#'le%.

'%' Programming St/le


0,e i%%ue o5 w,at con%titute% good ' og a##ing %t(le i%* o5 cou %e* %ubjective* ju%t a% i% t,e i%%ue o5 w,at con%titute% good w iting %t(le. & obabl( t,e be%t wa( to lea n good ' og a##ing %t(le i% to lea n b( e6a#'le and to alwa(% -ee' t,e i%%ue %o#ew,e e in t,e 5 ont o5 (ou #ind w,ile ' og a##ing. 0,i% i% not t,e 'lace to ente into a detailed di%cu%%ion o5 ' og a##ing %t(le. Aoweve * in #( o'inion* t,e two #o%t i#'o tant #a6i#% 5o good ' og a##ing a e:
C C

4,en in doubt* 5avo eadabilit( ove cleve ne%% o elegance. $ill (ou ' og a#% wit, lot% o5 meaningful co##ent%.

2.2.1 +o&&ents
3et u% ta-e t,e %econd 'oint 5i %t. /t i% not 'o%%ible to ove e%ti#ate t,e i#'o tance o5 adding #eaning5ul co##ent% to (ou ' og a#%<at lea%t an( ' og a# wit, #o e t,an a 5ew line%. 0,e ' oble# i% t,i%: good ' og a#% a e gene all( u%ed #an( ti#e% du ing a ea%onabl( long li5eti#e* w,ic, #a( be #ea%u ed in #ont,% o even (ea %. /nevitabl(* a ' og a##e will want to etu n to ,i% o ,e code to #a-e c,ange% ?%uc, a% adding additional 5eatu e%@ o to 5i6 bug%. Aoweve * de%'ite all e55o t%* ' og a##ing language% a e not a% ea%( to ead a% %'o-en language%. /t i% ju%t inevitable t,at a ' og a##e will not unde %tand ?o 'e ,a'% not even ecogniGeC@ code t,at wa% w itten %eve al #ont,% o (ea % ea lie * and #u%t el( on ca e5ull( w itten co##ent% to ,el' eac7uaint ,i#%el5 wit, t,e code. ?0,i% ,a% ,a''ened to #e #o e ti#e% t,at / would ca e to ecall.@

3et #e e#',a%iGe t,at co##enting code i% al#o%t a% #uc, o5 an a t a% w iting t,e code it%el5. / ,ave o5ten %een co##ent% %i#ila to t,e 5ollowing:
. Set x e/0al to & x = &

0,i% co##ent i% ' ett( u%ele%%* %ince t,e actual code i% %el5;e6'lanato (. /t %i#'l( wa%te% ti#e and %'ace. ?/n a teac,ing tool* %uc, a% t,i% boo-* (ou #a( 5ind %o#e co##ent% t,at would ot,e wi%e be le5t out o5 a ' o5e%%ionall( w itten ' og a#.@ A good te%t o5 t,e 7ualit( o5 (ou co##ent% i% to ead ju%t t,e co##ent% ?not t,e code@ to %ee i5 (ou get a good %en%e not onl( o5 w,at t,e ' og a# i% de%igned to do* but al%o o5 t,e %te'% t,at a e u%ed to acco#'li%, t,e ' og a#B% goal. $o e6a#'le* ,e e a e t,e co##ent% 5 o# a %,o t >A1/! ' og a# t,at a''ea % in A''endi6 $:
. 1AS2C 3ro4ra5 to co530te the avera4e . o6 a set o6 at 5ost 177 n05bers . As8 6or the n05ber o6 n05bers . 26 905 is between 1 and 177 then 3roceed . Loo3 to collect the n05bers to avera4e . As8 6or next n05ber . Add the n05ber to the r0nnin4 s05 . Co530te the avera4e . Dis3la the avera4e

2.2.2 ,ea#ability
Readabilit( i% al%o a %ubjective #atte . 4,at i% eadable to one 'e %on #a( not be eadable to anot,e . /n 5act* it i% ' obabl( 5ai to %a( t,at w,at i% eadable to t,e aut,o o5 a ' og a# i% li-el( to be le%% eadable to e#eryone else* at lea%t to %o#e deg ee. /t i% wi%e to -ee' t,i% in #ind w,en (ou %ta t ' og a##ing ?t,at i%* a%%u#ing (ou want ot,e % to be able to ead (ou ' og a#%@. One o5 t,e g eate%t o55ende % to code eadabilit( i% t,e in5a#ou% :*T* %tate#ent* o5 w,ic, #an( language% ?including =>A@ ,ave %o#e va iet( o ot,e . /t i% not #( intention to dwell u'on t,e :*T* %tate#ent* but it will ,el' illu%t ate t,e i%%ue o5 good ' og a##ing %t(le. 0,e :*T* %tate#ent i% ve ( %i#'le<it ju%t edi ect% ' og a# e6ecution to anot,e location. $o in%tance* t,e 5ollowing >A1/! code a%-% t,e u%e 5o a 'o%itive nu#be . /5 t,e u%e ente % a non'o%itive nu#be * t,e :*T* 'o tion o5 t,e code edi ect% e6ecution to t,e 5i %t line o5 t,e ' og a# ?t,e label Tr A4ain@. 0,i% cau%e% t,e enti e ' og a# to be e6ecuted again. /n %,o t* t,e ' og a# will e'eat until t,e u%e ente % a 'o%itive nu#be :
Tr A4ain29;<T "'nter a 3ositive n05ber- "= x 2> x $= 7 T?'9 :*T* Tr A4ain

4,ile t,e ' eviou% e6a#'le #a( not be good ' og a##ing %t(le* it i% at lea%t eadable. Aoweve * t,e 5ollowing code i% #uc, #o e di55icult to ead:
Tr A4ain29;<T "'nter a n05ber between 1 and 177- "= x 2> x ( 177 T?'9 :*T* TooLar4e 2> x $= 7 T?'9 :*T* TooS5all ;"29T "@o0r n05ber is- "= x

:*T* Done TooLar4e;"29T "@o0r n05ber is too lar4e" :*T* Tr A4ain TooS5all;"29T "@o0r n05ber is too s5all" :*T* Tr A4ain Done'9D

>ecau%e we need to ju#' a ound in t,e ' og a# in o de to 5ollow t,e 'o%%ible 5low% o5 e6ecution* t,i% t('e o5 ' og a##ing i% %o#eti#e% e5e ed to a% spaghetti code. /#agine t,i% %t(le o5 ' og a##ing in a ' og a# t,at wa% t,ou%and% o5 line% longC 0,e 5ollowing ve %ion i% #uc, #o e eadable* alt,oug, it i% %till not t,e be%t 'o%%ible %t(le:
Tr A4ain29;<T "'nter a n05ber between 1 and 177- "= x 2> x ( 177 T?'9 ;"29T "@o0r n05ber is too lar4e" :*T* Tr A4ain 'LS'2> x $= 7 T?'9 ;"29T "@o0r n05ber is too s5all" :*T* Tr A4ain '9D 2> ;"29T "@o0r n05ber is- "= x '9D

0,e 5ollowing code doe% t,e %a#e job* but avoid% t,e u%e o5 t,e :*T* %tate#ent altoget,e * and would no doubt be con%ide ed bette ' og a##ing %t(le b( #o%t ' og a##e %:
D* 29;<T "'nter a n05ber between 1 and 177- "= x 2> x ( 177 T?'9 ;"29T "@o0r n05ber is too lar4e" 'LS'2> x $= 7 T?'9 ;"29T "@o0r n05ber is too s5all" '9D 2> L**; <9T2L x (= 1 A9D x $= 177 ;"29T "@o0r n05ber is- "= x '9D

Readabilit( can al%o %u55e at t,e ,and% o5 ' og a##e % w,o li-e to t,in- t,at t,ei code i% e%'eciall( cleve o elegant but* in ealit(* ju%t tu n% out to be ,a d to ead and e o ;' one. 0,i% i% e%'eciall( ea%( to do w,en ' og a##ing in t,e ! language. $o in%tance* a% a ve ( %i#'le e6a#'le* con%ide t,e 5ollowing t, ee line% in !:
x = x A 1) x = x A i) i = i % 1)

0,e 5i %t line add% 1 to x * t,e %econd line add% i to x , and t,e t,i d line %ubt act% 1 5 o# i. 0,i% code i% ce tainl( eadable ?i5 not te ibl( #eaning5ul@. Aoweve * it can al%o be w itten a%:
x = AAxAi%%)

0,i% #a( be %o#e ' og a##e B% idea o5 cleve ' og a##ing* but to #e it i% ju%t obno6iou%. 0,i% i% w,( a %agaciou% ' og a##e alwa(% 5avo % eadabilit( ove cleve ne%% o elegance.

10

2.2." -o#ularity
Anot,e #ajo i%%ue t,at elate% to eadabilit( i% t,at o5 modular programming. /n t,e ea l( da(% o5 &! ' og a##ing ?in >A1/!@* #o%t ' og a#% we e w itten a% a %ingle code unit* %o#eti#e% wit, #an( ,und ed% o even t,ou%and% o5 line% o5 code. /t i% not ea%( to 5ollow %uc, a ' og a#* e%'eciall( %i6 #ont,% a5te it wa% w itten. Al%o* t,e%e ' og a#% tended to contain t,e %a#e code %eg#ent% ove and ove * w,ic, i% a wa%te o5 ti#e and %'ace. 0,e 5ollowing >A1/! e6a#'le will illu%t ate t,e 'oint. 3ine nu#be % ,ave been added 5o e5e ence. ?)onBt wo ( too #uc, about 5ollowing eac, line o5 code. Lou can %till 5ollow t,e di%cu%%ion in an( ca%e.@
17 27 B7 40 50 60 #0 D7 E7 177 110 1%0 1&0 140 1&7 1F7 1'0 1(0 %00 %10 227 . ;ro4ra5 to reverse the letters in o0r na5e

. Do 6irst na5e 29;<T "'nter o0r 6irst na5e- "= na5eC reverse$ = "" FOR i = LEN(name$) TO 1 STEP -1 reverse$ = reverse$ + M !$(name$" i" 1) NE$T i ;"29T ">irst na5e reversed- " A reverseC . Do 5iddle na5e 29;<T "'nter o0r 5iddle na5e- "= na5eC reverse$ = "" FOR i = LEN(name$) TO 1 STEP -1 reverse$ = reverse$ + M !$(name$" i" 1) NE$T i ;"29T "Middle na5e reversed- " A reverseC . Do last na5e 29;<T "'nter o0r last na5e- "= na5eC reverse$ = "" FOR i = LEN(name$) TO 1 STEP -1 reverse$ = reverse$ + M !$(name$" i" 1) NE$T i ;"29T "Last na5e reversed- " A reverseC

+ow* ob%e ve t,at line% 40;70* 110;140* and 180;210 ?in bold@ a e identical. 0,i% i% a wa%te o5 %'ace. A bette a'' oac, would be to %e'a ate t,e code t,at doe% t,e eve %ing o5 a %t ing name into a %e'a ate code module and call u'on t,at #odule t, ice* a% in t,e 5ollowing e6a#'le:
. ;ro4ra5 to reverse o0r na5e

D'CLA"' ><9CT2*9 "everseC (na5eC) . Do 6irst na5e 29;<T "'nter o0r 6irst na5e- "= na5eC ;"29T ">irst na5e reversed- " A "everseC(na5eC) . Do 5iddle na5e 29;<T "'nter o0r 5iddle na5e- "= na5eC ;"29T "Middle na5e reversed- " A "everseC(na5eC) . Do last na5e 29;<T "'nter o0r last na5e- "= na5eC ;"29T "Last na5e reversed- " A "everseC(na5eC)

0,e %e'a ate code #odule to eve %e a %t ing i%:


. "everses a strin4 ><9CT2*9 "everseC (ana5eC) Te53C = "" >*" i = L'9(ana5eC) T* 1 ST'; %1 Te53C = Te53C A M2DC(ana5eC= i= 1)

9'#T i "everseC = Te53C '9D ><9CT2*9

O5 cou %e* t,e %aving in %'ace i% not g eat in t,i% e6a#'le* but (ou can i#agine w,at would ,a''en i5 we e'lace t,e eve %ing ' ocedu e b( one t,at e7ui e% %eve al ,und ed line% o5 code and i5 we want to 'e 5o # t,i% ' ocedu e a 5ew ,und ed ti#e% in t,e #ain ' og a#. 0,i% #odula iGation could %ave t,ou%and% o5 line% o5 code. 0,e e i% anot,e ve ( i#'o tant advantage to #odula ' og a##ing. /5 we decide to w ite anot,e ' og a# t,at e7ui e% eve %ing %o#e %t ing%* we can %i#'l( add ou %t ing; eve %ing code #odule to t,e new ' og a#* wit,out ,aving to w ite an( new code. /ndeed* ' o5e%%ional ' og a##e % o5ten co#'ile cu%to# code libraries containing u%e5ul code #odule% t,at can be %li''ed into new a''lication% w,en nece%%a (. /t i% ,a d to ove e%ti#ate t,e i#'o tance o5 #odula ' og a##ing. $o tunatel(* a% we will %ee* =>A #a-e% it ea%( to c eate #odula ' og a#%. .ene all( %'ea-ing* t,e e a e two #ain g ou'% o5 code #odule%: functions and subroutines. 0,e di55e ence between t,e# i% t,at 5unction% etu n a value w,e ea% %ub outine% do not. ?O5 cou %e* we #a( c,oo%e not to u%e t,e value etu ned 5 o# a 5unction.@ $o in%tance* t,e "everse 5unction de%c ibed in t,e ' eviou% e6a#'le etu n% t,e eve %ed %t ing. On t,e ot,e ,and* t,e 5ollowing code #odule 'e 5o #% a %e vice but doe% not etu n a value<it %i#'l( 'au%e% a ce tain nu#be o5 %econd% ?given b( sec@:
S<1 dela (sec) . :et the c0rrent ti5e StartTi5e = T2M'" . 'nter a do%nothin4 loo3 6or sec seconds D* L**; <9T2L T2M'" % StartTi5e ( sec '9D S<1

$unction% and %ub outine% a e e6t e#el( co##on in #ode n coding. 0oget,e * t,e( a e e5e ed to a% procedures.

Chapter *% The ,isual Basic Editor0 Part &


0,e 5i %t %te' in beco#ing an E6cel =>A ' og a##e i% to beco#e 5a#ilia wit, t,e envi on#ent in w,ic, E6cel =>A ' og a##ing i% done. Eac, o5 t,e #ain O55ice a''lication% ,a% a ' og a##ing envi on#ent e5e ed to a% it% ntegrated "e#elopment En#ironment ?/)E@. "ic o%o5t al%o e5e % to t,i% ' og a##ing envi on#ent a% t,e Visual Basic Editor. Ou 'lan in t,i% c,a'te and !,a'te 4 i% to de%c ibe t,e #ajo co#'onent% o5 t,e E6cel /)E. 4e ealiGe t,at (ou a e ' obabl( an6iou% to get to %o#e actual ' og a##ing* but it i% nece%%a ( to gain %o#e 5a#ilia it( wit, t,e /)E be5o e (ou can u%e it. +eve t,ele%%* (ou #a( want to ead 7uic-l( t, oug, t,i% c,a'te and t,e ne6t and t,en e5e bac- to t,e# a% needed. /n O55ice 97* t,e 4o d* E6cel* and &owe &oint /)E% ,ave t,e %a#e a''ea ance* %,own in $igu e 3;1. ?>eginning wit, O55ice 2000* "ic o%o5t Acce%% al%o u%e% t,i% /)E.@ 0o %ta t t,e E6cel /)E* %i#'l( c,oo%e =i%ual >a%ic Edito 5 o# t,e "ac o% %ub#enu o5 t,e 0ool% #enu* o ,it Alt;$11. Figure "-1. he Excel *.% /(E

3et u% ta-e a loo- at %o#e o5 t,e co#'onent% o5 t,i% /)E.

*%$ The Pro1ect -indo"


0,e window in t,e u''e ;le5t co ne o5 t,e client a ea ?below t,e toolba @ i% called t,e Pro%ect Explorer. $igu e 3;2 %,ow% a clo%e;u' o5 t,i% window. Figure "-2. he Project Ex!lorer

+ote t,at t,e & oject E6'lo e ,a% a t eeli-e %t uctu e* %i#ila to t,e 4indow% E6'lo e B% 5olde % 'ane ?t,e le5t;,and 'ane@. Eac, ent ( in t,e & oject E6'lo e i% called a node. 0,e to' node%* o5 w,ic, t,e e a e two in $igu e 3;2* e' e%ent t,e cu entl( o'en E6cel =>A pro%ects ?,ence t,e na#e & oject E6'lo e @. 0,e view o5 eac, ' oject can be e6'anded o cont acted b( clic-ing on t,e %#all bo6e% ?ju%t a% wit, 4indow% E6'lo e @. +ote t,at t,e e i% one ' oject 5o eac, cu entl( o'en E6cel wo -boo-.

".1.1 Project Na&es


Eac, ' oject ,a% a na#e* w,ic, t,e ' og a##e can c,oo%e. 0,e de5ault na#e 5o a ' oject i% =>A& oject. 0,e to' node 5o eac, ' oject i% labeled:
ProjectName (WorkbookName)

w,e e ProjectName i% t,e na#e o5 t,e ' oject and WorkbookName i% t,e na#e o5 t,e E6cel wo -boo-.

".1.2 Project +ontents


At t,e level i##ediatel( below t,e to' ?' oject@ level* a% $igu e 3;2 %,ow%* t,e e a e node% na#ed: "ic o%o5t E6cel Object% $o #% "odule% !la%%e% 2nde t,e "ic o%o5t E6cel Object% node* t,e e i% a node 5o eac, wo -%,eet and c,a t%,eet in t,e wo -boo-* a% well a% a %'ecial node called 0,i%4o -boo-* w,ic, e' e%ent% t,e wo -boo- it%el5. 0,e%e node% ' ovide acce%% to t,e code window% 5o eac, o5 t,e%e object%* w,e e we can w ite ou

code. 2nde t,e $o #% node* t,e e i% a node 5o eac, 5o # in t,e ' oject. &orms a e al%o called 'ser&orms o cu%to# dialog bo6e%. 4e will di%cu%% 2%e $o #% late in t,i% c,a'te . 2nde t,e "odule% node* t,e e i% a node 5o eac, code #odule in t,e ' oject. !ode #odule% a e al%o called %tanda d #odule%. 4e will di%cu%% #odule% late in t,i% c,a'te . 2nde t,e !la%%e% node* t,e e i% a node 5o eac, cla%% #odule in t,e ' oject. 4e will di%cu%% cla%%e% late in t,i% c,a'te . 0,e #ain 'u 'o%e o5 t,e & oject E6'lo e i% to allow u% to navigate a ound t,e ' oject. 4o -%,eet% and 2%e $o #% ,ave two co#'onent%<a vi%ible co#'onent ?a wo -%,eet o dialog@ and a code co#'onent. >( ig,t;clic-ing on a wo -%,eet o 2%e $o # node* we can c,oo%e to view t,e object it%el5 o t,e code co#'onent 5o t,at object. 1tanda d #odule% and cla%% #odule% ,ave onl( a code co#'onent* w,ic, we can view b( double;clic-ing on t,e co e%'onding node. 3et u% ta-e a clo%e loo- at t,e va iou% co#'onent% o5 an E6cel ' oject. ".1.2.1 he his0or1boo1 object 2nde eac, node in t,e & oject E6'lo e labeled "ic o%o5t E6cel Object% i% a node labeled 0,i%4o -boo-. 0,i% node e' e%ent% t,e ' ojectB% wo -boo-* along wit, t,e code co#'onent ?al%o called a code #odule@ t,at %to e% event code 5o t,e wo -boo-. ?4e can al%o 'lace inde'endent ' ocedu e% in t,e code co#'onent o5 a wo -boo- #odule* but t,e%e a e gene all( 'laced in a %tanda d #odule* di%cu%%ed late in t,i% c,a'te .@ 1i#'l( 'ut* t,e 'u 'o%e o5 event% i% to allow t,e =>A ' og a##e to w ite code t,at will e6ecute w,eneve one o5 t,e%e event% 5i e%. E6cel ecogniGe% 19 event% elated to wo -boo-%. 4e will di%cu%% t,e%e event% in !,a'te 11H (ou can ta-e a 7uic- 'ee- at t,i% c,a'te now i5 (ou a e cu iou%. 1o#e e6a#'le%:
C C C C

0,e O'en event* w,ic, occu % w,en t,e wo -boo- i% o'ened. 0,e >e5o e!lo%e event* w,ic, occu % ju%t be5o e t,e wo -boo- i% clo%ed. 0,e +ew1,eet event* w,ic, occu % w,en a new wo -%,eet i% added to t,e wo -boo-. 0,e >e5o e& int event* w,ic, occu % ju%t be5o e t,e wo -boo- o an(t,ing in it i% ' inted.

".1.2.2 Sheet objects 2nde eac, "ic o%o5t E6cel Object% node in t,e & oject E6'lo e i% a node 5o eac, %,eet. ?A sheet i% a wo -%,eet o a c,a t%,eet.@ Eac, %,eet node e' e%ent% a wo -%,eet o c,a t%,eetB% vi%ible co#'onent* along wit, t,e code co#'onent ?al%o called a code #odule@ t,at %to e% event code 5o t,e %,eet. 4e can al%o 'lace inde'endent ' ocedu e% in t,e code co#'onent o5 a %,eet #odule* but t,e%e a e gene all( 'laced in a %tanda d #odule* di%cu%%ed ne6t. E6cel ecogniGe% 7 event% elated to wo -%,eet% and 13 event% elated to c,a t%,eet%. 4e will di%cu%% t,e%e event% in !,a'te 11. ".1.2." Stan#ar# &o#ules A module* al%o #o e clea l( e5e ed to a% a standard module* i% a code #odule t,at contain% gene al ' ocedu e% ?5unction% and %ub outine%@. 0,e%e ' ocedu e% #a( be #ac o% de%igned to be un b( t,e u%e * o t,e( #a( be %u''o t ' og a#% u%ed b( ot,e ' og a#%. ?Re#e#be ou di%cu%%ion o5 #odula ' og a##ing.@ ".1.2.$ +lass &o#ules (lass modules a e code #odule% t,at contain code elated to cu%to# object%. A% we will %ee* t,e E6cel object #odel ,a% a g eat #an( built;in object% ?al#o%t 200@* %uc, a% wo -boo- object%* wo -%,eet object%* c,a t object%* 5ont object%* and %o on. /t i% al%o 'o%%ible to c eate cu%to# object% and endow t,e# wit, va iou% ' o'e tie%. 0o do %o* we would 'lace t,e a'' o' iate code

wit,in a cla%% #odule. Aoweve * %ince c eating cu%to# object% i% be(ond t,e %co'e o5 t,i% boo-* we will not be u%ing cla%% #odule%. ?$o an int oduction to object;o iented ' og a##ing u%ing =>* allow #e to %ugge%t #( boo-* (oncepts of $b%ect-$riented Programming with Visual Basic* 'ubli%,ed b( 1' inge ;=e lag* +ew Lo -.@ ".1.2.' 2serFor& objects A% (ou no doubt -now* E6cel contain% a g eat #an( built;in dialog bo6e%. /t i% al%o 'o%%ible to c eate cu%to# dialog bo6e%* al%o called 5o #% o 2%e $o #%. 0,i% i% done b( c eating 2%e $o # object%. $igu e 3;3 %,ow% t,e de%ign envi on#ent 5o t,e 1elect 1'ecial 2%e $o # t,at we #entioned in !,a'te 1. Figure "-". % 2serFor& #ialog box

0,e la ge window on t,e u''e ;cente in $igu e 3;3 contain% t,e cu%to# dialog bo6 ?na#ed dlg1elect1'ecial@ in it% de%ign #ode. 0,e e i% a 5loating 0oolbo6 window on t,e ig,t t,at contain% icon% 5o va iou% 4indow% cont ol%. 0o 'lace a cont ol on t,e dialog bo6* %i#'l( clic- on t,e icon in t,e 0oolbo6 and t,en d ag and %iGe a ectangle on t,e dialog bo6. 0,i% ectangle i% e'laced b( t,e cont ol o5 t,e %a#e %iGe a% t,e ectangle. 0,e ' o'e tie% o5 t,e 2%e $o # object o o5 an( cont ol% on t,e 5o # can be c,anged b( %electing t,e object and #a-ing t,e c,ange% in t,e & o'e tie% window* w,ic, we di%cu%% in t,e ne6t %ection. /n addition to t,e 5o # it%el5 and it% cont ol%* a 2%e $o # object contain% code t,at t,e =>A ' og a##e w ite% in %u''o t o5 t,e%e object%. $o in%tance* a co##and button ,a% a !lic- event t,at 5i e% w,en t,e u%e clic-% on t,e button. /5 we 'lace %uc, a button on t,e 5o #* t,en we #u%t w ite t,e code t,at i% un w,en t,e !lic- event 5i e%H ot,e wi%e* clic-ing t,e button doe% not,ing. $o in%tance* t,e 5ollowing i% t,e code 5o t,e !lo%e buttonB% !lic- event in $igu e 3;3. +ote t,at t,e +a#e ' o'e t( o5 t,e co##and button ,a% been %et to cmdClose :
;rivate S0b c5dCloseGClic8() <nload Me 'nd S0b

All t,i% code doe% i% unload t,e 5o #. Along wit, event code 5o a 5o # and it% cont ol%* we can al%o include %u''o t ' ocedu e% wit,in

t,e 2%e $o # object. )onBt wo ( i5 all t,i% %ee#% at,e vague now. 4e will devote an enti e c,a'te to c eating cu%to# dialog bo6e% ?t,at i%* 2%e $o # object%@ late in t,e boo- and %ee %eve al eal;li5e e6a#'le% t, oug,out t,e boo-.

*%' The Properties -indo"


0,e & o'e tie% window ?%ee $igu e 3;1@ di%'la(% t,e ' o'e tie% o5 an object and allow% u% to c,ange t,e#. 4,en a %tanda d #odule i% %elected in t,e & oject window* t,e onl( ' o'e t( t,at a''ea % in t,e & o'e tie% window i% t,e #oduleB% na#e. Aoweve * w,en a wo -boo-* %,eet* o 2%e $o # i% %elected in t,e & oject% window* #an( o5 t,e objectB% ' o'e tie% a''ea in t,e & o'e tie% window* a% %,own in $igu e 3;4. 0,e & o'e tie% window can be u%ed to c,ange %o#e o5 t,e ' o'e tie% o5 t,e object w,ile no code i% unning<t,at i%* at design time. +ote* ,oweve * t,at %o#e ' o'e tie% a e ead;onl( and cannot be c,anged. 4,ile #o%t ' o'e tie% can be c,anged eit,e at de%ign ti#e o run time* %o#e ' o'e tie% can onl( be c,anged at de%ign ti#e and %o#e can onl( be c,anged at un ti#e. Run;ti#e ' o'e tie% gene all( do not a''ea in t,e & o'e tie% window. Figure "-$. he Pro!erties win#ow

*%* The Code -indo"


0,e !ode window di%'la(% t,e code t,at i% a%%ociated wit, t,e %elected ite# in t,e & oject window. 0o view t,i% code* %elect t,e object in t,e & oject% window and eit,e c,oo%e !ode 5 o# t,e =iew #enu o ,it t,e $7 -e(. $o object% wit, onl( a code co#'onent ?no vi%ual co#'onent@* (ou can ju%t double;clic- on t,e ite# in t,e & oject% window.

".".1 Proce#ure an# Full--o#ule *iews


.ene all(* a code #odule ?%tanda d* cla%%* o 2%e $o #@ contain% #o e t,an one ' ocedu e. 0,e /)E o55e % t,e c,oice between viewing one ' ocedu e at a ti#e ?called procedure #iew@ o all ' ocedu e% at one ti#e ?called full-module #iew@* wit, a ,o iGontal line %e'a ating t,e ' ocedu e%. Eac, view ,a% it% advantage% and di%advantage%* and (ou will ' obabl( want to u%e bot, view% at di55e ent ti#e%. 2n5o tunatel(* "ic o%o5t ,a% not %u''lied a #enu c,oice 5o %electing t,e view. 0o c,ange view%* we need to clic- on t,e %#all button% in t,e lowe ;le5t co ne o5 t,e !ode window. ?0,e de5ault view can be %et u%ing t,e Edito tab o5 t,e O'tion% dialog bo6.@ /ncidentall(* t,e de5ault 5ont 5o t,e #odule window i% !ou ie * w,ic, ,a% a at,e t,in loo-ing a''ea ance and #a( be %o#ew,at di55icult to ead. Lou #a( want to c,ange t,e 5ont to $i6ed1(% ?on t,e Edito $o #at tab o5 t,e O'tion% dialog* unde t,e 0ool% #enu@* w,ic, i% ve ( eadable.

".".2 he 3bject an# Proce#ure )ist .oxes


At t,e to' o5 t,e !ode window* t,e e a e two d o';down li%t bo6e% ?%ee $igu e 3;1@. 0,e Object bo6 contain% a li%t o5 t,e object% ?%uc, a% 5o #% and cont ol%@ t,at a e a%%ociated wit, t,e cu ent ' oject* and t,e & ocedu e bo6 contain% a li%t o5 all o5 t,e ' ocedu e% a%%ociated wit, t,e object %elected in t,e Object bo6. 0,e ' eci%e content% o5 t,e%e bo6e% va ie% de'ending on t,e t('e o5 object %elected in t,e & oject E6'lo e . ".".2.1 % wor1boo1 or sheet object

4,en a wo -boo- o %,eet object i% %elected in t,e & oject window* t,e Object bo6 contain% onl( two ent ie%: gene al* 5o gene al ' ocedu e%* and t,e object in 7ue%tion* eit,e Wor)boo)* Wor)sheet* o (hart. 4,en t,e object ent ( i% %elected* t,e & ocedu e bo6 contain% e#'t( code %,ell% 5o t,e event% t,at a e elevant to t,at object. $igu e 3;5 %,ow% an e6a#'le. Figure "-'. he events 4or a wor1boo1 object

/5* 5o e6a#'le* we c,oo%e t,e >e5o e!lo%e event in t,e & ocedu e% bo6* E6cel will c eate t,e 5ollowing code %,ell 5o t,i% event and 'lace t,e cu %o wit,in t,i% ' ocedu e:
;rivate S0b Hor8boo8G1e6oreClose(Cancel As 1oolean) 'nd S0b

".".2.2 % stan#ar# &o#ule 4,en a %tanda d #odule i% %elected in t,e & oject window* t,e Object bo6 contain% onl( t,e ent ( .ene al* and a & ocedu e bo6 li%t% all o5 t,e ' ocedu e% we ,ave w itten 5o t,at #odule ?i5 an(@. $igu e 3;6 %,ow% t,e o'en & ocedu e bo6* wit, a li%t o5 t,e cu ent ' ocedu e% 5o a 'a ticula #odule. 0,e )ecla ation% %ection i% w,e e we 'lace va iable decla ation% 5o module-le#el #ariables <t,at i%* 5o va iable% t,at we want to be available in eve ( ' ocedu e wit,in t,e #odule. 4e will di%cu%% t,i% in detail in !,a'te 5. Figure "-5. he Proce#ure box

".".2." % 2serFor& object 4,en a 2%e $o # object i% %elected in t,e & oject E6'lo e * t,e Object bo6 contain% a li%t o5 all o5 t,e object% contained in t,e 2%e $o #. $o in%tance* $igu e 3;7 %,ow% t,e content% o5 t,e Object bo6 5o t,e 2%e $o # object in $igu e 3;3. +ote t,at t,e e a e ent ie% 5o t,e va iou% co##and button% ?%uc, a% cmdClose@* t,e va iou% ot,e cont ol%* and even 5o t,e 2%e $o # it%el5. Figure "-6. he 3bject box

$igu e 3;8 %,ow% t,e content% o5 t,e & ocedu e bo6 w,en t,e cmdClose object i% %elected in t,e Object bo6. 0,i% li%t contain% t,e na#e% o5 t,e 13 di55e ent event% t,at a e a%%ociated wit, a co##and button. Figure "-7. he Proce#ure box

$o e6a#'le* i5 we %elect !lic-* we will be 'laced wit,in t,e !ode window between t,e 5ollowing two line%* w,e e we can w ite event code 5o t,e !lic- event o5 t,e cmdClose co##and button :
;rivate S0b c5dCloseGClic8() 'nd S0b

*%2 The &mmediate -indo"


0,e /##ediate window ?%ee $igu e 3;1@ ,a% two #ain 5unction%. $i %t* we can %end out'ut to t,i% window u%ing t,e co##and Deb04.;rint. $o in%tance* t,e 5ollowing code will ' int w,ateve te6t i% cu entl( in cell A1 o5 t,e active wo -%,eet to t,e /##ediate window:
Deb04.;rint ActiveSheet."an4e("A1").Text

0,i% ' ovide% a nice wa( to e6'e i#ent wit, di55e ent code %ni''et%. 0,e ot,e #ain 5unction o5 t,e /##ediate window i% to e6ecute co##and%. $o in%tance* b( %electing %o#e te6t in t,e active docu#ent* %witc,ing to t,e /##ediate window* and ente ing t,e line %,own in $igu e 3;9* t,e %elected te6t will be bold5aced ?a5te ,itting t,e Ente -e( to e6ecute t,e code@. Figure "-8. he /&&e#iate 0in#ow

0,e /##ediate window i% an e6t e#el( valuable tool 5o debugging a ' og a#* and (ou will ' obabl( u%e it o5ten ?a% / do@.

*%3 Arranging -indo"s


/5 (ou need #o e %'ace 5o w iting code* (ou can clo%e t,e & o'e tie% window* t,e & oject window* and t,e /##ediate window. On t,e ot,e ,and* i5 (ou a e 5o tunate enoug, to ,ave a la ge #onito * (ou can %'lit (ou %c een a% %,own in $igu e 3;10 to %ee t,e E6cel =>A /)E and an E6cel wo -boo- at t,e %a#e ti#e. 0,en (ou can t ace t, oug, eac, line o5 (ou code and watc, t,e e%ult% in t,e wo -boo-C ?Lou can toggle between E6cel and t,e /)E u%ing t,e Alt;$11 -e( co#bination.@ Figure "-19. % s!lit screen a!!roach

".'.1 (oc1ing
"an( o5 t,e window% in t,e /)E ?including t,e & oject* & o'e tie%* and /##ediate window%@ can be in one o5 two %tate%: doc-ed o 5loating. 0,e %tate can be %et u%ing t,e )oc-ing tab on t,e O'tion% dialog bo6* w,ic, i% %,own in $igu e 3;11. Figure "-11. he (oc1ing o!tions

A doc)ed window i% one t,at i% attac,ed* o anc,o ed* to an edge o5 anot,e window o to one edge o5 t,e client a ea o5 t,e #ain =>A window. 4,en a doc-able window i% #oved* it %na'% to an anc,o ed 'o%ition. On t,e ot,e ,and* a floating window can be 'laced an(w,e e on t,e %c een.

Chapter 2% The ,isual Basic Editor0 Part &&


/n t,i% c,a'te * we conclude ou di%cu%%ion o5 t,e =i%ual >a%ic Edito . Again* let u% e#ind t,e eade t,at ,e o %,e #a( want to ead 7uic-l( t, oug, t,i% c,a'te and e5e to it late a% needed.

2%$ 4a igating the &(E


/5 (ou ' e5e t,e -e(boa d to t,e #ou%e ?a% / do@* t,en (ou #a( want to u%e -e(boa d %,o tcut%. Ae e a e %o#e ti'%.

$.1.1 :eneral Navigation


0,e 5ollowing -e(boa d %,o tcut% a e u%ed 5o navigating t,e /)E: &* .o to t,e !ode window. &+ .o to t,e & o'e tie% window. (trl-, .o to t,e & oject window. (trl-.o to t,e /##ediate window. Alt-&..

0oggle between E6cel and t,e => /)E. $.1.1.1 Navigating the co#e win#ow at #esign ti&e 4it,in t,e code window* t,e 5ollowing -e(%t o-e% a e ve ( u%e5ul: &. Ael' on t,e ite# unde t,e cu %o . /hift-&0 .o to t,e de5inition o5 t,e ite# unde t,e cu %o . ?/5 t,e cu %o i% ove a call to a 5unction o %ub outine* ,itting 1,i5t;$2 %end% (ou to t,e de5inition o5 t,at ' ocedu e.@ (trl-/hift-&0

Retu n to t,e la%t 'o%ition w,e e editing too- 'lace. $.1.1.2 racing co#e 0,e 5ollowing -e(%t o-e% a e u%e5ul w,en t acing t, oug, code ?di%cu%%ed in 1ection 4.6* late in t,i% c,a'te @: &1 1te' into /hift-&1 1te' ove (trl-/hift-&1 1te' out (trl-&1 Run to cu %o &2 Run (trl-Brea) > ea/hift-&3 Ouic- watc, &3 0oggle b ea-'oint (trl-/hift-&3 !lea all b ea-'oint% $.1.1." .oo1&ar1s /t i% al%o 'o%%ible to in%e t boo)mar)s wit,in code. A boo-#a - #a -% a location to w,ic, we can etu n ea%il(. 0o in%e t a boo-#a -* o to #ove to t,e ne6t o ' eviou% boo-#a -* u%e t,e >oo-#a -% %ub#enu o5 t,e Edit #enu. 0,e ' e%ence o5 a boo-#a - i% indicated b( a %#all blue %7ua e in t,e le5t #a gin o5 t,e code.

2%' 5etting !elp


/5 (ou a e li-e #e* (ou will ' obabl( #a-e e6ten%ive u%e o5 "ic o%o5tB% E6cel =>A ,el' 5ile% w,ile ' og a##ing. 0,e %i#'le%t wa( to get ,el' on an ite# i% to 'lace t,e cu %o on t,at ite# and ,it t,e $1 -e(. 0,i% wo -% not onl( 5o =>A language -e(wo d% but al%o 5o 'o tion% o5 t,e =>A /)E. +ote t,at "ic o%o5t ' ovide% #ulti'le ,el' 5ile% 5o E6cel* t,e =>A language* and t,e E6cel

object #odel. 4,ile t,i% i% 7uite ea%onable* occa%ionall( t,e ,el' %(%te# get% a bit con5u%ed and e5u%e% to di%'la( t,e co ect ,el' 5ile w,en we %t i-e t,e $1 -e(. ?/ ,ave not 5ound a %i#'le e%olution to t,i% ' oble#* ot,e t,an %,utting down E6cel and t,e =i%ual >a%ic Edito along wit, it.@ +ote al%o t,at a %tanda d in%tallation o5 "ic o%o5t O55ice doe% not in%tall t,e =>A ,el' 5ile% 5o t,e va iou% a''lication%. 0,u%* (ou #a( need to un t,e O55ice %etu' ' og a# and in%tall E6cel =>A ,el' b( %electing t,at o'tion in t,e a'' o' iate %etu' dialog bo6. ?)o not con5u%e E6cel ,el' wit, E6cel =>A ,el'.@

2%* Creating a Procedure


0,e e a e two wa(% to c eate a new ' ocedu e ?t,at i%* a %ub outine o a 5unction@ wit,in a code #odule. $i %t* a5te %electing t,e co ect ' oject in t,e & oject E6'lo e * we can %elect t,e & ocedu e o'tion 5 o# t,e /n%e t #enu. 0,i% will ' oduce t,e dialog bo6 %,own in $igu e 4;1. Nu%t t('e in t,e na#e o5 t,e ' ocedu e and %elect 1ub o $unction ?t,e & o'e t( c,oice i% u%ed wit, cu%to# object% in a cla%% #odule@. 4e will di%cu%% t,e i%%ue o5 'ublic ve %u% ' ivate ' ocedu e% and %tatic va iable% late in t,i% c,a'te . Figure $-1. he %## Proce#ure #ialog box

A %i#'le alte native i% to %i#'l( begin t('ing:


S0b SubName

o: >0nction FunctionName in an( code window ?5ollowing t,e cu ent 'nd S0b o 'nd >0nction %tate#ent* o in t,e gene al decla ation% %ection@. A% %oon a% t,e Ente -e( i% %t uc-* E6cel will #ove t,e line o5 code to a new location and t,e eb( c eate a new %ub outine. ?/t will even add t,e a'' o' iate ending< 'nd S0b o 'nd >0nction.@

2%2 6un Time0 (esign Time0 and Break 7ode


0,e =>A /)E can be in an( one o5 t, ee #ode%: un #ode* b ea- #ode* o de%ign #ode. 4,en t,e /)E i% in de%ign #ode* we can w ite code o de%ign a 5o #. Run #ode occu % w,en a ' ocedu e i% unning. 0o un ?o e6ecute@ a ' ocedu e* ju%t 'lace t,e cu %o an(w,e e wit,in t,e ' ocedu e code and ,it t,e $5 -e( ?o %elect Run 5 o# t,e Run #enu@. /5 a unning ' ocedu e %ee#% to be ,anging* we can usually %to' t,e ' ocedu e b( ,itting !t l; > ea- ?,old down t,e !ont ol -e( and ,it t,e > ea- -e(@.

> ea- #ode i% ente ed w,en a unning ' ocedu e %to'% becau%e o5 eit,e an e o in t,e code o a delibe ate act on ou 'a t ?de%c ibed a bit late @. /n 'a ticula * i5 an e o occu %* E6cel will %to' e6ecution and di%'la( an e o dialog bo6* an e6a#'le o5 w,ic, i% %,own in $igu e 4;2. Figure $-2. %n error &essage

E o dialog bo6e% o55e a 5ew o'tion%: end t,e ' ocedu e* get ,el' ?%uc, a% it #a( be@ wit, t,e ' oble#* o ente b ea- #ode to debug t,e code. /n t,e latte ca%e* E6cel will %to' e6ecution o5 t,e ' ocedu e at t,e o55ending code and ,ig,lig,t t,at code in (ellow. 4e will di%cu%% t,e ' oce%% o5 debugging code a bit late . A%ide 5 o# encounte ing an e o * t,e e a e %eve al wa(% we can delibe atel( ente b ea- #ode 5o debugging 'u 'o%e%: Ait t,e !t l;> ea- -e( and c,oo%e )ebug 5 o# t,e e%ulting dialog bo6. /nclude a Sto3 %tate#ent in t,e code* w,ic, cau%e% E6cel to ente b ea- #ode. /n%e t a brea)point on an e6i%ting line o5 e6ecutable code. 0,i% i% done b( 'lacing t,e cu %o on t,at line and ,itting t,e $9 5unction -e( ?o u%ing t,e 0oggle > ea-'oint o'tion on t,e )ebug #enu@. E6cel will 'lace a ed dot in t,e le5t #a gin in 5 ont o5 t,at line and will %to' e6ecution w,en it eac,e% t,e line. Lou #a( ente #o e t,an one b ea-'oint in a ' ocedu e. 0,i% i% gene all( ' e5e ed ove u%ing t,e Sto3 %tate#ent* becau%e b ea-'oint% a e auto#aticall( e#oved w,en we clo%e down t,e =i%ual >a%ic Edito * %o we donBt need to e#e#be to e#ove t,e#* a% we do wit, Sto3 %tate#ent%. C 1et a watch %tate#ent t,at cau%e% E6cel to ente b ea- #ode i5 a ce tain condition beco#e% t ue. 4e will di%cu%% watc, e6' e%%ion% a bit late .
C C C

0o e6it 5 o# > ea- #ode* c,oo%e Re%et 5 o# t,e Run #enu. +ote t,at t,e ca'tion in t,e title ba o5 t,e =>A /)E indicate% w,ic, #ode i% cu entl( active. 0,e ca'tion contain% t,e wo d 8P unningQ8 w,en in un #ode and 8Pb ea-Q8 w,en in b ea- #ode.

2%3 Errors
/n co#'ute ja gon* an e o i% e5e ed to a% a bug. /n ca%e (ou a e inte e%ted in t,e o igin o5 t,i% wo d* t,e %to ( goe% t,at w,en o'e ating t,e 5i %t la ge;%cale digital co#'ute * called t,e "a - /* an e o wa% t aced to a #ot, t,at ,ad 5ound it% wa( into t,e ,a dwa e. /ncidentall(* t,e "a - / ?ci ca 1944@ ,ad 750*000 'a t%* wa% 51 5eet long* and weig,ed ove 5ive ton%. Aow about 'utting t,at on (ou de%-to'9 /t al%o e6ecuted about one in%t uction eve ( %i6 %econd%* a% co#'a ed to ove 200 #illion in%t uction% 'e %econd 5o a &entiu#C E o % can be g ou'ed into t, ee t('e% ba%ed on w,en t,e( occu <design time* compile time* o run time.

$.'.1 (esign- i&e Errors


A% t,e na#e i#'lie%* a design-time error occu % du ing t,e w iting o5 code. &e ,a'% t,e nice%t 5eatu e o5 t,e =i%ual >a%ic Edito i% t,at it can be in%t ucted to watc, a% we t('e code and %to' u% w,en we #a-e a %(nta6 e o . 0,i% auto#atic %(nta6 c,ec-ing can be enabled o di%abled in t,e O'tion% dialog bo6 %,own in $igu e 4;3* but / %t ongl( %ugge%t t,at (ou -ee' it enabled. Figure $-". he 3!tions #ialog box

+otice al%o t,at t,e e a e ot,e %etting% elated to t,e de%ign;ti#e envi on#ent* %uc, ,a% ,ow 5a to indent code in e%'on%e to t,e 0ab -e(. 4e will di%cu%% %o#e o5 t,e%e ot,e %etting% a bit late . 0o illu%t ate auto#atic %(nta6 c,ec-ing* $igu e 4;4 %,ow% w,at ,a''en% w,en we delibe atel( ente t,e %(ntacticall( inco ect %tate#ent x == & and t,en atte#'t to #ove to anot,e line. +ote t,at "ic o%o5t e5e % to t,i% t('e o5 e o a% a compile error in t,e dialog bo6 and 'e ,a'% we %,ould a% well. Aoweve * it %ee#% #o e de%c i'tive to call it a de%ign;ti#e e o o ju%t a %(nta6 e o. Figure $-$. % syntax error &essage

$.'.2 +o&!ile- i&e Errors


>e5o e a ' og a# can be e6ecuted* it #u%t be compiled* o t an%lated into a language t,at t,e co#'ute can unde %tand. 0,e co#'ilation ' oce%% occu % auto#aticall( w,en we e7ue%t t,at a ' og a# be e6ecuted. 4e can al%o %'eci5icall( e7ue%t co#'ilation b( c,oo%ing t,e !o#'ile & oject ite# unde t,e )ebug #enu. /5 E6cel encounte % an e o w,ile co#'iling code* it di%'la(% a co#'ile e o #e%%age. $o e6a#'le* t,e code in $igu e 4;5 contain% a co#'ile;ti#e e o . /n 'a ticula * t,e 5i %t line:
Di5 wb as Hor8boo8

de5ine% a va iable o5 t('e Wor)boo) to e' e%ent an E6cel wo -boo-. ?4e will go into all o5 t,i% in !,a'te 17* %o donBt wo ( about t,e detail% now.@ Aoweve * t,e %econd line:
Set wb = ActiveHor8boo8.9a5e

atte#'t% to a%%ign t,e va iable wb not to t,e active wo -boo-* w,ic, would be legal* but to t,e name o5 t,e active wo -boo-. 0,i% e o i% not caug,t du ing de%ign ti#e becau%e it i% not a %(nta6 e o . /t i% onl( at co#'ile ti#e* w,en E6cel con%ide % t,e %tate#ent in t,e conte6t o5 t,e 5i %t %tate#ent* t,at t,e e o beco#e% evident. Figure $-'. % co&!ilation error &essage

$.'." ,un- i&e Errors

An e o t,at occu % w,ile a ' og a# i% unning i% called a run-time error. $igu e 4;6 illu%t ate% a un;ti#e e o and it% co e%'onding e o #e%%age. /n t,i% e6a#'le* t,e code:
Hor8boo8s.*3en "d-Ite53I'xist9ot.xls"

atte#'t% to o'en an E6cel wo -boo- t,at doe% not e6i%t. +otice t,at t,i% e o #e%%age i% actuall( 7uite 5 iendl(<not onl( doe% it de%c ibe t,e e o in clea te #% ?t,e 5ile could not be 5ound@* but it al%o o55e % %o#e %ugge%tion% 5o eli#inating t,e ' oble#. Figure $-5. % run-ti&e error &essage

$.'.$ )ogical Errors


0,e e i% one #o e t('e o5 e o t,at we %,ould di%cu%%* %ince it i% t,e #o%t in%idiou% t('e o5 all. A logical error can be de5ined a% t,e ' oduction o5 an une6'ected and inco ect e%ult. A% 5a a% E6cel i% conce ned* t,e e i% no e o * becau%e E6cel ,a% no wa( o5 -nowing w,at we intend. ?0,u%* a logical e o i% not a un;ti#e e o * in t,e t aditional %en%e* even t,oug, it doe% occu at un ti#e.@ 0o illu%t ate* t,e 5ollowing code 'u 'o t% to co#'ute t,e ave age o5 %o#e nu#be %:
Di5 x(B) As 2nte4er Di5 Ave As Sin4le x(7) = 1 x(1) = B x(2) = D x(B) = & Ave = (x(7) A x(1) A x(2) A x(B)) J B Ms41ox "Avera4e is- " K Ave

0,e e%ult i% t,e #e%%age bo6 %,own in $igu e 4;7. 2n5o tunatel(* it i% inco ect. 0,e 'enulti#ate line in t,e ' eceding ' og a# %,ould be:
Ave = (x(7) A x(1) A x(2) A x(B)) J L

+ote t,e 4 in t,e deno#inato * %ince t,e e a e 4 nu#be % to ave age. 0,e co ect ave age i% 4.25. O5 cou %e* E6cel will not co#'lain becau%e it ,a% no wa( o5 -nowing w,et,e we eall( want to divide b( 3. Figure $-6. he result o4 a logical error

& eci%el( becau%e E6cel cannot wa n u% about logical e o %* t,e( a e t,e #o%t dange ou%* becau%e we thin) t,at eve (t,ing i% co ect.

2%8 (ebugging
/nva iabl(* (ou will encounte e o % in (ou code. )e%ign;ti#e and co#'ile;ti#e e o % a e elativel( ea%( to deal wit, becau%e E6cel ,el'% u% out wit, e o #e%%age% and b( indicating t,e o55ending code. 3ogical e o % a e #uc, #o e di55icult to detect and to 5i6. 0,i% i% w,e e debugging 'la(% a #ajo ole. 0,e E6cel /)E ' ovide% %o#e ve ( 'owe 5ul wa(% to 5ind bug%. )ebugging can be 7uite involved* and we could include a w,ole c,a'te on t,e %ubject. 0,e e a e even %'ecial %o5twa e a''lication% de%igned to a%%i%t in co#'le6 debugging ta%-%. Aoweve * 5o #o%t 'u 'o%e%* a 5ew %i#'le tec,ni7ue% a e %u55icient. /n 'a ticula * E6cel #a-e% it ea%( to trace t, oug, ou ' og a#%* e6ecuting one line at a ti#e* watc,ing t,e e55ect o5 eac, line a% it i% e6ecuted. 3et u% t ( a ve ( %i#'le e6a#'le* w,ic, (ou %,ould 5ollow along on (ou &!. /5 'o%%ible* (ou %,ould a ange (ou %c een a% in $igu e 4;8. 0,i% will #a-e it ea%ie to 5ollow t,e e55ect% o5 t,e code* %ince (ou wonBt need to %witc, bac- and 5o t, between t,e E6cel window and t,e E6cel =>A window. 0,e code t,at we will t ace i% %,own in E6a#'le 4;1. +ote t,at line% beginning wit, an a'o%t o',e a e co##ent% t,at a e igno ed b( E6cel. Figure $-7. o!-an#-botto& win#ows 4or easy #ebugging

Exa&!le $-1. % Si&!le Progra& to race


S0b Test() Di5 ws As Hor8sheet Set ws = ActiveSheet . 2nsert a val0e into cell A1 ws.Cells(1= 1).+al0e = "sa53le" . Ma8e it bold ws.Cells(1= 1).>ont.1old = Tr0e . Co3 cell ws.Cells(1= 1).Co3 . ;aste val0e onl ws.Cells(2= 1).;asteS3ecial ;aste-=xl+al0es 'nd S0b

"a-e %u e t,at an e#'t( wo -%,eet i% active in E6cel. 1witc, to t,e =>A /)E and 'lace t,e in%e tion 'oint %o#ew,e e in t,e code. 0,en ,it t,e $8 -e( once* w,ic, %ta t% t,e t acing ' oce%%. ?Lou can al%o c,oo%e 1te' /nto 5 o# t,e )ebug #enu.@ !ontinue %t i-ing t,e $8 -e(* 'au%ing between -e(%t o-e% to view t,e e55ect o5 eac, in%t uction in t,e E6cel window. ?Lou can toggle between E6cel and t,e /)E u%ing Alt;$11.@ A% (ou t ace t, oug, t,i% code* (ou will %ee t,e wo d 8%a#'le8 ente ed into cell A1 o5 t,e active wo -%,eet* c,anged to a''ea in bold5ace* co'ied to t,e !li'boa d* and 'a%ted a% no #al te6t into t,e cell A2. 0,en (ou can begin to %ee w,at E6cel =>A ' og a##ing i% all aboutC 3et u% di%cu%% %o#e o5 t,e tool% t,at E6cel ' ovide% 5o debugging code.

$.5.1 racing
0,e ' oce%% o5 e6ecuting code one line at a ti#e* a% we did in t,e ' eviou% e6a#'le* i% e5e ed to a% tracing o code stepping. E6cel ' ovide% t, ee o'tion% elated to t acing: %te''ing into* %te''ing ove * and %te''ing out o5. 0,e di55e ence between t,e%e #et,od% e5e % to ,andling call% to ot,e ' ocedu e%. 0o illu%t ate t,e di55e ence* con%ide t,e code %,own in E6a#'le 4;2. /n ;roced0reA* t,e 5i %t line o5 code %et% t,e value o5 cell A1 o5 t,e active wo -%,eet. 0,e %econd line call% ;roced0re1 and t,e t,i d line bold5ace% t,e content% o5 t,e cell. ;roced0re1 %i#'l( c,ange% t,e %iGe and na#e o5 t,e 5ont u%ed in cell A1. )onBt wo ( about t,e e6act %(nta6 o5 t,i% code. 0,e i#'o tant t,ing to notice i% t,at t,e %econd line o5 ;roced0reA call% ;roced0re1. Exa&!le $-2. Sa&!le +o#e 4or racing -etho#s
S0b ;roced0reA() ActiveSheet.Cells(1= 1).+al0e = "sa53le" Call ;roced0re1 ActiveSheet.Cells(1= 1).>ont.1old = Tr0e 'nd S0b S0b ;roced0re1() ActiveSheet.Cells(1= 1).>ont.SiMe = 2L ActiveSheet.Cells(1= 1).>ont.9a5e = "Arial" 'nd S0b

$.5.1.1 Ste!!ing into /tep nto e6ecute% code one %tate#ent ?o in%t uction@ at a ti#e. /5 t,e %tate#ent being e6ecuted call% anot,e ' ocedu e* %te''ing into t,at %tate#ent %i#'l( t an%5e % cont ol to t,e 5i %t line in t,e called ' ocedu e. $o in%tance* wit, e5e ence to t,e ' eviou% code* %te''ing into t,e line:
Call ;roced0re1

in ;roced0reA t an%5e % cont ol to t,e 5i %t line o5 ;roced0re1 :


ActiveSheet.Cells(1= 1).>ont.SiMe = 2L

$u t,e t acing ' oceed% in ;roced0re1. Once all o5 t,e line% o5 ;roced0re1 ,ave been t aced* cont ol etu n% to ;roced0reA at t,e line i##ediatel( 5ollowing t,e call to ;roced0re1<t,at i%* at t,e line:
ActiveSheet.Cells(1= 1).>ont.1old = Tr0e

1te' /nto ,a% anot,e i#'o tant u%e. /5 we c,oo%e 1te' /nto w,ile %till in de%ign #ode* t,at i%* be5o e an( code i% unning* e6ecution begin% but b ea- #ode i% ente ed before t,e 5i %t line o5 code i% actuall( e6ecuted. 0,i% i% t,e ' o'e wa( to begin t acing a ' og a#. $.5.1.2 Ste! 3ver ;Shi4t-F7 or choose Ste! 3ver 4ro& the (ebug &enu< /tep $#er i% %i#ila to 1te' /nto* e6ce't t,at i5 t,e cu ent %tate#ent i% a call to anot,e ' ocedu e* t,e enti e called ' ocedu e i% e6ecuted wit,out %to''ing ? at,e t,an t acing t, oug, t,e called ' ocedu e@. 0,u%* 5o in%tance* %te''ing ove t,e line:
Call ;roced0re1

in t,e ' eviou% ' ocedu e e6ecute% ;roced0re1 and %to'% at t,e ne6t line:
ActiveSheet.Cells(1= 1).>ont.1old = Tr0e

in ;roced0reA. 0,i% i% u%e5ul i5 we a e ce tain t,at ;roced0re1 i% not t,e cau%e o5 ou ' oble# and we donBt want to t ace t, oug, t,at ' ocedu e line b( line. $.5.1." Ste! 3ut ;+trl-Shi4t-F7 or choose Ste! 3ut 4ro& the (ebug &enu< /tep $ut i% intended to be u%ed wit,in a called ' ocedu e ?%uc, a% ;roced0re1@. 1te' Out e6ecute% t,e e#aining line% o5 t,e called ' ocedu e and etu n% to t,e calling ' ocedu e ?%uc, a% ;roced0reA@. 0,i% i% u%e5ul i5 we a e in t,e #iddle o5 a called ' ocedu e and decide t,at we donBt need to t ace an( #o e o5 t,at ' ocedu e* but want to etu n to t,e calling ' ocedu e. ?/5 (ou t ace into a called ' ocedu e b( #i%ta-e* ju%t do a 1te' Out to etu n to t,e calling ' ocedu e.@ $.5.1.$ ,un o +ursor ;+trl-F7 or choose ,un o +ursor 4ro& the (ebug &enu< /5 t,e =i%ual >a%ic Edito i% in b ea- #ode* we #a( want to e6ecute %eve al line% o5 code at one ti#e. 0,i% can be done u%ing t,e ,un !o (ursor 5eatu e. 1i#'l( 'lace t,e cu %o on t,e %tate#ent immediately following t,e la%t line (ou want to e6ecute and t,en e6ecute Run 0o !u %o . $.5.1.' Set Next State&ent ;+trl-F8 or choose Set Next State&ent 4ro& the (ebug &enu< 4e can al%o c,ange t,e 5low o5 e6ecution w,ile in b ea- #ode b( 'lacing t,e cu %o on t,e %tate#ent t,at we want to e6ecute ne6t and %electing 1et +e6t 1tate#ent. 0,i% will %et t,e %elected %tate#ent a% t,e ne6t %tate#ent to e6ecute* but will not e6ecute it until we continue t acing. $.5.1.5 .rea1ing out o4 (ebug &o#e

4,en we no longe need to t ace ou code* we ,ave two c,oice%. 0o etu n to de%ign #ode* we can c,oo%e Re%et 5 o# t,e Run #enu ?t,e e i% no ,ot-e( 5o t,i%@. 0o ,ave E6cel 5ini%, e6ecuting t,e cu ent ' og a#* we can ,it $5 o c,oo%e Run 5 o# t,e Run #enu.

$.5.2 0atching Ex!ressions


/t i% o5ten u%e5ul to watc, t,e value% o5 ce tain e6' e%%ion% o va iable% a% we t ace t, oug, a ' og a#. E6cel ' ovide% %eve al wa(% to do t,i%. $.5.2.1 =uic1 0atch ;Shi4t-F8< 0,i% 5eatu e i% u%ed to 7uic-l( c,ec- t,e value o5 a va iable o e6' e%%ion w,ile in b ea- #ode. 4e ju%t 'lace t,e in%e tion 'oint ove t,e va iable na#e and ,it 1,i5t;$9 ?o c,oo%e Ouic- 4atc, 5 o# t,e )ebug #enu@. $o in%tance* $igu e 4;9 %,ow% t,e Ouic- 4atc, dialog bo6 w,en t,e e6' e%%ion x A 2 i% %elected in t,e code in $igu e 4;10. Acco ding to $igu e 4;9* at t,e ti#e t,at Ouic- 4atc, wa% invo-ed* t,e e6' e%%ion 6 F 2 ,ad t,e value 8. +ote t,at i5 we ,ad ju%t 'laced t,e in%e tion 'oint in 5 ont o5 t,e lette x* t,en Ouic- 4atc, would ,ave e'o ted t,e value o5 t,i% va iable alone. Figure $-8. he =uic1 0atch win#ow

Anot,e wa( to 7uic-l( get value% 5o e6' e%%ion% o va iable% i% to enable Auto )ata 0i'% on t,e Edito tab o5 E6cel =>AB% O'tion% dialog bo6. 4it, t,i% 5eatu e enabled* w,en we 'lace t,e #ou%e 'ointe ove a va iable o %elect an e6' e%%ion and 'lace t,e #ou%e 'ointe ove it* a5te a %lig,t dela(* a %#all (ellow window will a''ea containing t,e value o5 t,e va iable o e6' e%%ion. 0,i% i% #ery u%e5ulC $.5.2.2 he )ocals an# 0atches win#ows 0,e e a e two %'ecial window% t,at aid in watc,ing e6' e%%ion%: t,e 4atc,e% window and t,e 3ocal% window. 0,e%e a e %,own in $igu e 4;10. Figure $-19. he )ocals an# 0atches win#ows

0,e Locals window %,ow% t,e value% o5 all local va iable%. A local #ariable i% a va iable de5ined wit,in t,e cu ent ' ocedu e* and i% t,e e5o e not valid in an( ot,e ' ocedu e. ?4e will di%cu%% local va iable% in t,e ne6t c,a'te .@ 0,e Watches window %,ow% all o5 t,e watc,e% t,at we ,ave %et. A watch i% a va iable o e6' e%%ion t,at we 'lace in t,e 4atc, window. E6cel auto#aticall( u'date% t,e e6' e%%ion% in t,e 4atc, window a5te eac, line o5 code i% e6ecuted and act% acco ding to t,e t('e o5 watc, de5ined* a% de%c ibed in t,e 5ollowing li%t. 0o add a watc,* c,oo%e Add 4atc, 5 o# t,e )ebug #enu. 0,i% will ' oduce t,e dialog bo6 %,own in $igu e 4;11. 4e can t,en ente a va iable o e6' e%%ion* %uc, a% x ( N* in t,e E6' e%%ion te6t bo6. +ote t,at t,e e a e t, ee t('e% o5 watc,e%:
C

4atc, E6' e%%ion %i#'l( add% t,e e6' e%%ion to t,e 4atc,e% window* %o we can watc, it% value a% code i% e6ecuted. /n t,i% e6a#'le* t,e value o5 t,e e6' e%%ion will be eit,e Tr0e o >alse* de'ending u'on w,et,e x i% g eate t,an N. > ea- 4,en =alue /% 0 ue a%-% E6cel to %to' e6ecution and ente b ea- #ode w,eneve t,e e6' e%%ion i% tr0e. /n t,i% e6a#'le* =>A will b ea- e6ecution w,en x ( N i% tr0e* t,at i%* w,en x beco#e% g eate t,an N. > ea- 4,en =alue !,ange% a%-% E6cel to ente b ea- #ode w,en t,e value o5 t,e e6' e%%ion c,ange% in an( wa(. ?/n t,i% ca%e* 5 o# Tr0e to >alse o vice;ve %a.@ Figure $-11. he %## 0atch #ialog box

Altoget,e * t,e va iou% t acing #ode% and watc, t('e% ' ovide a ve ( 'owe 5ul %et o5 tool% 5o debugging code. / u%e t,e# o5tenC

2%9 7acros
/n ea lie da(%* a macro con%i%ted o5 a %e ie% o5 -e(%t o-e% t,at wa% eco ded and a%%igned to a ,ot -e(. 4,en a u%e invo-ed t,e ,ot -e(* t,e eco ding would 'la( and t,e eco ded -e(%t o-e% would be e6ecuted. 0,e%e da(%* #ac o% ?at lea%t 5o "ic o%o5t O55ice@ a e #uc, #o e %o',i%ticated. /n 5act* an E6cel #ac o i% ju%t a %'ecial t('e o5 %ub outine<one t,at doe% not ,ave an( 'a a#ete %. ?4e will di%cu%% %ub outine% and 'a a#ete % in !,a'te 6.@

$.6.1 ,ecor#ing -acros


E6cel ,a% t,e ca'abilit( o5 eco ding ve ( %i#'le #ac o%. 4,en we a%- E6cel to eco d a #ac o b( %electing "ac o Reco d +ew "ac o 5 o# E6celB% ?not E6cel =>AB%@ 0ool% #enu* it ta-e% note o5 ou -e(%t o-e% and conve t% t,e# into a =>A %ub outine ?wit, no 'a a#ete %@. $o e6a#'le* %u''o%e we eco d a #ac o t,at doe% a 5ind and e'lace* e'lacing t,e wo d 8#ac o8 b( t,e wo d 8%ub outine.8 4,en we loo- in t,e & oject% window unde t,e ' oject in w,ic, t,e #ac o wa% eco ded* we will 5ind a new %ub outine in a %tanda d code #odule:
S0b Macro1() . . Macro1 Macro . Macro recorded EJ1BJED b . .

sr

Cells."e3lace Hhat-="5acro"= "e3lace5ent-="s0bro0tine"= G

Loo8At-=xl;art= Search*rder-=xl1 "ows= MatchCase-=>alse 'nd S0b

0,i% i% t,e %a#e code t,at we #ig,t ,ave w itten in o de to 'e 5o # t,i% 5ind and e'lace o'e ation. /n ce tain %ituation%* t,e #ac o eco de can %e ve a% a ve ( u%e5ul lea ning tool. /5 we canBt 5igu e out ,ow to code a ce tain action* we can eco d it in a #ac o and cut and 'a%te t,e e%ulting code into ou own ' og a#. ?/n 5act* (ou #ig,t want to t ( eco ding t,e c eation o5 a 'ivot table.@ Aoweve * be5o e (ou get too e6cited about t,i% cut;and;'a%te a'' oac, to ' og a##ing* we %,ould 'oint out t,at it i% not an(w,e e nea t,e 'anacea one #ig,t ,o'e. One ' oble# i% t,at t,e #ac o eco de ,a% a tendenc( to u%e ad ,oc code at,e t,an code t,at will wo - in a va iet( o5 %ituation%. $o in%tance* eco ded #ac o code will o5ten e5e to t,e cu ent %election* w,ic, #a( wo - at t,e ti#e t,e #ac o wa% eco ded but i% not o5 #uc, u%e in a gene al %etting* becau%e t,e ' og a##e cannot be %u e w,at t,e cu ent %election will be w,en t,e u%e invo-e% t,e code. Anot,e ' oble# i% t,at t,e #ac o eco de i% onl( ca'able o5 eco ding ve ( %i#'le ' ocedu e%. "o%t u%e5ul E6cel ' og a#% a e 5a too co#'licated to be eco ded auto#aticall( b( t,e #ac o eco de . $inall(* %ince t,e #ac o eco de doe% %uc, a t,o oug, job o5 t an%lating ou action% into code* it tend% to ' oduce ve ( bloated code* w,ic, o5ten un% ve ( %lowl(.

$.6.2 ,unning -acros


A% (ou #a( -now* to un a #ac o 5 o# t,e u%e inte 5ace* we ju%t c,oo%e "ac o% 5 o# t,e "ac o %ub#enu o5 t,e 0ool% #enu ?o ,it Alt;$8@. 0,i% di%'la(% t,e "ac o dialog bo6 %,own in $igu e 4;12. 0,i% dialog bo6 li%t% all #ac o% in t,e cu ent wo -boo- o in all wo -boo-%. $ o# ,e e* we can do %eve al t,ing%* including unning* editing* c eating* o deleting #ac o%. ?!,oo%ing Edit o ! eate 'lace% u% in t,e => Edito .@ Figure $-12. Excel>s -acro #ialog box

4e %,ould al%o co##ent on w,at a''ea % and doe% not a''ea in t,e "ac o li%t bo6. All #ac o% t,at we w ite will a''ea in t,e "ac o% dialog bo6 ?a% will all eco ded #ac o%@. Aoweve * t,e e a e a 5ew va iation%. /5 we give t,e #ac o a uni7ue na#e ?wit,in t,e conte6t given in t,e 8"ac o% in8 li%t bo6@* t,en onl( t,e na#e o5 t,e #ac o will a''ea in t,e li%t bo6. /5 t,e na#e i% not uni7ue* t,en it #u%t be 7uali5ied b( t,e na#e o5 t,e #odule in w,ic, t,e #ac o a''ea %* a% in:
Sheet&.ScrollChartT 3es

in $igu e 4;12. 2n5o tunatel(* t,e 5i %t ve %ion o5 a #ac o wit, a nonuni7ue na#e i% not 7uali5ied. ?+ote t,e ' e%ence o5 anot,e 1c oll!,a t0('e% #ac o in $igu e 4;12.@ +ote t,at we can ' event a #ac o ' ocedu e 5 o# a''ea ing in t,e "ac o% li%t bo6 b( #a-ing t,e ' ocedu e ' ivate* u%ing t,e ;rivate -e(wo d* a% in:
;rivate S0b ?ideThisMacro()

4e will di%cu%% ;rivate and ;0blic ' ocedu e% in !,a'te 6. $inall(* i5 (ou a e li-e #e* (ou will collect a g eat #an( #ac o% ove t,e (ea %. A% ti#e goe% b(* (ou #a( 5o get t,e na#e% o5 %o#e o5 t,e%e #ac o% and t,u% ,ave t ouble 5inding a #ac o w,en (ou need it. / would advi%e (ou to give %o#e ca e5ul t,oug,t to c eating a con%i%tent na#ing convention 5o #ac o%. / begin t,e na#e% o5 all #ac o% wit, a wo d t,at catego iGe% t,e #ac o. $o in%tance* all o5 #( #ac o% t,at deal wit, wo -%,eet% begin wit, t,e lette % 4-%* a% in:
H8sGSort H8sGCo53are H8sG;rint

Part &&+ The ,BA Programming )anguage


Chapter 3% ,ariables0 (ata T/pes0 and Constants
/n t,e ne6t 5ew c,a'te %* we will di%cu%% t,e ba%ic% o5 t,e =>A ' og a##ing language* w,ic, unde lie% all o5 t,e "ic o%o5t O55ice ' og a##ing envi on#ent%. )u ing ou di%cu%%ion* we will con%ide #an( %,o t coding e6a#'le%. / ,o'e t,at (ou will ta-e t,e ti#e to -e( in %o#e o5 t,e%e e6a#'le% and e6'e i#ent wit, t,e#.

3%$ Comments
4e ,ave al ead( di%cu%%ed t,e 5act t,at co##ent% a e i#'o tant. An( te6t t,at 5ollow% an a'o%t o',e i% con%ide ed a co##ent and i% igno ed b( E6cel. $o e6a#'le* t,e 5i %t line in t,e 5ollowing code i% a co##ent* a% i% eve (t,ing 5ollowing t,e a'o%t o',e on t,e t,i d line:
. Declare a strin4 variable Di5 H8s9a5e as Strin4 H8s9a5e = Activesheet.9a5e

. :et na5e o6 active sheet

4,en debugging code* it i% o5ten u%e5ul to te#'o a il( co##ent out line% o5 code %o t,e( will not e6ecute. 0,e line% can %ub%e7uentl( be unco##ented to e%to e t,e# to active dut(. 0,e !o##ent>loc- and 2nco##ent>loc- button%* w,ic, can be 5ound on t,e Edit toolba * will 'lace o e#ove co##ent #a -% 5 o# eac, cu entl( %elected line o5 code and a e ve ( u%e5ul 5o co##enting out %eve al line% o5 code in one %te'. ?2n5o tunatel(* t,e e a e no -e(boa d %,o tcut% 5o t,e%e co##and%* but t,e( can be added to a #enu and given #enu accele ato -e(%.@

3%' )ine Continuation

0,e ve ( natu e o5 E6cel =>A %(nta6 o5ten lead% to long line% o5 code* w,ic, can be di55icult to ead* e%'eciall( i5 we need to %c oll ,o iGontall( to %ee t,e enti e line. $o t,i% ea%on* "ic o%o5t ecentl( int oduced a line;continuation c,a acte into =>A. 0,i% c,a acte i% t,e unde %co e* w,ic, must be ' eceded b( a %'ace and cannot be 5ollowed b( an( ot,e c,a acte % ?including co##ent%@. $o e6a#'le* t,e 5ollowing code:
ActiveSheet."an4e("A1").>ont.1old = G Tr0e

i% t eated a% one line b( E6cel. /t i% i#'o tant to note t,at a line continuation c,a acte cannot be in%e ted in t,e #iddle o5 a lite al %t ing con%tant* w,ic, i% enclo%ed in 7uotation #a -%.

3%* Constants
0,e =>A language ,a% two t('e% o5 con%tant%. A literal constant ?al%o called a constant o literal @ i% a %'eci5ic value* %uc, a% a nu#be * date* o te6t %t ing* t,at doe% not c,ange* and t,at i% u%ed e6actl( a% w itten. +ote t,at %t ing con%tant% a e enclo%ed in double 7uotation #a -%* a% in "Donna S5ith" and date con%tant% a e enclo%ed between nu#be %ign%* a% in O1J1JENO. $o in%tance* t,e 5ollowing code %to e% a date in t,e va iable called dt:

Di5 dt As Date dt = O1J2JEFO

A symbolic constant ?al%o %o#eti#e% e5e ed to %i#'l( a% a constant@ i% a na#e 5o a lite al con%tant. 0o de5ine o decla e a %(#bolic con%tant in a ' og a#* we u%e t,e Const -e(wo d* a% in:
Const 2nvoice;ath = "d-I2nvoicesI"

/n t,i% ca%e* E6cel will e'lace eve ( in%tance o5 2nvoice;ath in ou code wit, t,e %t ing "d-I2nvoicesI". 0,u%* 2nvoice;ath i% a con%tant* %ince it neve c,ange% value* but it i% not a lite al con%tant* %ince it i% not u%ed a% w itten. 0,e vi tue o5 u%ing %(#bolic con%tant% i% t,at* i5 we decide late to c,ange "d-I2nvoicesI" to "d-I*ld2nvoicesI"* we onl( need to c,ange t,e de5inition o5 2nvoice;ath to:
Const 2nvoice;ath = "d-I*ld2nvoicesI"

at,e t,an %ea c,ing t, oug, t,e enti e ' og a# 5o eve ( occu ence o5 t,e ', a%e
"d-I2nvoicesI".

/t i% gene all( good ' og a##ing ' actice to decla e an( %(#bolic con%tant% at t,e beginning o5 t,e ' ocedu e in w,ic, t,e( a e u%ed ?o in t,e )ecla ation% %ection o5 a code #odule@. 0,i% i#' ove% eadabilit( and #a-e% ,ou%e-ee'ing %i#'le . /n addition to t,e %(#bolic con%tant% t,at (ou can de5ine u%ing t,e Const %tate#ent* =>A ,a% a la ge nu#be o5 built;in %(#bolic con%tant% ?about 700@* w,o%e na#e% begin wit, t,e lowe ca%e lette % #b. E6cel =>A add% additional %(#bolic con%tant% ?1266 o5 t,e#@ t,at begin wit, t,e lette % xl. 4e will encounte #an( o5 t,e%e con%tant% t, oug,out t,e boo-. A#ong t,e #o%t co##onl( u%ed =>A con%tant% a e vbCrL6* w,ic, i% e7uivalent to a ca iage etu n 5ollowed b( a line 5eed* and vbTab* w,ic, i% e7uivalent to t,e tab c,a acte .

'.".1 Enu&s

"ic o%o5t ,a% ecentl( int oduced a %t uctu e into =>A to catego iGe t,e 'let,o a o5 %(#bolic con%tant%. 0,i% %t uctu e i% called an enum * w,ic, i% %,o t 5o enumeration. A li%t o5 enu#% can be obtained u%ing #( Object "odel > ow%e %o5twa e. $o in%tance* a#ong E6celB% 152 enu#%* t,e e i% one 5o t,e 5ill t('e u%ed b( t,e Auto$ill #et,od* de5ined a% 5ollow%:
'n05 #lA0to>illT 3e xl>illDe6a0lt = 7 xl>illCo3 = 1 xl>illSeries = 2 xl>ill>or5ats = B xl>ill+al0es = L xl>illDa s = & xl>illHee8da s = N xl>illMonths = F xl>ill@ears = D xlLinearTrend = E xl:rowthTrend = 17 'nd 'n05

?0,e E6cel docu#entation inco ectl( e5e % to t,i% enu# a% #l>illT 3e.@ +ote t,at enu# na#e% begin wit, t,e lette % 4l ?wit, an u''e ca%e 4 @. 0,u%* t,e 5ollowing line o5 code will auto5ill t,e 5i %t %even cell% in t,e 5i %t ow o5 t,e active %,eet wit, t,e da(% o5 t,e wee-* a%%u#ing t,at t,e 5i %t cell contain% t,e wo d "onda(:
ActiveSheet."an4e("A1").A0to>ill ActiveSheet."an4e("A1-:1")= xl>illDa s

0,i% i% 5a #o e eadable t,an:


ActiveSheet."an4e("A1").A0to>ill ActiveSheet."an4e("A1-:1")= &

+ote t,at t,i% enu# i% built in* %o we do not need to add it to ou ' og a#% in o de to u%e t,e%e %(#bolic con%tant%. ?4e can c eate ou own enu#%* but t,i% i% gene all( not nece%%a ( in E6cel =>A ' og a##ing* %ince E6cel ,a% done %uc, a good job o5 t,i% 5o u%.@ A% anot,e e6a#'le* t,e built;in enu# 5o t,e con%tant value% t,at can be etu ned w,en t,e u%e di%#i%%e% a #e%%age bo6 ?b( clic-ing on a button@ i%:
'n05 +bMs41ox"es0lt vb*P = 1 vbCancel = 2 vbAbort = B vb"etr = L vb24nore = & vb@es = N vb9o = F 'nd 'n05

$o in%tance* w,en t,e u%e ,it% t,e OR button on a dialog bo6 ?a%%u#ing it ,a% one@* =>A etu n% t,e value vb*P. !e tainl(* it i% a lot ea%ie to e#e#be t,at =>A will etu n t,e %(#bolic con%tant vb*P t,an to e#e#be t,at it will etu n t,e con%tant 1. ?4e will di%cu%% ,ow to get and u%e t,i% etu n value late .@ =>A al%o de5ine% %o#e %(#bolic con%tant% t,at a e u%ed to %et t,e t('e% o5 button% t,at will a''ea on a #e%%age bo6. 0,e%e a e contained in t,e 5ollowing enu# ?w,ic, include% %o#e additional con%tant% not %,own@:
'n05 +bMs41oxSt le vb*P*nl = 7 vb*PCancel = 1 vbAbort"etr 24nore = 2 vb@es9oCancel = B vb@es9o = L vb"etr Cancel = & 'nd 'n05

0o illu%t ate* con%ide t,e 5ollowing code:


26 Ms41ox(";roceedQ"= vb*PCancel) = vb*P Then . 3lace code to exec0te when 0ser hits *P b0tton 'lse . 3lace code to exec0te when 0ser hits an other b0tton 'nd 26

/n t,e 5i %t line* t,e code Ms41ox(";roceedQ"= vb*PCancel) cau%e% E6cel to di%'la( a #e%%age bo6 wit, an OR button and a !ancel button and t,e #e%%age 8& oceed98* a% %,own in $igu e 5;1. Figure '-1. Exa&!le &essage box

/5 t,e u%e clic-% t,e OR button* E6cel will etu n t,e con%tant value vb*PH ot,e wi%e it will etu n t,e value vbCancel. 0,u%* t,e 26 %tate#ent in t,e 5i %t line will di%tingui%, between t,e two e%'on%e%. ?4e will di%cu%% t,e 26 %tate#ent in detail in !,a'te 8. Ae e* we a e inte e%ted in t,e ole o5 %(#bolic con%tant%.@ /n ca%e (ou a e not (et convinced o5 t,e value o5 %(#bolic con%tant%* con%ide t,e 5ollowing enu# 5o colo con%tant%:
'n05 ColorConstants vb1lac8 = 7 vb1l0e = 1NF11ND7 vbMa4enta = 1NF11EB& vbC an = 1NFFNEN7 vbHhite = 1NFFF21& vb"ed = 2&& vb:reen = N&2D7 vb@ellow = N&&B& 'nd 'n05

!on%ide w,ic, (ouBd at,e t('e* t,i%:


AText1ox.>oreColor = vb1l0e

o t,i%:
AText1ox.>oreColor = 1NF11ND7

+eed / %a( #o e9

3%2 ,ariables and (ata T/pes


A #ariable can be t,oug,t o5 a% a #e#o ( location t,at can ,old value% o5 a %'eci5ic t('e. 0,e value in a va iable #a( c,ange du ing t,e li5e o5 t,e ' og a#<,ence t,e na#e va iable. /n =>A* eac, va iable ,a% a %'eci5ic data type* w,ic, indicate% w,ic, t('e o5 data it #a( ,old. $o in%tance* a va iable t,at ,old% te6t %t ing% ,a% a 1t ing data t('e and i% called a string #ariable. A va iable t,at ,old% intege % ?w,ole nu#be %@ ,a% an /ntege data t('e and i% called an integer

#ariable. $o e5e ence* 0able 5;1 %,ow% t,e co#'lete %et o5 =>A data t('e%* along wit, t,e a#ount o5 #e#o ( t,at t,e( con%u#e and t,ei ange o5 value%. 4e will di%cu%% a 5ew o5 t,e #o e co##onl( u%ed data t('e% in a #o#ent. able '-1. *.% (ata Type Size in Memory y!es Range of Values >(te 1 b(te 0 to 255 >oolean 2 b(te% 0 ue o $al%e /ntege 2 b(te% ;32*768 to 32*767 3ong ?long intege @ 4 b(te% ;2*147*483*648 to 2*147*483*647 1ingle?%ingle 4 b(te% A'' o6i#atel( ;3.4E38 to 3.4E38 ' eci%ion eal@ )ouble?double 8 b(te% A'' o6i#atel( ;1.8E308 to 4.9E324 ' eci%ion eal@ !u enc(?%caled A'' o6i#atel( ;922*337*203*685*477.5808 8 b(te% intege @ to 922*337*203*685*477.5807 )ate 8 b(te% 1E1E100 to 12E31E9999 Object 4 b(te% An( Object e5e ence. =a iable lengt,:10 b(te% F =a iable lengt,: ST about 2 billion ?65*400 1t ing %t ing lengt,H$i6ed lengt,: 5o 4in 3.1@ $i6ed lengt,: u' to 65*400 %t ing lengt, 16 b(te% 5o nu#be %22 b(te% +u#be : %a#e a% )ouble1t ing: %a#e a% =a iant F %t ing lengt, 1t ing 2%e ;de5ined =a ie%

'.$.1 *ariable (eclaration


0o declare a va iable #ean% to de5ine it% data t('e. =a iable% a e decla ed wit, t,e Di5 -e(wo d ?o wit, t,e -e(wo d% ;rivate and ;0blic* w,ic, we will di%cu%% late in t,i% c,a'te @. Ae e a e %o#e e6a#'le%:
Di5 Di5 Di5 Di5 Di5 Di5 Di5 9a5e As Strin4 ?olida As Date A4e As 2nte4er ?ei4ht As Sin4le Mone As C0rrenc wb8 As Hor8boo8 ch As Chart

0,e gene al %(nta6 o5 a va iable decla ation i%:


Di5 VariableName As DataTy e

/5 a 'a ticula va iable i% u%ed wit,out 5i %t decla ing it* o i5 it i% decla ed wit,out #entioning a data t('e* a% in:
Di5 A4e

t,en =>A will t eat t,e va iable a% ,aving t('e =a iant. A% we can %ee 5 o# 0able 5;1* t,i% i% gene all( a wa%te o5 #e#o (* %ince va iant% e7ui e #o e #e#o ( t,an #o%t ot,e t('e% o5 va iable%.

$o in%tance* an intege va iable e7ui e% 2 b(te%* w,e ea% a va iant t,at ,old% t,e %a#e intege e7ui e% 16 b(te%* w,ic, i% a wa%te o5 14 b(te%. /t i% not unco##on to ,ave ,und ed% o even t,ou%and% o5 va iable% in a co#'le6 ' og a#* and %o t,e #e#o ( wa%te could be %igni5icant. $o t,i% ea%on* it i% a good idea to decla e all va iable%. &e ,a'% #o e i#'o tantl(* #uc, #o e ove ,ead i% involved in #aintaining a =a iant t,an it% co e%'onding 1t ing o /ntege * 5o e6a#'le. 0,i% in tu n #ean% t,at u%ing =a iant% t('icall( e%ult% in wo %e 'e 5o #ance t,an u%ing an e7uivalent %et o5 e6'licit data t('e%. 4e can 'lace #o e t,an one decla ation on a line to %ave %'ace. $o in%tance* t,e 5ollowing line decla e% t, ee va iable%:
Di5 A4e As 2nte4er= 9a5e As Strin4= Mone As C0rrenc

+ote* ,oweve * t,at a decla ation %uc, a%:


Di5 A4e= ?ei4ht= Hei4ht As 2nte4er

i% legal* but A4e and ?ei4ht a e decla ed a% =a iant%* not /ntege %. /n ot,e wo d%* we #u%t %'eci5( t,e t('e 5o each va iable e6'licitl(. /t i% al%o 'o%%ible to tell =>A t,e t('e o5 t,e va iable b( a''ending a %'ecial c,a acte to t,e va iable na#e. /n 'a ticula * =>A allow% t,e t('e;decla ation %u55i6e% %,own in 0able 5;2. ?/ 'e %onall( di%li-e t,e%e %u55i6e%* but t,e( do %ave %'ace.@ able '-2. y!e-(eclaration Su44ixes Type intege long %ingle double cu enc( %t ing

Suffix J K C U M V

$o in%tance* t,e 5ollowing line decla e% a va iable called 9a5eC o5 t('e 1t ing:
Di5 9a5eC

4e can t,en w ite:


9a5eC = "Donna"

$inall(* let u% note t,at alt,oug, E6cel allow% va iable and con%tant decla ation% to be 'laced an(w,e e wit,in a ' ocedu e ?be5o e t,e ite# i% u%ed* t,at i%@* it i% gene all( good ' og a##ing ' actice to 'lace all %uc, decla ation% at t,e beginning o5 t,e ' ocedu e. 0,i% i#' ove% code eadabilit( and #a-e% ,ou%e-ee'ing #uc, %i#'le .

'.$.2 he /&!ortance o4 Ex!licit *ariable (eclaration


4e ,ave %aid t,at u%ing t,e =a iant data t('e gene all( wa%te% #e#o ( and o5ten e%ult% in 'oo e 'e 5o #ance. 0,e e i% an additional* even #o e i#'o tant ea%on to decla e all va iable% e6'licitl(. 0,i% ,a% to do wit, #a-ing t('ing e o %* w,ic, we all do 5 o# ti#e to ti#e. /n 'a ticula * i5 we accidentall( #i%%'ell a va iable na#e* =>A will t,in- we #ean to c eate a new va iableC

0o illu%t ate ,ow dange ou% t,i% can be* con%ide t,e 9ew1oo8 ' ocedu e in E6a#'le 5;1* w,o%e 'u 'o%e i% to ta-e t,e 5i %t o'en wo -boo-* c,ange it% content%* a%- t,e u%e 5o a na#e unde w,ic, to %ave t,e c,anged wo -boo-* and t,en %ave t,e wo -boo- unde t,e new na#e. Exa&!le '-1. % Proce#ure with a y!o
S0b 9ew1oo8() Di5 Hb8 As Hor8boo8 Di5 Hb89a5e As Strin4 . :et 6irst o3en wor8boo8 Set Hb8 = Hor8boo8s(1) . :et the wor8boo8 na5e Hb89a5e = Hb8.9a5e . Code to chan4e the contents o6 the wor8boo8 . 4oes here . . . . As8 0ser 6or new na5e 6or doc05ent H8b9a5e = 2n30t1ox("'nter na5e 6or wor8boo8 " K Hb89a5e) . Save the wor8boo8 Hb8.SaveAs Hb89a5e 'nd S0b

Ob%e ve t,at t,e e i% a t('og a',ical e o ?t,e b and - a e t an%'o%ed@ in t,e 5ollowing line:
H8b9a5e = 2n30t1ox("'nter na5e 6or wor8boo8 " K Hb89a5e)

1ince t,e va iable WkbName i% not decla ed* E6cel will t eat it a% a new va iable and give it t,e =a iant data t('e. "o eove * =>A will a%%u#e t,at we want t,e new 5ilena#e to be a%%igned to t,e va iable WkbName* and will %ave t,e c,anged docu#ent unde it% o iginal na#e* w,ic, i% %to ed in WbkName. 0,u%* we will lo%e t,e o iginal wo -boo- w,en it i% inadve tentl( ove w itten wit,out wa ningC '.$.2.1 3!tion Ex!licit 0o avoid t,e ' oble# de%c ibed in t,e ' eviou% e6a#'le* we need a wa( to #a-e E6cel e5u%e to un a ' og a# i5 it contain% an( va iable% t,at we ,ave not e6'licitl( decla ed. 0,i% i% done %i#'l( b( 'lacing t,e line:
*3tion 'x3licit

in t,e )ecla ation% %ection o5 eac, code #odule. 1ince it i% ea%( to 5o get to do t,i%* =>A ' ovide% an o'tion called 8Re7ui e =a iable )ecla ation8 in it% O'tion% dialog bo6. 4,en t,i% o'tion i% %elected* =>A auto#aticall( in%e t% t,e *3tion 'x3licit line 5o u%. 0,e e5o e* / strongly eco##end t,at (ou enable t,i% o'tion. +ow let u% b ie5l( di%cu%% %o#e o5 t,e data t('e% in 0able 5;1.

'.$." Nu&eric (ata y!es


0,e nu#e ic data t('e% include /ntege * 3ong* 1ingle* )ouble* and !u enc(. A long i% al%o %o#eti#e% e5e ed to a% a long integer.

'.$.$ .oolean (ata y!e


A >oolean va iable i% a va iable t,at ta-e% on one o5 two value%: Tr0e o >alse. 0,i% i% a ve ( u%e5ul data t('e t,at wa% onl( ecentl( int oduced into =>A. & io to it% int oduction* =>A ecogniGed 0 a% >alse and an( nonGe o value a% Tr0e* and (ou #a( %till %ee t,i% u%age in olde code.

'.$.' String (ata y!e


A %t ing i% a %e7uence o5 c,a acte %. ?An e#'t( %t ing ,a% no c,a acte %* ,oweve .@ A %t ing #a( contain o dina ( te6t c,a acte % ?lette %* digit%* and 'unctuation@ a% well a% %'ecial cont ol c,a acte % %uc, a% vbCrL6 ?ca iage etu nEline 5eed c,a acte %@ o vbTab ?tab c,a acte @. A% we ,ave %een* a %t ing con%tant i% enclo%ed wit,in 7uotation #a -%. 0,e e#'t( %t ing i% denoted b( a 'ai o5 adjacent 7uotation #a -%* a% in:
'53t Strin4 = ""

0,e e a e two t('e% o5 %t ing va iable% in =>A: 5i6ed;lengt, and va iable;lengt,. A 5i6ed;lengt, %t ing va iable i% decla ed a% 5ollow%:
Di5 FixedStrin!VarName As Strin4 R Strin!"en

$o in%tance* t,e 5ollowing %tate#ent decla e% a 5i6ed;lengt, %t ing o5 lengt, 10 c,a acte %:
Di5 s9a5e As Strin4 R 17

Ob%e ve t,at t,e 5ollowing code* w,ic, concatenate% two %t ing%:


Di5 s As Strin4 R 17 s = "test" Deb04.;rint s K "J"

and ' oduce% t,e out'ut:


test J

0,i% %,ow% t,at t,e content o5 a 5i6ed;lengt, %t ing i% 'added wit, %'ace% in o de to eac, t,e co ect lengt,. A va iable;lengt, %t ing va iable i% a va iable t,at can ,old %t ing% o5 va (ing lengt,% ?at di55e ent ti#e%* o5 cou %e@. =a iable;lengt, %t ing va iable% a e decla ed %i#'l( a%:
Di5 VariableStrin!VarName As Strin4

A% an e6a#'le* t,e code:


Di5 s As Strin4 s = "test" Deb04.;rint s K "J" s = "another test" Deb04.;rint s K "J"

' oduce% t,e out'ut:

testJ another testJ

=a iable;lengt, %t ing va iable% a e u%ed #uc, #o e o5ten t,an 5i6ed;lengt, %t ing%* alt,oug, t,e latte ,ave %o#e ve ( %'eci5ic and i#'o tant u%e% ?w,ic, we will not go into in t,i% boo-@.

'.$.5 (ate (ata y!e


=a iable% o5 t,e )ate data t('e e7ui e 8 b(te% o5 %to age and a e actuall( %to ed a% deci#al ?5loating;'oint@ nu#be % t,at e' e%ent date% anging 5 o# Nanua ( 1* 100 to )ece#be 31* 9999 ?no (ea 2000 ' oble# ,e e@ and ti#e% 5 o# 0:00:00 to 23:59:59. A% di%cu%%ed ea lie * lite al date% a e enclo%ed wit,in nu#be %ign%* but w,en a%%igning a date to a date va iable* we can al%o u%e valid date% in %t ing 5o #at. $o e6a#'le* t,e 5ollowing a e all valid dateEti#e a%%ign#ent%:
Di5 dt As Date dt = O1J2JEDO dt = "San0ar 12= 2771" dt = O1J1JE&O dt = O12-&7-77 ;MO dt = O1J1BJFN 12-&7-77 ;MO

=>A ,a% #an( 5unction% t,at can #ani'ulate date% and ti#e%. /5 (ou need to #ani'ulate date% o ti#e% in (ou ' og a#%* (ou %,ould ' obabl( %'end %o#e ti#e wit, t,e E6cel =>A ,el' 5ile. ?1ta t b( loo-ing unde 8)ate )ata 0('e.8@

'.$.6 *ariant (ata y!e


0,e =a iant data t('e ' ovide% a catc,;all data t('e t,at i% ca'able o5 ,olding data o5 an( ot,e t('e e6ce't 5i6ed;lengt, %t ing data and u%e ;de5ined t('e%. 4e ,ave al ead( noted t,e vi tue% and vice% o5 t,e =a iant data t('e and di%cu%%ed w,( va iant% %,ould gene all( be avoided.

'.$.7 Excel 3bject (ata y!es


E6cel =>A ,a% a la ge nu#be o5 additional data t('e% t,at 5all unde t,e gene al catego ( o5 Object data t('e. 4e will %ee a co#'lete li%t in t,e c,a'te on t,e E6cel object #odel. 0o get t,e 5eel 5o t,e t('e% o5 object% in t,e E6cel object #odel* ,e e i% a 'a tial li%t o5 t,e #o e ' o#inent object%: (hart-related ob%ects5 A6i% A6i%0itle !,a t !,a tA ea !,a t!olo $o #at Pi#ot table-related ob%ects5 &ivot!ac,e &ivot$ield &ivot$o #ula &ivot/te# &ivot0able !,a t0itle )ata3abel )ata0able $loo . idline% 3egend 3egendEnt ( 3egendRe( &lotA ea &oint 1e ie% 1e ie%!ollection 0ic-3abel% 4all%

-eneral ob%ects5 !o##ent $ill$o #at $ilte $ont Outline &age1etu' Range 1,eet% 4indow 4o -boo4o -%,eet 4o -%,eet$unction

0,u%* we can decla e va iable% %uc, a%:


Di5 Di5 Di5 Di5 Di5 wb As Hor8boo8 w8s As Hor8sheet chrt As Chart ax As axis 36 As ;ivot>ield

4e will devote #uc, o5 t,i% boo- to %tud(ing t,e object% in t,e E6cel object #odel* 5o it i% t, oug, t,e%e object% t,at we can #ani'ulate E6cel ' og a##aticall(. '.$.7.1 he generic %s 3bject #eclaration /t i% al%o 'o%%ible to decla e an( E6cel object u%ing t,e gene ic object data t('e *bTect* a% in t,e 5ollowing e6a#'le:
Di5 chrt As *bTect

4,ile (ou #a( %ee t,i% decla ation 5 o# ti#e to ti#e* it i% #uc, le%% e55icient t,an a %'eci5ic object decla ation* %uc, a%:
Di5 chrt As Chart

0,i% i% becau%e E6cel cannot tell w,at t('e o5 object t,e va iable chrt e5e % to until t,e ' og a# i% unning* %o it #u%t u%e %o#e e6ecution ti#e to #a-e t,i% dete #ination. 0,i% i% e5e ed to a% late binding and can #a-e ' og a#% un %igni5icantl( #o e %lowl(. ?$o #o e on late ve %u% ea l( binding* %ee A''endi6 E.@ 0,u%* gene ic object decla ation% %,ould be avoided. 4e will di%cu%% object va iable% in %o#e detail in !,a'te 9. Aoweve * we %,ould b ie5l( di%cu%% t,e Set %tate#ent now* %ince it will a''ea 5 o# ti#e to ti#e in u'co#ing code e6a#'le%. '.$.7.2 he Set state&ent )ecla ing object va iable% i% done in t,e %a#e wa( a% decla ing nonobject va iable%. $o in%tance* ,e e a e two va iable decla ation%:
Di5 int As 2nte4er Di5 chrt As Chart . nonobTect variable declaration . obTect variable declaration

On t,e ot,e ,and* w,en it co#e% to a%%igning a value to va iable%* t,e %(nta6 di55e % 5o object and nonobject va iable%. /n 'a ticula * we #u%t u%e t,e Set -e(wo d w,en a%%igning a value to an object va iable. $o e6a#'le* t,e 5ollowing line a%%ign% t,e cu entl( active E6cel c,a t to t,e va iable chrt:
Set chrt = ActiveChart

?/5 t,e cu entl( active object i% not a c,a t* t,en t,e va iable chrt will be %et to t,e %'ecial value 9othin4. 4e will di%cu%% 9othin4 late .@

'.$.8 %rrays
An array #ariable i% a collection o5 va iable% t,at u%e t,e %a#e na#e* but a e di%tingui%,ed b( an inde6 value. $o in%tance* to %to e t,e 5i %t 100 cell% in t,e 5i %t ow o5 a wo -%,eet* we could decla e an a a( va iable a% 5ollow%:
Di5 Cell(1 To 177) As "an4e

?0,e e i% no !ell object in t,e E6cel object #odel: a cell i% a %'ecial Range object.@ 0,e a a( va iable i% Cell. /t ,a% %iGe 100. 0,e lowe bound o5 t,e a a( i% 1 and t,e u''e bound i% 100. Eac, o5 t,e 5ollowing va iable% a e Range va iable% ?t,at i%* va iable% o5 t,e object t('e Range@:
Cell(1)= Cell(2)=...= Cell(177)

+ote t,at i5 we o#it t,e 5i %t inde6 in t,e decla ation* a% in:


Di5 Cell(177) As "an4e

t,en =>A will auto#aticall( %et t,e 5i %t inde6 to 0 and %o t,e %iGe o5 t,e a a( will be 101. 0,e vi tue o5 decla ing a a( va iable% i% clea * %ince it would be ve ( un'lea%ant to ,ave to decla e 100 %e'a ate va iable%C /n addition* a% we will %ee* t,e e a e wa(% to wo - collectivel( wit, all o5 t,e ele#ent% in an a a(* u%ing a 5ew %i#'le ' og a##ing con%t uct%. $o in%tance* t,e 5ollowing code bold5ace% t,e value% in eac, o5 t,e 100 cell% along t,e diagonal o5 t,e active wo -%,eet:
>or i = 1 To 177 Set Cell(i) = Cells(i=i) Cell(i).>ont.1old = Tr0e 9ext i

'.$.8.1 he #i&ension o4 an array 0,e !ell a a( de5ined in t,e ' eviou% e6a#'le ,a% dimension one. 4e can al%o de5ine a a(% o5 #o e t,an one di#en%ion. $o in%tance* t,e a a(:
Di5 Cell(1 To 17= 1 To 177) As "an4e

i% a two;di#en%ional a a(* w,o%e 5i %t inde6 ange% 5 o# 1 to 10 and w,o%e %econd inde6 ange% 5 o# 1 to 100. 0,u%* t,e a a( ,a% %iGe 10W100 T 1000. '.$.8.2 (yna&ic arrays 4,en an a a( i% decla ed* a% in:
Di5 >ile9a5e(1 To 17) As Strin4

t,e u''e and lowe bound% a e bot, %'eci5ied and %o t,e %iGe o5 t,e a a( i% 5i6ed. Aoweve * t,e e a e #an( %ituation% in w,ic, we do not -now at decla ation ti#e ,ow la ge an a a( we #a( need. $o t,i% ea%on* =>A ' ovide% d(na#ic a a(% and t,e "eDi5 %tate#ent.

A d(na#ic a a( i% decla ed wit, e#'t( 'a ent,e%e%* a% in:


Di5 >ile9a5e() as Strin4

)(na#ic a a(% can be %iGed ?o e%iGed@ u%ing t,e "eDi5 %tate#ent* a% in:
"eDi5 >ile9a5e(1 to 17)

0,i% %a#e a a( can late be e%iGed again* a% in:


"eDi5 >ile9a5e(1 to 177)

+ote t,at e%iGing an a a( will de%t o( it% content% unless we u%e t,e ;reserve -e(wo d* a% in:
"eDi5 ;reserve >ile9a5e(1 to 277)

Aoweve * w,en ;reserve i% u%ed* we can onl( c,ange t,e u''e bound o5 t,e a a( ?and onl( t,e la%t di#en%ion in a #ultidi#en%ional a a(@. '.$.8." he 2.oun# 4unction 0,e <1o0nd 5unction i% u%ed to etu n t,e cu ent u''e bound o5 an a a(. 0,i% i% ve ( u%e5ul in dete #ining w,en an a a( need% edi#en%ioning. 0o illu%t ate* %u''o%e we want to collect an un-nown nu#be o5 5ilena#e% in an a a( na#ed FileName. /5 t,e ne6t 5ile nu#be i% iNextFile* t,e 5ollowing code c,ec-% to %ee i5 t,e u''e bound i% le%% t,an iNextFileH i5 %o* it inc ea%e% t,e u''e bound o5 t,e a a( b( 10* ' e%e ving it% cu ent content%* to #a-e oo# 5o t,e ne6t 5ilena#e:
26 <1o0nd(>ile9a5e) $ i9ext>ile Then "eDi5 ;reserve >ile9a5e(<1o0nd(>ile9a5e) A 17) 'nd 26

+ote t,at edi#en%ioning ta-e% ti#e* %o it i% wi%e to add %o#e 8wo -ing oo#8 at t,e to' to cut down on t,e nu#be o5 ti#e% t,e a a( #u%t be edi#en%ioned. 0,i% i% w,( we added 10 to t,e u''e bound in t,i% e6a#'le* at,e t,an ju%t 1. ?0,e e i% a t ade;o55 ,e e between t,e e6t a ti#e it ta-e% to edi#en%ion and t,e e6t a %'ace t,at #a( be wa%ted i5 we do not u%e t,e enti e edi#en%ioned a a(.@

'.$.19 *ariable Na&ing +onventions


=>A ' og a#% can get ve ( co#'licated* and we can u%e all t,e ,el' we can get in t (ing to #a-e t,e# a% eadable a% 'o%%ible. /n addition* a% ti#e goe% on* t,e idea% be,ind t,e ' og a# begin to 5ade* and we #u%t el( on t,e code it%el5 to e5 e%, ou #e#o (. 0,i% i% w,( adding co'iou% co##ent% to a ' og a# i% %o i#'o tant. Anot,e wa( to #a-e ' og a#% #o e eadable i% to u%e a con%i%tent naming con#ention 5o con%tant%* va iable%* ' ocedu e na#e%* and ot,e ite#%. /n gene al* a na#e %,ould ,ave two ' o'e tie%. $i %t* it %,ould e#ind t,e eade o5 t,e 'u 'o%e o 5unction o5 t,e ite#. $o in%tance* %u''o%e we want to a%%ign !,a t va iable% to %eve al E6cel c,a t%. 0,e code:
Di5 chrt1 As Chart= chrt2 as Chart Set chrt1 = Charts("Sales") Set chrt2 = Charts("Transactions")

i% 'e 5ectl( legal* but 1000 line% o5 code and %i6 #ont,% late * will we e#e#be w,ic, invoice i% chrt# and w,ic, i% chrt$ 9 1ince we went to t,e t ouble o5 na#ing t,e c,a t% in a de%c i'tive #anne * we %,ould do t,e %a#e wit, t,e !,a t va iable%* a% in:
Di5 chrtSales As Chart= chrtTrans as Chart Set chrtSales = Charts("Sales") Set chrtTrans = Charts("Transactions")

O5 cou %e* t,e e a e e6ce'tion% to all ule%* but* in gene al* it i% bette to c,oo%e de%c i'tive na#e% 5o va iable% ?a% well a% ot,e ite#% t,at e7ui e na#ing* %uc, a% con%tant%* ' ocedu e%* cont ol%* 5o #%* and code #odule%@. 1econd* a va iable na#e %,ould e5lect %o#et,ing about t,e ' o'e tie% o5 t,e va iable* %uc, a% it% data t('e. "an( ' og a##e % u%e a convention in w,ic, t,e 5i %t 5ew c,a acte % o5 a va iableB% na#e indicate t,e data t('e o5 t,e va iable. 0,i% i% %o#eti#e% e5e ed to a% a Aunga ian na#ing convention* a5te t,e Aunga ian ' og a##e !,a le% 1i#on(i* w,o i% c edited wit, it% invention. 0able 5;3 and 0able 5;4 de%c ibe t,e na#ing convention t,at we will generally u%e 5o %tanda d and object va iable%* e%'ectivel(. O5 cou %e* (ou a e 5 ee to #a-e c,ange% 5o (ou own 'e %onal u%e* but (ou %,ould t ( to be ea%onabl( con%i%tent. 0,e%e ' e5i6e% a e intended to e#ind u% o5 t,e data t('e* but it i% not ea%( to do t,i% 'e 5ectl( u%ing onl( a cou'le o5 c,a acte %* and t,e longe t,e ' e5i6* t,e le%% li-el( it i% t,at we will u%e itC ?+ote t,e c ' e5i6 5o intege % o long%. 0,i% i% a co##onl( u%ed ' e5i6 w,en t,e va iable i% intended to count %o#et,ing.@ able '-". Na&ing +onvention 4or Stan#ar# *ariables Variable Prefix >oolean bo 5 >(te b o bt !u enc( cu )ate dt )ouble d o dbl /ntege i* c* o int 3ong l* c* o lng 1ingle % o %ng 1t ing % o %t 2%e ;de5ined t('e u o ut =a iant v o va able '-$. Na&ing +onvention 4or So&e 3bject *ariables Variable Prefix c, o c, t wb o wbw% o w-% 't o 'vt 5nt ng

!,a t 4o -boo4o -%,eet &ivot 0able $ont Range

/n addition to a data t('e* eve ( va iable ,a% a scope and a lifetime. 1o#e ' og a##e % advocate including a ,int a% to t,e %co'e o5 a va iable in t,e ' e5i6* u%ing g 5o global and m 5o #odule level. $o e6a#'le* t,e va iable 4iSiMe i% a global va iable o5 t('e /ntege . 4e will di%cu%% t,e

%co'e and li5eti#e o5 a va iable ne6t ?but we will not gene all( include %co'e ' e5i6e% in va iable na#e%@.

'.$.11 *ariable Sco!e


=a iable% and con%tant% ,ave a scope* w,ic, indicate% w,e e in t,e ' og a# t,e va iable o con%tant i% ecogniGed ?o vi%ible to t,e code@. 0,e %co'e o5 a va iable o con%tant can be eit,e ' ocedu e;level ?al%o called local@* #odule;level ' ivate* o #odule;level 'ublic. 0,e ule% #a( %ee# a bit involved at 5i %t* but t,e( do #a-e %en%e. '.$.11.1 Proce#ure-level ;local< variables A local o procedure-le#el va iable o con%tant i% a va iable o con%tant t,at i% decla ed wit,in a ' ocedu e* a% i% t,e ca%e wit, t,e va iable Local+ar and t,e con%tant LocalConstant in $igu e 5;2. A local va iable o con%tant i% not vi%ible out%ide o5 t,e ' ocedu e. 0,u%* 5o in%tance* i5 we t ( to un ;roced0re1 in $igu e 5;2* we will get t,e e o #e%%age* 8=a iable not de5ined*8 and t,e na#e Local+ar will be ,ig,lig,ted. Figure '-2. Exa&!les o4 variable sco!e

One o5 t,e advantage% o5 local va iable% i% t,at we can u%e t,e %a#e na#e in di55e ent ' ocedu e% wit,out con5lict* %ince eac, va iable i% vi%ible onl( to it% own ' ocedu e. '.$.11.2 -o#ule-level variables A module-le#el va iable ?o con%tant@ i% one t,at i% decla ed in t,e decla ation% %ection o5 a code #odule ?%tanda d* cla%%* o 2%e $o #@. "odule;level va iable% and con%tant% co#e in two 5lavo %: ' ivate and 'ublic. 1i#'l( 'ut* a #odule;level public va iable ?o con%tant@ i% available to all ' ocedu e% in all o5 t,e #odule% in t,e ' oject* not ju%t t,e #odule in w,ic, it i% decla ed* w,e ea% a #odule;level pri#ate va iable ?o con%tant@ i% available onl( to t,e ' ocedu e% in t,e #odule in w,ic, it wa% decla ed. &ublic va iable% and con%tant% a e decla ed u%ing t,e ;0blic -e(wo d* a% in:
;0blic A;0b2nt As 2nte4er ;0blic Const A;0bConst = F

& ivate va iable% and con%tant% a e decla ed u%ing t,e ;rivate -e(wo d* a% in:
;rivate A;rivate2nt As 2nte4er ;rivate Const A;rivateConst = F

0,e Di5 -e(wo d* w,en u%ed at t,e #odule level* ,a% t,e %a#e %co'e a% ;rivate* but i% not a% clea * %o it %,ould be avoided. &ublic va iable% a e al%o e5e ed to a% global #ariables* but t,i% de%c i'tive te # i% not de rigueur.

'.$.12 *ariable )i4eti&e


=a iable% al%o ,ave a li5eti#e. 0,e di55e ence between li5eti#e and %co'e i% 7uite %i#'le: lifetime e5e % to ,ow long ?o w,en@ t,e va iable i% valid ?t,at i%* etain% a value@ w,e ea% scope e5e % to w,e e t,e va iable i% acce%%ible o vi%ible. 0o illu%t ate t,e di55e ence* con%ide t,e 5ollowing ' ocedu e:
S0b ;roced0reA() Di5 Local+ar As 2nte4er Local+ar = 7 Call ;roced0re1 Local+ar = 1 'nd S0b

+ote t,at "ocalVar i% a local va iable. 4,en t,e line:


Call ;roced0re1

i% e6ecuted* e6ecution %witc,e% to ;roced0re1. 4,ile t,e line% o5 ;roced0re1 a e being e6ecuted* t,e va iable Local+ar i% out o5 %co'e* %ince it i% local to ;roced0reA. >ut it i% %till valid. /n ot,e wo d%* t,e va iable %till e6i%t% and ,a% a value* but it i% %i#'l( not acce%%ible to t,e code in ;roced0re1. /n 5act* ;roced0re1 could al%o ,ave a local va iable na#ed "ocalVar* w,ic, would ,ave not,ing to do wit, t,e va iable o5 t,e %a#e na#e in ;roced0reA. Once ;roced0re1 ,a% co#'leted* e6ecution continue% in ;roced0reA wit, t,e line:
Local+ar = 1

0,i% i% a valid in%t uction* %ince t,e va iable "ocalVar i% bac- in %co'e. 0,u%* t,e li5eti#e o5 t,e local va iable "ocalVar e6tend% 5 o# t,e #o#ent t,at ;roced0reA i% ente ed to t,e #o#ent t,at it i% te #inated* including t,e 'e iod du ing w,ic, ;roced0re1 i% being e6ecuted a% a e%ult o5 t,e call to t,i% ' ocedu e* even t,oug, du ing t,at 'e iod* "ocalVar i% out o5 %co'e. /ncidentall(* (ou #a( notice t,at t,e "ic o%o5t ,el' 5ile% occa%ionall( get t,e notion% o5 %co'e and vi%ibilit( #i6ed u' a bit. 0,e c eato % o5 t,e 5ile% %ee# to unde %tand t,e di55e ence* but t,e( donBt alwa(% u%e t,e te #% co ectl(. '.$.12.1 Static variables 0o e'eat* a va iable #a( go in and out o5 %co'e and (et e#ain valid du ing t,at ti#e<t,at i%* etain a value du ing t,at ti#e. Aoweve * once t,e li5eti#e o5 a va iable e6'i e%* t,e va iable i%

de%t o(ed and it% value i% lo%t. /t i% t,e li5eti#e t,at dete #ine% t,e existence o5 a va iableH it% %co'e dete #ine% it% vi%ibilit(. 0,u%* con%ide t,e 5ollowing ' ocedu e%:
S0b ;roced0reA() Call ;roced0re1 Call ;roced0re1 Call ;roced0re1 Call ;roced0re1 Call ;roced0re1 'nd S0b S0b ;roced0re1() Di5 x As 2nte4er x = & . . . 'nd S0b

4,en ;roced0reA i% e6ecuted* it %i#'l( call% ;roced0re1 5ive ti#e%. Eac, ti#e ;roced0re1 i% called* t,e local va iable x i% c eated anew and de%t o(ed at t,e end o5 t,at call. 0,u%* x i% c eated and de%t o(ed 5ive ti#e%. +o #all(* t,i% i% ju%t want we want. Aoweve * t,e e a e ti#e% w,en we would li-e t,e li5eti#e o5 a local va iable to 'e %i%t longe t,an t,e li5eti#e o5 t,e ' ocedu e in w,ic, it i% decla ed. A% an e6a#'le* we #a( want a ' ocedu e to do %o#et,ing %'ecial t,e 5i %t ti#e it i% called* but not %ub%e7uent ti#e%. $o in%tance* t,e 5ollowing one;line #ac o c,ange% t,e 5ont o5 t,e %elected cell% to !o#ic 1an%:
S0b ToCo5ic() Selection.>ont.9a5e = "Co5ic Sans" 'nd S0b

1u''o%e* ,oweve * t,at we wi%, to wa n t,e u%e t,at !o#ic 1an% i% a bit in5o #al and a%- i5 ,e o %,e eall( want% to #a-e t,i% c,ange. 4e donBt want to #a-e a 'e%t o5 ou %elve% b( a%-ing eve ( ti#e t,e u%e invo-e% t,i% #ac o. 4,at we need i% a local va iable wit, a 8#e#o (8 t,at will allow it to -ee' t ac- o5 w,et,e o not a 'a ticula call to ToCo5ic i% t,e 5i %t call o not. 0,i% i% done wit, a %tatic va iable. A static #ariable i% a local va iable w,o%e li5eti#e i% t,e li5eti#e o5 t,e enti e #odule* not ju%t t,e ' ocedu e in w,ic, it wa% decla ed. /n 5act* a %tatic va iable etain% it% value a% long a% t,e docu#ent o te#'late containing t,e code #odule i% active ?even i5 no code i% unning@. 0,u%* a %tatic va iable ,a% t,e %co'e o5 a local va iable* but t,e li5eti#e o5 a #odule;level va iable. !Be%t tout di e 6 !on%ide now t,e #odi5ication o5 t,e ' eceding #ac o* w,ic, i% %,own in E6a#'le 5;2. 0,e code 5i %t decla e% a %tatic >oolean va iable called 9ot>irstTi5e. /t #a( %ee# %i#'le to u%e a va iable called >irstTi5e* but t,e e i% a ' oble#. +a#el(* >oolean va iable% a e auto#aticall( initialiGed a% $al%e* %o t,e 5i %t ti#e t,at t,e ToCo5ic #ac o i% un* >irstTi5e would be $al%e* w,ic, i% not want we want. ?4e will di%cu%% va iable initialiGation a bit late .@ Exa&!le '-2. o+o&ic;< -o#i4ie# to 2se a Static *ariable
S0b ToCo5ic() . Declare static 1oolean variable

Static 9ot>irstTi5e As 1oolean . 26 6irst ti5e= then as8 6or 3er5ission 26 9ot>irstTi5e = >alse Then 26 Ms41ox("Co5ic Sans is a bit in6or5al. ;roceedQ"= G vb@es9o) = vb@es Then . Ma8e the chan4e Selection.>ont.9a5e = "Co5ic Sans MS" 'nd 26 . 9o lon4er the 6irst ti5e 9ot>irstTi5e = Tr0e 'lse . 26 not the 6irst ti5e= T0st 5a8e the chan4e Selection.>ont.9a5e = "Co5ic Sans MS" 'nd 26 'nd S0b

0,e 26 %tate#ent c,ec-% to %ee i5 t,e value o5 NotFirstTime i% $al%e* a% it will be t,e 5i %t ti#e t,e ' ocedu e i% called. /n t,i% ca%e* a #e%%age bo6 i% di%'la(ed* a% %,own in $igu e 5;3. /5 t,e u%e c,oo%e% t,e Le% button* t,e 5ont i% c,anged. /n eit,e ca%e* t,e %tatic >oolean va iable NotFirstTime i% %et to 0 ue. & eci%el( becau%e NotFirstTime i% %tatic* t,i% value will be etained even a5te t,e #ac o end% ?but not i5 t,e docu#ent i% clo%ed@. Figure '-". (ialog that a!!ears i4 the static NotFirst i&e is 4alse

0,e ne6t ti#e t,e #ac o i% e6ecuted* t,e va iable NotFirstTime will be Tr0e* and %o t,e 26 condition:
26 9ot>irstTi5e = >alse Then

will be >alse and t,e Ms41ox 5unction will not be e6ecuted. /n%tead* t,e 'lse code will e6ecute. 0,i% code ju%t c,ange% t,e 5ont* wit,out bot,e ing t,e u%e wit, a #e%%age bo6. 1tatic va iable% a e not u%ed ve ( o5ten* but t,e( can be 7uite u%e5ul at ti#e%. /t #a( ,ave occu ed to (ou t,at we could acco#'li%, t,e %a#e e55ect b( u%ing a #odule;level ' ivate va iable to -ee' a eco d o5 w,et,e o not t,e #ac o ,a% been called* in%tead o5 a %tatic local va iable. Aoweve * it i% con%ide ed bette ' og a##ing %t(le to u%e t,e #o%t e%t ictive %co'e 'o%%ible w,ic,* in t,i% ca%e* i% a local va iable wit, an 8e6tended8 li5eti#e. 0,i% ,el'% ' event accidental alte ation o5 t,e va iable in ot,e 'o tion% o5 t,e code. ?Re#e#be t,at t,i% code #a( be 'a t o5 a #uc, la ge code #odule* wit, a lot o5 t,ing% going on. /t i% bette to ,ide t,e NotFirstTime va iable 5 o# t,i% ot,e code.@

'.$.1" *ariable /nitiali?ation


4,en a ' ocedu e begin% e6ecution* all o5 it% local va iable% a e auto#aticall( initiali7ed* t,at i%* given initial value%. /n gene al* ,oweve * it i% not good ' og a##ing ' actice to el( on t,i%

initialiGation* %ince it #a-e% t,e ' og a# le%% eadable and %o#ew,at #o e ' one to logical e o %. 0,u%* it i% a good idea to initialiGe all local va iable% e6'licitl(* a% in t,e 5ollowing e6a#'le:
S0b 'xa53le() Di5 x As 2nte4er Di5 s As Strin4 x = 7 s = "" . 2nitialiMe x to 7 . 2nitialiMe s to e53t strin4

. 5ore code here . . . 'nd S0b

+ote* ,oweve * t,at %tatic va iable% cannot be initialiGed* %ince t,at de5eat% t,ei 'u 'o%eC 0,u%* it i% i#'o tant to -now t,e 5ollowing ule% t,at =>A u%e% 5o va iable initialiGation ?note al%o t,at t,e( a e intuitive@:
C C C C C

+u#e ic va iable% ?/ntege * 3ong* 1ingle* )ouble* !u enc(@ a e initialiGed to Ge o. A va iable;lengt, %t ing i% initialiGed to a Ge o;lengt, ?e#'t(@ %t ing. A 5i6ed;lengt, %t ing i% 5illed wit, t,e c,a acte e' e%ented b( t,e A1!// c,a acte code 0* o Chr?0@. =a iant va iable% a e initialiGed to '53t . Object va iable% a e initialiGed to 9othin4.

0,e 9othin4 -e(wo d actuall( ,a% %eve al elated u%e% in E6cel =>A. A% we will %ee in !,a'te 8* it i% u%ed to elea%e an object va iable. Al%o* it i% u%ed a% a etu n value 5o %o#e 5unction%* gene all( to indicate t,at %o#e o'e ation ,a% 5ailed. $inall(* it i% u%ed to initialiGe object va iable%.

3%3 ,BA Operators


=>A u%e% a ,and5ul o5 %i#'le o'e ato % and elation%* t,e #o%t co##on o5 w,ic, a e %,own in 0able 5;5. able '-'. *.% 3!erators an# ,elations Name Addition 1ubt action "ulti'lication )ivi%ion )ivi%ion wit, /ntege e%ult E6'onentiation "odulo !oncatenation A+) OR +O0 E7ual 3e%% t,an

Type A it,#etic O'e ato %

Symbol F ; W E X Y "od K A+) OR +O0 T S

1t ing o'e ato 3ogical o'e ato %

!o#'a i%on elation%

. eate t,an 3e%% t,an o e7ual to . eate t,an o e7ual to +ot e7ual to 0,e Mod o'e ato etu n% t,e e#ainde a5te divi%ion. $o e6a#'le:
D Mod B

Z ST ZT SZ

etu n% 2* %ince t,e e#ainde a5te dividing 8 b( 3 i% 2. 0o illu%t ate %t ing concatenation* t,e e6' e%%ion:
"To be or " K "not to be"

i% e7uivalent to:
"To be or not to be"

Chapter 8% :unctions and Subroutines


A% we ,ave %een* =>A allow% two -ind% o5 ' ocedu e%: 5unction% and %ub outine%. A% a e#inde * t,e onl( di55e ence between a 5unction and a %ub outine i% t,at a 5unction etu n% a value* w,e ea% a %ub outine doe% not.

8%$ Calling :unctions


A 5unction decla ation ,a% t,e 5o #:
U;0blic or ;rivateV >0nction FunctionName(Param# As DataTy e#= G Param$ As DataTy e$=...) As %eturnTy e

+ote t,at we #u%t decla e t,e data t('e% not onl( o5 eac, 'a a#ete to t,e 5unction* but al%o o5 t,e etu n t('e. Ot,e wi%e* =>A decla e% t,e%e ite#% a% va iant%. 4e will di%cu%% t,e o'tional -e(wo d% ;0blic and ;rivate late in t,i% c,a'te * but (ou can ' obabl( gue%% t,at t,e( a e u%ed ,e e to indicate t,e %co'e o5 t,e 5unction* ju%t a% t,e( a e u%ed in va iable decla ation%. $o e6a#'le* t,e Add*ne 5unction in E6a#'le 6;1 add% 1 to t,e o iginal value. Exa&!le 5-1. he %##3ne Function
;0blic >0nction Add*ne(+al0e As 2nte4er) As 2nte4er Add*ne = +al0e A 1 'nd >0nction

0o u%e t,e etu n value o5 a 5unction* we ju%t 'lace t,e call to t,e 5unction wit,in t,e e6' e%%ion* in t,e location w,e e we want t,e value. $o in%tance* t,e code:
Ms41ox "Addin4 1 to & 4ives- " K Add*ne(&)

' oduce% t,e #e%%age bo6 in $igu e 6;1* w,e e t,e e6' e%%ion Add*ne ?5@ i% e'laced b( t,e etu n value o5 Add*ne* w,ic,* in t,i% ca%e* i% 6. Figure 5-1. he &essage #ialog #is!laye# by Exa&!le 5-1

+ote t,at* in gene al* an( 'a a#ete % to a 5unction #u%t be enclo%ed in 'a ent,e%e% wit,in t,e 5unction call.

59

/n o de to etu n a value 5 o# a 5unction* we #u%t a%%ign t,e 5unctionB% na#e to t,e etu n value %o#ew,e e wit,in t,e bod( o5 t,e 5unction. E6a#'le 6;2 %,ow% a %lig,tl( #o e co#'licated e6a#'le o5 a 5unction. Exa&!le 5-2. %ssigning a Function>s ,eturn *alue
>0nction "et0rnCo0nt() As +ariant . "et0rn co0nt o6 cells in c0rrent selection 26 T 3e9a5e(Selection) = ""an4e" Then "et0rnCo0nt = Selection.Co0nt 'lse "et0rnCo0nt = "9ot a33licable" 'nd 26 'nd >0nction

0,i% 5unction etu n% a count o5 t,e nu#be o5 cell% in t,e cu ent %election* ' ovided t,at t,e %election i% a ange o5 cell%. /5 t,e %election i% anot,e t('e o5 object ?%uc, a% a c,a t@* t,e 5unction etu n% t,e wo d% 8+ot a''licable.8 +ote t,at %ince t,e etu n value #a( be a nu#be o a %t ing* we decla e t,e etu n t('e a% =a iant. +ote al%o t,at %eturnCount i% a%%igned twice wit,in t,e bod( o5 t,e 5unction. /t% value* and ,ence t,e value o5 t,e 5unction* i% %et di55e entl( de'ending u'on t,e value etu ned b( t,e T 3e9a5e(Selection) 5unction. 1ince t,e%e a%%ign#ent% a e #utuall( e6clu%ive* onl( one o5 t,e# will occu eac, ti#e t,e 5unction i% called. >ecau%e 5unction% etu n value%* (ou canBt call t,e# di ectl( 5 o# t,e "ac o dialog t,at a''ea % w,en (ou %elect 0ool% "ac o "ac o%* no can (ou a%%ign t,e# to an E6cel toolba o #enu t, oug, E6celB% u%e inte 5ace. /5 (ou want to be able to call a 5unction* (ouBll ,ave to 8w a'8 it in<t,at i%* ,ave it called b(<a %ub outine* t,e to'ic t,at weBll cove ne6t.

8%' Calling Subroutines


A %ub outine decla ation ,a% t,e 5o #:
U;0blic or ;rivateV S0b SubroutineName(Param# As DataTy e#= G Param$ As DataTy e$=...)

0,i% i% %i#ila to t,e 5unction decla ation* wit, t,e notable ab%ence o5 t,e As %eturnTy e 'o tion. ?+ote al%o t,e wo d S0b in 'lace o5 >0nction .@ 1ince %ub outine% do not etu n a value* t,e( cannot be u%ed wit,in an e6' e%%ion. 0o call a %ub outine na#ed S0bro0tineA* we can w ite eit,e :
Call S0bro0tineA(3ara5eters= . . .)

o %i#'l(:
S0bro0tineA 3ara5eters= . . .

+ote t,at an( 'a a#ete % #u%t be enclo%ed in 'a ent,e%e% w,en u%ing t,e Call -e(wo d* but not ot,e wi%e.

8%* Parameters and Arguments


!on%ide t,e 5ollowing ve ( %i#'le %ub outine* w,ic, doe% not,ing #o e t,an di%'la( a #e%%age bo6 decla ing a 'e %onB% na#e:
S0b Dis3la 9a5e(s9a5e As Strin4) Ms41ox "M na5e is " K s9a5e 'nd S0b

0o call t,i% %ub outine* we would w ite* 5o e6a#'le:


Dis3la 9a5e "Hol64an4"

o:
Call Dis3la 9a5e("Hol64an4")

0,e va iable sName in t,e ' ocedu e decla ation:


S0b Dis3la 9a5e(s9a5e As Strin4)

i% called a parameter o5 t,e ' ocedu e. 0,e call to t,e ' ocedu e %,ould contain a %t ing va iable o a lite al %t ing t,at i% e' e%ented b( t,e va iable sName in t,i% ' ocedu e ?but %ee t,e di%cu%%ion o5 o'tional 'a a#ete % in t,e ne6t %ection@. 0,e value u%ed in 'lace o5 t,e 'a a#ete w,en we #a-e t,e ' ocedu e call i% called an argument. 0,u%* in t,e ' eviou% e6a#'le* t,e a gu#ent i% t,e %t ing 84ol5gang.8 +ote t,at #an( ' og a##e % 5ail to #a-e a di%tinction between 'a a#ete % and a gu#ent%* u%ing t,e na#e% inte c,angeabl(. Aoweve * %ince a 'a a#ete i% li-e a va iable and an a gu#ent i% li-e a #alue o5 t,at va iable* 5ailing to #a-e t,i% di%tinction i% li-e 5ailing to di%tingui%, between a va iable and it% valueC

5.".1 3!tional %rgu&ents


/n =>A* t,e a gu#ent% to a ' ocedu e #a( be %'eci5ied a% o'tional* u%ing t,e *3tional -e(wo d. ?/t #a-e% no %en%e to %a( t,at a 'a a#ete i% o'tionalH it i% t,e value t,at i% o'tional.@ 0o illu%t ate* con%ide t,e ' ocedu e in E6a#'le 6;3* w,ic, %i#'l( c,ange% t,e 5ont na#e and 5ont %iGe o5 t,e cu ent %election: Exa&!le 5-". 2sing an 3!tional %rgu&ent
S0b Chan4e>or5attin4(>ont9a5e As Strin4= G *3tional >ontSiMe As +ariant) . Chan4e 6ont na5e Selection.>ont.9a5e = >ont9a5e . Chan4e 6ont siMe i6 ar405ent is s033lied 26 9ot 2sMissin4(>ontSiMe) Then Selection.>ont.SiMe = C2nt(>ontSiMe) 'nd 26 'nd S0b

0,e %econd 'a a#ete i% decla ed wit, t,e *3tional -e(wo d. >ecau%e o5 t,i%* we #a( call t,e ' ocedu e wit, o wit,out an a gu#ent 5o t,i% 'a a#ete * a% in:
Chan4e>or5attin4("Arial 9arrow"= 2L)

and:
Chan4e>or5attin4("Arial 9arrow")

+ote t,at t,e 2sMissin4 5unction i% u%ed in t,e bod( o5 t,e ' ocedu e to te%t w,et,e t,e a gu#ent i% ' e%ent. /5 t,e a gu#ent i% ' e%ent* t,en t,e 5ont %iGe i% c,anged. +ote al%o t,at we decla ed t,e FontSi&e 'a a#ete a% t('e =a iant becau%e 2sMissin4 wo -% onl( wit, 'a a#ete % o5 t('e =a iant ?un5o tunatel(@. 0,u%* we conve ted t,e =a iant to t('e /ntege u%ing t,e C2nt 5unction. A ' ocedu e #a( ,ave an( nu#be o5 o'tional a gu#ent%* but t,e( #u%t all co#e at t,e end o5 t,e 'a a#ete li%t. 0,u%* 5o in%tance* t,e 5ollowing decla ation i% not legal:
S0b Chan4e>or5attin4(*3tional >ont9a5e As Strin4= >ontSiMe As Sin4le)

/5 we o#it an o'tional a gu#ent in t,e #iddle o5 a li%t* we #u%t include an e#'t( %'ace w,en calling t,at ' ocedu e. $o in%tance* i5 a ' ocedu e i% decla ed a% 5ollow%:
S0b Chan4e>or5attin4(*3tional >ont9a5e As Strin4= G *3tional >ontSiMe As Sin4le= G *3tional >ont1old as 1oolean)

t,en a call to t,i% ' ocedu e to %et t,e 5ont na#e to A ial and t,e bold5acing to 0 ue would looli-e:
Chan4e>or5at "Arial"= = Tr0e

0o avoid con5u%ion* we %,ould 'oint out t,at %o#e built;in E6cel ' ocedu e% ,ave o'tional a gu#ent% and ot,e % do not. O5 cou %e* we canBt leave out an a gu#ent unle%% t,e docu#entation o decla ation 5o t,e ' ocedu e %'eci5icall( %tate% t,at itB% o'tional.

5.".2 Na&e# %rgu&ents


1o#e =>A ' ocedu e% can contain a la ge nu#be o5 'a a#ete %. $o e6a#'le* one 5o # o5 t,e E6cel SaveAs 5unction ,a% t,e decla ation:
SaveAs (>ilena5e As strin4= >ile>or5at As +A"2A9T= ;assword As +A"2A9T= G Hrite"es;assword As +A"2A9T= "ead*nl "eco55ended As +A"2A9T= G Create1ac803 As +A"2A9T= AddToMr0 As +A"2A9T= TextCode3a4e As G +A"2A9T= Text+is0alLa o0t As +A"2A9T)

w,e e all o5 t,e 'a a#ete % a e o'tional. Ae e i% an e6a#'le o5 a call to t,i% ' ocedu e:
SaveAs "c-Ite53Itest.xls"= = = = = Tr0e = = = Tr0e

+ot ve ( eadable* i% it9 0,e a gu#ent% %,own in t,e ' eviou% call a e %aid to be positional arguments becau%e it i% t,ei 'o%ition t,at tell% =>A w,ic, 'a a#ete % t,e( a e intended to e'lace. 0,i% i% w,( we need to include %'ace 5o #i%%ing a gu#ent%.

Aoweve * =>A can al%o u%e named arguments* in w,ic, ca%e t,e ' eviou% call would be w itten a%:
SaveAs >ile9a5e-="c-Ite53Itest.xls"= G Create1ac803-=Tr0e= G AddToMr0-=Tr0e

+ote t,e %'ecial %(nta6 5o na#ed a gu#ent%* in 'a ticula * t,e colon be5o e t,e e7ual %ign. 0,i% 5unction call i% a g eat i#' ove#ent ove t,e 'o%itional a gu#ent ve %ion. /n gene al* t,e advantage% o5 na#ed a gu#ent% ove 'o%itional a gu#ent% a e t, ee5old:
C C C

+a#ed a gu#ent% can i#' ove eadabilit( and cla it(. >lan- %'ace% ?%e'a ated b( co##a%@ a e e7ui ed 5o #i%%ing o'tional a gu#ent% w,en u%ing a 'o%itional decla ation* but not w,en u%ing na#ed a gu#ent%. 0,e o de in w,ic, na#ed a gu#ent% a e li%ted i% i##ate ial* w,ic,* o5 cou %e* i% not t,e ca%e 5o 'o%itional a gu#ent%.

+a#ed a gu#ent% can i#' ove eadabilit( 7uite a bit and a e ,ig,l( eco##ended. Aoweve * t,e( can e7ui e con%ide abl( #o e %'ace* %o 5o t,e %,o t e6a#'le% in t,i% boo-* we u%uall( will not u%e t,e#.

5."." .y,e4 *ersus .y*al Para&eters


&a a#ete % co#e in two 5lavo %:1 "e6 and 1 +al. "an( ' og a##e % do not ,ave a clea unde %tanding o5 t,e%e conce't%* but t,e( a e ve ( i#'o tant and not t,at di55icult to unde %tand. 0o e6'lain t,e di55e ence* con%ide t,e two ' ocedu e% in E6a#'le 6;4. ;roced0reA %i#'l( %et% t,e value o5 t,e #odule;level va iable x to 5* di%'la(% t,at value* call% t,e ' ocedu e Add*ne wit, t,e a gu#ent x * and t,en di%'la(% t,e value o5 x again. Exa&!le 5-$. esting the .y*al an# .y,e4 @eywor#s
S0b ;roced0reA() x = & Ms41ox x Call Add*ne(x) Ms41ox x 'nd S0b . . . . Set x to & Dis3la x Call Add*ne Dis3la x a4ain

S0b Add*ne(1 "e6 i As 2nte4er) i = i A 1 'nd S0b

+ote t,e ' e%ence o5 t,e 1 "e6 -e(wo d in t,e Add*ne ' ocedu e decla ation. 0,i% -e(wo d tell% =>A to 'a%% a e5e ence to t,e va iable x to t,e Add*ne ' ocedu e. 0,e e5o e* t,e Add*ne ' ocedu e* in e55ect* e'lace% it% 'a a#ete i b( t,e va iable x. A% a e%ult* t,e line:
i = i A 1

e55ectivel( beco#e%:
x = x A 1

1o* a5te Add*ne i% called* t,e va iable x ,a% t,e value 6.

On t,e ot,e ,and* %u''o%e we c,ange t,e Add*ne ' ocedu e* e'lacing t,e -e(wo d 1 "e6 wit, t,e -e(wo d 1 +al:
S0b Add*ne(1 +al i As 2nte4er) i = i A 1 'nd S0b

/n t,i% ca%e* =>A doe% not 'a%% a e5e ence to t,e va iable x* but at,e it 'a%%e% it% #alue. Aence* t,e va iable i in Add*ne %i#'l( ta-e% on t,e value 5. Adding 1 to t,at value give% 6. 0,u%* i e7ual% 6* but t,e value o5 t,e a gu#ent x i% not a55ectedC Aence* bot, #e%%age bo6e% will di%'la( t,e value 5 5o x.
1 "e6 and 1 +al bot, ,ave t,ei u%e%. 4,en we want to c,ange t,e value o5 an a gu#ent* we #u%t decla e t,e co e%'onding 'a a#ete a% 1 "e6* %o t,at t,e called ' ocedu e ,a% acce%% to t,e actual a gu#ent it%el5. 0,i% i% t,e ca%e in t,e ' eviou% e6a#'le. Ot,e wi%e* t,e Add*ne ' ocedu e doe% ab%olutel( not,ing* %ince t,e local va iable i i% inc e#ented* and it i% de%t o(ed

i##ediatel( a5te wa d%* w,en t,e ' ocedu e end%.

On t,e ot,e ,and* w,en we 'a%% an a gu#ent 5o in5o #ational 'u 'o%e% onl(* and we do not want t,e a gu#ent to be alte ed* it %,ould be 'a%%ed b( value* u%ing t,e 1 +al -e(wo d. /n t,i% wa(* t,e called ' ocedu e get% onl( t,e #alue o5 t,e a gu#ent. 0o illu%t ate 5u t,e * ;roced0reA in E6a#'le 6;5 get% t,e te6t o5 t,e 5i %t cell and 5eed% it to t,e Co0ntCharacters 5unction. 0,e etu ned value ?t,e nu#be o5 c,a acte % in t,e active docu#ent@ i% t,en di%'la(ed in a #e%%age bo6. Exa&!le 5-'. Passing an %rgu&ent by *alue
S0b ;roced0reA() Di5 sText As Strin4 sText = ActiveSheet.Cells(1=1).Text Ms41ox Co0ntCharacters(sText) 'nd S0b >0nction Co0ntCharacters(1 +al sTxt As Strin4) Co0ntCharacters = Len(sTxt) 'nd >0nction

+ow* Co0ntCharacters doe% not need to* and indeed %,ould not* c,ange t,e te6t. /t onl( count% t,e nu#be o5 c,a acte % in t,e te6t. 0,i% i% w,( we 'a%% t,e a gu#ent b( value. /n t,i% wa(* t,e va iable sTxt get% t,e value o5 t,e te6t in sText* t,at i%* it get% a copy o5 t,e te6t. 0o a'' eciate t,e i#'o tance o5 t,i%* i#agine 5o a #o#ent t,at Co0ntCharacters i% e'laced b( a ' ocedu e t,at contain% ,und ed% o t,ou%and% o5 line% o5 code* w itten b( %o#eone el%e* 'e ,a'% not a% eliable a% we a e. +atu all(* we do not want t,i% ' ocedu e to c,ange ou te6t. Rat,e t,an ,aving to c,ec- t,e code 5o e o %* all we need to do i% notice t,at t,e sTxt 'a a#ete i% called b( value* w,ic, tell% u% t,at t,e ' ocedu e doe% not even ,ave acce%% to ou te6t. /n%tead* it get% onl( a co'( o5 t,e te6t. 0,e e i% one down%ide to 'a%%ing a gu#ent% b( value: it can ta-e a lot o5 #e#o ( ?and ti#e@. $o in%tance* in t,e ' eviou% e6a#'le* =>A need% to #a-e a co'( o5 t,e te6t to 'a%% to t,e 'a a#ete sTxt. 0,u%* we can %u##a iGe b( %a(ing t,at i5 we want t,e ' ocedu e to #odi5( an a gu#ent* t,e a gu#ent #u%t be 'a%%ed b( e5e ence. /5 not* t,e a gu#ent %,ould be 'a%%ed b( value unle%% t,i%

will ' oduce an unacce'table dec ea%e in 'e 5o #ance o unle%% we a e ve ( %u e t,at it will not get c,anged b( accident. /t i% i#'o tant to note t,at =>A de5ault% to 1 "e6 i5 we do not %'eci5( ot,e wi%e. 0,i% #ean% t,at t,e value% o5 a gu#ent% a e %ubject to c,ange b( t,e called ' ocedu e* unle%% we e6'licitl( include t,e -e(wo d 1 +al. (a#eat scriptor 6

8%2 Exiting a Procedure


=>A ' ovide% t,e 'xit S0b and 'xit >0nction %tate#ent%* %,ould we wi%, to e6it 5 o# a ' ocedu e be5o e t,e ' ocedu e would te #inate natu all(. $o in%tance* i5 t,e value o5 a 'a a#ete i% not %uitable* we #a( want to i%%ue a wa ning to t,e u%e and e6it* a% E6a#'le 6;6 %,ow%. Exa&!le 5-5. 2sing the Exit Sub State&ent
S0b Dis3la 9a5e(s9a5e As Strin4) 26 s9a5e = "" then Ms4box ";lease enter a na5e." 'xit S0b 'nd 26 Ms41ox "9a5e entered is " K s9a5e 'nd S0b

8%3 Public and Pri ate Procedures


Nu%t a% va iable% and con%tant% ,ave a %co'e* %o do ' ocedu e%. 4e can decla e a ' ocedu e u%ing t,e ;0blic o ;rivate -e(wo d* a% in:
;0blic >0nction Add*ne(i As 2nte4er) As 2nte4er

o:
;rivate >0nction Add*ne(i As 2nte4er) As 2nte4er

0,e di55e ence i% %i#'le: a ;rivate ' ocedu e can onl( be called 5 o# wit,in t,e #odule in w,ic, it i% de5ined* w,e ea% a ;0blic ' ocedu e can be called 5 o# wit,in an( #odule in t,e ' oject. +ote t,at i5 t,e ;0blic o ;rivate -e(wo d i% o#itted 5 o# a ' ocedu e decla ation* t,en t,e ' ocedu e i% con%ide ed to be ;0blic.

8%8 Pro1ect 6eferences


/n o de 5o code in one ' oject to call a 'ublic ' ocedu e in anot,e ' oject* t,e calling ' oject #u%t ,ave a e5e ence to t,e called ' oject.

.ene all(* a ' oject t,at i% a%%ociated wit, a wo -boo- i% inte e%ted onl( in ' ocedu e% t,at lie in t,at ' oject. /n 5act* gene all( it would be bad ' og a##ing ' actice to e7ui e a ' ocedu e in one ' oject to call a ' ocedu e in anot,e ' oject. +onet,ele%%* t,e e #a( be occa%ion% w,en t,i% i% e7ui ed. 0o add a e5e ence to t,e calling ' oject* we u%e t,e Re5e ence% dialog bo6 ?unde t,e 0ool% #enu@* %,own in $igu e 6;2. Figure 5-2. he ,e4erences #ialog box

5.5.1 Fully =uali4ie# Proce#ure Na&es


4,en we call a 'ublic ' ocedu e t,at lie% in anot,e #odule* t,e e i% a 'otential ' oble# wit, a#biguit(* 5o t,e e #a( be #o e t,an one 'ublic ' ocedu e wit, t,e %a#e na#e in anot,e #odule. =>A will e6ecute t,e 5i %t one it 5ind%* and t,i% #a( not be t,e one we ,ad in #indC 0,e %olution i% to u%e a 8ualified procedure name* w,ic, ,a% t,e 5o #:
Mod0le9a5e.;roced0re9a5e

$o in%tance* i5 a 'ublic ' ocedu e na#ed Add*ne lie% in a #odule na#ed 2tilitie%* t,en we can call t,i% ' ocedu e u%ing t,e %(nta6:
<tilities.Add*ne

/5 nece%%a (* we can al%o %'eci5( t,e ' oject na#e* u%ing t,e %(nta6 ?donBt 5o get to %et t,e e5e ence 5i %t@:
ProjectName'(oduleName'ProcedureName

/t i% i#'o tant to note t,at ProjectName i% t,e code na#e o5 t,e ' oject* not t,e 5ilena#e. ?0,e de5ault code na#e i% =>A& oject.@

Chapter 9% Built;in :unctions and Statements


=>A ,a% a la ge nu#be o5 built;in 5unction% and %tate#ent%. $o 'o%%ible e5e ence* 0able 7;1 %,ow% t,e =>A 5unction%* and 0able 7;2 %,ow% t,e %tate#ent%. 4e will ta-e a loo- at a 5ew o5 t,e #o e co##onl( u%ed 5unction% and %tate#ent% 5o ' og a##ing E6cel =>A in t,i% c,a'te and !,a'te 8. 0o ,el' %i#'li5( t,e e6'o%ition* we will 5ollow "ic o%o5tB% lead and u%e %7ua e b ac-et% to indicate o'tional 'a a#ete %. 0,u%* 5o in%tance* t,e %econd 'a a#ete in t,e 5ollowing ' ocedu e i% o'tional:
S0b Chan4e>or5at(FontName U= FontSi&eV)

+ote t,at we ,ave al%o o#itted t,e data t('e decla ation%* w,ic, will be di%cu%%ed %e'a atel(. able 6-1. *.% A1B Functions /%E#'t( /%E o /%"i%%ing /%+ull /%+u#e ic /%Object NoinW 3bound 3ca%e 3e5t 3e5t> 3en 3en> 3oad&ictu e 3oc 3O$ 3og 3t i# "id "id> "inute "/RR "ont, "ont,+a#eW "%g>o6 +ow +'e +&= Oct &a tition &#t

Ab% A a( A%c A%c> A%c4 Atn !all>(+a#eW !bool !b(te !cu !date !)bl !dec !,oo%e !, !, > !, 4 !int !3ng !o##and !o% ! eateObject !1ng !1t !u )i !va !=)ate !=E )ate )ateAdd )ate)i55

)oEvent% Envi on EO$ E o E6' $ileAtt $ile)ate0i#e $ile3en $ilte W $i6 $o #at $o #at!u enc(W $o #at)ate0i#eW $o #at+u#be W $o #at&e centW $ ee$ile $= .etAll1etting% .etAtt .etAuto1e ve 1etting% .etObject .et1etting Ae6 Aou /i5 /"E1tatu% /n'ut /n'ut> /n'ut>o6 /n1t /n1t >

Rig,t Rig,t> Rnd RoundW R0 i# 1econd 1ee1gn 1,ell 1in 13+ 1'ace 1'c 1'litW 17 1t 1t !o#' 1t !onv 1t ing 1t Reve %eW 1witc, 1L) 0ab 0an 0i#e 0i#e 0i#e1e ial 0i#e=alue 0 i# 0('e+a#e 2>ound

)ate&a t )ate1e ial )ate=alue )a( ))> )i


E1F

/n%t RevW /nt /'#t /RR /%A a( /%)ate

&&#t &= O>!olo Rate Re'laceW R.>

2!a%e =al =a 0('e 4ee-da(4ee-da(+a#eW Lea

Items mar2e( +ith an asteris2 GHI are 8or 688ice ;J1< onl'.

9%$ The 7sgBox :unction


4e ,ave been u%ing t,e Ms41ox 5unction uno55iciall( 5o %o#e ti#e now. 3et u% int oduce it o55iciall(. 0,e Ms41ox 5unction i% u%ed to di%'la( a #e%%age and wait 5o t,e u%e to e%'ond b( 'u%,ing a button. 0,e #o%t co##onl( u%ed %(nta6 i%:
Ms41ox( rom t U= buttonsV U= titleV)

0,i% i% not t,e 5unctionB% co#'lete %(nta6. 0,e e a e %o#e additional o'tional 'a a#ete % elated to ,el' conte6t% t,at (ou can loo- u' in t,e ,el' docu#entation.
rom t i% a 1t ing 'a a#ete containing t,e #e%%age to be di%'la(ed in t,e dialog bo6. +ote t,at a #ultiline #e%%age can be c eated b( inte %'e %ing t,e vbCrL6 con%tant wit,in t,e #e%%age. buttons i% a 3ong 'a a#ete giving t,e %u# o5 value% t,at %'eci5( va iou% ' o'e tie% o5 t,e

#e%%age bo6. 0,e%e ' o'e tie% a e t,e nu#be and t('e o5 button% to di%'la(* t,e icon %t(le to u%e* t,e identit( o5 t,e de5ault button* and t,e #odalit( o5 t,e #e%%age bo6. ?A system modal dialog bo6 e#ain% on to' o5 all cu entl( o'en window% and ca'tu e% t,e in'ut 5ocu% %(%te#wide* w,e ea% an application modal dialog bo6 e#ain% on to' o5 t,e a''licationB% window% onl( and ca'tu e% t,e a''licationB% 5ocu%.@ 0,e va iou% value% o5 )uttons t,at we can %u# a e %,own in 0able 7;2. ?0,e( a e o55iciall( de5ined in t,e +bMs41oxSt le enu#.@ able 6-2. he -sg.ox .uttons %rgu&ent *alues Constant Value Des ription vbOROnl( 0 )i%'la( OR button onl( vbOR!ancel 1 )i%'la( OR and !ancel button% vbAbo tRet (/gno e 2 )i%'la( Abo t* Ret (* and /gno e button% vbLe%+o!ancel 3 )i%'la( Le%* +o* and !ancel button% vbLe%+o 4 )i%'la( Le% and +o button% vbRet (!ancel 5 )i%'la( Ret ( and !ancel button% vb! itical 16 )i%'la( ! itical "e%%age icon vbOue%tion 32 )i%'la( 4a ning Oue ( icon vbE6cla#ation 48 )i%'la( 4a ning "e%%age icon vb/n5o #ation 64 )i%'la( /n5o #ation "e%%age icon vb)e5ault>utton1 0 $i %t button i% de5ault vb)e5ault>utton2 256 1econd button i% de5ault vb)e5ault>utton3 512 0,i d button i% de5ault vb)e5ault>utton4 768 $ou t, button i% de5ault

Purpose >utton t('e%

/con t('e%

)e5ault button

"odalit(

vbA''lication"odal vb1(%te#"odal

0 4096

A''lication #odal #e%%age bo6 1(%te# #odal #e%%age bo6

$o in%tance* t,e code:


Ms41ox ";roceedQ"= vb,0estion A vb@es9o

di%'la(% t,e #e%%age bo6 %,own in $igu e 7;1* w,ic, include% a 7ue%tion #a - icon and two co##and button%* labeled Le% and +o. Figure 6-1. % -sg.ox #ialog box

0,e title 'a a#ete i% a %t ing e6' e%%ion t,at i% di%'la(ed in t,e title ba o5 t,e dialog bo6. /5 we o#it t,i% a gu#ent* t,en 8"ic o%o5t E6cel8 will be di%'la(ed* a% in $igu e 7;1. 0,e Ms41ox 5unction etu n% a nu#be indicating w,ic, button wa% %elected. 0,e%e etu n value% a e given in 0able 7;3. ?0,e( a e o55iciall( de5ined in t,e +bMs41ox"es0lt enu#.@ able 6-". -sg.ox ,eturn *alues Value Des ription OR button ' e%%ed !ancel button ' e%%ed Abo t button ' e%%ed Ret ( button ' e%%ed /gno e button ' e%%ed Le% button ' e%%ed +o button ' e%%ed

Constant vbOR vb!ancel vbAbo t vbRet ( vb/gno e vbLe% vb+o 1 2 3 4 5 6 7

9%' The &nputBox :unction


0,e 2n30t1ox 5unction i% de%igned to get in'ut 5 o# t,e u%e . 0,e #o%t co##onl( u%ed ?but not t,e co#'lete@ %(nta6 i%:
2n30t1ox( rom t U= titleV U= de*aultV)

w,e e rom t i% t,e #e%%age in t,e in'ut bo6* title i% t,e title 5o t,e in'ut bo6* and de*ault i% t,e de5ault value t,at i% di%'la(ed in t,e te6t bo6. $o in%tance* t,e code:
s9a5e = 2n30t1ox("'nter o0r na5e."= "9a5e"= "Albert")

' oduce% t,e dialog bo6 in $igu e 7;2.

Figure 6-2. %n /n!ut.ox #ialog box

0,e 2n30t1ox 5unction etu n% t,e %t ing t,at t,e u%e ente % into t,e te6t bo6. 0,u%* in ou e6a#'le* t,e %t ing va iable sName will contain t,i% %t ing. +ote t,at i5 we want a nu#be 5 o# t,e u%e * we can %till u%e t,e 2n30t1ox 5unction and %i#'l( conve t t,e etu ned %t ing ?%uc, a% 812.558@ to a nu#be ?12.55@ u%ing t,e +al 5unction* di%cu%%ed late in t,e c,a'te .

9%* ,BA String :unctions


Ae e a e a ,and5ul o5 u%e5ul 5unction% t,at a''l( to %t ing% ?bot, con%tant% and va iable%@: !he Len function 0,e Len 5unction etu n% t,e lengt, o5 a %t ing<t,at i%* t,e nu#be o5 c,a acte % in t,e %t ing. 0,u%* t,e code:
Len("San0ar 2nvoice")

etu n% t,e nu#be 1&. !he '(ase and L(ase functions 0,e%e 5unction% etu n an all u''e ca%e o all lowe ca%e ve %ion o5 t,e %t ing a gu#ent. 0,e %(nta6 i%:
<Case(strin!) LCase(strin!)

$o in%tance:
Ms41ox <Case("Donna")

will di%'la( t,e %t ing D*99A. !he Left9 ,ight9 and Mid functions 0,e%e 5unction% etu n a 'o tion o5 a %t ing. /n 'a ticula :
Le6t(strin4= n05ber)

etu n% t,e le5t#o%t number c,a acte % in strin!* and:


"i4ht(strin!= number)

etu n% t,e ig,t#o%t number c,a acte % in strin!. $o in%tance:


Ms41ox "i4ht("Donna S5ith"= &)

di%'la(% t,e %t ing S5ith. 0,e %(nta6 5o Mid i%:


Mid(strin!= start= len!th)

0,i% 5unction etu n% t,e 5i %t len!th nu#be o5 c,a acte % o5 strin!* %ta ting at c,a acte nu#be start. $o in%tance:
Mid("Librar .xls"=E=B)

etu n% t,e %t ing xls. /5 t,e len!th 'a a#ete i% #i%%ing* a% in:
Mid("Librar .xls"=E)

t,e 5unction will etu n t,e e%t o5 t,e %t ing* %ta ting at start. !he n/tr9 n/tr,e# functions 0,e %(nta6 5o t,e ve ( u%e5ul /n1t 5unction i%:
2nstr(Start= Strin!ToSearch= Strin!ToFind)

0,e etu n value i% t,e 'o%ition* beginning at Start* o5 t,e 5i %t occu ence o5 Strin!ToFind wit,in Strin!ToSearch. /5 Start i% #i%%ing* t,en t,e 5unction %ta t% %ea c,ing at t,e beginning o5 Strin!ToSearch. $o in%tance:
Ms41ox 2nstr(1= "Donna S5ith"= "S5ith")

di%'la(% t,e nu#be F* becau%e 81#it,8 begin% at t,e %event, 'o%ition in t,e %t ing 8)onna 1#it,.8 0,e /n1t Rev 5unction i% analogou% to /n1t but %ea c,e% bac-wa d% t, oug, t,e 1t ing0o1ea c, %t ing. !he ,eplace function 0,i% ve ( u%e5ul 5unction ?not available in E6cel 97@ i% u%ed to e'lace a %ub%t ing in a %t ing b( anot,e %t ing. $o in%tance* t,e code:
Ms41ox "e3lace("the car is red"= "red"= "bl0e")

di%'la(% t,e %t ing 8t,e ca i% blue8. !he /tr and Val functions

0,e Str 5unction conve t% a nu#be to a %t ing. $o in%tance:


Str(12B)

etu n% t,e %t ing 12B. !onve %el(* t,e +al 5unction conve t% a %t ing t,at e' e%ent% a nu#be into a nu#be ?%o t,at we can do a it,#etic wit, it* 5o in%tance@. $o e6a#'le:
+al("L.&")

etu n% t,e nu#be L.& and:


+al("12BL Main Street")

etu n% t,e nu#be 12BL. +ote* ,oweve * t,at +al doe% not ecogniGe dolla %ign% o co##a%. 0,u%:
+al(C12.77)

etu n% 7* not 12.77. !he !rim9 L!rim9 and ,!rim functions 0,e LTri5 5unction e#ove% leading %'ace% 5 o# a %t ing. 1i#ila l(* "Tri5 e#ove% t ailing %'ace%* and Tri5 e#ove% bot, leading and t ailing %'ace%. 0,u%:
Tri5(" extra ")

etu n% t,e %t ing extra. !he /tring and /pace functions 0,e Strin4 5unction ' ovide% a wa( to 7uic-l( c eate a %t ing t,at con%i%t% o5 a %ingle c,a acte e'eated a nu#be o5 ti#e%. $o in%tance:
sText = Strin4(2&= "1")

%et% sText to a %t ing con%i%ting o5 25 >%. 0,e S3ace 5unction etu n% a %t ing con%i%ting o5 a given nu#be o5 %'ace%. $o in%tance:
sText = S3ace(2&)

%et% sText to a %t ing con%i%ting o5 25 %'ace%. !he Li)e operator and /tr(mp function 0,e Li8e o'e ato i% ve ( u%e5ul 5o co#'a ing two %t ing%. O5 cou %e* we can u%e t,e e7ual %ign* a% in:
strin41 = strin42

w,ic, i% t ue w,en t,e two %t ing% a e identical. Aoweve * Li8e will al%o #a-e a ca%e in%en%itive co#'a i%on o allow t,e u%e o5 'atte n #atc,ing. 0,e e6' e%%ion:

strin! Li8e

attern

etu n% Tr0e i5 strin! 5it% attern* and >alse ot,e wi%e. ?Actuall(* t,e e6' e%%ion can al%o etu n 90ll.@ 4e will de%c ibe attern in a #o#ent. 0,e t('e o5 %t ing co#'a i%on t,at t,e Li8e o'e ato u%e% de'end% u'on t,e %etting o5 t,e *3tion Co53are %tate#ent. 0,e e a e two 'o%%ibilitie%* one o5 w,ic, %,ould be 'laced in t,e )ecla ation% %ection o5 a #odule ?in t,e %a#e 'lace a% *3tion 'x3licit@:
*3tion Co53are 1inar *3tion Co53are Text

+ote t,at t,e de5ault i% *3tion Co53are 1inar . 2nde *3tion Co53are 1inar * %t ing co#'a i%on i% in t,e o de given b( t,e A+1/ c,a acte code* a% %,own ,e e: AS>S...S[SaSbS...SGS\S...S]S^S...S_ 2nde *3tion Co53are Text* %t ing co#'a i%on i% ba%ed on a ca%e;in%en%itive %o t o de ?dete #ined b( (ou &!B% locale %etting@. 0,i% give% a %o t o de a% %,own ,e e: ATaS\T^S>TbS...S[TGS]T_ >( t,e wa(* t,e la%t ite# in t,e Text %o t o de i% t,e 8Q8 c,a acte * wit, A+1/ value 91. 0,i% i% u%e5ul to -now i5 (ou want to 'lace an ite# la%t in al',abetical o de <ju%t %u ound it wit, %7ua e b ac-et%. 0,e 'atte n;#atc,ing 5eatu e% o5 t,e Li8e o'e ato allow t,e u%e o5 wildca d c,a acte %* c,a acte li%t%* o c,a acte ange%. $o e6a#'le: : "atc,e% an( %ingle c,a acte ; "atc,e% Ge o o #o e c,a acte % < #atc,e% an( %ingle digit ?0;9@ =charlist> "atc,e% an( %ingle c,a acte in charlist =6charlist> "atc,e% an( %ingle c,a acte not in charlist $o #o e detail%* c,ec- t,e =>A ,el' 5ile.

0,e StrC53 5unction al%o co#'a e% two %t ing%. /t% %(nta6 i%:
StrCo53(strin!#= strin!$ U= com areV)

and it etu n% a value indicating w,et,e strin!# i% e7ual to* g eate t,an* o le%% t,an strin!$. $o #o e detail%* c,ec- t,e =>A ,el' 5ile.

9%2 7iscellaneous :unctions and Statements


O5 t,e wealt, o5 5unction% o55e ed b( t,e =>A language* weBll 5ocu% on t,e 2s... 5unction% to dete #ine an att ibute o5 a va iable o object* t,e conve %ion 5unction%* and two 5unction%* 226 and Switch* t,at etu n a conditional e%ult.

6.$.1 he /s Functions
=>A ,a% %eve al 2s 5unction% t,at etu n >oolean value% indicating w,et,e o not a ce tain condition ,old%. 4e ,ave al ead( di%cu%%ed t,e 2sMissin4 5unction in connection wit, o'tional a gu#ent%. Ae e a e %o#e additional /% 5unction%. 6.$.1.1 he /s(ate 4unction
Thi% 5unction indicate% w,et,e an e6' e%%ion can be conve ted to a date. /t al%o a''lie% to a cell*

in w,ic, ca%e it evaluate% t,e content% o5 t,e cell. /5 t,e content% e' e%ent a valid date* t,e 5unction etu n% Tr0e. $o in%tance* t,e code:
2sDate("an4e(">B"))

will etu n Tr0e i5 t,e content% o5 cell $3 e' e%ent a date. 6.$.1.2 he /sE&!ty 4unction 0,i% 5unction indicate% w,et,e a va iable ,a% been initialiGed o w,et,e a wo -%,eet cell i% e#'t(. $o e6a#'le* t,e code:
26 2s'53t ("an4e("A1")) Then . . .

te%t% w,et,e o not cell A1 i% e#'t(. 6.$.1." he /sNull 4unction 0,i% 5unction i% u%ed to te%t w,et,e a va iable i% 90ll ?t,at i%* contain% no data@. +ote t,at code %uc, a%:
26 var = 90ll Then

will alwa(% etu n >alse becau%e #o%t e6' e%%ion% t,at involve 90ll auto#aticall( etu n >alse. 0,e ' o'e wa( to dete #ine i5 t,e va iable +ar i% 90ll i% to w ite:
26 2s90ll(var) Then

6.$.1.$ he /sNu&eric 4unction

0,i% 5unction indicate% w,et,e an e6' e%%ion can be evaluated a% a nu#be o w,et,e a cell contain% a value t,at can be evaluated a% a nu#be . $o in%tance* i5 cell A1 contain% t,e data 123 ?even i5 t,i% cell i% 5o #atted a% te6t@* t,en t,e condition in:
26 2s905eric("an4e("A1")) Then

will evaluate to Tr0e. On t,e ot,e ,and* i5 t,e cell contain% t,e data 123 "ain 1t eet* t,en t,e condition will evaluate to >alse.

6.$.2 he /&&e#iate /4 Function


0,e /##ediate /5 5unction ,a% t,e %(nta6:
226(,x ression= TruePart= FalsePart)

/5 ,x ression i% Tr0e* t,en t,e 5unction etu n% TruePart. /5 ,x ression i% >alse* t,e 5unction etu n% FalsePart. $o in%tance* t,e 5ollowing code di%'la(% a dialog indicating w,et,e o not t,e 5i %t ow in t,e active wo -%,eet i% e#'t(:
Di5 rn4 As "an4e Set rn4 = ActiveSheet."ows(1) Ms41ox 226(2s'53t (ActiveSheet.Cells(1= 1))= G "Cell is e53t "= "Cell is not e53t ")

/t i% ve ( i#'o tant to note t,at t,e /##ediate /5 5unction alwa(% evaluate% both TruePart and FalsePart* even t,oug, it etu n% onl( one o5 t,e#. Aence* we #u%t be ca e5ul about unde%i able %ide e55ect%. $o e6a#'le* t,e 5ollowing code will ' oduce a divi%ion b( Ge o e o becau%e even t,oug, t,e 226 5unction etu n% 1Jx onl( w,en x i% not e7ual to 0* t,e e6' e%%ion 1Jx i% evaluated in all ca%e%* including w,en x i% e7ual to 0:
x = 7 = 226(x = 7= x W 2= 1 J x)

6.$." he Switch Function


0,e %(nta6 o5 t,e Switch 5unction i%:
Switch(ex r#= +alue#= ex r$= +alue$= ... = ex rn= +aluen)

w,e e ex rn and +aluen a e e6' e%%ion%. +ote t,at t,e e need onl( be one e6' e%%ion;value 'ai * but t,e 5unction i% #o e #eaning5ul i5 t,e e a e at lea%t two %uc, 'ai %. 0,e Switch 5unction evaluate% eac, e6' e%%ion ex rn. 4,en it encounte % t,e 5i %t 0 ue e6' e%%ion* it etu n% t,e co e%'onding value. A% wit, t,e 226 5unction* Switch alwa(% evaluate% all o5 t,e e6' e%%ion%. /5 none o5 t,e e6' e%%ion% i% 0 ue* t,e 5unction etu n% 90ll. 0,i% can be te%ted wit, t,e 2s90ll 5unction. 0,e ' ocedu e in E6a#'le 7;1 di%'la(% t,e t('e o5 5ile ba%ed on it% e6ten%ion: 0e#'late* 4o -boo-* o Add;in. Exa&!le 6-1. he Switch Function
S0b Show>ileT 3e(>ile'xt As Strin4) Di5 >ileT 3e As +ariant

>ileT 3e = Switch(>ile'xt = "xlt"= "Te53late"= G >ile'xt = "xls"= "Hor8boo8"= G >ile'xt = "xla"= "Addin") . Dis3la res0lt 26 9ot 2s90ll(>ileT 3e) Then Ms41ox >ileT 3e 'lse Ms41ox "<nreco4niMed t 3e" 'nd 26 'nd S0b

0,e e i% one %ubtlet( in t,i% code. 1ince t,e Switch 5unction can etu n a 90ll value* we cannot a%%ign t,e etu n value to a 1t ing va iable* a% we #ig,t 5i %t t ( to do:
Di5 >ileT 3e As Strin4 >ileT 3e = Switch(>ile'xt = "xlt"= "Te53late"= G >ile'xt = "xls"= "Hor8boo8"= G >ile'xt = "xla"= "Addin")

0,i% will not ' oduce an e o unle%% File,xt i% not 86lt*8 86l%*8 o 86la*8 in w,ic, ca%e we will get t,e ve ( anno(ing e o #e%%age* 8/nvalid u%e o5 +ull.8 0,e %olution i% to decla e FileTy e a% a =a iant* w,ic, can ,old an( data t('e* including no data t('e* w,ic, i% indicated b( t,e 90ll -e(wo d. ?0,i% i%%ue can al%o be avoided b( u%ing a Select Case %tate#ent* di%cu%%ed in !,a'te 8.@

6.$.$ 2nits +onversions


0,e 2nchesTo;oints 5unction conve t% a #ea%u e#ent given in inc,e% to one given in 'oint%. 0,e ea%on t,i% i% i#'o tant i% t,at #an( E6cel value% need to be given ?o a e etu ned@ in 'oint%* but #o%t o5 u% ' e5e to t,in- in inc,e% ?t,e e a e 72 'oint% in one inc,@. 0,i% a''lie% e%'eciall( to 'o%itioning ' o'e tie%* %uc, a% 0o' and 3e5t. $o in%tance* t,e 0o' ' o'e t( o5 a !,a tObject %'eci5ie% t,e location o5 t,e to' o5 t,e c,a t object* #ea%u ed in 'oint%* 5 o# Row 1 o5 t,e wo -%,eet. 0,u%* to %et t,i% value to .25 inc,e%* we would w ite:
ActiveChart.Chart*bTect.To3 = 2nchesTo;oints(.2&)

0,e e i% al%o a ;ointsTo2nches 5unction t,at i% u%e5ul 5o di%'la(ing t,e etu n value o5 a 5unction in inc,e% w,en t,e 5unction etu n% t,e value in 'oint%.

6.$.' he .ee! State&ent


0,i% %i#'le %tate#ent* w,o%e %(nta6 i%:
1ee3

%ound% a %ingle tone t, oug, t,e co#'ute B% %'ea-e %. /t can be u%e5ul ?w,en u%ed wit, e%t aint@ i5 we want to get t,e u%e B% attention. Aoweve * t,e e i% a caveat: t,e e%ult% a e de'endent u'on t,e co#'ute B% ,a dwa e and %o t,e %tate#ent #a( not ' oduce a %ound at allC 0,u%* i5 (ou u%e t,i% %tate#ent in (ou code* be %u e to wa n t,e u%e . ?/t i% 'o%%ible* and ' obabl( bette in gene al* to u%e t,e E6cel %tatu% ba to di%'la( #e%%age% to t,e u%e t,at do not inte 5e e wit, e6ecution o5 a ' og a#. 0,i% i% done u%ing t,e 1tatu%>a ' o'e t( o5 t,e A''lication object.@

9%3 !andling Errors in Code


4e di%cu%%ed t,e va iou% t('e% o5 e o % in !,a'te 3* but we ,ave %c u'ulou%l( avoided t,e 7ue%tion o5 ,ow to ,andle un;ti#e e o % in code. /ndeed* =>A ' ovide% %eve al tool% 5o ,andling e o % ?*n 'rror* "es05e* t,e E object* and %o on@* and we could include an enti e c,a'te on t,e %ubject in t,i% boo-. & o'e e o ,andling i% extremely i#'o tant. /ndeed* i5 (ou a e* o intend to beco#e* a ' o5e%%ional a''lication develo'e * (ou %,ould 5a#ilia iGe (ou %el5 wit, e o ;,andling ' ocedu e%. On t,e ot,e ,and* i5 (ou intention i% to ' oduce E6cel =>A code 5o (ou own 'e %onal u%e* t,e ea%on% 5o adding e o ;,andling outine% a e %o#ew,at #itigated. 4,en an e o occu % wit,in one o5 (ou own ' og a#%* =>A will %to' e6ecution* di%'la( an e o #e%%age* and ,ig,lig,t t,e o55ending code. 0,i% %,ould enable (ou to debug t,e a''lication and 5i6 t,e ' oble#. ?/t would be un ea%onable to e6'ect anot,e u%e o5 (ou ' og a# to debug (ou code* ,oweve .@ 3et u% unde ta-e a b ie5 di%cu%%ion o5 t,e ,ig,lig,t% o5 e o ,andling. ?$o #o e detail%* #a( / %ugge%t #( boo- (oncepts of $b%ect-$riented Programming in Visual Basic* 'ubli%,ed b( 1' inge ;=e lag. /t ,a% a detailed c,a'te on e o ,andling.@

6.'.1 he 3n Error :oto )abel State&ent


0,e On E o %tate#ent tell% =>A w,at to do w,en a un;ti#e e o occu %. 0,e #o%t co##on 5o # o5 t,e %tate#ent i%:
*n 'rror :oTo label

w,e e label i% a label. $o in%tance* con%ide t,e 5ollowing code:


S0b exa53le() *n 'rror :oTo '""G'#AM;L' Ms41ox Selection.Cells.Co0nt 'xit S0b '""G'#AM;L'Ms41ox 'rr.Descri3tion= vbCritical 'xit S0b 'nd S0b

0,e 'u 'o%e o5 t,i% ' ocedu e i% %i#'l( to di%'la( t,e nu#be o5 cell% in t,e cu ent %election. 4,en t,e cu ent %election i% a wo -%,eet ange* t,e !ell% ' o'e t( etu n% t,e collection o5 cell% in t,e %election and t,e !ount ' o'e t( t,en etu n% t,e nu#be o5 cell%. Aoweve * i5 t,e cu ent %election i% not a wo -%,eet ange ?it #ig,t be a d awing object o a c,a t* 5o in%tance@* t,en t,e !ell% ' o'e t( 5ail%. 0o deal wit, t,i% 'o%%ibilit( in a 5 iendl( #anne * we add %o#e e o c,ec-ing. 0,e line:
*n 'rror :oTo '""G'#AM;L'

tell% =>A to #ove e6ecution to t,e label '""G'#AM;L' i5 an e o occu %. 0,e code 5ollowing t,i% label i% called t,e error-handling code. /5 an e o %,ould occu * t,e ne6t line e6ecuted i% t,e

Ms41ox line* in w,ic, ca%e t,e dialog in $igu e 7;3 will be di%'la(ed. 0,i% #e%%age give% a

de%c i'tion o5 t,e e o * obtained 5 o# t,e E o object* w,ic, we di%cu%% in t,e ne6t %ection. Figure 6-". %n error #ialog

/t i% i#'o tant to note t,e line ju%t be5o e t,e '""G'#AM;L' label:
'xit S0b

4it,out t,i% %tate#ent* t,e e o ;,andling code will alwa(% be e6ecuted* even w,en t,e e i% no e o C O#itting t,i% line i% a co##on #i%ta-e. +ote al%o t,at label% alwa(% end wit, a colon.

6.'.2 he Error 3bject


0,e e o object* E object* belong% to t,e =>A object #odel. 0,e #o%t i#'o tant ' o'e tie% o5 t,i% object a e: ?umber 0,e =>A e o nu#be /ource 0,e na#e o5 t,e cu ent =>A ' oject "escription A de%c i'tion o5 t,e e o +ote t,at t,e !lea #et,od o5 t,e E object will clea all o5 t,e ' o'e tie% o5 t,e E object* %etting it% +u#be ' o'e t( to 0 ?w,ic, indicate% t,e ab%ence o5 an e o @.

6.'." he 3n Error :o o 9 State&ent


0,e %tate#ent:
*n 'rror :oTo 7

tu n% o55 an( ' eviou% *n 'rror :oTo label %tate#ent%. An( e o occu ing %ub%e7uentl( will be ,andled b( =>A in it% own ini#itable wa(.

6.'.$ he 3n Error ,esu&e Next State&ent


0,e %(nta6:

*n 'rror "es05e 9ext

tell% =>A to continue e6ecuting t,e code i##ediatel( 5ollowing t,e line t,at cau%ed t,e e o . 0,e e a e two i#'o tant u%e% 5o t,i% 5o # o5 *n 'rror. 0,e 5i %t i% to cau%e =>A to igno e an e o . $o in%tance* t,e code:
S0b exa53le() *n 'rror "es05e 9ext Ms41ox Selection.Cells.Co0nt 'nd S0b

will e'o t t,e cell count w,en t,e %election i% a wo -%,eet ange and do not,ing w,en t,e %election i% not a wo -%,eet ange. Anot,e i#'o tant u%e 5o t,e *n 'rror "es05e 9ext %(nta6 i% 5o in-line error chec)ing* w,e e we c,ec- 5o e o % i##ediatel( 5ollowing t,e line t,at #a( ,ave cau%ed an e o . $o in%tance* anot,e wa( to ,andle e o % in t,e ' eviou% e6a#'le i%:
S0b exa53le() *n 'rror "es05e 9ext Ms41ox Selection.Cells.Co0nt 26 'rr.905ber $( 7 Then Ms41ox 'rr.Descri3tion= vbCritical 'nd 26 'nd S0b

6.'.' he ,esu&e State&ent


/t i% al%o 'o%%ible to include t,e "es05e %tate#ent in t,e e o ;,andling 'o tion o5 t,e code. 0,i% will cau%e =>A to e%u#e e6ecution at t,e line t,at 5ollow% t,e one t,at cau%ed t,e e o . 0,u%* t,e ' eviou% code i% e7uivalent to t,e 5ollowing:
S0b exa53le() *n 'rror :oTo '""G'#AM;L' Ms41ox Selection.Cells.Co0nt 'xit S0b '""G'#AM;L'Ms41ox 'rr.Descri3tion= vbCritical "es05e 9ext 'nd S0b

0,e e a e t, ee va iation% on t,e "es05e %tate#ent:


C C C "es05e "es05e 9ext "es05e -"abel

0,e 5i %t ve %ion will cau%e =>A to e%u#e wit, t,e line t,at cau%ed t,e e o . 0,i% i% u%e5ul i5 (ou e o ;,andling code actuall( e'ai % t,e e o condition and (ou want t,e line t,at cau%ed t,e o iginal e o to be e6ecuted again. 0o illu%t ate* t,e ' ocedu e in E6a#'le 7;2 i% de%igned to o'en a wo -boo- na#ed a5@testAxls. /5 it doe% not e6i%t* an e o will occu . 0,e e o ;,andling code give% t,e u%e a c,ance to ente a new wo -boo- na#e* in w,ic, ca%e we want to e6ecute t,e O'en #et,od again. Aence t,e u%e o5 t,e "es05e %tate#ent.

Exa&!le 6-2. Error Can#ling with the ,esu&e State&ent


S0b test() Di5 s9ew As Strin4 s9ew = "a-Itest.xls" *n 'rror :oTo '""GD2SP Hor8boo8s.*3en s9ew 'xit S0b '""GD2SP26 'rr.905ber = 177L Then s9ew = 2n30t1ox("Cannot 6ind 6ile. 'nter new location or leave blan8 to 26 s9ew $( "" Then "es05e 'lse 'xit S0b 'nd 26 'nd 26 'nd S0b

0,e t,i d va iation:


"es05e -"abel

cau%e% =>A to e%u#e e6ecution at t,e line labeled -"abel.

Chapter <% Control Statements


4e conclude ou di%cu%%ion o5 t,e =>A language wit, a di%cu%%ion o5 t,e #ain =>A control statements* w,ic, a e %tate#ent% t,at a55ect t,e 5low o5 cont ol ?o 5low o5 e6ecution@ in a ' og a#.

<%$ The &f%%%Then Statement


0,e 26...Then %tate#ent i% u%ed 5o conditional cont ol. 0,e %(nta6 i%:
26 Condition Then . state5ents 4o here 'lse26 -notherCondition . 5ore state5ents 4o 'lse . 5ore state5ents 4o 'nd 26 . . . Then here . . . here . . .

+ote t,at we #a( include #o e t,an one


'lse26 'a t and t,at bot, t,e 'lse26 'a t?%@ and t,e 'lse 'a t a e o'tional. 4e can al%o

%7ueeGe all 'a t% o5 t,i% %tate#ent onto a %ingle line* w,ic, i% gene all( onl( a good idea w,en t,e 'lse26 and 'lse 'a t% a e #i%%ing. A% an e6a#'le* t,e 5ollowing code delete% t,e cu ent %election in t,e active wo -%,eet i5 it contain% #o e t,an one cell:
26 Selection.Co0nt ( 1 Then Selection.Delete

0,e 5ollowing e6a#'le c,ange% t,e colo o5 t,e cu ent %election ba%ed u'on it% location< %elected cell% in odd;nu#be ed ow% a e colo ed ed* t,o%e in even;nu#be ed ow% a e colo ed blue:
Di5 oCell As "an4e >or 'ach oCell 2n Selection.Cells 26 (oCell."ow Mod 2) = 1 Then . odd oCell.2nterior.Color2ndex = B 'lse . even oCell.2nterior.Color2ndex = & 'nd 26 9ext

. red . bl0e

<%' The :or )oop


0,e >or...9ext %tate#ent ' ovide% a #et,od 5o e'eatedl( loo'ing t, oug, a bloc- o5 code ?t,at i%* one o #o e line% o5 code@. 0,i% loo' i% natu all( e5e ed to a% a >or loo'. 0,e ba%ic %(nta6 i%:
>or counter = start To end . bloc8 o6 code 4oes here . . .

9ext counter

0,e 5i %t ti#e t,at t,e bloc- o5 code i% e6ecuted* t,e va iable counter ?called t,e loo' va iable 5o t,e >or loo'@ i% given t,e value start. Eac, %ub%e7uent ti#e t,at t,e bloc- o5 code i% e6ecuted* t,e loo' va iable counter i% inc e#ented b( 1. 4,en counter e6ceed% t,e value end* t,e bloc- o5 code i% no longe e6ecuted. 0,u%* t,e code bloc- i% e6ecuted a total o5 end ; start . # ti#e%* eac, ti#e wit, a di55e ent value o5 counter. +ote t,at we can o#it t,e wo d counter in t,e la%t line o5 a >or loo' ? e'lacing 9ext co0nter wit, ju%t 9ext@. 0,i% #a( cau%e t,e >or loo' to e6ecute a bit #o e 7uic-l(* but it al%o det act% a bit 5 o# eadabilit(. 0o illu%t ate* t,e 5ollowing code loo'% t, oug, t,e collection o5 all cell% in t,e cu ent %election. /5 a cell ,a% a date in it* t,en t,e 5ont colo i% c,anged to ed:
Di5 i As 2nte4er Di5 oCell As "an4e >or i = 1 To Selection.Co0nt . :et the next cell Set oCell = Selection.Cells(i) . Color it i6 a date 26 2sDate(oCell) Then oCell.>ont.Color2ndex = B 'nd 26 9ext i >or loo'% a e o5ten u%ed to initialiGe an a a(. $o in%tance* t,e 5ollowing code a%%ign% a value o5 0 to eac, o5 t,e 11 va iable% i-rray ?0@ t, oug, i-rray ?10@: >or i = 7 To 17 iArra (i) = 7 9ext i

+ote t,at t,e loo' va iable counter will u%uall( a''ea wit,in t,e bloc- o5 code* a% it doe% in t,i% a a( initialiGation e6a#'le* but t,i% i% not a e7ui e#ent. Aoweve * i5 it doe% a''ea * we need to be ve ( ca e5ul not to c,ange it% value* %ince t,at will ce tainl( #e%% u' t,e >or loo'. ?=>A auto#aticall( inc e#ent% t,e loo' va iable eac, ti#e t, oug, t,e loo'* %o we %,ould leave it alone.@

7.2.1 Exit For


=>A ' ovide% t,e 'xit >or %tate#ent to e6it a >or loo' ' e#atu el(. $o in%tance* t,e code in E6a#'le 8;1 5ind% t,e 5i %t none#'t( cell in t,e 5i %t ow o5 t,e active wo -%,eet. /5 none e6i%t%* a #e%%age i% di%'la(ed. +ote t,e u%e o5 a >oolean va iable to -ee' t ac- o5 t,e e6i%tence 7ue%tion. Exa&!le 7-1. Fin#ing the First None&!ty +ell
S0b >ind>irst9on'53t () Di5 oCell As "an4e Di5 b9one As 1oolean b9one = Tr0e >or 'ach oCell 2n ActiveSheet."ows(1).Cells

26 9ot 2s'53t (oCell) Then oCell.Select b9one = >alse 'xit >or 'nd 26 9ext 26 b9one Then Ms41ox "9o none53t 'nd S0b cells in row 1"= vb2n6or5ation

4e can al%o cont ol t,e %te' %iGe and di ection 5o t,e counte in a >or loo' u%ing t,e Ste3 -e(wo d. $o in%tance* in t,e 5ollowing code* t,e counte i i% inc e#ented b( 2 eac, ti#e t,e bloc- o5 code i% e6ecuted:
>or i = 1 to 17 Ste3 2 . code bloc8 4oes here 9ext i

0,e 5ollowing loo' count% down 5 o# 10 to 1 in inc e#ent% o5 ;1. 0,i% can be u%e5ul w,en we want to e6a#ine a collection ?%uc, a% t,e cell% in a ow o colu#n@ 5 o# t,e botto# u':
>or i = 17 to 1 Ste3 %1 . code bloc8 4oes here 9ext i

<%* The :or Each )oop


0,e >or 'ach loo' i% a va iation on t,e >or loo' t,at wa% de%igned to ite ate t, oug, a collection o5 object% ?a% well a% t, oug, ele#ent% in an a a(@ and i% gene all( #uc, #o e e55icient t,an u%ing t,e t aditional >or loo'. 0,e gene al %(nta6 i%:
>or /bjectVar 2n CollectionName . bloc8 o6 code 4oes here . . . 9ext /bjectVar

w,e e /bjectVar i% a va iable o5 t,e %a#e object t('e a% t,e object% wit,in t,e collection. 0,e code bloc- will e6ecute once 5o eac, object in t,e collection. 0,e &ind&irst?onEmpty ' ocedu e %,own in E6a#'le 8;1 illu%t ate% t,e >or 'ach loo'. 0,u%* w,en ite ating t, oug, a collection o5 object%* we ,ave two c,oice%:
>or 'ach object in Collection . code bloc8 here 9ext object

o:
>or i = 1 to Collection'Count . code bloc8 here 9ext i

/t i% i#'o tant to -ee' in #ind t,at t,e >or 'ach loo' can be much 5a%te t,an t,e >or loo' w,en dealing wit, collection% o5 E6cel object%. 0,u%* e6ce't 5o %#all collection%* it i% t,e ' e5e ed #et,od.

<%2 The (o )oop


0,e Do loo' ,a% %eve al va iation%. 0o de%c ibe t,e%e va iation%* we u%e t,e notation:
XHhile Y <ntilZ

to e' e%ent eit,e t,e wo d Hhile o t,e wo d <ntil* but not bot,. 4it, t,i% in #ind* ,e e a e t,e 'o%%ible %(nta6e% 5o t,e Do loo':
Do XHhile Y <ntilZ condition . code bloc8 here Loo3

o:
Do . code bloc8 here Loo3 XHhile Y <ntilZ condition

Actuall(* t,e e i% a 5i5t, 'o%%ibilit(* becau%e we can di%'en%e wit, condition co#'letel( and w ite:
Do . code bloc8 here Loo3

1o#e o5 t,e%e va iation% a e actuall( 7uite %ubtle. $o in%tance* t,e 5ollowing code c(cle% t, oug, t,e cell% in t,e 5i %t ow o5 t,e active wo -%,eet a% long a% t,e cell% a e none#'t(:
i = 1 Do Hhile 2s'53t (ActiveSheet."ows(1).Cells(i)) i = i A 1 Loo3 ActiveSheet."ows(1).Cells(i).Select

?0,i% code will cau%e %o#e ' oble#% i5 t,e 5i %t ow ,a% no none#'t( cell%* but letB% not wo ( about t,at now.@ !on%ide al%o t,e 5ollowing code* w,o%e 'u 'o%e i% %i#ila :
i = 1 Do i = i A 1 Loo3 Hhile 9ot 2s'53t (ActiveSheet."ows(1).Cells(i)) ActiveSheet."ows(1).Cells(i).Select

0,e di55e ence between t,e%e two ve %ion% i% t,at* in t,e 5i %t ca%e* t,e 2s'53t condition i% c,ec-ed i##ediatel(* be5o e an( code wit,in t,e Do loo' i% e6ecuted. 0,u%* i5 t,e 5i %t cell i% e#'t(* t,e condition will 5ail* no code will be e6ecuted wit,in t,e Do loo'* and %o t,i% cell will be %elected ?a% it %,ould be@.

On t,e ot,e ,and* in t,e %econd ca%e* t,e condition i% c,ec-ed at t,e end o5 eac, loo'* %o t,e loo' will e6ecute t,e 5i %t ti#e* even i5 t,e 5i %t cell i% e#'t(. Nu%t a% t,e >or loo' ,a% an 'xit >or %tate#ent 5o te #inating t,e loo'* a Do loo' a% an 'xit Do %tate#ent 5o e6iting t,e Do loo'.

<%3 The Select Case Statement


A% we ,ave %een* t,e 26 . . . Then . . . con%t uct i% u%ed to 'e 5o # di55e ent ta%-% ba%ed on di55e ent 'o%%ibilitie%. An alte native con%t uct t,at i% o5ten #o e eadable i% t,e Select Case %tate#ent* w,o%e %(nta6 i%:
Select Case testex ression Case +alue# . state5ents to exec0te i6 testex3ression = val0e1 Case +alue$ . state5ents to exec0te i6 testex3ression = val0e2 . . . Case 'lse . state5ents to exec0te otherwise 'nd Select

+ote t,at t,e Case 'lse 'a t i% o'tional. 0o illu%t ate* t,e 5ollowing code i% t,e Select Case ve %ion o5 E6a#'le 7;1 in !,a'te 7* ?%ee t,e di%cu%%ion o5 t,e Switch 5unction@ t,at di%'la(% t,e t('e o5 a 5ile ba%ed on it% e6ten%ion. / t,in- (ou will ag ee t,at t,i% i% a bit #o e eadable t,an t,e ' eviou% ve %ion:
S0b Show>ileT 3e(>ile'xt As Strin4) Di5 >ileT 3e As +ariant Select Case >ile'xt Case "xlt" >ileT 3e = "Te53late" Case "xls" >ileT 3e = "Hor8sheet" Case "xla"= "0tl" >ileT 3e = "Addin" Case 'lse >ileT 3e = "0n8nown" 'nd Select . Dis3la res0lt Ms41ox >ileT 3e 'nd S0b

+ote t,e 'enulti#ate ca%e %tate#ent:


Case "xla"= "0tl"

=>A allow% u% to 'lace #o e t,an one condition in a ca%e %tate#ent* %e'a ated b( co##a%. 0,i% i% u%e5ul w,en #o e t,an one ca%e ' oduce% t,e %a#e e%ult.

<%8 A :inal 4ote on ,BA


0,e e i% a lot #o e to t,e =>A language t,an we ,ave cove ed ,e e. /n 5act* t,e =>A e5e ence #anual i% about 300 'age% long. Aoweve * we ,ave cove ed t,e #ain 'oint% needed to begin E6cel E1F =>A ' og a##ing.
E1F

I8 'o%'( li2e a goo( re8erence g%i(e to the !" lang%age) see VB & VBA in a Nutshell: The Language) +ritten b' Pa%l Lomax an( p%blishe( b' 6'1eill' K "ssociates.

Actuall(* #an( E6cel =>A ' og a##ing ta%-% e7ui e onl( a %#all 'o tion o5 =>AB% 5eatu e% and (ou will ' obabl( 5ind (ou %el5 w e%tling #uc, #o e wit, E6celB% object #odel t,an wit, t,e =>A language it%el5. 4e conclude ou di%cu%%ion o5 t,e =>A language 'e %e wit, a b ie5 outline o5 to'ic% 5o 5u t,e %tud(* w,ic, (ou can do u%ing t,e =>A ,el' 5ile%.

7.5.1 File-,elate# Functions


=>A ,a% a la ge nu#be o5 5unction% elated to 5ile and di ecto ( ,ou%e-ee'ing. 0able 8;1 contain% a %election o5 t,e#. able 7-1. So&e *.% File an# (irectory Functions !un tion Des ription )i $ind a 5ile wit, a ce tain na#e. $ile3en .et t,e lengt, o5 a 5ile. $ile0i#e)ate .et t,e date %ta#' o5 a 5ile. $ile!o'( !o'( a 5ile. Rill )elete a 5ile. +a#e Rena#e a 5ile o di ecto (. R#)i )elete a di ecto (. "-)i "a-e a new di ecto (. /n addition to t,e 5ile; elated 5unction% in 0able 8;1* t,e e #a( be ti#e% w,en it i% u%e5ul to c eate new te6t 5ile% to %to e data. =>A ' ovide% a nu#be o5 5unction% 5o t,i% 'u 'o%e* ,eaded b( t,e *3en %tate#ent* w,o%e ?%i#'li5ied@ %(nta6 i%:
*3en athname >or mode As UOV*ilenumber

Once a 5ile ,a% been o'ened* we can ead o w ite to it.

7.5.2 (ate- an# i&e-,elate# Functions


=>A ,a% a la ge nu#be o5 5unction% elated to #ani'ulating date% and ti#e%. 0able 8;2 contain% a %election. able 7-2. So&e (ate- an# i&e-,elate# Functions !un tion Des ription .et t,e cu ent date o ti#e.

)ate* +ow* 0i#e

)ateAdd* )ate)i55* )ate&a t )ate1e ial* )ate=alue 0i#e1e ial* 0i#e=alue )ate* 0i#e 0i#e

&e 5o # date calculation%. Retu n a date. Retu n a ti#e. 1et t,e date o ti#e. 0i#e a ' oce%%.

7.5." he For&at Function


0,e >or5at 5unction i% u%ed to 5o #at %t ing%* nu#be %* and date%. 0able 8;3 give% a 5ew e6a#'le%. able 7-". For&at Function Exa&!les "xpression Return #alue #at?)ate* 83ong )ate8@ 0,u %da(* A' il 30* 1998 #at?0i#e* 83ong 0i#e8@ 5:03:47 &" #at?)ate* 8##EddE(( ,,:##:%% A"&"8@ 04E30E98 12:00:00 A" #at?1234.5* 8VUU*UU0.008@ V1*234.50 #at?8AE33O8* 8S8@ 8,ello8

$o $o $o $o $o

Team879 7l'D

Part &&&+ Excel Applications and the Excel Ob1ect 7odel


Chapter =% Ob1ect 7odels
/n t,i% c,a'te * we ' e%ent a gene al ove view o5 object #odel% and t,e %(nta6 u%ed to #ani'ulate t,e# in code. A% we ,ave di%cu%%ed* =>A i% t,e ' og a##ing language t,at unde lie% %eve al i#'o tant 4indow% a''lication%* including "ic o%o5t E6cel* 4o d* Acce%%* &owe &oint* =i%ual >a%ic* and* in O55ice 2000* Outloo-. An( a''lication t,at u%e% =>A in t,i% wa( i% called a host application. 4e al%o di%cu%%ed t,e 5act t,at eac, ,o%t a''lication en,ance% =>A b( ' oviding an ob%ect model ?'e ,a'% #o e t,an one@ to deal wit, t,e object% t,at a e 'a ticula to t,at a''lication. "ic o%o5t ' ovide% ove a doGen di55e ent object #odel% 5o it% O55ice a''lication %uite and elated ' oduct%. 0,e%e include object #odel% 5o E6cel* 4o d* Acce%%* )AO ?)ata Acce%% Object%@* Outloo-* &owe &oint* >inde * . a',* $o #%* =>A* =>* A1& ?Active 1e ve &age%@* and #o e. O5 cou %e* ou inte e%t in t,i% boo- i% wit, t,e E6cel object #odel* and we will devote #o%t o5 t,e e%t o5 t,e boo- to de%c ibing t,e #ajo 'o tion% o5 t,i% #odel. ?4e will al%o di%cu%% a 'o tion o5 t,e O55ice object #odel in t,e c,a'te on cu%to#iGing E6cel #enu% and toolba %.@

=%$ Ob1ects0 Properties0 and 7ethods


/n t,e 'a lance o5 =>A ' og a##ing* an ob%ect i% %o#et,ing t,at i% identi5ied b( it% ' o'e tie% and it% #et,od%. $o e6a#'le* wo -boo-%* wo -%,eet%* c,a t%* and 5ont% a e all e6a#'le% o5 object% in t,e E6cel object #odel. Actuall(* t,e E6cel object #odel contain% 192 di55e ent object%* including %eve al ,idden and ob%olete one%.

8.1.1 Pro!erties
0,e te # property i% u%ed in t,e ' e%ent conte6t in ' ett( #uc, t,e %a#e wa( t,at it i% u%ed in eve (da( Engli%,H it i% a t ait o att ibute o c,a acte i%tic o5 an object. $o in%tance* a 4o -%,eet object ,a% 55 ' o'e tie%* a#ong w,ic, a e !ell%* +a#e* & otection"ode* and 2%edRange. A ' o'e t(B% value can be an( valid data t('e* %uc, a% /ntege * 1ingle* 1t ing* o even anot,e object t('e. 4,en t,e value o5 a ' o'e t( ,a% t('e /ntege * 5o in%tance* we will e5e to t,e ' o'e t( a% an integer property. /ntege ' o'e tie% a e 7uite co##on* and %o "ic o%o5t ,a% de5ined a la ge nu#be o5 built;in enu#% ?152* to be e6act* wit, 1266 individual con%tant%@ to give %(#bolic na#e% to t,e%e ' o'e t( value%. $o in%tance* t,e !alculation ' o'e t( o5 t,e A''lication object can ta-e on an( o5 t,e value% in t,e enu# de5ined b(:
'n05 #lCalc0lation xlCalc0lationMan0al = %L1B& xlCalc0lationA0to5atic = %L17& xlCalc0lationSe5ia0to5atic = 2 'nd 'n05

/5 a ' o'e t(B% value i% an object* it i% e5e ed to a% an ob%ect property. $o in%tance* a 4o -booobject ,a% an Active!,a t ' o'e t( t,at etu n% a !,a t object. O5 cou %e* t,e !,a t object ,a% it% own %et o5 ' o'e tie% and #et,od%.

>ecau%e a !,a t object can be obtained 5 o# a 4o -boo- object* we e5e to !,a t a% a child ob%ect o5 4o -boo- and 4o -boo- a% a parent o5 !,a t. 4e will ,ave #o e to %a( about t,i% 'a ent;c,ild elation%,i' a bit late .

8.1.2 -etho#s
A method o5 an object i% an action t,at can be 'e 5o #ed on ?o on be,al5 o5 @ t,e object. $o in%tance* a 4o -%,eet object ,a% a & otect #et,od t,at cau%e% t,e wo -%,eet to be ' otected. /n ' og a##ing te #%* t,e ' o'e tie% and #et,od% o5 an object a e ju%t built;in 5unction% o %ub outine%. /t i% i#'o tant to e#',a%iGe t,at t,e di%tinction between ' o'e t( and #et,od i% one o5 intent and i% o5ten #ade %o#ew,at a bit a il(. ?/n 5act* t,e /te# #e#be i% %o#eti#e% cla%%i5ied a% a ' o'e t( and %o#eti#e% a% a #et,od* de'ending u'on t,e object in 7ue%tionH it a''ea % t,at even "ic o%o5t ,a% t ouble #a-ing u' it% collective #ind 5 o# ti#e to ti#e.@ 0,e ' o'e tie% and #et,od% o5 an object a e collectivel( e5e ed to a% t,e objectB% members. 0,i% %,ould not be con5u%ed wit, an objectB% c,ild en.

=%' Collection Ob1ects


/n ' og a##ing wit, t,e E6cel object #odel ?o indeed an( object #odel@* it i% co##on to ,ave a g eat #an( object% 8alive8 at t,e %a#e ti#e. $o in%tance* eac, cell wit,in t,e cu ent %election i% an object ?a Range object@* a% i% eac, ow and colu#n in eac, o'en wo -%,eet. Aence* at an( given ti#e* t,e e a e t,ou%and% o5 object% in e6i%tence. 0o #anage t,e%e object%* t,e de%igne % o5 an object #odel gene all( include a %'ecial t('e o5 object called a collection ob%ect. A% t,e na#e i#'lie%* collection object% e' e%ent collection% o5 object%<gene all( object% o5 a %ingle t('e. $o in%tance* t,e E6cel object #odel ,a% a collection object called Row% t,at e' e%ent% t,e %et o5 all ow% in t,e wo -%,eet in 7ue%tion ?a% Range object%@. /t i% cu%to#a ( to %a( t,at t,e Row% collection object contains t,e ow% in t,e %,eet* %o we will u%e t,i% te #inolog( a% well. 0,e e i% one Row% collection 5o eac, o'en wo -%,eet. !ollection object% a e gene all( ju%t called collections* but it i% ve ( i#'o tant to e#e#be t,at a collection i% ju%t a %'ecial t('e o5 object. A% we will %ee* t,e ' o'e tie% and #et,od% o5 a !ollection object a e %'eci5icall( de%igned to #anage t,e collection. 4e can gene all( %'ot a collection object b( t,e 5act t,at it% na#e i% t,e 'lu al o5 t,e na#e o5 t,e object% contained wit,in t,e collection. $o in%tance* t,e 4o -%,eet% collection contain% 4o -%,eet object%. Aoweve * in %o#e ca%e%* t,i% na#ing convention i% not 5ollowed. $o in%tance* t,e Row% collection contain% Range object%. /n t,e E6cel object #odel* t,e e a e no !ell* Row* o !olu#n object%. 0,e%e a e all e' e%ented b( Range object%. 4e will devote an enti e c,a'te ?!,a'te 19@ to t,e i#'o tant Range object. !ollection% a e e6t e#el( co##on in t,e O55ice object #odel%. /n 5act* al#o%t one;,al5 o5 all o5 t,e object% in t,e E6cel object #odel a e collection%C 0able 9;1 %,ow% %o#e o5 t,e #o e co##onl( u%ed collection% in t,e E6cel object #odel. able 8-1. So&e Excel +ollection 3bjects $o #at!ondition% 1e ie%!ollection 3egendEnt ie% 1,eet% +a#e% 4indow%

A ea% A6e% >o de %

!,a tObject% !,a t% )ata3abel% $ilte %

&ivot$ield% &ivot0able% &oint% Range

4o -boo-% 4o -%,eet%

4e e#',a%iGe t,e 5act t,at a collection i% ju%t a %'ecial t('e o5 object. /ndeed* t,e ' o'e tie% and #et,od% o5 a !ollection object a e %'eci5icall( de%igned to #anage t,e collection. Acco dingl(* t,e basic e7ui e#ent% 5o a collection object a e:
C

C C C

A ' o'e t( called !ount t,at etu n% t,e nu#be o5 object% in t,e collection. 0,i% i% a ead;onl( ' o'e t(H t,at i%* it cannot be %et b( t,e ' og a##e . /t i% auto#aticall( u'dated b( =>A it%el5. A #et,od called Add ?o %o#et,ing %i#ila * %uc, a% Add+ew@ t,at allow% t,e ' og a##e to add a new object to t,e collection. A #et,od called Re#ove* !lo%e* o )elete* o %o#et,ing %i#ila * t,at allow% t,e ' og a##e to e#ove an object 5 o# t,e collection. A #et,od called /te# t,at 'e #it% t,e ' og a##e to acce%% an( 'a ticula object in t,e collection. 0,e ite# i% u%uall( identi5ied eit,e b( na#e o b( an inde6 nu#be .

+ote t,at t,e%e ba%ic e7ui e#ent% a e not ,a d and 5a%t. 1o#e collection object% #a( not i#'le#ent all o5 t,e%e #e#be %* and #an( i#'le#ent additional #e#be %. $o in%tance* t,e A ea% and >o de % collection% do not ,ave an Add #et,od* %ince we a e not allowed to add object% to t,e%e collection%. 4e can onl( #ani'ulate t,e ' o'e tie% o5 t,e%e collection%. 1o#e E6cel collection% a e con%ide abl( #o e co#'licated t,an ot,e %* %ince t,e( ,ave %eve al ' o'e tie% and #et,od% t,at elate %'eci5icall( to t,e t('e o5 object t,e( contain. $o in%tance* t,e 1,eet% collection ,a% 10 ' o'e tie% and 8 #et,od%. 1eve al o5 t,e%e #e#be %* %uc, a% t,e & intOut #et,od* a e included %'eci5icall( %o t,at t,e( can o'e ate on all o5 t,e %,eet% in t,e collection at t,e %a#e ti#e. ?A sheet i% eit,e a wo -%,eet o a c,a t%,eet.@

8.2.1 he .ase o4 a +ollection


+ote t,at collection% can be eit,e 0;ba%ed o 1;ba%ed. /n a B-based collection* t,e 5i %t #e#be ,a% inde6 0* and in a .-based collection* t,e 5i %t #e#be ,a% inde6 1. "o%t* but not all* collection% in t,e E6cel object #odel and in =>A it%el5 a e 1;ba%ed. Aoweve * %o#e olde collection% tend to be 0;ba%ed. ?/ gue%% t,at "ic o%o5t got a lot o5 co#'laint% about 0;ba%ed collection% %o t,e( decided to %witc,.@ /t i% i#'o tant to dete #ine t,e ba%e o5 an( collection be5o e t (ing to acce%% #e#be % b( inde6. 0,i% can be done b( c,ec-ing t,e ,el' %(%te# ?%o#eti#e%@ o t (ing %o#e %a#'le code. $o in%tance* t,e code:
>or i = 1 To Selection.Cells.Co0nt Deb04.;rint Selection.Cells(i).+al0e 9ext i

i% co ect* %ince t,e !ell% collection i% 1;ba%ed. Aoweve * t,e 2%e $o #% collection* w,ic, e' e%ent% all cu entl( loaded u%e 5o #% in E6cel* i% 0;ba%ed* %o t,e code:
>or i = 1 To <ser>or5s.Co0nt Deb04.;rint <ser>or5s(i).9a5e 9ext i

will ' oduce an e o . 0,e co ect code i%:

>or i = 7 To <ser>or5s.Co0nt % 1 Deb04.;rint <ser>or5s(i).9a5e 9ext i

?+ote t,at t,i% e'o t% t,e nu#be o5 loaded 5o #% in t,e ' oject.@

=%* Ob1ect 7odel !ierarchies


0,e 5act t,at one objectB% ' o'e tie% and #et,od% can etu n anot,e object* t,u% c eating t,e conce't o5 child ob%ects* i% o5 'a a#ount i#'o tance* 5o it add% a ve ( u%e5ul %t uctu e to t,e object #odel. /t %ee#% b( loo-ing at t,e lite atu e t,at t,e e i% not total ag ee#ent on w,en one object i% con%ide ed a c,ild o5 anot,e object. $o ou 'u 'o%e%* i5 object A ,a% a ' o'e t( o #et,od t,at etu n% object >* t,en we will con%ide object > to be a c,ild o5 object A and object A to be a 'a ent o5 object >. $o e6a#'le* t,e Range object ,a% a $ont ' o'e t(* w,ic, etu n% a $ont object. Aence* $ont i% a c,ild o5 Range and Range i% a 'a ent o5 $ont. 0,e $ont object i% al%o a c,ild o5 t,e !,a tA ea object* w,ic, e' e%ent% t,e c,a t a ea wit,in an E6cel c,a t. ?4e will di%cu%% t,i% object in !,a'te 21.@ /n 5act* an object #a( ,ave #an( 'a ent% and #an( c,ild en. /t i% i#'o tant not to ta-e t,e 'a ent;c,ild analog( too lite all(. $o in%tance* t,e object ,ie a c,( i% 5ull o5 ci cula 'a ent;c,ild elation%,i'%. A% an e6a#'le* Range i% a c,ild o5 4o -%,eet and 4o -%,eet i% a c,ild o5 Range. /ndeed* in #o%t object #odel%* #o%t object% ,ave a ' o'e t( t,at etu n% t,e to' object o5 t,e #odel. /n t,e E6cel object #odel* al#o%t eve ( object ,a% an A''lication ' o'e t( t,at etu n% t,e A''lication object* w,ic, i% t,e to' object in t,e E6cel object #odel. 0,i% ' ovide% a 7uic- wa( to etu n to t,e to' o5 t,e object ,ie a c,(. Aence* al#o%t eve ( object in t,e object #odel i% a 'a ent o5 t,e to' objectC 0,e object ,ie a c,( o5 an object #odel i% o5ten 'ictu ed in a t ee;li-e %t uctu e. A %#all 'o tion o5 t,e E6cel object #odel i% %,own in $igu e 9;1. Figure 8-1. % s&all !ortion o4 the Excel object &o#el ;the tag DvXE &eans that the object is new in version X o4 Excel<

=%2 Ob1ect 7odel S/ntax


/t i% ti#e t,at we 5o #all( di%cu%% t,e ba%ic %(nta6 t,at i% u%ed w,en ' og a##ing wit, an object #odel. 0,e gene al %(nta6 5o e5e ing to an objectB% ' o'e tie% and #et,od% i% ve ( %i#'le. /5 objVar i% an object va iable t,at e5e % to a 'a ticula object and -Pro erty i% a ' o'e t( o5 t,i% object* t,en we can acce%% t,i% ' o'e t( ?5o eading o 5o c,anging@ u%ing t,e %(nta6:
obT+ar.A;ro3ert (any re0uired arameters)

$o in%tance* t,e 5ollowing code %et% t,e 5ont na#e ' o'e t( o5 t,e 5i %t ow in t,e active wo -%,eet:
. Declare obTect variable Di5 rn4 As "an4e . Set rn4 to re6er to 6irst row Set rn4 = ActiveSheet."ows(1) . Set 6ont na5e rn4.>ont.9a5e = "Arial"

+ote t,at t,e la%t line o5 code actuall( invo-e% two ' o'e tie%H t,e $ont ' o'e t( o5 rn! etu n% a $ont object* w,o%e +a#e ' o'e t( i% %et to A ial. /5 -(ethod i% a #et,od 5o t,i% object* t,en we can invo-e t,at #et,od wit, t,e %(nta6:

obT+ar.AMethod(any re0uired

arameters)

+ote t,at t,i% %(nta6 i% 7uite %i#ila to t,e %(nta6 u%ed to call an o dina ( =>A %ub outine o 5unction* e6ce't t,at ,e e we e7ui e 7uali5ication wit, t,e na#e o5 t,e va iable t,at 'oint% to t,e object w,o%e ' o'e t( o #et,od i% being called. $o in%tance* continuing t,e ' eviou% code* we can a''l( t,e !,ec-1'elling #et,od to t,e ow e5e ed to b( rn! a% 5ollow%:
rn4.Chec8S3ellin4

4e could include t,e na#e o5 a cu%to# dictiona ( a% a 'a a#ete to t,i% #et,od.

=%3 Ob1ect ,ariables


0o acce%% a ' o'e t( o5 an object* o to invo-e a #et,od* we can gene all( ta-e two a'' oac,e%: direct o indirect. 0,e indi ect a'' oac, u%e% an object va iable<t,at i%* a va iable t,at ,a% an object data t('e<w,e ea% t,e di ect a'' oac, doe% not. $o in%tance* to %et t,e >old ' o'e t( o5 t,e $ont object 5o t,e 5i %t ow in t,e active wo -%,eet* we can ta-e a di ect a'' oac,* a% in:
ActiveSheet."ows(1).>ont.1old = Tr0e

Alte nativel(* we can a%%ign an object va iable. Ae e a e two 'o%%ibilitie%:


Di5 rn4 As "an4e Set rn4 = ActiveSheet."ows(1) rn4.>ont.1old = Tr0e Di5 6nt As >ont Set 6nt = ActiveSheet."ows(1).>ont 6nt.1old = Tr0e

Object va iable% a e #o e i#'o tant t,an t,e( #ig,t %ee# at 5i %t. 0,e #o%t obviou% ea%on 5o t,ei u%e i% t,at t,e( can i#' ove code eadabilit( w,en we need to e5e to t,e %a#e object #o e t,an once. $o in%tance* in%tead o5 w iting:
ActiveSheet."ows(1).>ont.1old = Tr0e ActiveSheet."ows(1).>ont.2talic = Tr0e ActiveSheet."ows(1).>ont.<nderline = >alse ActiveSheet."ows(1).>ont.SiMe = 12 ActiveSheet."ows(1).>ont.9a5e = "Arial"

we can u%e a $ont va iable to i#' ove eadabilit( a% 5ollow%:


Di5 6nt As >ont Set 6nt = ActiveSheet."ows(1).>ont 6nt.1old = Tr0e 6nt.2talic = Tr0e 6nt.<nderline = >alse 6nt.SiMe = 12 6nt.9a5e = "Arial"

8.'.1 he 0ith State&ent


/n 5act* =>A ' ovide% a Hith %tate#ent to ,andle ju%t t,e %ituation in t,e ' eviou% e6a#'le* w,ic, could be w itten a% 5ollow%:
Di5 6nt As >ont Set 6nt = ActiveSheet."ows(1).>ont Hith 6nt .1old = Tr0e .2talic = Tr0e .<nderline = >alse .SiMe = 12 .9a5e = "Arial" 'nd Hith

0,e gene al %(nta6 o5 t,e Hith %tate#ent i%:


Hith object . statements !o here 'nd Hith

w,e e t,e %tate#ent% gene all( e5e to t,e object* but do not e7ui e 7uali5ication u%ing t,e objectB% na#e* a% in t,e ' eviou% e6a#'le.

8.'.2 3bject *ariables Save Execution i&e


0,e #ain ea%on t,at objec t va iable% a e i#'o tant i% not to i#' ove eadabilit(* but to %ave e6ecution ti#e. /n 'a ticula * to e6ecute eac, o5 t,e 5ive line% in t,e 5i %t ve %ion o5 t,e ' eviou% code* =>A need% to e%olve t,e e5e ence% to t,e va iou% E6cel object% Active1,eet* Row%?1@* and $ont. 0,at i%* =>A need% to 8cli#b down8 t,e E6cel object #odel. 0,i% ta-e% ti#e. Aoweve * in t,e code t,at u%e% an object va iable o5 t('e $ont* =>A onl( need% to e%olve t,e%e e5e ence% once. 0,e e5o e* t,e %econd ve %ion un% #uc, #o e 7uic-l(. 0,i% di55e ence can be ve ( noticeable w,en t,e e a e ,und ed% o t,ou%and% o5 e5e ence% to e%olve.

8.'." %n 3bject *ariable /s a Pointer


0,e e a e %o#e ve ( i#'o tant di55e ence% between object va iable% and nonobject va iable%* %uc, a% t,o%e o5 t('e /ntege * 1ingle* o 1t ing. A% we ,ave #entioned* a nonobject va iable can be t,oug,t o5 a% a na#e 5o a location in t,e co#'ute B% #e#o ( t,at ,old% %o#e data. $o in%tance* in t,e code:
Di5 i+ar As 2nte4er i+ar = 12B

t,e va iable iVar i% a 4;b(te #e#o ( location t,at ,old% t,e intege value 123. 0,i% can be 'ictu ed a% in $igu e 9;2. ?Actuall(* t,e 4;b(te #e#o ( location ,old% t,e value 123 in bina ( 5o #at* but t,at i% not elevant to ou di%cu%%ion.@ Figure 8-2. /nteger variables in &e&ory

$u t,e * i5 we we e to w ite:
Di5 i+ar2 As 2nte4er i+ar2 = i+ar i+ar2 = &NF

we would not e6'ect t,e la%t line o5 code to ,ave an( e55ect u'on t,e value o5 t,e va iable iVar* w,ic, %,ould %till be 123. 0,i% i% becau%e iVar and iVar$ e' e%ent di55e ent a ea% o5 #e#o (* a% 'ictu ed in $igu e 9;2. Aoweve * an object va iable i% not t,e na#e o5 a #e#o ( location t,at ,old% t,e object. Rat,e * an object va iable i% t,e na#e o5 a #e#o ( location t,at ,old% t,e address o5 t,e #e#o ( location t,at ,old% t,e object* a% %,own in $igu e 9;3. &ut anot,e wa(* t,e object va iable ,old% a reference to o points to t,e object. $o t,i% ea%on* it i% an e6a#'le o5 a pointer #ariable* o %i#'l( a pointer. /n $igu e 9;3* t,e object va iable rn! 'oint% to an object o5 t('e Range* na#el(* t,e 5i %t colu#n in t,e active %,eet. Figure 8-". %n object variable in &e&ory

0,e code t,at goe% wit, $igu e 9;3 i%:


Di5 rn4 as "an4e Set rn4 = ActiveSheet.Col05ns(1)

One o5 t,e con%e7uence% o5 t,e 5act t,at object va iable% a e 'ointe % i% t,at #o e t,an one object va iable can 'oint to ?o e5e to@ t,e %a#e object* a% in:
Di5 Di5 Set Set rn4 as "an4e rn42 as "an4e rn4 = ActiveSheet.Col05ns(1) rn42 = rn4

0,i% code c eate% t,e %ituation 'ictu ed in $igu e 9;4. Figure 8-$. wo object variables re4erencing the sa&e object

4e e#',a%iGe t,at w,ile rn! and rn!$ a e di55e ent object va iable%* t,e( ,old t,e %a#e value and %o* 'oint to t,e %a#e object. 0,u%* we can c,ange t,e 5i %t colu#n u%ing eit,e o5 t,e%e object va iable%. /t i% i#'o tant w,en ' og a##ing wit, object% to -ee' ve ( ca e5ul t ac- o5 all object va iable% and w,at t,e( a e e5e encing. $u t,e #o e* it i% gene all( not a good idea to ,ave #o e t,an one object va iable 'ointing to t,e %a#e object ?a% in $igu e 9;4@ unle%% t,e e i% a co#'elling ea%on to do %o. /t i% ve ( ea%( to c,ange t,e object u%ing one object va iable ?%a( rn!@ and t,en late u%e t,e ot,e va iable ?rn!$@* t,in-ing it e5e % to t,e unc,anged object.

8.'.$ Freeing an 3bject *ariableF the Nothing @eywor#


0o 5 ee an object va iable %o t,at it no longe 'oint% to an(t,ing* we u%e t,e 9othin4 -e(wo d* a% in:
Set rn42 = 9othin4

/t i% good ' og a##ing ' actice to 5 ee object va iable% w,en t,e( a e no longe needed* %ince t,i% can %ave e%ou ce%. An object va iable i% al%o %et to 9othin4 auto#aticall( w,en it% li5eti#e e6'i e%. +ote t,at once an object no longe ,a% an( e5e ence% to it* t,e object will auto#aticall( be de%t o(ed b( =>A* t,u% 5 eeing u' it% e%ou ce% ?#e#o (@. Aoweve * all e5e ence% to t,e object #u%t be 5 eed be5o e t,e object i% de%t o(ed. 0,i% i% anot,e ea%on not to 'oint #o e t,an one object va iable at t,e %a#e object i5 'o%%ible.

8.'.' he /s 3!erator
0o co#'a e t,e value% o5 two o dina ( va iable%* Var# and Var$* we would ju%t w ite:
26 +ar1 = +ar2 Then . . .

Aoweve * t,e %(nta6 5o co#'a ing two object va iable% to %ee i5 t,e( e5e to t,e %a#e object i% %'ecial ?a% i% t,e %(nta6 5o %etting t,e value o5 an object va iable<u%ing t,e Set %tate#ent@. /t i% done u%ing t,e 2s o'e ato :
26 rn4 2s rn42 then . . .

1i#ila l(* to te%t w,et,e o not an object va iable ,a% been %et to 9othin4* we w ite:
26 rn4 2s 9othin4 Then . . .

>e advi%ed t,at t,e e i% a ' oble# wit, t,e 2s o'e ato in t,e cu ent ve %ion o5 =>A. 0,i% ' oble# e6i%t% in t,e ve %ion o5 =>A u%ed b( O55ice 97 and O55ice 2000. ?"ic o%o5t ,a% ac-nowledged t,e ' oble#.@ $o e6a#'le* t,e code:
Di5 H8s As Hor8sheet Di5 H8s2 As Hor8sheet Set H8s = ActiveSheet Set H8s2 = ActiveSheet Ms41ox H8s 2s H8s2

will co ectl( di%'la( t,e value Tr0e. Aoweve * t,e analogou% code:
Di5 rn4 As "an4e Di5 rn42 As "an4e Set rn4 = ActiveSheet."ows(1) Set rn42 = ActiveSheet."ows(1) Ms41ox rn4 2s rn42

inco ectl( di%'la(% t,e value >alse. /5 we c,ange t,e 'enulti#ate line to:

Team979 7l'D

Set rn42 = rn4

t,en t,e #e%%age bo6 co ectl( di%'la(% Tr0e.

8.'.5 (e4ault -e&bers


/n #o%t object #odel%* #an( object% ,ave a default member ?' o'e t( o #et,od@ t,at i% invo-ed w,en a ' o'e t( o #et,od i% e6'ected but we do not %'eci5( one. $o in%tance* in t,e "ic o%o5t 4o d object #odel* t,e de5ault #e#be 5o t,e Range object i% t,e 0e6t ' o'e t(. Aence* t,e =>A 4o d code:
Di5 rn4 As "an4e Set rn4 = ActiveDoc05ent.Hords(1) rn4 = "Donna"

%et% t,e 5i %t wo d in t,e active docu#ent to )onna* %ince 4o d a''lie% t,e de5ault ' o'e t( in t,e la%t line* e55ectivel( e'lacing it wit,:
rn4.Text = "Donna"

2n5o tunatel(* neit,e t,e E6cel =>A docu#entation no t,e E6cel object #odel #a-e an e55o t to identi5( t,e de5ault #e#be % o5 E6cel object%. Acco dingl(* #( %ugge%tion i% to avoid t,e i%%ue w,en ' og a##ing E6cel. /n an( ca%e* de5ault #e#be % tend to #a-e code le%% eadable* and 5o t,i% ea%on* / gene all( avoid t,e#. One notable e6ce'tion i% 5o a collection object. /t i% gene all( t,e ca%e t,at t,e de5ault #e#be o5 a collection object i% t,e /te# #et,od. Aence* 5o in%tance* we can e5e to t,e 5ou t, cell in t,e cu ent %election b(:
Selection.Cells(L)

at,e t,an b( t,e #o e clu#%(:


Selection.Cells.2te5(L)

1ince t,i% u%e o5 t,e de5ault #e#be i% not li-el( to cau%e an( con5u%ion* we will u%e it.

8.'.6 :lobal -e&bers


"an( o5 t,e ' o'e tie% and #et,od% o5 t,e A''lication object can be u%ed wit,out 7uali5(ing t,e# wit, t,e wo d A33lication. 0,e%e a e called global members . $o in%tance* t,e 1election ' o'e t( i% global* and %o we can w ite:
Selection.Cells.Co0nt

in%tead o5:
A33lication.Selection.Cells.Co0nt

0o identi5( t,e global #e#be %* t,e E6cel object #odel ,a% a %'ecial object called t,e .lobal object. 0,i% object i% not u%ed di ectl(<it% 'u 'o%e i% %i#'l( to identi5( t,e global #e#be % o5 t,e object #odel. +ote t,at t,e #e#be % o5 t,e .lobal object 5o # a ' o'e %ub%et o5 t,e #e#be % o5 t,e A''lication object ?w,ic, #ean% t,at not all o5 t,e #e#be % o5 t,e Application object a e global@.

0able 9;2 li%t% t,e ?non,idden@ global #e#be % o5 t,e E6cel object #odel able 8-2. Excel global &e&bers !o##and>a % &a ent ! eato Range ))EA''Retu n!ode Row% ))EE6ecute Run ))E/nitiate 1election ))E&o-e 1endRe(% ))ERe7ue%t 1,eet% ))E0e #inate 1,o tcut"enu% )ialog1,eet% 0,i%4o -booEvaluate 0oolba % E6cel4/ntl"ac o1,eet% 2nion E6cel4"ac o1,eet% 4indow% E6ecuteE6cel4"ac o 4o -boo-% /nte %ect 4o -%,eet$unction "enu>a % 4o -%,eet% "odule% +a#e%

IEvaluate IRun2 Active!ell Active!,a t Active)ialog Active"enu>a Active& inte Active1,eet Active4indow Active4o -booAdd/n% A''lication A%%i%tant !alculate !ell% !,a t% !olu#n%

Chapter $>% Excel Applications


1i#'l( 'ut* we can de5ine an $ffice application to be an O55ice 8docu#ent8 ?5o in%tance* an Acce%% databa%e* E6cel wo -boo-* 4o d docu#ent* 4o d te#'late* o &owe &oint ' e%entation@ t,at contain% %o#e %'ecial cu%to#iGation. 0,i% cu%to#iGation u%uall( ta-e% t,e 5o # o5 a co#bination o5 =>A ' ocedu e% and #enu andEo toolba cu%to#iGation% and i% gene all( de%igned to %i#'li5( o auto#ate ce tain ta%-%. /t #a( ' ovide utilities* w,ic, a e ' og a#% 5o 'e 5o #ing a %'eci5ic ta%-* %uc, a% ' inting o %o ting. 0,i% #a( %ee# li-e a 5ai l( libe al de5inition. $o in%tance* i5 we add a %ingle cu%to# #enu ite# to a 4o d te#'late t,at %i#'l( add% a clo%ing ?1ince el( (ou %* etc.@ to t,e end o5 a 4o d docu#ent* we could con%ide t,i% te#'late to be a 4o d a''lication. Aoweve * it i% doubt5ul t,at we could get an(one to bu( t,i% 4o d a''licationC 0,e 'oint we want to e#',a%iGe i% t,at an O55ice a''lication i% 7uite di55e ent 5 o# a t aditional 4indow% a''lication* %uc, a% E6cel it%el5. 0 aditional 4indow% a''lication% a e built a ound a #ain e6ecutable 5ile. /n t,e ca%e o5 E6cel* t,i% 5ile i% called excelAexe. O5 cou %e* a co#'le6 a''lication li-e E6cel involve% #an( additional %u''o ting 5ile%* %uc, a% additional e6ecutable%* ,el' 5ile%* object lib a ( 5ile%* e%ou ce 5ile%* in5o #ation 5ile%* ActiveD cont ol 5ile%* and t,e ubi7uitou% )33 5ile%. On t,e ot,e ,and* O55ice a''lication% do not evolve a ound %tandalone e6ecutable 5ile%. Rat,e * t,e( a e c eated wit,in an O55ice docu#ent. /n 'a ticula * an Acce%% a''lication i% c eated wit,in an Acce%% databa%e* an E6cel a''lication i% c eated wit,in an E6cel wo -boo-* a 4o d a''lication i% c eated wit,in a 4o d docu#ent* and a &owe &oint a''lication i% c eated wit,in a &owe &oint ' e%entation. O55ice a''lication% can be c eated wit,in O55ice te#'late% o add;in% a% well. 0,i% ai%e% a w,ole new %et o5 i%%ue% elated to t,e di%t ibution o5 O55ice a''lication%. /n develo'ing an O55ice a''lication 5o di%t ibution* we #u%t i##ediatel( deal wit, two i%%ue%. 4,e e do we 'ut t,e code 5o t,i% a''lication* and w,at #ean% do we ' ovide t,e u%e to invo-e t,e 5eatu e% o5 t,e a''lication9 0,e 5i %t i%%ue i% co#'licated b( w,et,e we will allow t,e u%e to ,ave acce%% to t,e a''licationB% code and data o not. 0,e an%we % to t,e%e 7ue%tion% de'end* not %u ' i%ingl(* on t,e natu e o5 t,e a''lication.

$>%$ Pro iding Access to an Application's :eatures


/ ecentl( c eated an E6cel a''lication 5o a well;-nown 5a%t 5ood co#'an(. 0,e co#'an( wanted to %end out data on %ale% and ot,e t,ing% to it% 5ield o55ice%* in t,e 5o # o5 a at,e co#'licated E6cel 'ivot table. 0,e( wanted t,e 5ield 'e %onnel to be able to 5ilte t,e 'ivot table b( va iou% #ean% ?t,u% c eating %#alle 'ivot table%@ a% well a% gene ate a va iet( o5 c,a t% %,owing di55e ent view% o5 t,e data. ?0,e co#'lete a''lication involved ot,e 5eatu e%* but t,i% will illu%t ate t,e 'oint.@ /n 'a ticula * t,e #ain 'ivot table contain% %eve al t('e% o5 data ?%ale%* t an%action count%* and %o on@ 5o %eve al )e%ignated "a -eting A ea% ?)"A%@ and %to e t('e% ?co#'an(* 5 anc,i%e* o bot,@. One 5eatu e o5 t,e a''lication i% a c,a t;c eating utilit( 5o t,i% data. >ut w,e e %,ould t,e code 5o t,i% 5eatu e go and ,ow %,ould t,e 5ield 'e %onnel be given acce%% to t,i% c,a ting utilit(9 1ince t,e c,a ting utilit( di ectl( involve% t,e 'ivot table* it %ee#% ea%onable in t,i% ca%e to %i#'l( 'lace a co##and button labeled "a-e !,a t?%@ di ectl( on t,e 'ivot table wo -%,eet.

4,en t,e u%e clic-% t,e button* a dialog bo6 %uc, a% t,e one %,own in $igu e 10;1 a''ea %* allowing t,e u%e to #a-e va iou% %election% and t,en c eate t,e c,a t o c,a t%. Figure 19-1. (ialog 4or a charting utility

/n gene al* t,e e a e %eve al 'o%%ible o'tion% 5o ' oviding acce%% to t,e c,a ting utilit(* t,at i%* 5o di%'la(ing t,e dialog bo6 in $igu e 10;1 ?o * 5o t,at #atte * 5o ' oviding acce%% to an( #ac o@:
C

C C C C C C

1elect it 5 o# t,e "ac o dialog b( c,oo%ing 0ool% "ac o "ac o%. 0,e "ac o dialog wa% di%cu%%ed in !,a'te 4. 0,i% i% t,e #o%t e55icient #et,od 5o a u%e w,o w ite% #ac o% and want% to un one 7uic-l( ?and it ' ovide% an ea%( #et,od to un #an( o5 t,e ve ( %,o t e6a#'le% ' e%ented in t,i% boo-@. >ut %ince t,e dialog di%'la(% onl( t,e na#e% o5 #ac o% to be un* itB% not %uitable 5o a u%e w,o i% un5a#ilia wit, t,e #ac o%* no i% it a ve ( e55icient #et,od o5 unning 5 e7uentl( u%ed #ac o%. Run o di%'la( it auto#aticall( w,en a wo -boo- o'en% b( attac,ing code to one o5 E6celB% event%* in t,i% ca%e t,e O'en event. Event% a e di%cu%%ed in detail in !,a'te 11. &lace a button di ectl( on t,e wo -%,eet. &lace a button on an e6i%ting E6cel toolba . 0,i% can be done ' og a##aticall( ?a to'ic di%cu%%ed in !,a'te 12@ o t, oug, t,e u%e inte 5ace ?%ee 1ection 10.1.2 late in t,i% %ection@. ! eate a new toolba and add t,e button to it* eit,e ' og a##aticall( o t, oug, t,e u%e inte 5ace. $o in5o #ation on t,e latte * %ee 1ection 10.1.1 late in t,i% %ection. Add a #enu ite# to an e6i%ting E6cel #enu* eit,e ' og a##aticall( o t, oug, t,e u%e inte 5ace. ! eate a new #enu ba and add a #enu ite#* eit,e ' og a##aticall( o t, oug, t,e u%e inte 5ace.

/n t,i% ca%e* %ince we did not want t,e u%e to be able to invo-e t,e c,a t;' inting utilit( unle%% t,e wo -%,eet containing t,e 'ivot table wa% active* we o'ted 5o t,e button on t,e wo -%,eet a'' oac,. 0,i% i% not to %a(* ,oweve * t,at t,e ot,e a'' oac,e% would not wo -. On t,e ot,e ,and* i5 t,e utilit( in 7ue%tion ,a% wide a''licabilit(* t,en it would ' obabl( #a-e #o e %en%e to u%e a toolba o add a #enu ite#. ?/ #uc, ' e5e #enu ite#% ove toolba button%* becau%e t,e( a e ea%il( invo-ed u%ing t,e -e(boa d and donBt get in t,e wa( o5 ot,e window%.@

/ndeed* an a''lication t,at ,a% #an( 5eatu e% #ig,t bene5it 5 o# a dedicated toolba o #enu ba o a dedicated 'o'u' #enu on* %a(* t,e #ain E6cel wo -%,eet and c,a t #enu ba %. /n %,o t* t,e deci%ion a% to ,ow to ' ovide acce%% to t,e 5eatu e% o5 an O55ice a''lication de'end% on %eve al t,ing%* including t,e co#'le6it( o5 t,e a''lication* t,e %co'e o5 it% 5eatu e%* and 'e %onal ' e5e ence%.

19.1.1 0or1ing with oolbars an# -enus /nteractively


4,et,e we c,oo%e to 'lace a co##and button 5o a #ac o on an e6i%ting E6cel toolba o on a cu%to# toolba o5 ou own #a-ing* we #a( need to %'eci5(* u%ing t,e E6cel u%e inte 5ace* w,en t,e toolba in 7ue%tion will be di%'la(ed. 4e can c eate a new toolba and di%'la( o ,ide e6i%ting toolba % b( %electing t,e !u%to#iGe o'tion 5 o# t,e 0ool% #enu. 0,e 0oolba % tab 5o t,e !u%to#iGe dialog bo6 i% %,own in $igu e 10;2. Figure 19-2. he oolbars tab o4 the +usto&i?e #ialog

0o c eate a new toolba * %i#'l( clic- t,e +ew button. E6cel o'en% t,e +ew 0oolba dialog* w,ic, ' o#'t% u% 5o a na#e 5o t,e toolba . A5te we a%%ign it a uni7ue na#e* E6cel will c eate t,e toolba * li%t it in t,e 0oolba % li%t bo6* and di%'la( t,e toolba . 4e can t,en 'o'ulate t,e toolba wit, button%. 0o di%'la( o ,ide e6i%ting toolba %* we %i#'l( c,ec- o unc,ec- t,ei bo6e% in t,e 0oolba % li%t bo6. 4e can al%o c eate a new %ub#enu* w,ic, can t,en be added to an e6i%ting #enu o toolba . 0o do t,i%* we %elect t,e !o##and% tab o5 t,e !u%to#iGe dialog ?%ee $igu e 10;3@* t,en %elect t,e +ew "enu o'tion in t,e !atego ie% li%t bo6. !lic- on t,e +ew "enu ite# in t,e !o##and% li%t bo6 and d ag it to t,e a'' o' iate #enu o toolba . $inall(* we ig,t;clic- on t,e new #enu and ente it% ca'tion in t,e conte6t #enuB% +a#e 5ield. Figure 19-". he +o&&an#s tab o4 the +usto&i?e #ialog

19.1.2 %ssigning -acros to -enus an# oolbars


E6cel al%o allow% u% to a%%ign #ac o% to e6i%ting #enu% and toolba %* w,ic, i% al%o done 5 o# t,e !o##and% tab o5 t,e !u%to#iGe dialog %,own in $igu e 10;3. Alt,oug, #an( u%e % 5ind t,e !o##and% tab* and t,e #enu and toolba cu%to#iGation 5eatu e% in O55ice* to be con5u%ing and inti#idating* t,e( a e actuall( 7uite %i#'le i5 we -ee' t,e 5ollowing in #ind: o dina il(* #enu% and toolba % a e in run mode. /n t,i% #ode* %electing a #enu ite# o a toolba button cau%e% t,e co e%'onding action to be 'e 5o #ed. On t,e ot,e ,and* w,eneve t,e !u%to#iGe dialog i% vi%ible* #enu% and toolba % a e in edit mode. 4,ile in edit #ode* clic-ing on a #enu ite# o button ,a% an enti el( di55e ent e55ect. /n 'a ticula * ig,t;clic-ing on a #enu ite# di%'la(% a #enu wit, t,e ite#B% ' o'e tie%. Al%o* we can #ove* delete* o add ite#% to a #enu %i#'l( b( d agging and d o''ing t,e%e ite#%C 1ince edit #ode i% active w,eneve t,e !u%to#iGe dialog i% vi%ible* (ou %,ould be ve ( ca e5ul not to inadve tentl( d ag a #enu ite# ?o toolba button@ o55 o5 a #enu ?o toolba @* becau%e t,i% will delete t,at ite# 5 o# t,e #enu ?o toolba @. 1o* to a%%ign a #ac o to a toolba o #enu ite#* #a-e %u e t,e !u%to#iGe dialog i% vi%ible* %elect "ac o% in t,e !atego ie% li%t ?%ee $igu e 10;3@* and d ag t,e #ac o 5 o# t,e !o##and% li%t to t,e a'' o' iate location on t,e #enu o toolba . 0,atB% it. /t i% wo t, 'ointing out t,at cu%to#iGing #enu% and toolba % t, oug, t,e !u%to#iGe dialog* a% we ,ave ju%t de%c ibed* #a( be t,e ig,t wa( to ' oceed 5o develo'e %* but it al%o #a( be too #uc, to a%- a client to 'e 5o # t,i% cu%to#iGation ,i#%el5. 0,e alte native i% to c eate t,e cu%to# object ' og a##aticall(* a% di%cu%%ed in !,a'te 12. 0,i% i% %o#et,ing (ou will need to judge 5o (ou %el5.

$>%' -here to Store an Application


/n t,e ca%e o5 t,e E6cel a''lication 5o t,e a5o e#entioned 5a%t 5ood co#'an(* all o5 t,e data 5o t,e a''lication i% contained in a %ingle wo -boo-. 1ince none o5 t,i% data need% to be ,idden 5 o# t,e u%e * it i% ea%onable to di%t ibute t,e code and an( conco#itant data 5o t,e a''lication di ectl( in t,e wo -boo- t,at contain% t,e data ?t,e 'ivot table@. 0,i% #a-e% t,e wo -boo- totall(

%el5;contained and eli#inate% t,e need 5o an in%tallation ' ocedu e. All t,e #ain o55ice need% to do i% e#ail t,e wo -boo- to it% 5ield o55ice%. 0,e e a e %eve al 'o%%ibilitie% ,e e* ,oweve :
C

C C C

1to e t,e a''lication and it% data in t,e docu#ent in w,ic, it will be u%ed. 0,i% i% %uitable 5o a %tandalone a''lication li-e t,e one %,own in $igu e 10;1. /t i% al%o %uitable 5o %#all #ac o%* %uc, a% t,o%e contained in code 5 ag#ent% t, oug,out t,i% boo-* t,at we want to un ju%t to %ee ,ow %o#e E6cel =>A 5eatu e i% i#'le#ented. 1to e t,e a''lication and it% data in an E6cel te#'late. 0,i% i% %uitable* o5 cou %e* w,en t,e te#'late will %e ve a% t,e ba%i% o5 #ulti'le %' ead%,eet%. 1to e t,e a''lication and it% data in a ,idden E6cel wo -boo- in E6celB% %ta tu' di ecto (. 1to e t,e a''lication and it% data in an E6cel add;in.

Eac, o5 t,e%e c,oice% ,a% it% advantage% and di%advantage%* w,ic,* incidentall(* va ( a#ong t,e O55ice a''lication%. $o in%tance* te#'late% a e #uc, #o e u%e5ul in 4o d t,an in E6cel* and add in% a e #o e u%e5ul in E6cel t,an in Acce%%. /n an( ca%e* ou inte e%t ,e e i% in E6cel.

19.2.1 he Excel Startu! Fol#er


4,en E6cel load%* it auto#aticall( load% an( %' ead%,eet% %to ed in it% %ta tu' and alte nate %ta tu' 5olde %. 0,e de5ault location o5 t,e %ta tu' 5olde i% u%uall( a %ub5olde o5 t,e #ain E6cel 5olde na#ed 4l/tart. >( de5ault* t,e e i% no alte nate %ta tu' 5olde * alt,oug, one can be de5ined u%ing t,e .ene al tab o5 t,e O'tion% dialogH to o'en it* %elect O'tion% 5 o# t,e 0ool% #enu. >ecau%e t,e content% o5 t,e%e 5olde % a e o'ened at %ta tu' a% o dina ( wo -boo-%* t,ei #ac o% a e ea%il( acce%%ible to all ot,e E6cel wo -boo-%. 0,i% #a-e% t,e# ideal a% a %to age location 5o #ac o%. 0,e onl( d awbac- i% t,at E6cel actuall( o'en% t,e %' ead%,eet% %to ed in t,e%e di ecto ie%H to ' event t,i%* t,e( %,ould be ,idden b( %electing t,e Aide o'tion 5 o# E6celB% 4indow #enu ?not t,e $o #at #enu@ w,en t,e %' ead%,eet to be ,idden i% active. "ac o% t,at a e %to ed in t,e %ta tu' and alte nate %ta tu' 5olde % a e available 5 o# t,e "ac o dialog* and we can a%%ign t,e# to toolba % and #enu% t, oug, t,e E6cel u%e inte 5ace* a% well a% ' og a##aticall(. ?On t,e ot,e ,and* an add;in* w,ic, i% di%cu%%ed late in t,i% c,a'te * doe% not #a-e it% %ub outine% di ectl( acce%%ible to ot,e E6cel wo -boo-%* but in%tead e7ui e% t,at t,e( be a%%igned to toolba o #enu ite#% ' og a##aticall(.@ A wo -boo- %to ed in eit,e o5 t,e%e 5olde % i% an e6cellent c,oice 5o a lib a ( o5 #ac o% t,at (ou want to be globall( available to (ou %' ead%,eet%. /t i% al%o %uitable 5o develo'ing E6cel #ac o% 5o ot,e % to u%e* alt,oug, E6cel add;in% ?w,ic, a e di%cu%%ed in 1ection 10.2.3 late in t,i% c,a'te @ ' ovide g eate 5le6ibilit( and cont ol* and a e #uc, #o e %uitable 5o #ac o% intended 5o di%t ibution. 4e will a%%u#e in t,i% boo- t,at (ou want to %to e #ac o% in an add;in. A% we will %ee* t,e e a e clea advantage% to u%ing add;in%. "o eove * t,i% will give u% a c,ance to di%cu%% ,ow add;in% a e c eated in E6cel. Aoweve * (ou can 5eel 5 ee to 'lace t,e e6a#'le #ac o% in a %' ead%,eet t,at i% -e't in t,e %ta tu' o alte nate %ta tu' 5olde .

19.2.2 Excel e&!lates


0,e 'u 'o%e o5 an E6cel te#'late i% to ' ovide a %ta ting 'lace 5o a new wo -boo-* wo -%,eet* c,a t* o code #odule. ! eating a te#'late i% ea%(. 4e %i#'l( c eate a new wo -boo- and %ave it a% a te#'late u%ing t,e 1ave A% co##and. $o in%tance* %u''o%e we %ta t a new wo -boo- and ente t,e nu#be 123 in t,e 5i %t cell o5 t,e 5i %t %,eet. 0,en we %ave t,e wo -boo- in t,e te#'late% di ecto ( ?#o e on t,i% late @ a% a te#'late called testAxlt. 4,en we ne6t invo-e t,e +ew co##and 5 o# t,e $ile #enu* E6cel will di%'la( a

+ew dialog wit, an icon 5o ou te#'late* a% %,own in $igu e 10;4. 4,en we %elect t,e testAxlt icon* E6cel will c eate a new wo -boo- and copy t,e data 5 o# t,e te#'late into t,e wo -boo-. Figure 19-$. he New #ialog showing te&!late icons

/t i% ve ( i#'o tant to note t,at t,e data ?and ot,e t,ing% %uc, a% 5o #atting@ a% well a% #ac o% a e actuall( co'ied to t,e wo -boo-* a5te w,ic, all connection between t,e te#'late and t,e new wo -boo- i% %eve ed. 0,i% i% 7uite di55e ent 5 o# t,e wa( t,at "ic o%o5t 4o d u%e% te#'late%. A 4o d te#'late e#ain% attac,ed to t,e docu#ent. !e tain c,ange%* %uc, a% t,e addition o5 %t(le% o #ac o%* can be %aved eit,e in t,e te#'late o in t,e docu#ent it%el5* but 4o d neve co'ie% #ac o% 5 o# a te#'late into a docu#ent. Al%o* %eve al te#'late% can be o'ened at one ti#e ?t,e %o;called global te#'late%@* eac, o5 w,ic, #a( a55ect t,e docu#ent. 4o d te#'late% a e d(na#icH E6cel te#'late% a e %tatic. 0,i% educe% t,e u%e5ulne%% o5 E6cel te#'late% con%ide abl(* 5o i5 we c eate a te#'late t,at contain% lot% o5 code* 5o in%tance* t,en eac, wo -boo- t,at i% ba%ed on t,at te#'late will contain it% own co'( o5 t,at code. 0,i% can be a #ajo wa%te o5 %'ace and can al%o #a-e it ve ( di55icult to #aintain and u'g ade t,e code. $o t,e%e ea%on%* / gene all( avoid u%ing E6cel te#'late% w,eneve 'o%%ible. $o t,e eco d* ,oweve * we %,ould note t,at t,e 5ollowing ite#% a e t an%5e ed to a new wo -boo- o wo -%,eet t,at i% ba%ed on a te#'late:
C C C C C C C C C

0,e nu#be and t('e o5 %,eet% in a wo -boo!ell and %,eet 5o #at% %et u%ing t,e $o #at #enu !ell %t(le% &age 5o #at% and ' int;a ea %etting% 5o eac, %,eet !ell content% 4o -%,eet g a',ic% !u%to# toolba %* #ac o%* ,('e lin-%* and ActiveD cont ol% on 5o #%H cu%to# toolba % #u%t be attac,ed to t,e te#'late & otected and ,idden a ea% o5 t,e wo -boo4o -boo- calculation o'tion% and window di%'la( o'tion% %et u%ing t,e O'tion% co##and on t,e 0ool% #enu

4e %,ould al%o note t,at E6cel %u''o t% %eve al t('e% o5 %'ecial te#'late% called autotemplates. 0,e( a e te#'late% wit, t,e 5ollowing na#e%:
C C C C C C

Boo)Axlt /heetAxlt (hartAxlt "ialogAxlt ModuleAxlt MacroAxlt ?5o E6cel ve %ion 4 #ac o%@

4,en t,e Boo)Axlt te#'late i% %to ed in t,e 4l/tart %ubdi ecto (* E6cel ba%e% all new wo -boo-% on t,i% te#'late w,en (ou %elect t,e Wor)boo) icon in t,e +ew dialog ?%ee $igu e 10;2@. /5 (ou want new wo -%,eet% to ,ave a %'ecial 5o #at* t,en (ou can c eate a te#'late na#ed /heetAxlt and 'lace it in t,e 4l/tart 5olde . 0,en eve ( ti#e t,e /n%e t 4o -%,eet #enu ite# i% invo-ed* E6cel will #a-e co'ie% o5 all o5 t,e wo -%,eet% in t,e /heetAxlt te#'late and 'lace t,e# in t,e cu ent wo -boo-. +ote t,at t,i% can be #o e t,an one %,eet i5 t,e e i% #o e t,an one %,eet in /heetAxlt. >( now (ou get t,e idea. 0,e ot,e autote#'late% wo - %i#ila l(. /t i% al%o i#'o tant to -now t,at all o5 t,e O55ice a''lication% u%e t,e %a#e de5ault di ecto ( 5o te#'late%. Aence* t,i% di ecto ( #a( contain 4o d* E6cel* &owe &oint* and Acce%% te#'late%. >ut 4o d i% t,e onl( O55ice a''lication ?a% o5 O55ice 97@ t,at ' ovide% a wa( 5o t,e u%e to c,ange t,i% di ecto ( ?5 o# t,e $ile 3ocation% tab o5 t,e O'tion% dialog unde t,e 4o d 0ool% #enu@. /t 5ollow% t,at* c,anging t,i% di ecto ( u%ing 4o d will c,ange it 5o all O55ice a''lication%C

19.2." Excel %##-/ns


An E6cel add;in i% a %'ecial t('e o5 wo -boo- t,at i% u%uall( %aved wit, an .xla 5ile e6ten%ion. ?4e will di%cu%% ,ow to c eate add;in% late in t,i% %ection.@ An add;in can be connected to E6cel b( c,ec-ing it% c,ec- bo6 in t,e Add;/n% dialog ?%ee $igu e 10;5@* w,ic, i% di%'la(ed b( %electing Add;/n% 5 o# t,e 0ool% #enu. Figure 19-'. he %##-/ns #ialog

Once an add;in i% connected* it e#ain% %o ?even i5 E6cel i% clo%ed and eo'ened@ until t,e c,ecbo6 in t,e Add;/n% dialog i% unc,ec-ed. 4,en connected* an add;inB% 5unctionalit( ?=>A ' ocedu e%@ i% acce%%ible 5 o# an( E6cel wo -boo-. 0,u%* it i% t ul( an e6ten%ion o5 E6cel. 0('icall(* an add;in contain% code t,at c eate% new #enu ite#% o toolba ite#% t,at ' ovide t,e u%e wit, acce%% to t,e ' ocedu e% in t,e add;in. 0,i% code i% 'laced in t,e 4o -boo-IO'en event o5 t,e add;in %o t,at t,e #enu% ?o toolba %@ a e c eatedEcu%to#iGed a% %oon a% t,e add;in i% connected. ?4e will %ee e6a#'le% o5 t,i% %oon.@ 19.2.".1 +reating an a##-in

! eating an add;in i% a %i#'le ' oce%%. /t begin% wit, an E6cel wo -boo-* %a( /,4'tilsAxls. ?0,i% %tand% 5o 1teven Ro#anB% E6cel 2tilitie%.@ 0,e wo -boo-* o5 cou %e* contain% a nu#be o5 #ac o%. 0o c eate an add;in 5 o# t,e wo -boo-* 5ollow t,e%e %te'%: 1. !o#'ile t,e ' oject u%ing E6celB% =>A Edito . 4,en t,e code in an( =>A ' ocedu e i% edited and t,en e6ecuted* E6cel #u%t 5i %t compile t,e codeH t,at i%* t an%late t,e code into a language t,at t,e co#'ute can unde %tand. 0,i% i% w,( t,e e #a( be a %lig,t dela( t,e 5i %t ti#e code i% e6ecuted. 1ub%e7uent e6ecution o5 t,e %a#e code doe% not e7ui e co#'ilation unle%% t,e code ,a% been c,anged %ince t,e ' eviou% co#'ilation. 0o co#'ile t,e code in /,4'tilsAxls* %elect t,e !o#'ile o'tion 5 o# t,e )ebug #enu. 2. 1et a 5ew wo -%,eet ' o'e tie% and a 5ew ' oject ' o'e tie%. 4e %,ould al%o %et a 5ew ' o'e tie% 5o t,e add;in. 4,en /,4'tilsAxls i% t,e active wo -boo- in E6cel* c,oo%e t,e & o'e tie% o'tion 5 o# t,e E6cel $ile #enu and t,en di%'la( t,e 1u##a ( tab* a% %,own in $igu e 10;6. 0,e 0itle i% t,e %t ing t,at will be u%ed in t,e Add;/n% dialog* %,own in $igu e 10;7. 0,e !o##ent% will be %,own at t,e botto# o5 t,e Add;/n% dialog. 0,e e5o e* (ou %,ould 5ill in bot, o5 t,e%e %ection% in t,e & o'e tie% dialog* a% %,own in $igu e 10;6. Figure 19-5. %##-in !ro!erties

A E

L F M

Figure 19-6. he %##-/ns #ialog

+e6t* we u%e E6celB% =>A Edito to %et t,e ' o'e tie% o5 t,e =>A ' oject. /n t,e & oject E6'lo e o5 t,e =>A Edito * %elect t,e ' oject w,o%e 5ilena#e i% /,4'tilsAxls. 0,en c,oo%e & o'e tie% 5 o# t,e 0ool% #enu to di%'la( t,e dialog. $ill in t,e ' oject na#e and de%c i'tion a% %,own in $igu e 10;8. Figure 19-7. *.% !roject !ro!erties

3. & otect t,e code 5 o# viewing. 0o ' otect t,e code in an E6cel wo -boo- 5 o# unaut,o iGed viewing* we can u%e t,e =>A & oject & o'e tie% dialog. 1electing t,e dialogB% & otection tab* we get t,e dialog %,own in $igu e 10;9. !,ec-ing 83oc- ' oject 5o viewing8 and ente ing a 'a%%wo d ' otect% t,e code 5 o# viewing ?and 5 o# alte ation@. 0,e ' oject will %till a''ea in t,e =>/)E & oject window* but E6cel will not allow t,e u%e to e6'and t,e t ee 5o t,i% ' oject wit,out t,e 'a%%wo d. Figure 19-8. Protection tab

4. 1ave t,e wo -boo- a% an add;in in a di ecto ( o5 (ou c,oice. 1elect t,e 1ave A% o'tion 5 o# t,e $ile #enu* %elect 8"ic o%o5t E6cel Add;/n ?W.6la@8 5 o# t,e 81ave a% t('e8 d o';down li%t* navigate to t,e di ecto ( in w,ic, (ouBd li-e to %ave t,e 5ile* ente t,e 5ilena#e in t,e 8$ile na#e8 d o';down li%t bo6 ?in ou e6a#'le* itB% /,4'tilsAxla@ and ' e%% t,e 1ave button. Eve ( E6cel wo -boo- ,a% a ' o'e t( called /%Add/n. 4,en t,i% ' o'e t( i% Tr0e* E6cel con%ide % t,e wo -boo- to be an add;in. One o5 t,e con%e7uence% o5 t,i% i% t,at t,e wo -boo- beco#e% invi%ible* %o we cannot %i#'l( %et t,e /%Add/n ' o'e t( and t,en %ave t,e ' oject a% an D3A 5ile* %ince it% wo -boo- will be inacce%%ible 5 o# t,e E6cel u%e inte 5ace. $o tunatel(* "ic o%o5t ealiGed t,i% and a anged it %o t,at w,en we %ave t,e 5ile a% an add;in u%ing t,e 1ave A% dialog and c,oo%ing xla in t,e 81ave a% t('e8 d o' down li%tbo6* E6cel will auto#aticall( c,ange t,e /%Add/n ' o'e t( value to Tr0e. ?4e can c,ange t,e value to >alse a% di%cu%%ed late * in t,e %ection* 1ection 10.2.3.3.@ 19.2.".2 +haracteristics o4 an a##-in An add;in ,a% t,e 5ollowing c,a acte i%tic% t,at %et it a'a t 5 o# o dina ( E6cel wo -boo-%:
C

0,e wo -boo- window and an( wo -%,eet% in an add;in a e ,idden 5 o# view. 0,e intention i% t,at t,e c eato o5 t,e add;in can u%e wo -%,eet% to %to e %u''o ting data 5o t,e add;in. Aoweve * t,i% data %,ould not be vi%ible to t,e u%e o5 t,e add;in. /n 5act* an add;in i% de%igned to be t an%'a ent to t,e u%e H bot, t,e code and an( %u''o ting data a e ,idden 5 o# t,e u%e . 0,u%* i5 (ou want (ou add;in to e6'o%e wo -%,eet% to t,e u%e * t,e( #u%t be 'laced in %e'a ate E6cel wo -boo- 5ile%* w,ic, can be o'ened b( code in t,e add;in at t,e de%i ed ti#e. A% (ou ' obabl( -now* w,en an E6cel wo -boo- i% c,anged and t,e u%e t ie% to clo%e t,e wo -boo-* E6cel di%'la(% a wa ning #e%%age a%-ing i5 t,e u%e want% to %ave t,e c,ange% be5o e clo%ing t,e wo -boo-. +o %uc, #e%%age i% di%'la(ed 5o an add;in. 0,u%* t,e c eato o5 an add;in can c,ange t,e data in an add;in wo -%,eet t, oug, code wit,out wo (ing t,at t,e u%e o5 t,e add;in will be bot,e ed b( a #e%%age to w,ic, ,e o %,e could not 'o%%ibl( e%'ond intelligentl(. ?O5 cou %e* it i% u' to t,e add;inB% c eato to %ave an( c,ange% i5 de%i ed* u%ing t,e 1ave A% #et,od o5 t,e 4o -%,eet object.@ 4,en an E6cel wo -boo- i% o'ened* t,e 4o -boo-IO'en event i% 5i ed. $o an o dina ( 4o -boo-* t,e u%e can %u'' e%% t,i% event b( ,olding down t,e 1,i5t -e(. 0,e O'en event 5o an add;in cannot be %u'' e%%ed. 0,i% i% in -ee'ing wit, t,e ta#'e ;' oo5 natu e o5 add;in%. Add;in #ac o% a e not di%'la(ed in t,e "ac o% dialog bo6* t,u% ,iding t,e# 5 o# t,e u%e .

Add;ins and CO7 Add;ins


E6cel 2000 %u''o t% t,e %a#e add;in #odel t,at i% %u''o ted b( E6cel 97. 0,i% i% t,e add;in #odel t,at we u%e to c eate t,e 1RD2til% add;in. /n addition* t,e O55ice 2000 %uite %u''o t% a new add;in #odel called t,e !O" add;in #odel. A !O" add;in i% an ActiveD )33 o e6ecutable 5ile t,at can be connected to #ulti'le O55ice 2000 a''lication%. 1ince t,i% t('e o5 add;in i% an ActiveD )33 o e6ecutable* it #u%t be c eated u%ing a ' og a##ing envi on#ent* %uc, a% =i%ual >a%ic o =i%ual !FF* t,at i% ca'able o5 c eating t,e%e t('e% o5 5ile%. Aoweve * =i%ual >a%ic 5o A''lication% cannot c eate ActiveD )33% o e6ecutable%* %o it cannot be u%ed to c eate !O" add;in%. 19.2."." (ebugging a##-ins An add;in can be debugged ju%t li-e an( ot,e E6cel wo -boo-. Lou do not need to e5e again to t,e o iginal D31 5ile. /n 'a ticula * an add;in can be o'ened li-e an( ot,e E6cel wo -boo-. Aoweve * unle%% (ou -now t,e 'a%%wo d ?a%%u#ing t,at t,e add;in ,a% one@* (ou will not be able to %ee eit,e t,e add;inB% code o it% wo -boo- window. 2%ing t,e 'a%%wo d* (ou can e6'and t,e ' oject node in t,e & oject window to view t,e code and* i5 (ou %elect t,e 0,i%4o -boo- node and o'en t,e & o'e tie% window* c,ange t,e /%Add/n ' o'e t( to >alse to di%'la( t,e wo -boo- window. +ow (ou can t eat t,e wo -boo- ju%t li-e an( ot,e E6cel wo -boo-. Once t,e nece%%a ( c,ange% ,ave been #ade* (ou can eco#'ile t,e code and etu n t,e /%Add/n ' o'e t( to Tr0e. 19.2.".$ (eleting an a##-in Lou #a( ,ave noticed t,at t,e Add;/n% dialog %,own in $igu e 10;5 doe% not ,ave a )elete button. 0o e#ove an add;in 5 o# t,e li%t* unc,ec- t,e add;in* ena#e t,e D3A 5ile* and t,en c,ec- t,e add;in again. Lou will get a #e%%age a%-ing i5 E6cel %,ould e#ove t,e add;in 5 o# t,e li%t. And w,ile we a e on t,e %ubject o5 idio%(nc atic be,avio * note t,at c,ange% to an add;inB% 0itle ' o'e t( #a( not be e5lected in t,e Add;/n% dialog until E6cel i% %,ut down and eo'ened.

$>%* An Example Add;&n


3etB% begin t,e c eation o5 an E6cel add;in b( c eating an add;in %,ell. 0,i% will de#on%t ate t,e ' oce%% o5 add;in c eation and ' ovide a %ta ting 'oint 5 o# w,ic, we can c eate a 5ull;5ledged add;in;ba%ed E6cel a''lication* adding new 5eatu e% a% we ' oceed t, oug, t,e boo-. / %t ongl( %ugge%t t,at (ou 5ollow along in t,e c eation ' oce%%. /n t,i% c,a'te * we will c eate t,e add;in %,ell w,o%e 5eatu e% ju%t di%'la( #e%%age bo6e% ?5o now@. At t,i% ti#e* we do not want to cloud t,e i%%ue o5 add;in c eation b( i#'le#enting an( eal 5eatu e%. /n !,a'te 12* we will inc ea%e t,e nu#be o5 #oc- 5eatu e% %o t,at we can de#on%t ate ,ow to ,andle #ulti'le 5eatu e% in an add;in* a% well a% ,ow to c eate a cu%to# #enu %(%te# 5o an add;in. /n late c,a'te %* we will i#'le#ent t,e%e 5eatu e% and add additional one%.

19.".1 +reating the Source 0or1boo1

0,e 5i %t %te' i% to c eate a new wo -boo- t,at will act a% t,e %ou ce 5o t,e add;in. &lea%e do t,i% now. 0,i% wo -boo- will eventuall( be %aved a% an add;in. / will e5e to t,e wo -boo- a% /,4'tilsAxls* but (ou can 5eel 5 ee to na#e (ou ve %ion an(t,ing (ou li-e. /ncidentall(* a% we #a-e c,ange% to ou add;in* we will do %o in t,e /,4'tilsAxls wo -%,eet and t,en %ave t,at wo -%,eet ove t,e cu ent add;in. >e5o e doing %o* o5 cou %e* we #u%t unload t,e cu ent ve %ion o5 t,e add;in.

19.".2 Setting 2! the +usto& -enus


0o activate t,e #oc- 5eatu e% o5 ou add;in %,ell* we will c eate a cu%to# #enu. 4e will di%cu%% t,e c eation o5 #enu% and toolba % at lengt, in !,a'te 12. $o now* we will -ee' t,e detail% to a #ini#u# %o we can get t,e ove all 'ictu e o5 add;in c eation. Ou cu%to# #enu %,ould be c eated auto#aticall( w,en t,e add;in load% and de%t o(ed w,en t,e add;in unload%. Acco dingl(* we begin b( 'lacing %o#e code in t,e O'en and >e5o e!lo%e event% o5 0,i%4o -boo-* a% %,own in E6a#'le 10;1. Exa&!le 19-1. he 0or1boo1>s 3!en an# .e4ore+lose Event Can#lers
;rivate S0b Hor8boo8G1e6oreClose(Cancel As 1oolean) DeleteC0sto5Men02te5 'nd S0b ;rivate S0b Hor8boo8G*3en() CreateC0sto5Men02te5 'nd S0b

0,i% event code ju%t call% ' ocedu e% to c eate o delete t,e cu%to# #enu. 0,e%e ' ocedu e% %,ould be 'laced in a new code #odule* %o add a #odule to t,e /,4'tils ' oject and na#e it basMen0s. +e6t* 'lace t,e CreateC0sto5Men02te5 ' ocedu e %,own in E6a#'le 10;2 in basMen0s. /t i% not nece%%a ( to co#'letel( unde %tand t,i% ' ocedu e now* %ince we will go ove t,e detail% in !,a'te 12. $o t,e #o#ent* note t,at E6a#'le 10;2 c eate% an Activate1,eet #enu ite# on t,e !u%to# #enu* and t,at w,en we clic- t,e ite#* t,e outine de5ined b( it% OnAction ' o'e t(<in t,i% ca%e* t,e ActivateSheet %ub outine<i% un. Exa&!le 19-2. he +reate+usto&-enu/te& Proce#ure
S0b CreateC0sto5Men02te5() Di5 cbc3o3 As Co55and1arControl . Chec8 6or c0sto5 5en0. 26 it exists then exit. Set cbc3o3 = A33lication.Co55and1ars( G "Hor8sheet 5en0 bar"). G >indControl(T 3e-=5soControl;o303= G Ta4-="S"#<tilsC0sto5Men0") 26 9ot cbc3o3 2s 9othin4 Then 'xit S0b . Control does not exist %% create it. Set cbc3o3 = A33lication.Co55and1ars( G "Hor8sheet 5en0 bar"). G Controls.Add(T 3e-=5soControl;o303= G Te53orar -=Tr0e) cbc3o3.Ca3tion = "C0Ksto5"

. Set ta4 3ro3ert to 6ind it later 6or deletion cbc3o3.Ta4 = "S"#<tilsC0sto5Men0" . Add 5en0 ite5 to 3o303 5en0 Hith cbc3o3.Controls.Add(T 3e-=5soControl10tton= G Te53orar -=Tr0e) .Ca3tion = "KActivateSheet" .*nAction = "ActivateSheet" 'nd Hith 'nd S0b

Al%o 'lace t,e DeleteC0sto5Men02te5 ' ocedu e %,own in E6a#'le 10;3 into basMen0s: Exa&!le 19-". he (elete+usto&-enu/te& Proce#ure
S0b DeleteC0sto5Men02te5() Di5 cbc As Co55and1arControl Set cbc = A33lication.Co55and1ars( G "Hor8sheet 5en0 bar"). G >indControl(T 3e-=5soControl;o303= G Ta4-="S"#<tilsC0sto5Men0") 26 9ot cbc 2s 9othin4 Then cbc.Delete 'nd S0b

19."." /&!le&enting the Features o4 the %##-/n


1ince t,e ActivateSheet utilit( ?w,ic, i% invo-ed w,en t,e u%e %elect% t,e Activate1,eet cu%to# #enu ite# c eated b( t,e code in E6a#'le 10;2@ i% ve ( %i#'le* it doe% not e7ui e it% own code #odule. 4e %i#'l( add t,e 5ollowing ' ocedu e to t,e basMain code #odule* w,ic, we al%o #u%t c eate:
;0blic S0b ActivateSheet() Ms41ox "This is the ActivateSheet 0tilit " 'nd S0b

19.".$ Final Ste!s


$inall(* (ou %,ould 5ollow t,e%e %te'%: 1. (ompile the pro%ectA 2%e t,e )ebug #enu to co#'ile t,e /,4'tilsAxls ' oject. 2. /et the propertiesA 1et t,e wo -boo- and ' oject ' o'e tie% a% %,own in $igu e 10;6 and $igu e 10;8* #a-ing an( nece%%a ( c,ange% ba%ed on t,e na#e (ou ,ave c,o%en 5o (ou add;in. 3. Protect the add-inA 2nde t,e & otection tab o5 t,e ' ojectB% & o'e tie% dialog* c,ec- t,e 83oc- ' oject 5o viewing8 c,ec-bo6 and ente a 'a%%wo d. 4. /a#e the add-inA 1ave t,e ' oject a% an add;in na#ed /,4'tilsAxla in a di ecto ( o5 (ou c,oice. +ow we a e ead( to t ( t,e add;in. !lo%e t,e /,4'tilsAxls wo -boo- and o'en a new wo -boo-. 1elect t,e Add;/n% #enu ite# unde t,e 0ool% #enu and ,it t,e > ow%e button on t,e Add;/n% dialog. 3ocate (ou /,4'tilsAxla 5ile. 0,en c,ec- t,e ent ( in t,e Add;/n% dialog. Lou %,ould %ee t,e new !u%to# #enu in t,e wo -%,eet #enu ba . 1elect t,e Activate1,eet ite#. Lou %,ould get t,e e6'ected #e%%age bo6. &inis. A% #entioned ea lie * a% we ' og e%% t, oug, t,e boo-* we will #a-e t,i% e6a#'le add;in #uc, #o e #eaning5ul.

Chapter $$% Excel E ents


)u ing t,e cou %e o5 u%ing E6cel* ce tain e#ents ,a''en. $o in%tance* w,en a wo -%,eet i% c eated* t,at i% an event. 4,en a c,a t i% e%iGed* t,at i% an event. "ic o%o5t E6cel de5ine% a total o5 63 di55e ent event%. 4,en an event occu %* ' og a##e % li-e to %a( t,at t,e event fires. 0,e 'u 'o%e o5 an event i% %i#'l( to allow t,e =>A ' og a##e to w ite code t,at will e6ecute w,eneve an event 5i e%. A% we will %ee* t,i% code i% 'laced in an e#ent procedure. 0,e code it%el5 i% e5e ed to a% e#ent code. 4e w ote %o#e %i#'le event code 5o t,e O'en and >e5o e!lo%e wo -boo- event% w,en we c eated t,e /,4'tils add;in in t,e ' eviou% c,a'te . "o%t E6cel event% b ea- natu all( into 5ive g ou'%* a% indicated in 0able 11;1 t, oug, 0able 11;5. 0,e%e g ou'% 'a tiall( e5lect t,e level at w,ic, t,e event ta-e% 'lace<t,e a''lication level ?,ig,e%t@* t,e wo -boo- level ?#iddle@* o t,e wo -%,eetEc,a t%,eet level ?lowe%t@. 0o illu%t ate* w,en a wo -%,eet i% activated b( t,e u%e o t, oug, code ?b( calling t,e Activate #et,od@ %eve al event% will 5i e. 0,e( a e* in 5i ing o de :
C C C

0,e Activate event o5 t,e wo -%,eet. 0,i% event 5i e% w,eneve t,e wo -%,eet i% activated. 0,e 1,eetActivate event o5 t,e wo -boo-. 0,i% event 5i e% w,eneve an( wo -%,eet in t,e wo -boo- i% activated. 0,e 1,eetActivate event o5 t,e a''lication. 0,i% event 5i e% w,eneve an( wo -%,eet in an( wo -boo- in t,e cu entl( unning in%tance o5 E6cel i% activated. ?Aoweve * a% we will di%cu%% late * to enable t,i% event* we #u%t w ite %'ecial code.@

$$%$ The EnableE ents Propert/


/t i% i#'o tant to note t,at no E6cel event will 5i e unle%% t,e EnableEvent% ' o'e t( i% %et to Tr0e ?alt,oug, it i% %et to Tr0e b( de5ault@. 0,u%* t,e ' og a##e ,a% cont ol ove w,et,e E6cel event% a e enabled. 0,e EnableEvent% ' o'e t( i% a ' o'e t( o5 t,e A''lication object* %o* 5o in%tance* to ' event t,e 1ave event 5 o# 5i ing w,en t,e active wo -boo- i% %aved* we can w ite:
A33lication.'nable'vents = >alse ActiveHor8boo8.Save A33lication.'nable'vents = Tr0e

$$%' E ents and the Excel Ob1ect 7odel


0,e E6cel object #odel contain% %eve al object% t,at e6i%t %i#'l( a% a convenience* in o de to include t,e E6cel event% in t,e object #odel. ?4e do not actuall( ' og a# wit, t,e%e object%.@ 0,e%e object% a e A''Event%* )ocEvent%* !,a tEvent%* 4o ->oo-Event%* O3EObjectEvent%* and Re5 e%,Event%. 0,e event% a%%ociated wit, a wo -%,eet* 5o in%tance* a e #et,od% o5 t,e )ocEvent% object* w,ic, i% a c,ild o5 t,e 4o -%,eet object and t,e !,a t object.

$$%* Accessing an E ent Procedure

>( now (ou a e ' obabl( wonde ing ,ow to w ite an event ' ocedu e. 0,e %,o t an%we i% t,at 5o eac, event* E6cel ' ovide% u% wit, an e#ent code shell w,e e we can 'lace t,e event code 5o t,at event. 0o illu%t ate* con%ide t,e 1election!,ange event o5 t,e 4o -%,eet object. $igu e 11;1 %,ow% t,e code window 5o a wo -%,eet ?1,eet1@. +ote t,at t,e 4o -%,eet object i% %elected in t,e object% li%t bo6. 0,i% cau%e% t,e ' ocedu e% li%t bo6 to be 5illed wit, t,e na#e% o5 t,e wo -%,eet event%. 4e can %i#'l( c,oo%e t,e event 5o w,ic, we want to w ite event code. Figure 11-1. Events 4or the 0or1sheet object

$o in%tance* i5 we c,oo%e t,e 1election!,ange event* E6cel will auto#aticall( ' oduce t,e 5ollowing code %,ell:
;rivate S0b Hor8sheetGSelectionChan4e(1 +al Tar4et As 'xcel."an4e) 'nd S0b

E6cel will even 'lace t,e cu %o between t,e two code line% %o we can begin ente ing event code. A% t,e na#e i#'lie%* t,i% event 5i e% w,en t,e cu ent %election i% c,anged in t,e wo -%,eet. +ote t,at E6cel will 5ill in t,e Tar!et 'a a#ete wit, t,e Range object t,at e' e%ent% t,e new %election. 0,u%* ou event code ,a% acce%% to t,e new %election* but not to t,e ' eviou% %election. "an( event% ,ave 'a a#ete % a%%ociated wit, t,e#. 0,i% ' ovide% a wa( 5o E6cel to 'a%% u% in5o #ation elated to t,e event. 0,e %a#e a'' oac, will wo - 5o t,e wo -boo- and c,a t event%* but A''lication event% e7ui e a di55e ent a'' oac,* w,ic, we will di%cu%% late in t,e c,a'te . 0,e E6cel event% a e li%ted in 0able 11;1 t, oug, 0able 11;5.

$$%2 -orksheet E ents


0,e wo -%,eet; elated event% a e %,own in 0able 11;1. 0,e%e event% a e al%o e5e ed to a% docu#ent event%. able 11-1. 0or1sheet Events ;(ocEvents< Des ription Occu % w,en a wo -%,eet i% activated. Occu % w,en a wo -%,eet i% double;clic-ed* be5o e t,e de5ault double;clic- action.

"#ent name Activate >e5o e)ouble!lic-

>e5o eRig,t!lic!alculate !,ange )eactivate &ivot0able2'date ?E6cel 10 onl(@ 1election!,ange

Occu % w,en a wo -%,eet i% ig,t;clic-ed* be5o e t,e de5ault ig,t clic- action. Occu % a5te t,e wo -%,eet i% ecalculated. Occu % w,en cell% on t,e wo -%,eet a e c,anged b( t,e u%e o b( an e6te nal lin-. Occu % w,en t,e wo -%,eet i% deactivated. Occu % a5te a &ivot0able e'o t i% u'dated on a wo -%,eet. Occu % w,en t,e %election c,ange% on a wo -%,eet.

$$%3 -orkBook E ents


0able 11;2 %,ow% t,e wo -boo-; elated event%. able 11-2. 0or1boo1 Events "#ent name Des ription Activate Occu % w,en a wo -boo- i% activated. Addin/n%tall Occu % w,en t,e wo -boo- i% in%talled a% an add;in. Addin2nin%tall Occu % w,en t,e wo -boo- i% unin%talled a% an add;in. >e5o e!lo%e Occu % be5o e t,e wo -boo- clo%e%. >e5o e& int Occu % be5o e t,e wo -boo- ?o an(t,ing in it@ i% ' inted. >e5o e1ave Occu % be5o e t,e wo -boo- i% %aved. )eactivate Occu % w,en t,e wo -boo- i% deactivated. +ew1,eet Occu % w,en a new %,eet i% c eated in t,e wo -boo-. O'en Occu % w,en t,e wo -boo- i% o'ened. &ivot0able!lo%e!onnection ?E6cel 10 Occu % a5te a &ivot0able clo%e% t,e connection to it% data %ou ce. onl(@ &ivot0ableO'en!onnection?E6cel 10 Occu % a5te a &ivot0able o'en% t,e connection to it% data onl(@ %ou ce. 1,eetActivate Occu % w,en an( %,eet i% activated. Occu % w,en an( wo -%,eet i% double;clic-ed* be5o e t,e 1,eet>e5o e)ouble!licde5ault double;clic- action. Occu % w,en an( wo -%,eet i% ig,t;clic-ed* be5o e t,e 1,eet>e5o eRig,t!licde5ault ig,t;clic- action. Occu % a5te an( wo -%,eet i% ecalculated o a5te an( 1,eet!alculate c,anged data i% 'lotted on a c,a t. Occu % w,en cell% in an( wo -%,eet a e c,anged b( t,e 1,eet!,ange u%e o b( an e6te nal lin-. 1,eet)eactivate Occu % w,en an( %,eet i% deactivated. Occu % w,en t,e %election c,ange% on an( wo -%,eet 1,eet1election!,ange ?doe% not occu i5 t,e %election i% on a c,a t %,eet@. 4indowActivate Occu % w,en an( wo -boo- window i% activated. 4indow)eactivate Occu % w,en an( wo -boo- window i% deactivated. 4indowRe%iGe Occu % w,en an( wo -boo- window i% e%iGed.

/ncidentall(* a u%e can %u'' e%% t,e O'en event 5o a wo -boo- b( ,olding down t,e 1,i5t -e( w,en o'ening t,e wo -boo-.

$$%8 Chart E ents


0able 11;3 %,ow% t,e c,a t; elated event%. able 11-". +hart EventsDes ription "#ent name Activate Occu % w,en a c,a t %,eet o e#bedded c,a t i% activated. Occu % w,en an e#bedded c,a t i% double;clic-ed* be5o e t,e de5ault double; >e5o e)ouble!licclic- action. Occu % w,en an e#bedded c,a t i% ig,t;clic-ed* be5o e t,e de5ault ig,t;clic>e5o eRig,t!licaction. !alculate Occu % a5te t,e c,a t 'lot% new o c,anged data. )eactivate Occu % w,en t,e c,a t i% deactivated. ) agOve Occu % w,en a ange o5 cell% i% d agged ove a c,a t. ) ag&lot Occu % w,en a ange o5 cell% i% d agged and d o''ed on a c,a t. "ou%e)own Occu % w,en a #ou%e button i% ' e%%ed w,ile t,e 'ointe i% ove a c,a t. "ou%e"ove Occu % w,en t,e 'o%ition o5 t,e #ou%e 'ointe c,ange% ove a c,a t. "ou%e2' Occu % w,en a #ou%e button i% elea%ed w,ile t,e 'ointe i% ove a c,a t. Re%iGe Occu % w,en t,e c,a t i% e%iGed. 1elect Occu % w,en a c,a t ele#ent i% %elected. 1e ie%!,ange Occu % w,en t,e u%e c,ange% t,e value o5 a c,a t data 'oint.

$$%9 Application E ents


0able 11;4 %,ow% t,e A''lication;level event%. 0,e%e event% a''l( to all object% in t,e cu entl( unning in%tance o5 E6cel. able 11-$. %!!lication Events "#ent name Des ription +ew4o -booOccu % w,en a new wo -boo- i% c eated. 1,eetActivate Occu % w,en an( %,eet i% activated. Occu % w,en an( wo -%,eet i% double;clic-ed* 1,eet>e5o e)ouble!licbe5o e t,e de5ault double;clic- action. Occu % w,en an( wo -%,eet i% ig,t;clic-ed* be5o e 1,eet>e5o eRig,t!lict,e de5ault ig,t;clic- action. Occu % a5te an( wo -%,eet i% ecalculated o a5te 1,eet!alculate an( c,anged data i% 'lotted on a c,a t. Occu % w,en cell% in an( wo -%,eet a e c,anged b( 1,eet!,ange t,e u%e o b( an e6te nal lin-. Occu % a5te t,e %,eet containing t,e &ivot0able 1,eet&ivot0able2'date ?E6cel 10 onl(@ e'o t ,a% been u'dated.

1,eet)eactivate

Occu % w,en an( %,eet i% deactivated. Occu % w,en t,e %election c,ange% on an( wo -%,eet ?doe% not occu i5 t,e %election i% on a 1,eet1election!,ange c,a t %,eet@. 4indowActivate Occu % w,en an( wo -boo- window i% activated. 4indow)eactivate Occu % w,en an( wo -boo- window i% deactivated. 4indowRe%iGe Occu % w,en an( wo -boo- window i% e%iGed. 4o -boo-Activate Occu % w,en an( wo -boo- i% activated. 4o -boo-Addin/n%tall Occu % w,en a wo -boo- i% in%talled a% an add;in. 4o -boo-Addin2nin%tall Occu % w,en an( add;in wo -boo- i% unin%talled. Occu % i##ediatel( be5o e an( o'en wo -boo4o -boo->e5o e!lo%e clo%e%. 4o -boo->e5o e& int Occu % be5o e an( o'en wo -boo- i% ' inted. 4o -boo->e5o e1ave Occu % be5o e an( o'en wo -boo- i% %aved. 4o -boo-)eactivate Occu % w,en an( o'en wo -boo- i% deactivated. Occu % w,en a new %,eet i% c eated in an( o'en 4o -boo-+ew1,eet wo -boo-. 4o -boo-O'en Occu % w,en a wo -boo- i% o'ened. 4o -boo-&ivot0able!lo%e!onnection Occu % a5te a &ivot0able e'o t connection ,a% ?E6cel 10 onl(@ been clo%ed. 4o -boo-&ivot0ableO'en!onnection ?E6cel Occu % a5te a &ivot0able e'o t connection ,a% 10 onl(@ been o'ened. 2n5o tunatel(* E6cel #a-e% it a bit #o e di55icult to eac, t,e A''lication event% t,an event% in t,e ot,e catego ie%. Ae e i% a %te';b(;%te' ' ocedu e 5o eac,ing t,e event code %,ell% 5o t,e A''lication event%: 1. 2%e t,e =>A /n%e t #enu to in%e t a cla%% #odule into (ou ' oject. 3et u% call t,i% cla%% #odule CA33 ?%,o t 5o Class A33lication@. /n t,e decla ation %ection o5 t,e cla%% #odule* add t,e line:
;0blic Hith'vents A33 As A33lication

!,oo%ing t,e A'' object in t,e object% d o';down %,ould now give (ou acce%% to t,e A''lication event code %,ell%* a% %,own in $igu e 11;2. Figure 11-2. %!!lication-level events

2. /n t,e code #odule in w,ic, (ou want to activate A''lication;level event% ?%a(* t,e code #odule a%%ociated wit, a wo -boo-* wo -%,eet* o c,a t@* 'lace t,e 5ollowing decla ation in t,e decla ation% %ection o5 t,e #odule:

Di5 A33*bT As 9ew CA33

?Lou can u%e an( va iable na#e (ou wi%, in 'lace o5 3. $inall(* a%%ign t,e A'' ' o'e t( o5 code:

/bj @.

/bj to t,e A''lication object* b( e6ecuting t,e

Set A33*bT.A33 = 'xcel.A33lication

/t i% u' to (ou w,e e to 'lace t,i% line o5 code* but it #u%t be e6ecuted in o de to activate A''lication;level event%. ?0,e e i% a ce tain ci cula it( ,e e* %ince a natu al 'lace to 'ut t,i% code i% in t,e 4o -boo-O'en event. Aoweve * t,i% event will not 5i e until t,i% code ,a% been e6ecuted.@ /n addition to u%ing t,e EnableEvent% ' o'e t(* (ou can tu n o55 A''lication;level event% b( e6ecuting t,e code:
Set A33*bT.A33 = 9othin4

$$%< ?uer/Table 6efresh E ents


0able 11;5 %,ow% t,e event% elated to Oue (0able%. 4e will not di%cu%% Oue (0able% in t,i% boo-* but at lea%t now (ou a e awa e o5 t,e e6i%tence o5 t,e%e event% %,ould (ou decide to 'u %ue t,i% #atte on (ou own. able 11-'. ,e4resh Events Des ription Occu % a5te a 7ue ( i% co#'leted o canceled. Occu % be5o e an( e5 e%,e% o5 t,e 7ue ( table.

"#ent name A5te Re5 e%, >e5o eRe5 e%,

Chapter $'% Custom 7enus and Toolbars


/n t,i% c,a'te * we di%cu%% #et,od% 5o ' og a##aticall( cont olling #enu% and toolba %. Even t,oug, t,e %ubject o5 #enu% and toolba % i% 5ai l( %t aig,t5o wa d* it can %ee# ve ( con5u%ing* e%'eciall( %ince t,e docu#entation i% le%% ,el'5ul t,an it #ig,t be.

$'%$ 7enus and Toolbars+ An O er ie"


Actuall(* E6celB% #enu and toolba object% do not belong to t,e E6cel object #odel. 0,e #enu% and toolba % t, oug,out t,e "ic o%o5t O55ice a''lication %uite belong to t,e O55ice object #odel. 0,e 'o tion o5 t,e O55ice object #odel t,at elate% to #enu% and toolba % i% %,own in $igu e 12;1. Figure 12-1. he &enu an# toolbar !ortion o4 the 344ice object &o#el

+ote t,at t,i% #odel i% actuall( 7uite %#all* containing onl( two object% and t,ei co e%'onding collection%:
C C

!o##and>a object% and t,e !o##and>a % collection !o##and>a !ont ol object% and t,e !o##and>a !ont ol% collection

12.1.1 -enu er&inology


0o ,el' %et t,e notation* $igu e 12;2 %,ow% t,e co#'onent% o5 t,e O55ice #enu %t uctu e ?t,i% ,a''en% to be a 4o d #enu* but no #atte @. Figure 12-2. %n 344ice &enu

12.1.2 he +o&&an#.ar 3bject


0oolba %* #enu ba %* #enu%* %ub#enu%* and %,o tcut #enu% a e all !o##and>a object%. ?A %,o tcut #enu i% a #enu t,at 'o'% u' in e%'on%e to a ig,t #ou%e clic-.@ 0,u%* eve ( ite# 'ictu ed in $igu e 12;2 i% a co##and ba e6ce't t,e 'o'u' cont ol% and t,e button cont ol. O5 cou %e* toolba %* #enu ba %* and %,o tcut #enu% a e 8to' level8 object%* w,e ea% #enu% and %ub#enu% e#anate 5 o# toolba %* #enu ba %* o %,o tcut #enu%. /t i% i#'o tant to note t,at O55ice =>A doe% not t eat eac, o5 t,e%e !o##and>a object% in t,e %a#e wa(. $o in%tance* t,e !ount ' o'e t( o5 t,e !o##and>a % collection count% onl( t,e to' level ite#%: #enu ba %* toolba %* and %,o tcut #enu%. /t doe% not count #enu% o %ub#enu%. Al%o* t,e Add #et,od o5 t,e !o##and>a % collection can be u%ed to c eate toolba % o #enu ba %* but not #enu% o %ub#enu%. 0,e !o##and>a object ,a% a 0('e ' o'e t( t,at can a%%u#e one o5 t,e con%tant% in t,e 5ollowing enu#:
'n05 Mso1arT 3e 5so1arT 3e9or5al = 7 5so1arT 3eMen01ar = 1 5so1arT 3e;o303 = 2 'nd 'n05 . toolbar . 5en0 bar . 5en0= s0b5en0= or shortc0t 5en0

12.1." +o&&an#-.ar +ontrols


0,e ite#% on a toolba * #enu ba * #enu* o %ub#enu a e actuall( cont ol%* called command-bar controlsH t,at i%* t,e( a e !o##and>a !ont ol object%. A% we will %ee* t,e e a e va iou% t('e% o5 co##and;ba cont ol%* 5alling into two b oad catego ie%: cu%to# co##and;ba cont ol% ?including cu%to# te6t bo6e%* d o';down li%t bo6e%* and co#bo bo6e%@ and built;in co##and;ba cont ol%. +ote t,at co##and;ba cont ol% a e not t,e %a#e a% t,e cont ol% t,at we can 'lace on a 'ser&ormC t,e( a e de%igned %'eci5icall( 5o toolba % and #enu%. 0,e e a e two %'ecial t('e% o5 cu%to# co##and;ba cont ol% t,at a e not t('ical o5 ot,e t('e% o5 cont ol%. 0,e%e a e &o'u' cont ol% and >utton cont ol%.

12.1.".1 Po!u! controls A co##and;ba cont ol o5 t('e 5soControl;o303 i% a cont ol w,o%e %ole 'u 'o%e i% to 'o' u' a #enu ?w,en t,e cont ol i% on a #enu ba @ o a %ub#enu ?w,en t,e cont ol i% on a #enu@. 0,e%e cont ol% a e natu all( e5e ed to a% popup controls ?%ee $igu e 12;2@. &o'u' cont ol% t,at a e located on a #enu ba ta-e on t,e a''ea ance o5 a ece%%ed button w,en t,e #ou%e 'ointe i% ove t,e cont ol. &o'u' cont ol% on a #enu o %ub#enu ,ave a %#all a ow on t,e 5a ig,t to identi5( t,e#. 0,u%* t,e te # popup i% u%ed in two di55e ent wa(%. A 'o'u' control i% a co##and;ba cont ol o5 t('e 5soControl;o303 and i% u%ed to 'o' u' a #enu o %ub#enu. A 'o'u' command bar i% a co##and ba o5 t('e 5so1arT 3e;o303 and i% eit,e a #enu* %ub#enu* o %,o tcut #enu. +ote t,at to di%'la( a 'o'u' co##and ba * t,e u%e need% to activate a 'o'u' cont ol. 12.1.".2 .utton controls A co##and;ba cont ol o5 t('e 5soControl10tton i% called a button control. 4,en a button cont ol i% activated ?u%ing an accele ato -e( o #ou%e clic-@* a #ac o i% e6ecuted. >utton cont ol% ,ave a %t ing ' o'e t( called OnAction* w,ic, we can %et to t,e na#e o5 t,e #ac o t,at i% e6ecuted w,en t,e cont ol i% activated.

12.1.$ %##ing a -enu /te&


/t i% wo t, #entioning now t,at t,e e a e a 5ew counte intuitive w in-le% in t,e ' oce%% o5 #enu c eation. /n 'a ticula * we #ig,t t,in- at 5i %t t,at adding a new #enu %,ould be done u%ing t,e Add #et,od o5 t,e !o##and>a % collection* %'eci5(ing t,e na#e o5 t,e 'a ent #enu and t,e location o5 t,e new #enu on t,e 'a ent. A5te all* a #enu i% a !o##and>a object* and t,i% ' ocedu e would be con%i%tent wit, ot,e ca%e% o5 adding object% to a collection. Aoweve * t,i% i% not ,ow it i% done. /n%tead* a% we will %ee* a new #enu ?o %ub#enu@ i% c eated b( adding a co##and;ba cont ol o5 t('e 5soControl;o303 to t,e !o##and>a !ont ol% collection o5 t,e 'a ent #enu ?and %'eci5(ing t,e new cont olB% 'o%ition on t,e 'a ent@. Actuall(* t,i% e' e%ent% a %aving% o5 e55o t on ou be,al5. $o * a% we ,ave e#a -ed* a #enu o %ub#enu e7ui e% a 'o'u' cont ol 5o activation. 0,u%* "ic o%o5t #a-e% t,e ta%- o5 c eating #enu% and %ub#enu% ea%ie b( auto#aticall( c eating t,e co e%'onding ?e#'t(@ #enu o %ub#enu in e%'on%e to ou c eation o5 a 'o'u' cont ol. ?4e will %ee an e6a#'le o5 t,i% late * %o donBt wo ( too #uc, i5 t,i% i% not 'e 5ectl( clea (et.@ One wo d o5 advice be5o e ' oceeding: A% we will %ee* w,en c eating a new toolba o #enu* (ou can %et one o5 t,e 'a a#ete % to #a-e t,e object te#'o a (* #eaning t,at it will be de%t o(ed w,en E6cel i% clo%ed. /n t,i% wa(* i5 an(t,ing une6'ected ,a''en%* it i% ea%( to ecove <ju%t clo%e E6cel and eo'en it. Alte nativel(* b( o'ening t,e !u%to#iGe dialog bo6 ?5 o# t,e 0ool% #enu@* (ou can delete #enu ite#% b( d agging t,e# o55 o5 t,e #enu* and (ou can delete toolba % b( u%ing t,e )elete button.

$'%' The CommandBars Collection


0,e to'#o%t object t,at elate% to #enu% and toolba % i% t,e !o##and>a % collection* w,ic, contain% all o5 t,e a''licationB% !o##and>a object%. 0,e !o##and>a % collection i% acce%%ible t, oug, t,e !o##and>a % ' o'e t( o5 t,e A''lication object* t,at i%:
A33lication.Co55and1ars

0,e code in E6a#'le 12;1 will ' int a li%t o5 all o5 t,e !o##and>a object% to t,e i##ediate window. Lou #a( be %u ' i%ed at t,e la ge nu#be o5 object%* #o%t o5 w,ic, a e not cu entl( vi%ible. Exa&!le 12-1. )isting Excel>s +o&&an#.ar 3bjects
;0blic S0b ShowC5d1ars() Di5 sT 3e as strin4= cbar as Co55and1ar >or 'ach cbar 2n A33lication.Co55and1ars Select Case cbar.T 3e Case 5so1arT 3e9or5al . A toolbar sT 3e = "9or5al" Case 5so1arT 3eMen01ar . A 5en0 bar sT 3e = "Men0 bar" Case 5so1arT 3e;o303 . Men0= s0b5en0 sT 3e = ";o303" 'nd Select Deb04.;rint cbar.9a5e K "=" K sT 3e K "=" K cbar.+isible 9ext 'nd S0b

/5 (ou e6ecute t,i% code* (ou %,ould get t,e 5ollowing ent ie%* a#ong #an( ot,e %:
Hor8sheet Men0 1ar=Men0 bar=Tr0e Chart Men0 1ar=Men0 bar=>alse

0,i% indicate% t,at E6celB% #ain #enu ba % a e di55e ent 5o wo -%,eet% t,an 5o c,a t%,eet%* a% i% evident i5 (ou loo- at t,e #enu% t,e#%elve%. 0,e wo -%,eet #enu ba ,a% di55e ent cont ol% t,an t,e !,a t #enu ba . 0,u%* i5 (ou want to add a cu%to# #enu ite# to E6celB% 8#ain8 #enu ba * ega dle%% o5 w,at t('e o5 %,eet i% cu entl( active* (ou will need to do %o 5o bot, t,e 4o -%,eet "enu >a and t,e !,a t "enu >a . 0,e e i% a %lig,t co#'lication conce ning t,e !o##and>a % ' o'e t( t,at we %,ould di%cu%%. 4,en 7uali5ied wit, t,e A''lication object* a% in A33lication.Co55and1ars* t,i% ' o'e t( etu n% t,e collection o5 all available built;in and cu%to# co##and ba % 5o t,e a''lication w,ic, in t,i% ca%e i% E6cel. 0,i% i% w,( we u%ed t,e 5ull( 7uali5ied e6' e%%ion A33lication.Co55and1ars in E6a#'le 12;1. +ote t,at 5 o# a standard code #odule* we can %-i' t,e 7uali5ication and ju%t w ite !o##and>a %. Aoweve * 5 o# a 4o -boo-* t,e !o##and>a % ' o'e t( etu n% a di55e ent collection. /n 'a ticula * t,e e a e two 'o%%ibilitie%. 4,en t,e wo -boo- i% e#bedded wit,in anot,e a''lication and E6cel i% activated b( double;clic-ing on t,at e#bedded wo -boo-* t,e !o##and>a % collection etu n% t,e collection o5 co##and ba % t,at a e available in t,at %etting. 0,i% #a( be di55e ent 5 o# t,e 5ull collection o5 E6cel co##and ba %. /5 t,e wo -boo- i% not e#bedded in anot,e a''lication* t,en t,e !o##and>a % ' o'e t( etu n% 9othin4. +ote al%o t,at t,e 4o -boo- object ,a% a !o##and>a % ' o'e t(. Aoweve * t,i% ' o'e t( i% #eaning5ul onl( w,en t,e wo -boo- i% e#bedded wit,in anot,e a''lication* in w,ic, ca%e t,e ' o'e t( etu n% t,e !o##and>a % collection 5o t,at a''lication. 4,en a''lied to a none#bedded wo -boo-* t,e ' o'e t( etu n% 9othin4. "o eove * t,e e i% no ' og a##atic wa( to etu n t,e %et o5 co##and ba % attac,ed to a wo -boo-.

$'%* Creating a 4e" 7enu Bar or Toolbar


A% we ,ave %aid* one wa( in w,ic, #enu ba % and toolba % di55e 5 o# #enu% and %ub#enu% i% in t,ei c eation. 0o c eate a new #enu ba o %,o tcut #enu* we u%e t,e Add #et,od o5 t,e !o##and>a % collection. 0,e %(nta6 5o t,e Add #et,od i%:
Command)ars/bject'Add(Name, Position, (enu)ar, Tem orary)

0,e o'tional Name 'a a#ete i% t,e na#e o5 t,e new co##and ba . /5 t,i% a gu#ent i% o#itted* E6cel =>A a%%ign% a de5ault na#e ?%uc, a% 8!u%to# 18@ to t,e co##and ba . 0,e o'tional Position 'a a#ete give% t,e 'o%ition o5 t,e new co##and ba . 0,i% can be %et to 5so1arLe6t* 5so1arTo3* 5so1ar"i4ht* 5so1ar1otto5 * 5so1ar>loatin4 ?5o a 5loating co##and ba @* o 5so1ar;o303 ?5o a %,o tcut #enu@. 0,e o'tional >oolean (enu)ar 'a a#ete i% %et to Tr0e 5o a #enu ba and >alse 5o a toolba . 0,e de5ault value i% >alse* %o i5 t,e a gu#ent i% o#itted* a toolba i% c eated. +ote t,at i5 (ou c eate a new #enu ba and #a-e it vi%ible* it will e'lace t,e e6i%ting E6cel #enu ba C /5 t,i% ,a''en%* (ou can %till e6it E6cel b( t('ing Alt;$4* and t,e no #al E6cel #enu will ea''ea t,e ne6t ti#e t,at (ou launc, E6cel. 1etting t,e o'tional Tem orary 'a a#ete to Tr0e #a-e% t,e new co##and ba te#'o a (. 0e#'o a ( co##and ba % a e deleted w,en E6cel i% clo%ed. 0,e de5ault value i% >alse. 0o illu%t ate* t,e 5ollowing code c eate% a new 5loating toolba called 8!u%to# 0oolba 8 and #a-e% it vi%ible:
Di5 cbar As *66ice.Co55and1ar Set cbar = A33lication.Co55and1ars.Add("C0sto5 Toolbar"= G 5so1ar>loatin4= >alse= Tr0e) cbar.+isible = Tr0e

/t i% i#'o tant to note t,at* i5 a !o##and>a object b( t,e na#e !u%to# 0oolba al ead( e6i%t%* t,e ' eviou% code will ' oduce a unti#e 8/nvalid ' ocedu e call8 e o . 0,u%* we eall( %,ould te%t 5o t,e e6i%tence o5 t,e !o##and>a object be5o e u%ing t,e Add #et,od* a% %,own in E6a#'le 12;2. Exa&!le 12-2. +reating a New oolbar
;0blic S0b CreateToolbar() Di5 cbar As *66ice.Co55and1ar Di5 b'xists As 1oolean b'xists = >alse >or 'ach cbar 2n A33lication.Co55and1ars 26 cbar.9a5e = "C0sto5 Toolbar" Then b'xists = Tr0e 9ext 26 9ot b'xists Then Set cbar = A33lication.Co55and1ars.Add("C0sto5 Toolbar"= G 5so1ar>loatin4= >alse= Tr0e) cbar.+isible = Tr0e 'nd 26 'nd S0b

$'%2 Command;Bar Controls


/nitiall(* one o5 t,e #o%t con5u%ing a%'ect% o5 t,e O55ice #enu %(%te# i% t,at t,e ite#% t,at a''ea on a #enu ba a e not #enu%* o even na#e% o5 #enu%. Rat,e * t,e( a e controls o5 t('e !o##and>a !ont ol. !o##and;ba cont ol% can be added to a #enu ba * toolba * #enu* %ub#enu* o %,o tcut #enu. ?0,in- o5 toolba %* #enu ba %* and %o on a% 85o #%8 u'on w,ic, (ou 'lace cont ol%.@ Eve ( co##and;ba cont ol i% an object o5 t('e !o##and>a !ont ol and %o it belong% to t,e !o##and>a !ont ol% collection. ?4e a e not %a(ing t,at t,e 0('e ' o'e t( o5 a co##and;ba cont ol i% !o##and>a !ont ol.@ /n addition* eve ( co##and;ba cont ol i% an object o5 one o5 t,e 5ollowing t, ee object t('e%:
C C C

!o##and>a >utton !o##and>a !o#bo>o6 !o##and>a &o'u'

0,i% dual identit( o5 !o##and>a !ont ol object% allow% t,e va iou% t('e% o5 co##and;ba cont ol% to 'o%%e%% on t,e one ,and a co##on %et o5 ' o'e tie% and #et,od% ?t,o%e o5 t,e !o##and>a !ont ol object@ and* on t,e ot,e ,and* an additional %et o5 ' o'e tie% and #et,od% t,at e5lect% t,e dive %it( o5 t,e%e cont ol%. 0,i% #a-e% %en%e* %ince* 5o in%tance* te6t bo6e% a e 7uite di55e ent 5 o# 'o'u' cont ol%. "o eove * a% we will %ee* !o##and>a &o'u' object% need a %'ecial ' o'e t( ?called !ont ol%@ t,at ' ovide% acce%% to t,e associated menuDs cont ol%. ?0,e ot,e t('e% o5 !o##and>a !ont ol object% do not need* and do not ,ave* t,i% ' o'e t(.@ 0,e 0('e ' o'e t( o5 a !o##and>a !ont ol ,el'% to identi5( t,e data t('e o5 t,e cont ol. /t can a%%u#e an( o5 t,e value% in t,e 5ollowing enu#:
'n05 MsoControlT 3e 5soControlC0sto5 = 7 5soControl10tton = 1 5soControl'dit = 2 5soControlDro3down = B 5soControlCo5bo1ox = L 5soControl10ttonDro3down = & 5soControlS3litDro3down = N 5soControl*C#Dro3down = F 5soControl:enericDro3down = D 5soControl:ra3hicDro3down = E 5soControl;o303 = 17 5soControl:ra3hic;o303 = 11 5soControl10tton;o303 = 12 5soControlS3lit10tton;o303 = 1B 5soControlS3lit10ttonM"<;o303 = 1L 5soControlLabel = 1& 5soControl'x3andin4:rid = 1N 5soControlS3lit'x3andin4:rid = 1F 5soControl:rid = 1D 5soControl:a04e = 1E 5soControl:ra3hicCo5bo = 27 'nd 'n05

. . . . . . . . . . . . .

Co55and1ar10tton Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1arCo5bo1ox Co55and1ar;o303 Co55and1ar;o303 Co55and1ar;o303 Co55and1ar;o303 Co55and1ar;o303

. Co55and1arCo5bo1ox

0,e co##ent% t,at 5ollow %o#e o5 t,e con%tant% in t,i% enu# indicate t,e data t('e o5 t,e cont ol. 0,i% in5o #ation co#e% 5 o# t,e "ic o%o5t ,el' 5ile%. 0,e #i%%ing co##ent% #ean eit,e t,at %o#e co##and;ba cont ol% do not belong to one o5 t,e t, ee data t('e% in 7ue%tion o el%e t,at t,e ,el' 5ile ,a% not -e't u' wit, late addition% to t,e enu#.

12.$.1 +reating a New +o&&an#-.ar +ontrol


0o c eate and add a co##and;ba cont ol to a co##and ba * u%e t,e Add #et,od o5 t,e !o##and>a !ont ol% collection. 0,i% #et,od etu n% a !o##and>a >utton* !o##and>a !o#bo>o6* o !o##and>a &o'u' object* de'ending on t,e value o5 t,e 0('e 'a a#ete . 0,e %(nta6 i%:
Command)arControls/bject.Add(Ty e= 1d= Parameter= )e*ore= Tem orary) Ty e i% t,e t('e o5 cont ol to be added to t,e %'eci5ied co##and ba . 0able 12;1 %,ow% t,e

'o%%ible value% 5o t,i% 'a a#ete * along wit, t,e co e%'onding cont ol and t,e etu n t('e o5 t,e Add #et,od. able 12-1. &so+ontrol Type Parameter $Value% 5soControl10tton (1) 5soControl'dit (2) 5soControlDro3down (B) soControlCo5bo1ox (L) 5soControl;o303 (17) y!e *alues 4or the y!e Para&eter Control Returne& ob'e t >utton !o##and>a >utton 0e6t bo6 !o##and>a !o#bo>o6 3i%t bo6 !o##and>a !o#bo>o6 !o#bo bo6 !o##and>a !o#bo>o6 &o'u' !o##and>a &o'u'

0,e o'tional )e*ore 'a a#ete i% a nu#be t,at indicate% t,e 'o%ition o5 t,e new cont ol on t,e co##and ba . 0,e new cont ol will be in%e ted be5o e t,e cont ol t,at i% at t,i% 'o%ition. /5 t,i% a gu#ent i% o#itted* t,e cont ol i% added at t,e end o5 t,e co##and ba . 0o add a %o;called cu%to# cont ol o5 one o5 t,e t('e% li%ted in 0able 12;1* %et t,e 1d 'a a#ete to 1 o leave it out. 0o add a built;in cont ol* %et t,e 1d 'a a#ete to t,e /) nu#be o5 t,e cont ol ?and leave out t,e Ty e a gu#ent@. 4e will di%cu%% built;in cont ol /)%* and con%ide %o#e e6a#'le%* in t,e 5ollowing %ection. A% wit, co##and ba %* we can %et t,e o'tional Tem orary 'a a#ete to Tr0e to #a-e t,e new co##and;ba cont ol te#'o a (. /t will t,en be deleted w,en E6cel i% clo%ed. /t i% ve ( i#'o tant to note t,at a !o##and>a object doe% not ,ave a !o##and>a !ont ol% ' o'e t(* a% #ig,t be e6'ected. /n o de to etu n a !o##and>a !ont ol% object* we #u%t u%e t,e !ont ol% ' o'e t(* a% in:
Co55and1ars("Hor8sheet Men0 bar").Controls

/t i% e7uall( i#'o tant to note t,at* a#ong all o5 t,e t('e% o5 !o##and>a !ont ol%* one and onl( one t('e ,a% a !ont ol% ' o'e t(. /n 'a ticula * a !o##and>a !ont ol o5 t('e !o##and>a &o'u' ,a% a !ont ol% ' o'e t(* w,ic, ' ovide% acce%% to t,e !o##and>a !ont ol% collection a%%ociated wit, t,e co e%'onding #enu 5o t,e 'o'u' cont ol. A% we will %ee in an u'co#ing e6a#'le* t,e !ont ol% ' o'e t( t,u% ' ovide% t,e #ean% b( w,ic, we can add cont ol% to t,e #enuC

$'%3 Built;in Command;Bar;Control &(s

A% we will %ee in E6a#'le 12;3* it i% 'o%%ible to 'lace built;in co##and;ba cont ol% on toolba % ?o #enu%@. 0,i% i% done b( %etting t,e 1d 'a a#ete o5 t,e Add #et,od o5 t,e !o##and>a !ont ol% collection to t,e /) o5 t,e built;in co##and;ba cont ol. 4e #u%t now add e%% t,e i%%ue o5 ,ow to dete #ine t,e /)% 5o t,e built;in cont ol%. One a'' oac, to 5inding t,e /) o5 a 'a ticula cont ol i% to u%e t,e $ind!ont ol #et,od to get a e5e ence to t,e cont ol. Once t,i% i% done* we can e6a#ine t,e cont olB% /) ' o'e t(. 0,e %(nta6 5o $ind!ont ol i%:
ex ression.>indControl(Ty e= 1d= Ta!= Visible= %ecursi+e)

w,e e ex ression i% eit,e a !o##and>a o !o##and>a % object. 0,e ot,e 'a a#ete % a e o'tional. 0,e #et,od etu n% t,e first !o##and>a !ont ol object t,at 5it% t,e c ite ia %'eci5ied b( t,e 'a a#ete %* o 9othin4 i5 t,e %ea c, i% un%ucce%%5ul. > ie5l(* t,e 'a a#ete % a e:
Ty e

One o5 t,e MsoControlT 3e con%tant% in t,e enu# given ea lie in t,i% c,a'te
1d

0,e /) o5 t,e cont ol


Ta!

0,e tag value o5 t,e cont ol


Visible

1et to Tr0e to include onl( vi%ible co##and;ba cont ol% in t,e %ea c,
%ecursi+e

0 ue to include t,e co##and ba and all o5 it% 'o'u' %ubtoolba % in t,e %ea c, 4,ile t,e $ind!ont ol #et,od can be 7uite u%e5ul* t,e ' oble# in t,i% %ituation i% t,at t,e #et,od e7ui e% anot,e wa( to identi5( t,e cont ol* %uc, a% t, oug, it% 0ag ' o'e t(. 0,u%* t,e $ind!ont ol #et,od i% #o%t u%e5ul in 5inding a cu%to# cont ol t,at we ,ave c eated and a%%igned a 0ag value. An alte native a'' oac, to getting built;in cont ol /)% i% to c eate a one;ti#e li%t 5o 5utu e e5e ence. 0,e code in E6a#'le 12;3 will c eate a te6t 5ile and 5ill it wit, a li%t o5 all built;in cont ol na#e% and /)%. ?+ote t,at it e7ui e% t,at a di ecto ( na#ed @temp e6i%t on (ou ): d iveH 5eel 5 ee to c,ange t,e d ive and 'at, to one %uitable 5o (ou %(%te#.@ 0,e code c eate% a te#'o a ( toolba * add% a built;in cont ol 5o eac, 'o%%ible cont ol /) u%ing a %i#'le >or loo'* and t,en e6a#ine% eac, o5 t,e%e cont ol%. 0,i% i% a at,e ad ,oc a'' oac,* but %ee#% to be t,e onl( a'' oac, available. Exa&!le 12-". +o#e to :enerate a )ist o4 +ontrol /(s
;0blic Di5 Di5 Di5 Di5 S0b ListControl2Ds() 6r As 2nte4er cbar As *66ice.Co55and1ar ctl As Co55and1arControl i As 2nte4er

Const 5axid = L777 6r = >ree>ile *3en "d-Ite53Iids.txt" >or *0t30t As O6r . Create te53orar toolbar Set cbar = A33lication.Co55and1ars.Add("te53orar "= 5so1arTo3= G >alse= Tr0e) >or i = 1 To 5axid *n 'rror "es05e 9ext . s8i3 i6 cannot add cbar.Controls.Add 2d-=i 9ext i *n 'rror :oTo 7 >or 'ach ctl 2n cbar.Controls ;rint O6r= ctl.Ca3tion K " 9ext cbar.Delete Close O6r

" K ctl.2d

E6a#'le 12;4 %,ow% a %#all 'o tion o5 t,e e%ulting 5ile w,en t,e code i% un on #( %(%te#. A''endi6 !* contain% a co#'lete li%t. Exa&!le 12-$. 3ut!utting the /(s o4 +o&&an#-.ar +ontrols
$C0sto5( 1 KS3ellin4... 2 KSave B K;rint... L K9ew... 1D KCo3 1E C0Kt 21 K;aste 22 *3en 2B Can.t "e3eat BF KMicroso6t Hord L2 Clear Contents LF C0sto5 &1 K;i44 1an8 &2 C0sto5 &E KDo0ble <nderline N7 C0sto5 NF C0sto5 ND KClose 17N A0to>or5at 17F K>or5at ;ainter 17D ;rint ;reKview 17E C0sto5 112 K1old 11B K2talic 11L K<nderline 11&

4e will con%ide an e6a#'le t,at u%e% built;in cont ol% late in t,e c,a'te ?at w,ic, ti#e it %,ould beco#e clea e ju%t w,at a built;in cont ol i%.@

$'%8 Example+ Creating a 7enu


0,e ' og a# %,own in E6a#'le 12;5 c eate% t,e #enu %(%te# %,own in $igu e 12;3 on E6celB% wo -%,eet #enu ba . +ote t,at t,e #ac o% t,at a e invo-ed b( t,e %election o5 t,e #enu ite#% a e na#ed 'xa53leMacro1 and 'xa53leMacro2. Figure 12-". %n exa&!le custo& &enu

Exa&!le 12-'. %n Exa&!le -enu


S0b Create;o303() Di5 cb3o3 As Co55and1arControl Di5 cbctl As Co55and1arControl Di5 cbs0b As Co55and1arControl . Create a 3o303 control on the 5ain 5en0 bar Set cb3o3 = A33lication.Co55and1ars("Hor8sheet Men0 1ar"). G Controls.Add(T 3e-=5soControl;o303) cb3o3.Ca3tion = "KC0sto5" cb3o3.+isible = Tr0e . Add a 5en0 ite5 Set cbctl = cb3o3.Controls.Add(T 3e-=5soControl10tton) cbctl.+isible = Tr0e . 9ext is re/0ired 6or ca3tion cbctl.St le = 5so10ttonCa3tion cbctl.Ca3tion = "Men02te5K1" . Action to 3er6or5 cbctl.*nAction = "'xa53leMacro1" . Add a 3o303 6or a s0b5en0 Set cbs0b = cb3o3.Controls.Add(T 3e-=5soControl;o303) cbs0b.+isible = Tr0e cbs0b.Ca3tion = "KS0bMen02te51" . Add a 5en0 ite5 to the s0b5en0 Set cbctl = cbs0b.Controls.Add(T 3e-=5soControl10tton) cbctl.+isible = Tr0e . 9ext is re/0ired 6or ca3tion cbctl.St le = 5so10ttonCa3tion cbctl.Ca3tion = "S0bMen02te5K2" . Action to 3er6or5 cbctl.*nAction = "'xa53leMacro2" 'nd S0b

+ote al%o t,e u%e o5 t,e a#'e %and c,a acte ?K@ in t,e !a'tion ' o'e tie%. 0,i% c,a acte %ignal% a ,ot -e( ?o accele ato -e(@. 0,u%* 8K!u%to#8 a''ea % a% (u%to# in t,e #enu ba and can be invo-ed u%ing t,e -e(%t o-e co#bination Alt;!.

$'%9 Example+ Creating a Toolbar


3et u% con%t uct a cu%to# toolba wit, 5ou di55e ent t('e% o5 cont ol%* a% %,own in $igu e 12;4. 0,i% will illu%t ate t,e u%e o5 t,e built;in cont ol%. 0,e code in E6a#'le 12;6 doe% t,e job. 4e will di%cu%% va iou% 'o tion% o5 t,e code a5te (ou ,ave glanced at it. Figure 12-$. % custo& toolbar

Exa&!le 12-5. %n Exa&!le oolbar


S0b CreateToolbar() Di5 cbar As Co55and1ar= cbctl As Co55and1arControl . Delete i6 it exists >or 'ach cbar 2n A33lication.Co55and1ars 26 cbar.9a5e = "Toolbar 'xa53le" Then cbar.Delete 9ext . Create a 6loatin4 toolbar Set cbar = A33lication.Co55and1ars.Add(9a5e-="Toolbar 'xa53le"= G ;osition-=5so1ar>loatin4) cbar.+isible = Tr0e . Add a c0sto5 b0tton control to exec0te a 5acro Set cbctl = cbar.Controls.Add(T 3e-=5soControl10tton) cbctl.+isible = Tr0e cbctl.St le = 5so10ttonCa3tion cbctl.Ca3tion = "C0sto510tton" . "0n the 6ollowin4 5acro cbctl.*nAction = "'xa53leMacro" . Add b0ilt%in *3en... control Set cbctl = cbar.Controls.Add(2d-=2B) . 2con 6or b0tton cbctl.>ace2d = 2B cbctl.+isible = Tr0e . Add b0ilt%in s3ell chec8in4 b0tton Set cbctl = cbar.Controls.Add(2d-=2) cbctl.>ace2d = 2 cbctl.+isible = Tr0e . Add a list box Set cbctl = cbar.Controls.Add(T 3e-=5soControlDro3down) . Add a ta4 so 5acro can 6ind it cbctl.Ta4 = "Co53oserList" cbctl.+isible = Tr0e cbctl.Ca3tion = "ListCa3tion"

. Set list 3ro3erties o6 the list box Hith cbctl .Add2te5 "Cho3in"= 1 .Add2te5 "MoMart"= 2 .Add2te5 "1ach"= B .Dro3DownLines = 7 .Dro3DownHidth = F& . select nothin4 to start .List2ndex = 7 'nd Hith . Set 5acro to exec0te when an ite5 . is selected cbctl.*nAction = "'xa53leListMacro" 'nd S0b

0,e 5i %t %te' i% to c,ec- 5o an e6i%ting toolba na#ed 0oolba E6a#'le. /5 it e6i%t%* we delete it. 0,en we c eate a 5loating toolba na#ed 0oolba E6a#'le. 0,e na#e i% i#'o tant* %ince we will u%e it late 5o identi5ication. +e6t* we add a cu%to# button cont ol ?1d a gu#ent #i%%ing@ and a%%ign it t,e #ac o 'xa53leMacro* w,o%e code* w,ic, i% %,own in E6a#'le 12;7* %i#'l( tell% u% t,at we 'u%,ed t,e button. Exa&!le 12-6. he Exa&!le-acro -acro
S0b 'xa53leMacro() Ms41ox "C0sto5 b0tton 3ressed" 'nd S0b

+e6t* we add a built;in $ile O'en... cu%to# cont ol* w,o%e 1d ,a''en% to be 23. ?4e ,ave al ead( di%cu%%ed ,ow to get built;in cont ol /)%.@ 0,i% cu%to# cont ol auto#aticall( di%'la(% t,e O'en dialog bo6. +ote t,at we %et t,e Face1d to 23 a% well. 0,i% di%'la(% t,e de5ault icon 5o t,e O'en co##and* but we could c,oo%e anot,e icon i5 de%i ed. 0,en we add t,e built;in 1'elling cu%to# cont ol* w,ic, c,ec-% t,e %'elling o5 t,e active docu#ent. $inall(* we add a cu%to# li%t bo6 and 'o'ulate it wit, t,e na#e% o5 t, ee co#'o%e %. +ote t,at we %et t,e 0ag ' o'e t( o5 t,i% li%t bo6. 0,e ea%on i% t,at we want to be able to u%e t,e $ind!ont ol #et,od to 5ind t,e li%t bo6 5 o# wit,in t,e #ac o t,at i% a%%igned to t,e OnAction ' o'e t(* w,ic, i% %,own in E6a#'le 12;8. Exa&!le 12-7. -acro /nvo1e# by Selecting a +o&!oser 4ro& the )ist .ox
S0b 'xa53leListMacro() Di5 cbctl As Co55and1arControl >ind the list box control Set cbctl = Co55and1ars("Toolbar 'xa53le"). G >indControl(Ta4-="Co53oserList") 26 9ot cbctl 2s 9othin4 Then Ms41ox "@o0 selected " K cbctl.List(cbctl.List2ndex) 'nd 26 'nd S0b

/n t,i% #ac o* we u%e t,e $ind!ont ol #et,od to locate t,e li%t bo6 cont ol* via it% tag* on t,e toolba . Once we ,ave located t,e li%t bo6* we can get t,e cu entl( %elected ite# ?w,ic, we %i#'l( di%'la( 5o t,i% e6a#'le@. +ote t,at i5 two o #o e cont ol% 5it t,e %ea c, c ite ia* $ind!ont ol etu n% t,e 5i %t cont ol t,at it 5ind%. Al%o* i5 no cont ol 5it% t,e c ite ia* $ind!ont ol etu n% 9othin4* %o we can c,ec- t,i% a% we ,ave done in ou ' og a#.

$'%< Example+ Adding an &tem to an Existing 7enu


O5 cou %e* at,e t,an c eating a cu%to# toolba o adding a cu%to# #enu to E6celB% #enu %(%te#* (ou #a( ' e5e to add a button to an e6i%ting toolba o a #enu ite# to an e6i%ting #enu. /n t,at ca%e* (ou %i#'l( need to et ieve a e5e ence to t,e !o##and>a object to w,ic, (ou wi%, to add t,e ite# and call t,e !ont ol% collectionB% Add #et,od to add an ite# to it. /n addition* (ou can et ieve t,e /nde6 ' o'e t( o5 t,e ite# be5o e w,ic, (ouBd li-e to 'o%ition (ou new #enu ite# o toolba button. E6a#'le 12;9* w,ic, contain% t,e %ou ce code 5o a 4o -boo-IO'en event t,at add% an 8About 1RD2til%8 #enu ite# i##ediatel( be5o e t,e 8About "ic o%o5t E6cel8 ite#* %,ow% ,ow t,i% can be done. +ote t,at t,e ' ocedu e i% able to dete #ine t,e ' eci%e location o5 t,e About "ic o%o5t E6cel #enu ite# b( et ieving a e5e ence to it% !o##and>a !ont ol object and it% /nde6 ' o'e t(. Exa&!le 12-8. %##ing a -enu /te& to an Existing -enu
;rivate S0b Hor8boo8G*3en() Di5 ln4;os As Lon4 Di5 obT?el3Men0 As Co55and1ar Di5 obT?el3Men02te5 As Co55and1arControl Di5 obT'xcelAbo0t As Co55and1arControl .:et re6erence to ?el3 5en0 Set obT?el3Men0 = A33lication.Co55and1ars("?el3") . Deter5ine 3osition o6 "Abo0t Microso6t 'xcel" Set obT'xcelAbo0t = obT?el3Men0.Controls("Abo0t Microso6t 'xcel") 26 9ot obT'xcelAbo0t 2s 9othin4 Then ln4;os = obT'xcelAbo0t.2ndex 'lse ln4;os = obT?el3Men0.Controls.Co0nt 'nd 26 . Add "Abo0t S"#<tils" 5en0 ite5 Set obT?el3Men02te5 = obT?el3Men0.Controls.Add(5soControl10tton= G 1= = ln4;os= Tr0e) obT?el3Men02te5.Ca3tion = "Abo0t KS"#<tils" obT?el3Men02te5.1e4in:ro03 = Tr0e obT?el3Men02te5.*nAction = "ShowAbo0tMacros" 'nd S0b

$'%= Augmenting the S6@#tils Application


A #ed wit, ou -nowledge o5 O55ice !o##and>a %* we can aug#ent ou add;in %,ell* 5i %t di%cu%%ed in !,a'te 10.

12.8.1 +reating the (ata 0or1sheet


A% an E6cel a''lication get% #o e co#'le6* t,e a%%ociated #enu get% #o e co#'le6. Rat,e t,an %to e all data di ectl( in code* it #a-e% %en%e to u%e a wo -%,eet. Recall t,at add;in wo -%,eet% a e ,idden 5 o# t,e u%e * %o t,e( a e t,e 'e 5ect 'lace to -ee' data 5o t,e add;in. O'en t,e /,4'tilsAxls %ou ce wo -boo-* delete all %,eet% but one* and na#e t,at %,eet )ata1,eet. $ill in t,e %,eet a% %,own in $igu e 12;5. 0,i% %,eet contain% one ow 5o eac, ' ocedu e ?o utilit(@ o5 t,e add;in ?we will add #o e ow% late in t,e boo-@. 0,e 5i %t ow i% 5o t,e ActivateSheet utilit( w,o%e code %,ell we included ea lie . 4e will add code %,ell% 5o t,e ot,e utilitie% a bit late . /n late c,a'te %* we will i#'le#ent t,e%e utilitie% ' o'e l(. Figure 12-'. (ataSheet o4 S,X2tils.xls

3et u% ta-e a clo%e loo- at t,e content% o5 )ata1,eet. 0,e 5i %t colu#n i% t,e na#e o5 t,e utilit(. 0,i% i% not u%ed out%ide o5 t,e %,eet. 0,e %econd colu#n i% t,e na#e o5 t,e ' ocedu e t,at i% activated w,en t,e utilit( i% invo-ed b( t,e u%e t, oug, a #enu ite# c eated b( t,e add;in. /n t,i% ca%e* all #enu ite#% 5i e t,e %a#e utilit(: "0n<tilit . 0,i% utilit( will dete #ine t,e #enu ite# t,at wa% clic-ed and call t,e a'' o' iate ' ocedu e. 0,e t,i d colu#n give% t,e location o5 t,i% ' ocedu e. A% (ou can %ee* we ,ave 'laced t,e ' inting ' ocedu e% in a %e'a ate wo -boo- called PrintAutl. A% an a''lication get% #o e co#'le6* (ou #a( want to %'lit it u' into %eve al wo -boo-%. /n t,i% wa(* (ou add;in can be w itten to load a 5ile onl( w,en it i% needed* t,u% %aving e%ou ce%. ?/n t,i% e6a#'le* we a e %'litting u' t,e a''lication 5o de#on%t ation 'u 'o%e% onl(. 0,e ' inting utilitie% a e not eall( co#'le6 enoug, to wa ant a %e'a ate wo -boo-.@ 0,e 5ou t, colu#n contain% t,e ca'tion 5o t,e #enu ite# t,at will invo-e t,e utilit(. +ote t,e a#'e %and c,a acte ?K@* w,ic, dete #ine% t,e #enu ,ot -e(. $o e6a#'le* t,e Activate1,eet #enu ite# can be invo-ed u%ing t,e A -e(. 0,e 5i5t, colu#n give% t,e #enu ite# na#e in ca%e t,e e i% a %ub#enu. 0,u%* t,e ' int utilitie% a e acce%%ed t, oug, t,e & int %ub#enu. 0,e 5inal two colu#n% dete #ine w,et,e t,e #enu ?o %ub#enu@ ite# will be enabled o di%abled w,en a wo -%,eet o c,a t%,eet i% active. A% we ,ave %een* E6cel u%e% a di55e ent #ain #enu ba w,en a wo -%,eet i% active ?4o -%,eet "enu >a @ t,an w,en a c,a t%,eet i% active ?!,a t "enu >a @. $o a utilit( t,at 'e tain% onl( to c,a t%* 5o in%tance* we #a( not want t,e co e%'onding #enu ite# to be available 5 o# t,e 4o -%,eet #enu ba and vice;ve %a. +e6t* (ou %,ould c eate a new %tanda d code #odule called basMain and 'lace t,e 5ollowing con%tant decla ation% in t,e )ecla ation% %ection:
;0blic ;0blic ;0blic ;0blic ;0blic ;0blic ;0blic ;0blic Const Const Const Const Const Const Const Const <tilit GCol = 1 *nActionGCol = 2 ;roced0reGCol = B 2nHor8boo8GCol = L Men02te5GCol = & S0bMen02te5GCol = N *nH8sMen0GCol = F *nChartMen0GCol = D

>( u%ing t,e%e con%tant% t, oug,out t,e add;in* i5 we need to #ove an( colu#n% in t,e )ata1,eet %,eet* all we need to do i% c,ange t,e value% o5 t,e%e con%tant%. ?0,i% i% ' eci%el( w,at %(#bolic con%tant% a e 5o C@

12.8.2 Setting 2! the +usto& -enus


0,e 5i %t %te' in c eating t,e cu%to# #enu% 5o ou 5eatu e% i% to #a-e a %lig,t alte ation in t,e code 5o t,e O'en event 5o ThisHor8boo8. !,ange t,e code a% %,own in E6a#'le 12;10. Exa&!le 12-19. he ,evise# *ersions o4 his0or1boo1>s 3!en an# +lose Events
;rivate S0b Hor8boo8G*3en() CreateC0sto5Men0s 'nd S0b

0,e code 5o c eating t,e cu%to# #enu i% #o e co#'licated t,an t,e one 5 o# !,a'te 10 becau%e we #u%t now e6t act t,e nece%%a ( in5o #ation 5 o# t,e )ata1,eet wo -%,eet. 0,e e a e #an( wa(% to do t,i%* but we ,ave elected to %'lit t,e ' oce%% into two ' ocedu e%. 0,e 5i %t ' ocedu e* CreateC0sto5Men0s* c,ec-% 5o t,e e6i%tence o5 t,e cu%to# #enu% u%ing t,e 0ag ' o'e t(. /5 t,e #enu e6i%t%* it i% deleted. 0,en t,e ' ocedu e call% t,e %econd ' ocedu e* CreateC0sto5Men0* w,ic, actuall( doe% t,e #enu c eation. 0,i% i% done once 5o t,e wo -%,eet #enu ba and once 5o t,e c,a t #enu ba . 0,e 5i %t ' ocedu e i% %,own in E6a#'le 12 11. Exa&!le 12-11. he +reate+usto&-enus Proce#ure
S0b CreateC0sto5Men0s() . Create c0sto5 5en0 on both wor8sheets and chartsheets . 5en0 bars i6 the do not alread exist. . <se the control.s ta4 3ro3ert to identi6 it. Di5 cbc As Co55and1arControl Set cbc = A33lication.Co55and1ars( G "Hor8sheet 5en0 bar").>indControl( G T 3e-=5soControl;o303= Ta4-="S"#<tilsC0sto5Men0") 26 9ot cbc 2s 9othin4 Then cbc.Delete CreateC0sto5Men0 "Hor8sheet Men0 1ar" Set cbc = A33lication.Co55and1ars( G "Chart 5en0 bar").>indControl( G T 3e-=5soControl;o303= Ta4-="S"#<tilsC0sto5Men0") 26 9ot cbc 2s 9othin4 Then cbc.Delete CreateC0sto5Men0 "Chart Men0 1ar" 'nd S0b

0,e CreateC0sto5Men0 ' ocedu e i% %,own in E6a#'le 12;12. +ote t,at t,e OnAction ' o'e t( o5 eve ( #enu ite# i% %et to a ' ocedu e called "0n<tilit * a% t,e 8onActivation & oc8 colu#n in $igu e 12;3 %,ow%. 0,i% ' ocedu e will %o t out w,ic, #enu ite# wa% %elected and call t,e a'' o' iate ' ocedu e. 0o 'a%% t,e in5o #ation to "0n<tilit * we %et eac, cont olB% 0ag ' o'e t( to t,e na#e o5 t,e ' ocedu e and it% &a a#ete ' o'e t( to t,e na#e o5 t,e wo -boo- t,at contain% t,e ' ocedu e. ?0,e 0ag and &a a#ete ' o'e tie% a e 8%'a e8 ' o'e tie% de%igned to allow t,e ' og a##e to %to e i#'o tant in5o #ation* w,ic, i% ' eci%el( w,at we a e doing.@ /n

t,e "0n<tilit ' ocedu e* we can u%e t,e Action!ont ol ' o'e t( to etu n t,e cont ol t,at cau%ed t,e "0n<tilit ' ocedu e to e6ecute. 0,en it i% a %i#'le #atte to ead t,e 0ag and &a a#ete ' o'e tie% o5 t,at cont ol. Exa&!le 12-12. he +reate+usto&-enu Proce#ure
S0b CreateC0sto5Men0(s1ar9a5e As Strin4) Di5 cb3o3 As Co55and1arControl Di5 cbctl As Co55and1arControl Di5 cbctlC0rrent;o303 As Co55and1arControl Di5 i'nabledCol05n As 2nte4er Di5 iLast"ow As 2nte4er Di5 iC0rrent"ow As 2nte4er Di5 sC0rrentMen02te5 As Strin4 Di5 sC0rrentS0bMen02te5 As Strin4 Di5 sC0rrent;roced0re As Strin4 Di5 sC0rrentHor8boo8 As Strin4 Di5 sC0rrent*nAction As Strin4 Di5 ws As Hor8sheet i'nabledCol05n = *nH8sMen0GCol . Col05n 6or wor8sheet 5en0 bar 26 LCase(s1ar9a5e) = "chart 5en0 bar" Then G i'nabledCol05n = *nChartMen0GCol Set ws = ThisHor8boo8.Hor8sheets("DataSheet") . Create a 3o303 control on 5ain 5en0 bar s1ar9a5e Set cb3o3 = A33lication.Co55and1ars(s1ar9a5e). G Controls.Add(T 3e-=5soControl;o303= Te53orar -=Tr0e) Hith cb3o3 .Ca3tion = "C0Ksto5" .Ta4 = "S"#<tilsC0sto5Men0" 'nd Hith . :et last 0sed row o6 DataSheet iLast"ow = A33lication.Hor8sheet>0nction.Co0ntA(ws."an4e("A-A")) . :o thro04h DataSheet to 4et 5en0 ite5s >or iC0rrent"ow = 2 To iLast"ow . Set the val0es sC0rrent;roced0re = ws.Cells(iC0rrent"ow= ;roced0reGCol).+al0e sC0rrentHor8boo8 = ws.Cells(iC0rrent"ow= 2nHor8boo8GCol).+al0e sC0rrentMen02te5 = ws.Cells(iC0rrent"ow= Men02te5GCol).+al0e sC0rrentS0bMen02te5 = ws.Cells(iC0rrent"ow= S0bMen02te5GCol).+al0e sC0rrent*nAction = ThisHor8boo8.9a5e K "!" K G ws.Cells(iC0rrent"ow= *nActionGCol).+al0e . 26 no S0b5en0 ite5 then this is a b0tton control . else it is a 3o303 control 26 sC0rrentS0bMen02te5 = "" Then . Add b0tton control Hith cb3o3.Controls.Add(T 3e-=5soControl10tton= Te53orar -=Tr0e) .Ca3tion = sC0rrentMen02te5 .*nAction = sC0rrent*nAction .Ta4 = sC0rrent;roced0re . to 3ass this on .;ara5eter = sC0rrentHor8boo8 . to 3ass this on .'nabled = ws.Cells(iC0rrent"ow= i'nabledCol05n).+al0e

'nd Hith 'lse . Add 3o303 control i6 it is not alread added 26 sC0rrentMen02te5 $( "" Then Set cbctlC0rrent;o303 = cb3o3.Controls.Add( G T 3e-=5soControl;o303= Te53orar -=Tr0e) cbctlC0rrent;o303.Ca3tion = sC0rrentMen02te5 'nd 26 . 9ow add the s0b5en0 ite5= which is a b0tton control Hith cbctlC0rrent;o303.Controls.Add( G T 3e-=5soControl10tton= Te53orar -=Tr0e) .Ca3tion = sC0rrentS0bMen02te5 .*nAction = sC0rrent*nAction .Ta4 = sC0rrent;roced0re . to 3ass this on .;ara5eter = sC0rrentHor8boo8 . to 3ass this on .'nabled = ws.Cells(iC0rrent"ow= i'nabledCol05n).+al0e 'nd Hith 'nd 26 9ext . row 'nd S0b

12.8." /&!le&enting the Features o4 the %##-in


4e a e now ead( to 8i#'le#ent8 t,e 5eatu e% o5 t,e add;in. A% di%cu%%ed ea lie * 5o now we will ju%t %u''l( a #e%%age bo6 5o eac, 5eatu e. 0,e ActivateSheet utilit( ,a% al ead( been ta-en ca e o5* %ince t,e e %,ould be a code #odule na#ed basMain in t,e /,4'tilsAxls ' oject. $o now* t,i% #odule %,ould contain onl( t,e 5ollowing ' ocedu e:
;0blic S0b ActivateSheet() Ms41ox "This is the ActivateSheet 0tilit " 'nd S0b

$o t,e ' inting utilitie%* we need a new E6cel wo -boo-. ! eate a new wo -boo- and na#e it PrintAxls. Add a code #odule ?wit, an( na#e@ containing t,e code %,own in E6a#'le 12;13. Exa&!le 12-1". +o#e 4or the Printing Proce#ures
;0blic S0b ;rintCharts() Ms41ox "This is the 3rint charts 0tilit " 'nd S0b ;0blic S0b ;rint;ivotTables() Ms41ox "This is the 3rint 3ivot tables 0tilit " 'nd S0b ;0blic S0b ;rintSheets() Ms41ox "This is the 3rint sheets 0tilit " 'nd S0b

+ow* t,e PrintAxls wo -boo- i% an o dina ( E6cel wo -boo-* %o i5 ou add;in o'en% t,i% wo -booin o de to call one o5 it% ' ocedu e%* t,e wo -boo- will be vi%ible to t,e u%e . 0,i% i% not good. Aence* we need to c eate an add;in 5 o# t,i% wo -%,eet a% well. 3et u% call it PrintAutl. ?Lou can %ave t,e wo -%,eet unde t,i% na#e b( 'lacing t,e na#e in 7uotation #a -% in t,e $ile na#e bo6 in E6celB% 1ave A% dialog. /5 (ou o#it t,e 7uotation #a -%* E6cel will %ave t,e 5ile a% PrintAutlAxla.@ )onBt 5o get to 'e 5o # t,e u%ual add;in c eation itual% 5o t,i% wo -boo- ?co#'ile t,e code* %et

t,e wo -boo- and ' oject ' o'e tie%* and loc- t,e wo -boo- 5 o# viewing@ be5o e %aving it a% an add;in. 4e now need to i#'le#ent t,e "0n<tilit ' ocedu e. 0,i% ' ocedu e* w,ic, %,ould be 'laced in t,e basMain code #odule* i% %,own in E6a#'le 12;14. Exa&!le 12-1$. he ,un2tility Proce#ure
S0b "0n<tilit () . <se Ta4 and ;ara5eter 3ro3erties to 6ind the 3roced0re 6or . the re/0ested 0tilit . ;roced0re na5e is in Ta4 3ro3ert . and wor8boo8 na5e is in the ;ara5eter 3ro3ert . . <se ActionControl to ret0rn the control. Di5 H8b9a5e As Strin4 Di5 ;roc9a5e As Strin4 H8b9a5e = A33lication.Co55and1ars.ActionControl.;ara5eter 26 H8b9a5e = "" *r H8b9a5e = "ThisHor8boo8" Then G H8b9a5e = ThisHor8boo8.9a5e ;roc9a5e = A33lication.Co55and1ars.ActionControl.Ta4 . *3en wor8boo8 i6 necessar *n 'rror :oTo H8b9ot>o0nd 26 9ot 2s1oo8*3en(H8b9a5e) Then Hor8boo8s.*3en ThisHor8boo8.;ath K A33lication.;athSe3arator K H8b9a5e 'nd 26 . "0n 3roced0re *n 'rror :oTo ;roc9ot>o0nd A33lication."0n H8b9a5e K "!" K ;roc9a5e 'xit S0b H8b9ot>o0ndMs41ox "Cannot 6ind wor8boo8 " K H8b9a5e K " in " K G ThisHor8boo8.;ath= vbCritical= "Test Add%2n" 'xit S0b ;roc9ot>o0ndMs41ox "Cannot 6ind 3roced0re " K ;roc9a5e K " in " K G H8b9a5e= vbCritical= "Test Add%2n" 'xit S0b 'nd S0b

E6a#'le 12;14 #a-e% a call to t,e 2s1oo8*3en 5unction ?w,ic, i% %,own in E6a#'le 12;15@ to %ee i5 t,e wo -boo- containing t,e ' ocedu e i% o'en. &e ,a'% t,e obviou% c,oice 5o dete #ining w,et,e o not a wo -boo- i% o'en i% to loo- t, oug, t,e Wor)boo)s collection* w,ic, i% t,e collection o5 all 8o'en8 wo -boo-% ?#o e on t,i% in !,a'te 17@. Aoweve * an add;in i% ,idden* even 5 o# t,i% collection. $o tunatel(* we can %till e5e to an add;in wo -boo- b( na#e* %o we ju%t t ( to get t,i% na#e u%ing t,e line:
s9a5e = Hor8boo8s(sH8b9a5e).9a5e

/5 t,i% gene ate% an e o * we -now t,at t,e wo -boo- i% not o'en. Ot,e wi%e* it will etu n t,e na#e o5 t,e wo -boo-. ?O5 cou %e* we al ead( -new t,e na#e in t,i% ca%e* but t,at doe%nBt #atte .@

Exa&!le 12-1'. he /s.oo13!en Function


;rivate >0nction 2s1oo8*3en(sH8b9a5e) As 1oolean . Chec8 to see i6 wor8boo8 is o3en . 9ote that an add%in wor8boo8 does not a33ear in . the Hor8boo8s collection= so we need another 5ethod. . ?owever= an add%in can be re6erenced b na5e= so we si53l . access its 9a5e 3ro3ert . 26 an error occ0rs= then . the wor8boo8 is not o3en. Di5 s9a5e As Strin4 *n 'rror :oTo H8b9ot*3en 2s1oo8*3en = Tr0e s9a5e = Hor8boo8s(sH8b9a5e).9a5e 'xit >0nction H8b9ot*3en2s1oo8*3en = >alse 'nd >0nction

12.8.$ +losing %ny 3!en %##-/ns


4,en t,e u%e unc,ec-% t,e 1RD2til% ite# in t,e Add;/n% dialog* E6cel will clo%e t,e /,4'tilsAxla wo -boo-. >ut it will not clo%e an( add;in%* %uc, a% PrintAutl* t,at we e o'ened in code. 0,e 'lace to clo%e all o'en add;in% i% in t,e wo -boo-B% >e5o e!lo%e event* w,ic, cu entl( onl( delete% t,e cu%to# #enu. A %i#'le ?but 'e ,a'% not elegant@ a'' oac, i% to clo%e eve ( add;in li%ted in t,e )ata1,eet e6ce't t,e #ain /,4'tilsAxla ?w,ic, i% clo%ed w,en t,e u%e de%elect% t,e add;in@. $o t,i%* we need an *n 'rror "es05e 9ext line %o t,at an atte#'t to clo%e a wo -boo- t,at i% not o'en will be igno ed. 0,u%* (ou %,ould c,ange t,e code 5o t,e e6i%ting >e5o e!lo%e event to t,at %,own in E6a#'le 12;16. Exa&!le 12-15. he 0or1boo1G.e4ore+lose Event Can#ler
;rivate S0b Hor8boo8G1e6oreClose(Cancel As 1oolean) . Delete c0sto5 5en0 and close all add%ins Di5 r As 2nte4er Di5 ws As Hor8sheet Di5 s9a5e As Strin4 . 2n case we tr to close a wor8boo8 that is not o3en *n 'rror "es05e 9ext DeleteC0sto5Men0s Set ws = ThisHor8boo8.Hor8sheets("DataSheet") >or r = 2 To A33lication.Hor8sheet>0nction.Co0ntA(ws."an4e("A-A")) s9a5e = ws.Cells(r= 2nHor8boo8GCol).+al0e 26 s9a5e $( "" And s9a5e $( "ThisHor8boo8" Then Hor8boo8s(s9a5e).Close 'nd 26 9ext r 'nd S0b

0,e DeleteC0sto5Men0s ' ocedu e i% %,own in E6a#'le 12;17. Exa&!le 12-16. he (elete+usto&-enus Proce#ure
S0b DeleteC0sto5Men0s() Di5 cbc As Co55and1arControl Set cbc = A33lication.Co55and1ars("Hor8sheet 5en0 bar"). G >indControl(T 3e-=5soControl;o303= Ta4-="TestAdd2nC0sto5Men0") 26 9ot cbc 2s 9othin4 Then cbc.Delete Set cbc = A33lication.Co55and1ars("Chart 5en0 bar"). G >indControl(T 3e-=5soControl;o303= Ta4-="TestAdd2nC0sto5Men0") 26 9ot cbc 2s 9othin4 Then cbc.Delete 'nd S0b

0,e 'iece% a e now co#'lete* %o (ou can %ave t,e /,4'tilsAxls 5ile a% an add;in* ju%t a% we did in !,a'te 10. ?/5 (ou ,ave a ' oble#* (ou can download t,e %ou ce code 5o t,i% add;in 5 o# t,e OBReill( web %ite and co#'a e it wit, (ou code.@

Chapter $*% Built;&n (ialog Boxes


0,e E6cel object #odel contain% a )ialog object 5o eac, o5 E6celB% built;in dialog bo6e%. 0,e%e )ialog object% a e -e't in t,e )ialog% collection and a e inde6ed b( t,e #l10ilt2nDialo4 con%tant% %,own in 0able 13;1 and 0able 13;2. 0,e )ialog% collection i% etu ned b( t,e )ialog% ' o'e t( o5 t,e A''lication object. /n 0able 13;1* 8SvDZ8 indicate% t,at t,e con%tant i% new 5o E6cel ve %ion D. able 1"-1. Xl.uilt/n(ialog constants an# values
I6l)ialog!,a t1ou ce)ata ?541@Sv9Z I6l)ialog&,onetic ?538@Sv9Z 6l)ialogActivate ?103@ 6l)ialogActive!ell$ont ?476@ 6l)ialogAdd!,a tAuto5o #at ?390@ 6l)ialogAddin"anage ?321@ 6l)ialogAlign#ent ?43@ 6l)ialogA''l(+a#e% ?133@ 6l)ialogA''l(1t(le ?212@ 6l)ialogA''"ove ?170@ 6l)ialogA''1iGe ?171@ 6l)ialogA angeAll ?12@ 6l)ialogA%%ign0oObject ?213@ 6l)ialogA%%ign0o0ool ?293@ 6l)ialogAttac,0e6t ?80@ 6l)ialogAttac,0oolba % ?323@ 6l)ialogAuto!o ect ?485@ 6l)ialogA6e% ?78@ 6l)ialog>o de ?45@ 6l)ialog!alculation ?32@ 6l)ialog!ell& otection ?46@ 6l)ialog!,ange3in- ?166@ 6l)ialog!,a tAdd)ata ?392@ 6l)ialog!,a t3ocation ?527@ 6l)ialog!,a tO'tion%)ata3abel"ulti'le ?724@Sv10Z 6l)ialog$o 6l)ialog$o 6l)ialog$o 6l)ialog$o 6l)ialog$o 6l)ialog$o 6l)ialog$o 6l)ialog& inte 1etu' ?9@ 6l)ialog& int& eview ?222@ 6l)ialog& o#ote ?202@ 6l)ialog& o'e tie% ?474@ 6l)ialog& o'e t($ield% ?754@Sv10Z 6l)ialog& otect)ocu#ent ?28@ 6l)ialog& otect1,a ing ?620@ 6l)ialog&ubli%,A%4eb&age 6l)ialog$o #ulaRe'lace ?130@ ?653@Sv9Z 6l)ialog$unction4iGa d ?450@ 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog.alle 6l)ialog&u%,button& o'e tie% ?445@ (3dA ea ?193@ 6l)ialogRe'lace$ont ?134@ (3d>a ?272@ 6l)ialogRouting1li' ?336@ (3d!olu#n ?194@ 6l)ialogRowAeig,t ?127@ (3d3ine ?195@ 6l)ialogRun ?17@ (3d&ie ?196@ 6l)ialog1aveA% ?5@ (3d1u 5ace ?273@ 6l)ialog1ave!o'(A% ?456@ (A ea ?67@ 6l)ialog1ave+ewObject ?208@ (>a ?68@ 6l)ialog1ave4o -boo- ?145@ (!olu#n ?69@ 6l)ialog1ave4o -%'ace ?285@ (!u%to# ?388@ 6l)ialog1cale ?87@ ()oug,nut ?344@ 6l)ialog1cena ioAdd ?307@ (3ine ?70@ 6l)ialog1cena io!ell% ?305@ (&ie ?71@ 6l)ialog1cena ioEdit ?308@ (Rada ?249@ 6l)ialog1cena io"e ge ?473@ (1catte ?72@ 6l)ialog1cena io1u##a ( ?311@ 6l)ialog1c ollba & o'e tie% ?420@ #at"ove ?128@ #at+u#be ?42@ #atOve la( ?226@ #at1iGe ?129@ #at0e6t ?89@ #ula$ind ?64@ #ula.oto ?63@

6l)ialog.oal1ee- ?198@

6l)ialog!,a tO'tion%)ata3abel% ?505@ 6l)ialog!,a tO'tion%)ata0able ?506@ 6l)ialog!,a t1ou ce)ata ?540@ 6l)ialog!,a t0 end ?350@ 6l)ialog!,a t0('e ?526@ 6l)ialog!,a t4iGa d ?288@ 6l)ialog!,ec-bo6& o'e tie% ?435@ 6l)ialog!lea ?52@ 6l)ialog!olo &alette ?161@ 6l)ialog!olu#n4idt, ?47@ 6l)ialog!o#bination ?73@ 6l)ialog!onditional$o #atting ?583@ 6l)ialog!on%olidate ?191@

6l)ialog. idline% ?76@ 6l)ialog/#'o t0e6t$ile ?666@Sv9Z 6l)ialog/n%e t ?55@ 6l)ialog/n%e tA('e lin- ?596@ 6l)ialog/n%e t+a#e3abel ?496@ 6l)ialog/n%e tObject ?259@ 6l)ialog/n%e t&ictu e ?342@ 6l)ialog/n%e t0itle ?380@ 6l)ialog3abel& o'e tie% ?436@

6l)ialog1ea c, ?731@Sv10Z 6l)ialog1elect1'ecial ?132@

6l)ialog1end"ail ?189@ 6l)ialog1e ie%A6e% ?460@ 6l)ialog1e ie%O'tion% ?557@ 6l)ialog1e ie%O de ?466@ 6l)ialog1e ie%1,a'e ?504@ 6l)ialog1e ie%D ?461@ 6l)ialog1e ie%L ?462@ 6l)ialog1et>ac-g ound&ictu e 6l)ialog3i%tbo6& o'e tie% ?437@ ?509@ 6l)ialog"ac oO'tion% ?382@ 6l)ialog1et& int0itle% ?23@ 6l)ialog"ailEdit"aile ?470@ 6l)ialog1et2'date1tatu% ?159@ 6l)ialog"ail3ogon ?339@ 6l)ialog1,ow)etail ?204@

6l)ialog!o'(!,a t ?147@ 6l)ialog!o'(&ictu e ?108@ 6l)ialog! eate+a#e% ?62@ 6l)ialog! eate&ubli%,e ?217@ 6l)ialog!u%to#iGe0oolba ?276@ 6l)ialog!u%to#=iew% ?493@ 6l)ialog)ata)elete ?36@ 6l)ialog)ata3abel ?379@ 6l)ialog)ata3abel"ulti'le ?723@Sv10Z 6l)ialog)ata1e ie% ?40@ 6l)ialog)ata=alidation ?525@ 6l)ialog)e5ine+a#e ?61@ 6l)ialog)e5ine1t(le ?229@ 6l)ialog)elete$o #at ?111@ 6l)ialog)elete+a#e ?110@ 6l)ialog)e#ote ?203@ 6l)ialog)i%'la( ?27@ 6l)ialogEditbo6& o'e tie% ?438@ 6l)ialogEdit!olo ?223@ 6l)ialogEdit)elete ?54@ 6l)ialogEditionO'tion% ?251@ 6l)ialogEdit1e ie% ?228@ 6l)ialogE o ba D ?463@ 6l)ialogE o ba L ?464@ 6l)ialogE o !,ec-ing ?732@Sv10Z 6l)ialogEvaluate$o #ula ?709@Sv10Z 6l)ialogE6te nal)ata& o'e tie% ?530@Sv9Z 6l)ialogE6t act ?35@ 6l)ialog$ile)elete ?6@ 6l)ialog$ile1,a ing ?481@ 6l)ialog$ill. ou' ?200@ 6l)ialog$ill4o -g ou' ?301@ 6l)ialog$ilte ?447@ 6l)ialog$ilte Advanced ?370@ 6l)ialog$ind$ile ?475@ 6l)ialog$ont ?26@ 6l)ialog$ont& o'e tie% ?381@ 6l)ialog$o #atAuto ?269@ 6l)ialog$o #at!,a t ?465@ 6l)ialog$o #at!,a tt('e ?423@

6l)ialog"ail+e6t3ette ?378@ 6l)ialog"ain!,a t ?85@ 6l)ialog"ain!,a t0('e ?185@ 6l)ialog"enuEdito ?322@ 6l)ialog"ove ?262@ 6l)ialog+ew ?119@ 6l)ialog+ew4ebOue ( ?667@Sv9Z 6l)ialog+ote ?154@ 6l)ialogObject& o'e tie% ?207@ 6l)ialogObject& otection ?214@ 6l)ialogO'en ?1@ 6l)ialogO'en3in-% ?2@ 6l)ialogO'en"ail ?188@ 6l)ialogO'en0e6t ?441@ 6l)ialogO'tion%!alculation ?318@ 6l)ialogO'tion%!,a t ?325@ 6l)ialogO'tion%Edit ?319@ 6l)ialogO'tion%.ene al ?356@

6l)ialog1,ow0oolba ?220@ 6l)ialog1iGe ?261@ 6l)ialog1o t ?39@ 6l)ialog1o t1'ecial ?192@ 6l)ialog1'lit ?137@ 6l)ialog1tanda d$ont ?190@ 6l)ialog1tanda d4idt, ?472@ 6l)ialog1t(le ?44@ 6l)ialog1ub%c ibe0o ?218@ 6l)ialog1ubtotal! eate ?398@ 6l)ialog1u##a (/n5o ?474@ 6l)ialog0able ?41@ 6l)ialog0abO de ?394@ 6l)ialog0e6t0o!olu#n% ?422@ 6l)ialog2n,ide ?94@

6l)ialog2'date3in- ?201@ 6l)ialog=ba/n%e t$ile ?328@ 6l)ialog=ba"a-eAddin ?478@ 6l)ialog=ba& ocedu e)e5inition 6l)ialogO'tion%3i%t%Add ?458@ ?330@ 6l)ialogO'tion%"E ?647@Sv9Z 6l)ialog=iew3d ?197@ 6l)ialogO'tion%0 an%ition 6l)ialog4ebO'tion%> ow%e % ?355@ ?773@Sv10Z 6l)ialogO'tion%=iew ?320@ 6l)ialogOutline ?142@ 6l)ialogOve la( ?86@ 6l)ialogOve la(!,a t0('e ?186@ 6l)ialog&age1etu' ?7@ 6l)ialog&a %e ?91@ 6l)ialog&a%te+a#e% ?58@ 6l)ialog&a%te1'ecial ?53@ 6l)ialog&atte n% ?84@ 6l)ialog&,onetic ?656@ 6l)ialog&ivot!alculated$ield ?570@ 6l)ialog&ivot!alculated/te# ?572@ 6l)ialog&ivot!lient1e ve 1et ?689@Sv9Z 6l)ialog4ebO'tion%Encoding ?686@Sv9Z 6l)ialog4ebO'tion%$ile% ?684@Sv9Z 6l)ialog4ebO'tion%$ont% ?687@Sv9Z 6l)ialog4ebO'tion%.ene al ?683@Sv9Z 6l)ialog4ebO'tion%&ictu e% ?685@Sv9Z 6l)ialog4indow"ove ?14@ 6l)ialog4indow1iGe ?13@ 6l)ialog4o -boo-Add ?281@ 6l)ialog4o -boo-!o'( ?283@ 6l)ialog4o -boo-/n%e t ?354@ 6l)ialog4o -boo-"ove ?282@ 6l)ialog4o -boo-+a#e ?386@ 6l)ialog4o -boo-+ew ?302@

6l)ialog&ivot$ield. ou' ?433@ 6l)ialog4o -boo-O'tion% ?284@ 6l)ialog&ivot$ield& o'e tie% 6l)ialog4o -boo-& otect ?417@ ?313@ 6l)ialog&ivot$ield2ng ou' ?434@ 6l)ialog4o -boo-0ab1'lit ?415@

6l)ialog&ivot1,ow&age% ?421@ 6l)ialog4o -boo-2n,ide ?384@ 6l)ialog&ivot1olveO de ?568@ 6l)ialog4o -g ou' ?199@ 6l)ialog&ivot0ableO'tion% 6l)ialog4o -%'ace ?95@ ?567@

6l)ialog$o #at$ont ?150@ 6l)ialog$o #at3egend ?88@ 6l)ialog$o #at"ain ?225@

6l)ialog&ivot0able4iGa d ?312@ 6l)ialog&lace#ent ?300@ 6l)ialog& int ?8@

6l)ialog[oo# ?256@

Excel 8.9 I6l)ialog!,a t1ou ce)ata ?541@ I6l)ialog&,onetic ?538@ 6l)ialogE6te nal)ata& o'e tie% ?530@ 6l)ialog/#'o t0e6t$ile ?666@ 6l)ialog+ew4ebOue ( ?667@

able 1"-2. %##itional Xl.uilt/n(ialog +onstants an# heir *alues 4or


6l)ialogO'tion%"E ?647@ 6l)ialog4ebO'tion%$ont% ?687@ 6l)ialog&ivot!lient1e ve 1et ?689@ 6l)ialog4ebO'tion%.ene al ?683@ 6l)ialog&ubli%,A%4eb&age ?653@ 6l)ialog4ebO'tion%&ictu e% ?685@ 6l)ialog4ebO'tion%Encoding ?686@ 6l)ialog4ebO'tion%$ile% ?684@

+ote t,at eac, o5 t,e con%tant% in 0able 13;1 i% 5o #ed 5 o# t,e ' e5i6 xlDialo4 5ollowed b( t,e na#e o5 t,e dialog bo6. $o e6a#'le* t,e O'en dialog bo6 con%tant i% xlDialo4*3en and %o t,e co e%'onding )ialog object i%:
A33lication.Dialo4s(xlDialo4*3en)

0,e O'en dialog bo6 i% %,own in $igu e 13;1. Figure 1"-1. he 3!en File #ialog box

2n5o tunatel(* t,e )ialog object ,a% onl( one u%e5ul ' o'e t( o #et,od: t,e 1,ow #et,od.

$*%$ The Sho" 7ethod


0,e 1,ow #et,od di%'la(% a dialog bo6. 0,i% ' ovide% a convenient wa( to 8lead8 t,e u%e to a built;in dialog bo6. 2n5o tunatel(* we cannot acce%% t,e value% t,at t,e u%e ente % into t,at dialog. 2ntil t,e dialog i% di%#i%%ed b( t,e u%e and t,e action% %'eci5ied in t,e dialog a e co#'leted* we ,ave no cont ol ove t,e c,ain o5 event%. ?/n 4o d 97* 5o in%tance* we can u%e built;in dialog bo6e% to get value% 5 o# t,e u%e * wit,out letting 4o d act auto#aticall( on t,o%e value%.@ 0o illu%t ate* t,e code:
A33lication.Dialo4s(xlDialo4*3en).Show

di%'la(% t,e O'en dialog bo6 in $igu e 13;1. 0,e 1,ow #et,od etu n% Tr0e i5 t,e u%e clic-% t,e OR button and >alse i5 t,e u%e clic-% t,e !ancel button.

4,en t,e dialog bo6 i% di%#i%%ed b( t,e u%e u%ing t,e OR button* an( a'' o' iate action% indicated b( t,e 5ield% in t,e dialog bo6 a e ca ied out. /n t,e ca%e o5 t,e O'en dialog* t,i% #ean%* o5 cou %e* t,at t,e 5ile %elected b( t,e u%e i% actuall( o'ened in E6cel. Aoweve * no action% a e ta-en i5 t,e u%e di%#i%%e% t,e dialog bo6 u%ing t,e !ancel button. 0,e 1,ow #et,od ,a% %(nta6:
Dialo!/bject.Show(ar!#= ar!$= ...= ar!23)

w,e e t,e a gu#ent% a e u%ed to %et %o#e dialog o'tion%. /n 'a ticula * it i% 'o%%ible to %et %o#e o5 t,e value% on a built;in E6cel dialog bo6 u%ing a gu#ent% to t,e /how #et,od. 0,e%e a gu#ent% a e li%ted in t,e E6cel =>A Ael' 5ile unde 8>uilt;/n )ialog >o6 A gu#ent 3i%t%.8 $o in%tance* t,e xlDialo4*3en dialog bo6 ,a% t,e 5ollowing a gu#ent%:
*ile4text u date4links read4only *ormat rot4 wd write4res4 wd i!nore4rorec *ile4ori!in custom4delimit add4lo!ical editable *ile4access noti*y4lo!ical con+erter

Aence* t,e code:


A33lication.Dialo4s(xlDialo4*3en).Show "R.R"= >alse= Tr0e

di%'la(% t,e O'en dialog* %et% t,e 8$ile% o5 t('e8 d o';down bo6 to All $ile% 8W.W8 %o t,at t,e dialog will di%'la( t,e na#e% o5 all 5ile%* %et% u date4links to >alse ?%o t,at E6cel lin-% a e not auto#aticall( u'dated@ and read4only to Tr0e ?t,u% an( 5ile t,at i% o'ened will be ead onl(@. 2n5o tunatel(* "ic o%o5t doe% not %ee# to ,ave docu#ented t,e meaning o5 t,e va iou% a gu#ent%. Al%o* t,e a gu#ent% a e not na#ed a gu#ent%* %o we #u%t include %'ace 5o all a gu#ent% t,at ' ecede t,e a gu#ent% t,at we want to %et. 0,u%* a t ial;and;e o a'' oac, %ee#% to be t,e onl( %olution i5 (ou #u%t %et %o#e dialog o'tion%. ?Aave 5un.@

Chapter $2% Custom (ialog Boxes


A% we ,ave %een* E6celB% built;in dialog% o55e ve ( e%t icted co##unication wit, t,e u%e . $o tunatel(* E6cel #a-e% it 'o%%ible to c eate cu%to# dialog bo6e% t,at allow #uc, #o e 5le6ible co##unication. !u%to# dialog bo6e% a e al%o called forms o 'ser&orms. Ou intention ,e e i% to ' e%ent an int oduction to t,e %ubject* w,ic, will ' ovide a good ju#'ing;o55 'oint 5o 5u t,e %tud(. .ene all( %'ea-ing* #o%t E6cel a''lication% will e7ui e onl( ve ( %i#'le 5o #%. $o e6a#'le* we #a( want to di%'la( a 5o # wit, a te6t bo6 5o te6t in'ut* a li%t bo6 to allow u%e %election* o %o#e o'tion button% to %elect 5 o# %eve al c,oice%. O5 cou %e* we will want %o#e co##and button% to allow t,e u%e to e6ecute ' ocedu e%. /n 5act* "ic o%o5tB% =i%ual >a%ic i% a #o e a'' o' iate ' og a##ing envi on#ent t,an "ic o%o5t O55ice 5o c eating a''lication% t,at involve co#'le6 5o #%* %ince it wa% de%igned %'eci5icall( 5o t,at 'u 'o%e. And =i%ual >a%ic allow% (ou to acce%% an( o5 t,e object #odel% in t,e "ic o%o5t O55ice %uite* ju%t a% E6cel doe%.

$2%$ -hat &s a #ser:orm Ob1ect.


A 'ser&orm ob%ect can be t,oug,t o5 a% a %tanda d code #odule wit, a vi%ual inte 5ace ?a 5o #@ t,at i% u%ed to inte act wit, t,e u%e ?,ence t,e te # 'ser&orm@. Aoweve * we #u%t be ca e5ul not to ta-e t,i% de%c i'tion too lite all(. $o in%tance* ' ocedu e% ?even 'ublic one%@ t,at a e decla ed in t,e .ene al %ection o5 a 2%e $o # #odule a e gene all( intended to %u''o t object% ?o code@ on t,e 5o # it%el5* w,e ea% 'ublic ' ocedu e% decla ed in a %tanda d #odule a e gene all( intended to %u''o t code an(w,e e in t,e ' oject ?not ju%t in it% own #odule@. 0o illu%t ate t,e 'oint* %u''o%e we decla e a 'ublic ' ocedu e called ;roced0reA in t,e .ene al %ection o5 a 2%e $o # #odule called 2%e $o #1. Even t,oug, t,i% ' ocedu e i% 'ublic* we cannot acce%% it 5 o# anot,e #odule ?even wit,in t,e %a#e ' oject@ b( %i#'l( w iting:
;roced0reA

a% we could i5 t,e ' ocedu e wa% de5ined wit,in a %tanda d #odule. /n%tead* we #u%t u%e t,e 7uali5ied na#e:
<ser>or51.;roced0reA

$2%' Creating a #ser:orm Ob1ect


0o c eate a u%e 5o # at de%ign ti#e* we ju%t %elect t,e ' oject in w,ic, t,e 5o # will e%ide and c,oo%e 2%e $o # 5 o# t,e /n%e t #enu. ?$o #% can be c eated at un ti#e u%ing t,e Add #et,od o5 t,e 2%e $o #% collection* but we will con5ine ou attention to c eating 5o #% at de%ign ti#e.@ $igu e 14;1 %,ow% t,e de%ign envi on#ent w,en a 2%e $o # object i% %elected in t,e & oject window. Figure 1$-1. % 2serFor& #ialog box ;#esign ti&e<

+ote t,at t,e window on t,e ig,t in $igu e 14;1 contain% t,e dialog bo6* in w,ic, we ,ave 'laced a te6t bo6 cont ol and two co##and button cont ol%. 0,e e i% al%o a 0oolbo6 window t,at contain% icon% u%ed to add va iou% 4indow% cont ol% to t,e 5o #. 0o 'lace a cont ol on a 5o #* we %i#'l( clic- on t,e icon in t,e 0oolbo6 and t,en d ag a ectangle on t,e 5o #. 0,i% ectangle i% e'laced b( t,e cont ol. 4e can c,ange t,e ' o'e tie% o5 t,e 5o # it%el5 ?o an( cont ol% on t,e 5o #@ b( %electing t,e object and #a-ing t,e c,ange% in t,e & o'e tie% window. ?+ote t,e c,ange to t,e 5o #B% ca'tion in $igu e 14;1.@ Additional cont ol% #a( al%o be available on (ou %(%te#. 0,e%e can be acce%%ed b( c,oo%ing 8Additional cont ol%8 unde t,e 0ool% #enu. ?0,i% #enu o'tion i% enabled* t,oug, onl( i5 a u%e 5o # ,a% t,e 5ocu% in t,e => /)E.@

$2%* Acti e@ Controls


/5 (ou ,ave been u%ing "ic o%o5t 4indow% 5o %o#e ti#e ?a% we ' e%u#e (ou ,ave* %ince (ou a e eading t,i% boo-@* t,en (ou a e 7uite 5a#ilia wit, cont ol% at t,e u%e level. 0,e 5ollowing a e e6a#'le% o5 cont ol%:
C C C C C C C C C

!o##and button% 0e6t bo6e% 3i%t bo6e% !o#bo bo6e% O'tion button% !,ec- bo6e% 3abel% 0ab% 1c oll ba %

All o5 t,e%e cont ol% ,ave a #isual interface 5o inte action wit, t,e u%e . Aoweve * %o#e cont ol% do not ,ave a vi%ual inte 5ace. One e6a#'le i% t,e 0i#e cont ol* w,ic, can be %et to 5i e an event at egula inte val%. 0,u%* t,e ' og a##e can w ite code t,at will e6ecute at egula inte val%.

.ene all( %'ea-ing* a control ?o Acti#e4 control @ can be t,oug,t o5 a% a %'ecial t('e o5 code co#'onent t,at can be 'laced wit,in a la ge containe object ?%uc, a% a 5o #@ and ,a% t,e 5ollowing ' o'e tie%:
C C C C

!ont u%e . !ont !ont !ont

ol% gene all( ?but not alwa(%@ ' ovide a vi%ual inte 5ace 5o co##unication wit, t,e ol% can ,ave methods t,at can be invo-ed b( t,e u%e . ol% can ,ave properties t,at can be ead and %et b( t,e u%e . ol% can ,ave e#ents 5o w,ic, t,e u%e can w ite event code.

4e di%cu%%ed event% t,at a e a%%ociated wit, E6cel object% ?wo -%,eet%* wo -boo-%* c,a t%* and %o on@ in !,a'te 11. !ont ol event% wo - in ' eci%el( t,e %a#e wa(* a% we will %ee in t,e u'co#ing e6a#'le%.

$2%2 Adding #ser:orm Code


/n gene al* =>A ' og a##e % add two t('e% o5 code to a 2%e $o # #odule: event code t,at unde lie% t,e va iou% cont ol% on t,e 5o # ?and 'e ,a'% t,e 5o # it%el5@ and additional ' ocedu e% t,at 'e 5o # utilit( 5unction% needed b( t,e a''lication. 0,e latte code i% added to t,e gene al %ection o5 t,e 2%e $o # code #odule. 0o illu%t ate t,e 'oint wit, a ve ( %i#'le e6a#'le* %u''o%e we want to c eate an a''lication t,at %o t% %elected colu#n% ?t eating eac, colu#n a% a %ingle object@ u%ing t,e 5i %t ow a% t,e %o t -e(. Ou 5o # #ig,t loo- %o#et,ing li-e t,e one %,own in $igu e 14;2. Figure 1$-2. % Sort #ialog box

4,en t,e u%e clic-% t,e 1o t button* =>A will a%- ,i# o ,e to con5i # t,e %o t o'e ation and t,en act acco dingl(. +ow* w,en t,e 1o t button i% %elected b( t,e u%e * =>A 5i e% t,e !lic- event 5o t,i% button. /5 t,e button i% na#ed c5dSort* t,en =>A ' ovide% t,e event code %,ell:
;rivate S0b c5dSortGClic8() 'nd S0b

!lea l(* we want to 'e 5o # t,e %o ting o'e ation w,en t,i% event i% 5i ed. Aoweve * it would not be a good idea to 'lace t,e actual code to 'e 5o # t,e %o t in t,i% event code %,ell. /n%tead* we w ite a %e'a ate %o ting ' ocedu e to do t,e %o ting and 'lace it in t,e .ene al %ection o5 t,e 2%e $o # #odule* o 'e ,a'% #a-e it a 'ublic ' ocedu e in a %e'a ate %tanda d code #odule wit,in t,e ' oject:
;0blic S0b SortCol05ns() . code here to sort text 'nd S0b

0,e e a e %eve al ea%on% w,( it i% bette to 'lace t,e %o ting code in a %e'a ate ' ocedu e. 0,i% code modularity #a-e% it ea%ie to:
C C C C C

2%e t,e code in ot,e location% in t,e a''lication "ove t,e code to ot,e a''lication% $ind and e'ai bug% in t,e code "a-e i#' ove#ent% o addition% to t,e code Nu%t 'lain ead t,e code

Once t,e %o ting ' ocedu e i% co#'lete* we can add t,e 5ollowing code to t,e (lic) event:
;rivate S0b c5dSortGClic8() 26 Ms41ox("Sort c0rrentl selected col05nsQ"= G vb,0estion A vb@es9o) = vb@es Then SortCol05ns 'nd S0b

/ncidentall(* t,e !lic- event 5o t,e !ancel button i% o5ten ju%t t,e 5ollowing:
;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b

All t,i% doe% i% unload t,e 5o #. 4,ile on t,e %ubject o5 unloading a 5o #* it i% i#'o tant to unde %tand t,e di%tinction between unloading a 5o # and ,iding a 5o #. 4e can ,ide a 5o # b( %etting t,e 5o #B% =i%ible ' o'e t( to >alse. 0,i% #a-e% t,e 5o # invi%ible* but it %till con%u#e% e%ou ce%* %uc, a% #e#o (. 4,en we unload a 5o #* it no longe con%u#e% e%ou ce%. ?4ell* t,i% i% not 7uite t ue. 4e need to not onl( unload t,e 5o #* but al%o to %et an( va iable% t,at e5e ence t,e 5o # to 9othin4.@

$2%3 Excel's Standard Controls


E6cel ,a% two t('e% o5 cont ol%. $igu e 14;3 %,ow% two toolbo6e%* eac, o5 w,ic, ' ovide% acce%% to one t('e o5 cont ol. ?>elow eac, toolbo6 i% a cont ol c eated u%ing t,at toolbo6.@ Figure 1$-". +ontrol toolbars

0,e cont ol% on t,e !ont ol 0oolbo6 ?on t,e le5t in $igu e 14;3@ a e ActiveD cont ol%. 0,e%e cont ol% can be 'laced eit,e on a 2%e $o # o di ectl( on a wo -%,eet ?but not a c,a t%,eet@. 0,e( a e t,e %a#e a% t,e cont ol% t,at a e acce%%ible 5 o# t,e => edito B% 0oolbo6 w,en de%igning a 2%e $o #. ActiveD cont ol% a e ve ( 5le6ible and gene all( %u''o t a wide ange o5 event%. 0,e !ont ol 0oolbo6 can be o'ened 5 o# wit,in E6cel ?not t,e E6cel =>A /)E@ b( %electing t,e !u%to#iGe o'tion 5 o# t,e 0ool% #enu and c,ec-ing t,e !ont ol 0oolbo6 toolba in t,e 0oolba % tab. +ote t,at t,e !ont ol 0oolbo6 in $igu e 14;3 i% not t,e %a#e a% t,e 0oolbo6 in $igu e 14;1* even t,oug, bot, a e u%ed to acce%% ActiveD cont ol%. 0,e 0oolbo6 in $igu e 14;1 'lace% ActiveD cont ol% on u%e 5o #%H t,e !ont ol 0oolbo6 in $igu e 14;3 'lace% ActiveD cont ol% on wo -%,eet%. 0,e 5i %t button on t,e !ont ol 0oolbo6* called t,e )e%ign "ode button* i% 'a ticula l( i#'o tant. & e%%ing it 'ut% t,e wo -%,eet in design mode at lea%t wit, e%'ect to it% cont ol%. 4,en in de%ign #ode* we can #ove and e%iGe t,e cont ol% on t,e wo -%,eet u%ing t,e #ou%e. 4e can al%o ig,t clic- t,e cont ol to b ing u' a dialog bo6 wit, cont ol o'tion%. 4,en t,e )e%ign "ode button i% not de' e%%ed* clic-ing on a cont ol wit, t,e #ou%e %i#'l( 5i e% t,e !lic- eventC >( %electing t,e !u%to#iGe o'tion 5 o# t,e 0ool% #enu and c,ec-ing t,e $o #% toolba in t,e 0oolba % tab* (ou o'en t,e $o #% toolbo6. 0,e cont ol% on t,e $o #% toolbo6 ?on t,e ig,t in $igu e 14;3@ a e e5e ed to a% 8%tanda d E6cel wo -%,eet cont ol%8 and a e a e#nant 5 o# E6cel 5.0. 0,e( can be 'laced on wo -%,eet% o c,a t%,eet% ?but not 2%e $o #%@ and ,ave onl( a %ingle event: t,e !lic- event. $o in%tance* i5 (ou 'lace a %tanda d button on a wo -%,eet* E6cel i##ediatel( o'en% t,e A%%ign "ac o dialog bo6* a% %,own in $igu e 14;4. 0,i% allow% (ou to a%%ign a #ac o to t,e buttonB% !lic- event. Figure 1$-$. ,es!onse to !lacing a stan#ar# Excel co&&an# button

EA

1ince %tanda d E6cel cont ol% a e t,e onl( cont ol% t,at can be 'laced on a c,a t%,eet* t,e( e#ain u%e5ul. >ut ActiveD cont ol% a e 5a #o e 5le6ible and %,ould be u%ed w,eneve 'o%%ible. 4e will %'ea- no 5u t,e about t,e %tanda d E6cel cont ol%.

$2%8 Example+ The Acti ateSheet #tilit/


/t i% ti#e now to i#'le#ent t,e ActivateSheet utilit( in ou /,4'tils a''lication. 0,i% will de#on%t ate t,e u%e o5 2%e $o #%.

/n 'a ticula * w,en t,e u%e %elect% ActivateSheet* we would li-e to ' e%ent ,e wit, a cu%to# dialog t,at li%t% all o5 t,e %,eet% in t,e active wo -boo-* a% %,own in $igu e 14;5. 0,e u%e can %elect one o5 t,e%e %,eet%* w,ic, will t,en be activated. Figure 1$-'. he activate sheet #ialog

0o i#'le#ent t,i% utilit(* we need to do t,e 5ollowing:


C C C

!,ange t,e ActivateSheet ' ocedu e in basMain to o'en t,e Activate 1,eet dialog ?in%tead o5 di%'la(ing t,e cu ent #e%%age@. )e%ign t,e Activate 1,eet dialog it%el5. 4 ite t,e code be,ind t,e Activate 1,eet dialog.

1$.5.1 .ac1 to S,X2tils


1o c an- u' t,e /,4'tilsAxls wo -%,eet and e'lace t,e ActivateSheet ' ocedu e in basMain:
;0blic S0b ActivateSheet() Ms41ox "This is the ActivateSheet 0tilit " 'nd S0b

wit, t,e ' ocedu e:


;0blic S0b ActivateSheet() dl4ActivateSheet.Show 'nd S0b

w,ic, %i#'l( di%'la(% t,e Activate 1,eet dialog ?w,ic, we will call dl!-cti+ateSheet@.

1$.5.2 +reate the 2serFor&


A5te (ou in%e t a 2%e $o # into (ou ' oject* (ou %,ould u%e t,e & o'e tie% window to c,ange it% +a#e ' o'e t( to dl!-cti+ateSheet and it% !a'tion ' o'e t( to 8Activate 1,eet.8 0,en (ou can add t,e cont ol% to t,e 5o #. 0,e 2%e $o # in $igu e 14;5 ,a% two co##and button% and one li%t bo6. 1$.5.2.1 )ist box &lace a 3i%t bo6 on t,e 5o # a% in $igu e 14;5. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 14;1. +ote t,at t,e 0ab/nde6 ' o'e t( dete #ine% not onl( t,e o de t,at t,e cont ol% a e vi%ited a% t,e u%e ,it% t,e 0ab -e(* but al%o dete #ine% w,ic, cont ol ,a% t,e initial 5ocu%. 1ince we want t,e initial 5ocu% to be on t,e li%t bo6* we %et it% tab inde6 to 0. able 1$-1. Non#e4ault Pro!erties o4 the )ist.ox +ontrol Property Value l%t1,eet%

+a#e

0ab/nde6

4e %,ould al%o note t,at* in gene al* t,e e a e two 'lace% in w,ic, a cont ol ' o'e t( can be %et: in t,e & o'e tie% window at de%ign ti#e o u%ing code du ing un ti#e. 1o#e ' o'e tie% %,ould be ?o #u%t be@ %et at de%ign ti#e* w,e ea% ot,e % can onl( be %et at un ti#e. Aoweve * #o%t ' o'e tie% can be %et at eit,e ti#e. A% a %i#'le e6a#'le* a cont olB% =i%ible o Enabled ' o'e t( i% o5ten %et du ing un ti#e* in e%'on%e to action% b( t,e u%e . $o in%tance* we #a( want to di%able a co##and button labeled & int until t,e u%e ,a% %elected an object to ' int 5 o# a li%t o5 object%. 1etting t,e Enabled ' o'e t( o5 a co##and button w,o%e na#e i% Print)utton i% ea%il( done:
;rint10tton.'nabled = >alse

/n gene al* t,e c,oice o5 w,e e to %et a given ' o'e t( o5 a cont ol i% 'a tl( a #atte o5 ta%te. / 5avo %etting ' o'e tie% in code becau%e it tend% to #a-e t,e code #o e co#'lete and t,u% #o e eadable. /t can al%o #a-e c,anging ' o'e tie% %i#'le . Aoweve * %o#e 5unda#ental ' o'e tie%* %uc, a% +a#e and !a'tion* a e be%t %et at de%ign ti#e. 1$.5.2.2 %ctivate button &lace a co##and button on t,e 5o #* a% in $igu e 14;5. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 14;2. able 1$-2. Non#e4ault Pro!erties o4 the %ctivate .utton Property Value c#dActivate A Activate 1

+a#e Accele ato !a'tion 0ab/nde6

1$.5.2." +ancel button &lace anot,e co##and button on t,e 5o #* a% in $igu e 14;5. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 14;3. able 1$-". Non#e4ault Pro!erties o4 the +ancel .utton Property Value c#d!ancel ! !ancel 2 0 ue

+a#e Accele ato !a'tion 0ab/nde6 !ancel

1$.5." +reate the +o#e .ehin# the 2serFor&


+ow it i% ti#e to c eate t,e code be,ind t,e%e cont ol%. 1$.5.".1 +ancel button co#e )ouble clic- on t,e !ancel button to di%'la( t,e !lic- event code %,ell. Adding t,e line:

<nload Me

will 5ill out t,e code %,ell a% 5ollow% and cau%e t,e 5o # to be unloaded w,en t,e u%e ,it% t,e Cancel button:
;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b

1$.5.".2 %ctivateSelecte#Sheet !roce#ure +e6t* we c eate a ' ocedu e t,at will activate t,e %elected %,eet. 4e want t,i% ' ocedu e to be called in t, ee %ituation%H na#el(* w,en t,e u%e :
C C C

1elect% a %,eet na#e 5 o# t,e li%t bo6 and clic-% t,e Activate button ?o u%e% t,e Alt;A ,ot -e(@ )ouble;clic-% on a %,eet na#e in t,e li%t bo6 1elect% a %,eet na#e 5 o# t,e li%t bo6 and ,it% t,e Ente -e(

1ince t,i% code will be u%ed in t, ee di55e ent %ituation%* we can avoid e'eating t,e code b( 'lacing it in it% own ' ocedu e in t,e .ene al %ection o5 a 2%e $o #* a% %,own in E6a#'le 14;1. Exa&!le 1$-1. he %ctivateSelecte#Sheet Proce#ure
S0b ActivateSelectedSheet() 26 lstSheets.List2ndex ( %1 Then Sheets(lstSheets.List(lstSheets.List2ndex)).Activate 'nd 26 <nload Me 'nd S0b

0,i% code de#on%t ate% %o#e li%t bo6 ' o'e tie%. $i %t* t,e 3i%t/nde6 ' o'e t( etu n% t,e inde6 nu#be ?%ta ting at 0@ o5 t,e cu entl( %elected ite# in t,e li%t bo6. 0,u%* t,e 5ollowing code c,ec-% to %ee i5 an ite# i% %elected ?ot,e wi%e List2ndex = %1@:
26 lstSheets.List2ndex ( %1 Then

0,e code:
lstSheets.List(i)

etu n% t,e it, ite# in t,e li%t bo6 ?a% a %t ing@. 0,u%:
lstSheets.List(lstSheets.List2ndex))

i% t,e cu entl( %elected ite#<t,at i%* t,e cu entl( %elected %,eet na#e. $inall(* t,e code:
Sheets(lstSheets.List(lstSheets.List2ndex)).Activate

activate% t,at wo -%,eet b( invo-ing it% Activate #et,od. 4e will di%cu%% t,e Activate #et,od in !,a'te 18. $o now* we %i#'l( note t,at i5 a wo -%,eet ,a% t,e na#e "(1,eet* t,en t,e code:
Sheets("M Sheet").Activate

activate% t,at %,eet.

$inall(* t,e la%t t,ing done in t,e c#dActivateI!lic- event i% to unload t,e 5o #* %ince it i% no longe needed. 1$.5."." %ctivate button co#e 0o %et t,e code be,ind t,e Activate button* %elect c#dActivate in t,e Object% d o';down bo6 ?above t,e u''e ;le5t co ne o5 t,e code window@ and %elect !lic- in t,e & ocedu e% d o';down bo6 ?above t,e u''e ; ig,t co ne o5 t,e code window@. Lou can now 5ill in t,e code 5o t,e !licevent o5 t,e c5dActivate button:
;rivate S0b c5dActivateGClic8() ActivateSelectedSheet 'nd S0b

1$.5.".$ (ouble-clic1 lstSheets co#e 4e al%o want ActivateSelectedSheet to be called w,en t,e u%e double;clic-% on a %,eet na#e. 0,e )bl!lic- event 5o t,e li%t bo6 5i e% w,en t,e u%e double;clic-% on an ite# in t,e li%t bo6. 1elect l%t1,eet% in t,e Object% d o';down and )bl!l- in t,e & ocedu e% d o';down. 0,en 5ill in t,e )bl!l- event code %,ell:
;rivate S0b lstSheetsGDblClic8(1 +al Cancel As G MS>or5s."et0rn1oolean) ActivateSelectedSheet 'nd S0b

1$.5.".' Enter 1ey event 4e al%o want to invo-e ActivateSelectedSheet w,en t,e u%e %elect% a %,eet na#e and ,it% t,e Ente -e(. 4,en t,e li%t bo6 ,a% t,e 5ocu%* an( -e(%t o-e 5i e% t,e Re()own event. !,oo%e t,i% event in t,e & ocedu e% d o';down and add t,e code %,own in E6a#'le 14;2 to t,e event %,ell. Exa&!le 1$-2. he lstSheetsG@ey(own Event Proce#ure
;rivate S0b lstSheetsGPe Down(1 +al Pe Code As G MS>or5s."et0rn2nte4er= 1 +al Shi6t As 2nte4er) 26 Pe Code = vbPe "et0rn Then ActivateSelectedSheet 'nd S0b

/n t,i% ca%e* we #u%t add code to dete #ine w,et,e t,e Ente -e( wa% %t uc-. $o tunatel(* E6cel will 5ill in t,e 5eyCode 'a a#ete o5 t,e Re()own event wit, t,e -e( code 5o t,e -e( t,at cau%ed t,e event to be 5i ed. ?$o a li%t o5 -e( code%* c,ec- 8Re(!ode8 in t,e E6cel =>A ,el' 5ile.@ 1$.5.".5 Fill the lstSheets list box +e6t* we need to 5ill t,e lstSheets li%t bo6 wit, a li%t o5 all o5 t,e %,eet% in t,e cu ent wo -boo-. 4e want t,i% to be done auto#aticall(* %o we will 'lace t,e e7ui ed code in t,e /nitialiGe event o5 t,e 2%e $o #. 0,i% event i% 5i ed b( E6cel w,en t,e 5o # i% loaded* but be5o e it beco#e% vi%ible. A% t,e na#e i#'lie%* it i% de%igned to initialiGe va iou% ' o'e tie% o5 t,e 5o # and it% cont ol%. 1elect 2%e $o # in t,e Object d o';down and /nitialiGe in t,e & ocedu e% d o';down. Lou %,ould get t,e 2%e $o #I/nitialiGe event code %,ell. $ill it wit, t,e code %,own in E6a#'le 14;3. Exa&!le 1$-". he 2serFor&G/nitiali?e Event Proce#ure

;rivate S0b <ser>or5G2nitialiMe() . >ill lstSheets with the list o6 sheets Di5 cSheets As 2nte4er Di5 i As 2nte4er cSheets = Sheets.Co0nt lstSheets.Clear >or i = 1 To cSheets lstSheets.Add2te5 Sheets(i).9a5e 9ext 'nd S0b

0,i% code 5i %t get% t,e total nu#be o5 %,eet% ?wo -%,eet% and c,a t%@ in t,e cu ent wo -boo-. ?4e will di%cu%% t,i% in detail in late c,a'te %* %o donBt wo ( about it now.@ 0,e li%t bo6 i% t,en clea ed o5 an( ' eviou% content. 0,en we ,ave a >or loo' t,at add% t,e %,eet na#e% to t,e li%t bo6. 0,i% i% done u%ing t,e 3i%t>o6 cont olB% Add/te# #et,od. 0,e na#e o5 a %,eet i% given b( it% +a#e ' o'e t(.

1$.5.$ rying the %ctivate 2tility


/5 all ,a% gone well* (ou can now %ave 1RD2til% a% an add;in* load it t, oug, t,e 0ool% #enu ?i5 it i% cu entl( loaded* (ou will need to unload it be5o e %aving t,e add;in o E6cel will co#'lain@* and t ( out t,e new Activate1,eet 5eatu e.

$2%9 Acti e@ Controls on -orksheets


A% (ou #a( -now* ActiveD cont ol% ?and %tanda d E6cel cont ol%@ can be 'laced di ectl( on a wo -%,eet. !a e #u%t be ta-en* ,oweve * not to clutte u' a wo -%,eet wit, cont ol% t,at would be bette 'laced on a 2%e $o #. 4,en onl( a %#all nu#be o5 cont ol% a e e7ui ed* 'lacing t,e%e cont ol% di ectl( on a wo -%,eet #a( be a'' o' iate. 0,e e a e %o#e %'ecial con%ide ation% w,en cont ol% a e 'laced di ectl( on a wo -%,eet. /n 'a ticula * eac, ActiveD cont ol on a wor)sheet ?not on a 2%e $o #@ i% e' e%ented b( an O3EObject in t,e E6cel object #odel. Aoweve * it i% i#'o tant to note t,at O3EObject object% can al%o e' e%ent e#bedded O3E object%. 0,u%* 5o in%tance* i5 we in%e t a bit#a' on a wo -%,eet ?%elect Object 5 o# E6celB% /n%e t #enu@* t,i% bit#a' object will be e' e%ented b( an O3EObject. 0,e 4o -%,eet object ,a% a ' o'e t( called O3EObject% t,at etu n% t,e O3EObject% collection con%i%ting o5 all O3EObject object% on t,e wo -%,eet. 0,u%* t,e O3EObject% collection 5o t,e active wo -%,eet i%:
ActiveSheet.*L'*bTects

>ecau%e O3EObject% al%o e' e%ent e#bedded O3E object% ?%uc, a% bit#a'%@* we cannot be ce tain t,at* %a(:
ActiveSheet.*L'*bTects(1)

i% a cont ol. 0,u%* it i% wi%e w,en adding a cont ol o e#bedded O3E object to a wo -%,eet to i##ediatel( a%%ign t,e cont ol o object a na#e and t,en e5e to it b( t,i% na#e at,e t,an b( inde6* a% in:

ActiveSheet.*L'*bTects("M 10tton")

1$.6.1 ,e4erring to a +ontrol on a 0or1sheet


$o tunatel(* E6cel let% u% e5e to an ActiveD cont ol on a wo -%,eet b( u%ing it% na#e* wit,out e5e ence to t,e O3EObject% collection. $o in%tance* i5 we 'lace a co##and button on a wo -%,eet* E6cel will give it t,e de5ault na#e !o##and>utton1. >ot, o5 t,e 5ollowing line% %et t,e ,eig,t o5 t,i% co##and button to 20 'oint%:
ActiveSheet.*L'*bTects("Co55and10tton1").?ei4ht = 27 ActiveSheet.Co55and10tton1.?ei4ht = 27

2n5o tunatel(* ,oweve * t,e ' o'e tie% and #et,od% t,at we acce%% in t,i% #anne a e t,e ' o'e tie% and #et,od% o5 t,e O3EObject* not t,e cont ol it%el5. 0,e%e ' o'e tie% a e %,own in 0able 14;4. able 1$-$. -e&bers o4 the 3)E3bject object Enabled & intObject Aeig,t & og/d /nde6 1elect /nte io 1end0o>ac3e5t 1,adow 3in-ed!ell 1,a'eRange 3i%t$illRange 1ou ce+a#e 3oc-ed 0o' +a#e 0o'3e5t!ell Object 2'date O3E0('e =e b OnAction =i%ible &a ent 4idt, &lace#ent [O de

AltA0"3 Activate A''lication Auto3oad Auto2'date >o de >otto#Rig,t!ell > ing0o$ ont !o'( !o'(&ictu e ! eato !ut )elete )u'licate

0,u%* 5o in%tance* w,ile we can %et t,e Aeig,t ' o'e t( o5 t,e co##and button* we cannot %et it% !a'tion ' o'e t( in t,i% wa(. 0,at i%* t,e code:
ActiveSheet.*L'*bTects("Co55and10tton1").Ca3tion = "Clic8Me"

will gene ate an e o . 0,e wa( to eac, t,e #e#be % o5 t,e cont ol it%el5 i% to u%e t,e Object ' o'e t( o5 an O3EObject object* w,ic, etu n% t,e unde l(ing cont ol* and #a-e% it% ' o'e tie% and #et,od% acce%%ible. 0,u%* t,e 5ollowing two line% eac, %et t,e buttonB% ca'tion:
ActiveSheet.*L'*bTects("Co55and10tton1").*bTect.Ca3tion = "Clic8Me" ActiveSheet.Co55and10tton1.*bTect.Ca3tion = "Clic8Me"

/n addition to t,e %tanda d ' o'e tie% available 5o ActiveD cont ol%* t,e 5ollowing ' o'e tie% can be u%ed wit, ActiveD cont ol% e#bedded in %,eet% in "ic o%o5t E6cel: Bottom,ight(ell

Retu n% a Range object t,at e' e%ent% t,e cell t,at lie% unde t,e lowe ; ig,t co ne o5 t,e object. Lin)ed(ell Retu n% o %et% t,e wo -%,eet ange t,at i% lin-ed to t,e value o5 t,e cont ol. 0,u%* i5 we 'lace a value in t,e lin-ed cell* t,e cont ol will a%%u#e t,i% value* and vice;ve %a. List&ill,ange Retu n% o %et% t,e wo -%,eet ange t,at i% u%ed to 5ill a li%t bo6 cont ol. Placement Retu n% o %et% t,e wa( t,at t,e cont ol i% attac,ed to t,e cell% below it. 0,e 'o%%ible value% a e t,e #l;lace5ent con%tant%: xlMoveAndSiMe* xlMove* and xl>ree>loatin4. Print$b%ect & int% t,e cont ol w,en t,e wo -%,eet i% ' inted i5 t,i% ' o'e t( i% %et to Tr0e. !opLeft(ell Retu n% a Range object t,at e' e%ent% t,e cell t,at lie% unde t,e to';le5t co ne o5 t,e object. E$rder Retu n% t,e [O de 'o%ition o5 t,e cont ol. +ote al%o t,at 0able 14;4 ,a% %o#e ' o'e tie% t,at a e not ' o'e tie% o5 cont ol% t,e#%elve%. 0,e( elate to t,e O3EObject* w,ic, i% t,e containe 5o t,e cont ol* and t,u% to t,e cont olB% elation%,i' wit, t,e wo -%,eet. $o in%tance* t,e code:
ActiveSheet.Co55and10tton1.To3Le6tCell.Address

etu n% t,e add e%% o5 t,e to';le5t cell o5 t,e wo -%,eet t,at lie% unde t,e cont ol ?o at,e * t,e cont olB% containe : t,e O3EObject@. A% anot,e e6a#'le* t,e 5ollowing code will locate t,e to';le5t cell unde t,e co##and button and t,en %c oll t,e active window %o t,at t,i% cell ?and t,e e5o e t,e co##and button@ i% at t,e u''e le5t co ne o5 t,e window:
Di5 rn4 As "an4e Set rn4 = ActiveSheet.Co55and10tton1.To3Le6tCell Hith ActiveHindow .Scroll"ow = rn4."ow .ScrollCol05n = rn4.Col05n 'nd Hith

/t i% i#'o tant to note t,at %o#e ' o'e tie% and #et,od% o5 %o#e E6cel object% a e di%abled w,en an ActiveD cont ol ,a% t,e 5ocu%. $o e6a#'le* t,e 1o t #et,od o5 t,e Range object cannot be u%ed w,en a cont ol i% active. 1ince a cont ol on a wo -%,eet e#ain% active a5te it i% clic-ed* t,e 5ollowing code will 5ail:

;rivate S0b Co55and10tton1GClic8 "an4e("A-A").Sort Pe 1-="an4e("A-A") 'nd S0b

?4e will di%cu%% t,e %o t #et,od in !,a'te 19. )onBt wo ( about t,at now.@ 0,i% i% one di%advantage o5 'lacing cont ol% di ectl( on wo -%,eet%. O5 cou %e* one wa( to avoid t,i% ' oble# i% to activate anot,e object be5o e calling t,e %o t #et,od. $o in%tance* we can a#end t,e ' eviou% code a% 5ollow%:
;rivate S0b Co55and10tton1GClic8 "an4e("A-A").Activate "an4e("A-A").Sort Pe 1-="an4e("A-A") Co55and10tton1.Activate . *3tional 'nd S0b

/t i% al%o wo t, #entioning t,at i5 (ou %ave an E6cel 97 o E6cel 2000 wo -boo- in E6cel 5.0E95 4o -boo- 5ile 5o #at* all ActiveD cont ol in5o #ation will be lo%t.

1$.6.2 %##ing a +ontrol to a 0or1sheet Progra&&atically


0o ' og a##aticall( add an ActiveD cont ol to a wo -%,eet* we u%e t,e Add #et,od o5 t,e O3EObject% collection. 0,e %(nta6 i%:
/",/bjectCollection.Add(ClassTy e, FileName, "ink, Dis lay-s1con, G 1conFileName, 1con1ndex, 1con"abel, "e*t, To , Width, 6ei!ht)

0,e ClassTy e 'a a#ete i% t,e %o;called programmatic identifier ?o & og/)@ 5o t,e cont ol. 0able 14;5 %,ow% t,e & og/)% 5o va iou% cont ol%. able 1$-'. Prog/(s 4or %ctiveX +ontrols Prog(D $o #%.!,ec->o6.1 $o #%.!o#bo>o6.1 $o #%.!o##and>utton.1 $o #%.$ a#e.1 $o #%./#age.1 $o #%.3abel.1 $o #%.3i%t>o6.1 $o #%."ulti&age.1 $o #%.O'tion>utton.1 $o #%.1c oll>a .1 $o #%.1'in>utton.1 $o #%.0ab1t i'.1 $o #%.0e6t>o6.1 $o #%.0oggle>utton.1

Control !,ec->o6 !o#bo>o6 !o##and>utton $ a#e /#age 3abel 3i%t>o6 "ulti&age O'tion>utton 1c oll>a 1'in>utton 0ab1t i' 0e6t>o6 0oggle>utton

0,e onl( ot,e 'a a#ete % t,at a e elevant to adding ActiveD cont ol% ?t,i% #et,od i% u%ed 5o ot,e t('e% o5 O3E object% a% well@ a e t,e 3e5t* 0o'* 4idt,* and Aeig,t 'a a#ete %* w,ic, %'eci5( in 'oint% t,e location ?wit, e%'ect to t,e u''e ;le5t co ne o5 cell A1@ and %iGe o5 t,e cont ol. All ot,e 'a a#ete % %,ould be o#itted. ?0,i% i% a good 'lace 5o na#ed a gu#ent%C@

$o in%tance* t,e code:


ActiveSheet.*L'*bTects.Add ClassT 3e-=">or5s.Textbox.1"= G Le6t-=F2= To3-=F2= ?ei4ht-=27= Hidth-=177

'lace% a new te6t bo6 a'' o6i#atel( one inc, 5 o# t,e to' and le5t edge% o5 t,e active wo -%,eet. ?0,e di#en%ion% do not %ee# to be te ibl( accu ate.@

The Excel Ob1ect 7odel


0,e E6cel object #odel i% one o5 t,e #o%t e6ten%ive object #odel% in "ic o%o5tB% a %enal* wit, al#o%t 200 object% and ove 5000 ' o'e tie% and #et,od%. A% we ,ave #entioned* ,oweve * #an( o5 t,e%e object% and #e#be % a e included %olel( 5o bac-wa d co#'atibilit( wit, ea lie ve %ion% o5 E6cel. 4,en we igno e t,e%e object% and #e#be %* t,e object count d o'% to 140 and t,e #e#be count i% about 3000. 0,i% #a-e% t,e E6cel object #odel %econd in %iGe onl( to t,e 4o d object #odel. 4e will not di%cu%% t,e object% and #e#be % t,at a e included 5o bac-wa d co#'atibilit( onl(. Aoweve * %ince (ou %,ould at lea%t be awa e o5 t,e e6i%tence o5 t,e%e object%* we will include t,e# in ou 'ictu e% o5 t,e #odel ?a'' o' iatel( #a -ed@ but not in t,e table%. /t i% ce tainl( not ou intention in t,i% boo- to cove all* o even #o%t* o5 t,e object% and #e#be % o5 t,e E6cel object #odel. Ou goal i% to ac7uaint (ou wit, t,e #ajo 'o tion% o5 t,i% #odel* %o t,at (ou can ea%il( lea n #o e a% needed. /t %ee#% a'' o' iate to begin b( t (ing to ' e%ent an ove all view o5 t,e E6cel object #odel.

$3%$ A Perspecti e on the Excel Ob1ect 7odel


0o 'ut t,e E6cel object #odel in %o#e 'e %'ective* 0able 15;1 give% %o#e %tati%tic% on va iou% "ic o%o5t object #odel% 5o O55ice 97 ?t,e nu#be % a e %o#ew,at la ge 5o late ve %ion% o5 O55ice@. able 1'-1. So&e 3bject -o#el Statistics 4or 344ice 86 Appli ation )b'e ts Properties Met*o&s "nums Constants Acce%% 8 51 1596 532 31 485 >inde 8 4 37 15 4 11 )AO 3.5 37 235 174 26 185 E6cel 8 192 3245 1716 152 1266 $o #% 2 64 588 352 42 191 . a', 8 44 1120 234 58 447 O55ice 97 40 615 209 78 801 Outloo- 8 42 1568 534 34 154 &owe &oint 8 110 1197 322 53 370 4o d 8 188 2300 837 192 1969 $o e5e ence* 0able 15;2 %,ow% all non,idden object% in t,e E6cel D& object #odel* along wit, t,e nu#be o5 c,ild en 5o eac, object. able 1'-2. Excel 19 objects an# their chil# counts Add/n ?1@ $ill$o #at ?1@ & otection ?1@ Add/n% ?2@ $ilte ?1@ &ubli%,Object ?1@ Adju%t#ent% ?0@ $ilte % ?2@ &ubli%,Object% ?2@ AllowEditRange ?2@ $loo ?4@ Oue (0able ?3@ AllowEditRange% ?1@ $ont ?1@ Oue (0able% ?2@

A''lication ?32@ A ea% ?2@ Auto!o ect ?1@ Auto$ilte ?3@ AutoRecove ?1@ A6e% ?2@ A6i% ?6@ A6i%0itle ?6@ >o de ?1@ >o de % ?2@ !alculated$ield% ?2@ !alculated/te#% ?2@ !alculated"e#be ?1@ !alculated"e#be % ?2@ !allout$o #at ?0@ !ell$o #at ?4@ !,a acte % ?2@ !,a t ?16@ !,a tA ea ?5@ !,a t!olo $o #at ?1@ !,a t$ill$o #at ?2@ !,a t. ou' ?7@ !,a t. ou'% ?2@ !,a tObject ?6@ !,a tObject% ?6@ !,a t% ?4@ !,a t0itle ?6@ !olo $o #at ?0@ !o##ent ?3@ !o##ent% ?2@ !onnecto $o #at ?2@ !ont ol$o #at ?1@ !o ne % ?1@ !ube$ield ?3@ !ube$ield% ?2@ !u%to#& o'e tie% ?2@ !u%to#& o'e t( ?1@ !u%to#=iew ?1@ !u%to#=iew% ?2@ )ata3abel ?6@ )ata3abel% ?6@ )ata0able ?3@ )e5ault4ebO'tion% ?1@ )iag a# ?2@ )iag a#+ode ?3@ )iag a#+ode!,ild en ?1@

$o #at!ondition ?4@ $o #at!ondition% ?2@ $ ee5o #>uilde ?2@ . a',ic ?1@ . idline% ?2@ . ou'1,a'e% ?3@ Ai3o3ine% ?2@ A&age> ea- ?3@ A&age> ea-% ?2@ A('e lin- ?3@ A('e lin-% ?2@ /nte io ?1@ /Rtd1e ve ?0@ /R0)2'dateEvent ?0@ 3eade 3ine% ?2@ 3egend ?5@ 3egendEnt ie% ?2@ 3egendEnt ( ?3@ 3egendRe( ?4@ 3ine$o #at ?1@ 3in-$o #at ?1@ "aile ?1@ +a#e ?2@ +a#e% ?2@ O)>!E o ?1@ O)>!E o % ?2@ O3E)>E o ?1@ O3E)>E o % ?2@ O3E$o #at ?1@ O3EObject ?5@ O3EObject% ?6@ Outline ?1@ &age1etu' ?2@ &ane ?2@ &ane% ?2@ &a a#ete ?2@ &a a#ete % ?2@ &,onetic ?2@ &,onetic% ?2@ &ictu e$o #at ?0@ &ivot!ac,e ?2@ &ivot!ac,e% ?2@ &ivot!ell ?6@ &ivot$ield ?5@ &ivot$ield% ?2@ &ivot$o #ula ?1@

Range ?22@ Recent$ile ?2@ Recent$ile% ?2@ Routing1li' ?1@ R0) ?0@ 1cena io ?2@ 1cena io% ?2@ 1e ie% ?6@ 1e ie%!ollection ?2@ 1e ie%3ine% ?2@ 1,adow$o #at ?1@ 1,a'e ?22@ 1,a'e+ode ?0@ 1,a'e+ode% ?1@ 1,a'eRange ?17@ 1,a'e% ?4@ 1,eet% ?3@ 1#a t0ag ?4@ 1#a t0agAction ?1@ 1#a t0agAction% ?2@ 1#a t0agO'tion% ?1@ 1#a t0agRecogniGe ?1@ 1#a t0agRecogniGe % ?2@ 1#a t0ag% ?2@ 1ound+ote ?1@ 1'eec, ?0@ 1'ellingO'tion% ?0@ 1t(le ?4@ 1t(le% ?2@ 0ab ?1@ 0e6tE55ect$o #at ?0@ 0e6t$ a#e ?2@ 0, ee)$o #at ?1@ 0ic-3abel% ?2@ 0 eeview!ont ol ?1@ 0 endline ?3@ 0 endline% ?2@ 2'>a % ?4@ 2%edObject% ?1@ 2%e Acce%% ?0@ 2%e Acce%%3i%t ?1@ =alidation ?1@ =&age> ea- ?3@ =&age> ea-% ?2@ 4all% ?4@ 4atc, ?1@

)iag a#+ode% ?1@ )ialog ?1@ )ialog% ?2@ )i%'la(2nit3abel ?6@ )own>a % ?4@ ) o'3ine% ?2@ E o ?1@ E o >a % ?2@ E o !,ec-ingO'tion% ?1@ E o % ?2@

&ivot$o #ula% ?2@ &ivot/te# ?4@ &ivot/te#3i%t ?2@ &ivot/te#% ?2@ &ivot3a(out ?4@ &ivot0able ?8@ &ivot0able% ?2@ &lotA ea ?4@ &oint ?5@ &oint% ?2@

4atc,e% ?2@ 4ebO'tion% ?1@ 4indow ?7@ 4indow% ?2@ 4o -boo- ?14@ 4o -boo-% ?2@ 4o -%,eet ?17@ 4o -%,eet$unction ?1@ 4o -%,eet% ?3@

0able 15;3 %,ow% t,e E6cel object% t,at ,ave at lea%t 5ive c,ild en. A% we can %ee b( co#'a ing t,e %iGe% o5 0able% 15;2 and 15;3* #o%t object% b( 5a ,ave 5ewe t,an 5ive c,ild en. able 1'-". Excel 19 objects with ' or &ore chil#ren )ata3abel ?6@ &oint ?5@ )ata3abel% ?6@ Range ?22@ )i%'la(2nit3abel ?6@ 1e ie% ?6@ 3egend ?5@ 1,a'e ?22@ O3EObject ?5@ 1,a'eRange ?17@ O3EObject% ?6@ 4indow ?7@ &ivot!ell ?6@ 4o -boo- ?14@ &ivot$ield ?5@ 4o -%,eet ?17@ &ivot0able ?8@

A''lication ?32@ A6i% ?6@ A6i%0itle ?6@ !,a t ?16@ !,a tA ea ?5@ !,a t. ou' ?7@ !,a tObject ?6@ !,a tObject% ?6@ !,a t0itle ?6@

0,i% li%t %,ow% t,e onl( E6cel 10 object% w,o%e c,ild count i% in t,e double digit%: A''lication ?32@ 1,a'e ?22@ Range ?22@ 4o -%,eet ?17@ 1,a'eRange ?17@ !,a t ?16@ 4o -boo- ?14@ /ndeed* #uc, o5 t,e 'owe o5 t,e E6cel object ,ie a c,( i% concent ated in t,e %even object% and #uc, o5 t,e e#ainde o5 t,i% boo- i% devoted to t,o%e object%.

$3%' Excel Enums


/t i% al%o inte e%ting to glance ove t,e li%t o5 E6cel enu#%* w,o%e na#e% begin wit, Dl ?wit, t,e %ole e6ce'tion o5 t,e Constants enu#@. 0able% 0able 15;4 t, oug, 0able 15;6 %,ow t,e%e enu#% 5o E6cel 8* 9* and 10* along wit, a count o5 t,e nu#be o5 con%tant% 'e enu#. +ote t,at t,e e a e %o#e at,e la ge enu#% in t,e object #odel. 0,e enu#% wit, at lea%t 20 con%tant% a e:
C C C

Dl>uilt/n)ialog ?241@ !on%tant% ?167@ Dl!,a t0('e ?73@

C C C C C C C C C

DlA''lication/nte national ?45@ Dl$ile$o #at ?43@ DlRangeAuto$o #at ?43@ Dl&a'e 1iGe ?42@ Dl!li'boa d$o #at ?33@ Dl!,a t/te# ?32@ Dl&ivot$o #at0('e ?22@ Dl&a a#ete )ata0('e ?21@ Dl&atte n ?20@

able 1'-$. he Excel Enu&s an# their nu&ber o4 constants ;Excel 7< !on%tant% ?163@ DlEnable1election ?3@ Dl&ivot$ieldO ientation ?5@ DlA''lication/nte national ?45@ DlEnd1t(le!a' ?2@ Dl&ivot0able1ou ce0('e ?4@ DlA''l(+a#e%O de ?2@ DlE o >a )i ection ?2@ Dl&lace#ent ?3@ DlA ange1t(le ?4@ DlE o >a /nclude ?4@ Dl&lat5o # ?3@ DlA owAead3engt, ?3@ DlE o >a 0('e ?5@ Dl& int3ocation ?3@ DlA owAead1t(le ?5@ Dl$ileAcce%% ?2@ Dl& io it( ?3@ DlA owAead4idt, ?3@ Dl$ile$o #at ?39@ Dl&01election"ode ?6@ DlAuto$ill0('e ?11@ Dl$ill4it, ?3@ DlRangeAuto$o #at ?21@ DlAuto$ilte O'e ato ?6@ Dl$ilte Action ?2@ DlRe5e ence1t(le ?2@ DlA6i%! o%%e% ?4@ Dl$ind3oo-/n ?3@ DlRe5e ence0('e ?4@ DlA6i%. ou' ?2@ Dl$o #at!onditionO'e ato ?8@ DlRouting1li')elive ( ?2@ DlA6i%0('e ?3@ Dl$o #at!ondition0('e ?2@ DlRouting1li'1tatu% ?3@ Dl>ac-g ound ?3@ Dl$o #!ont ol ?10@ DlRow!ol ?2@ Dl>a 1,a'e ?6@ Dl$o #ula3abel ?4@ DlRunAuto"ac o ?4@ Dl>o de %/nde6 ?8@ DlAAlign ?8@ Dl1aveAction ?2@ Dl>o de 4eig,t ?4@ DlAig,lig,t!,ange%0i#e ?3@ Dl1aveA%Acce%%"ode ?3@ Dl>uilt/n)ialog ?221@ Dl/"E"ode ?11@ Dl1ave!on5lictRe%olution ?3@ Dl!alculation ?3@ Dl/n%e t1,i5t)i ection ?2@ Dl1cale0('e ?2@ Dl!atego (0('e ?3@ Dl3egend&o%ition ?5@ Dl1ea c,)i ection ?2@ Dl!ell/n%e tion"ode ?3@ Dl3ine1t(le ?8@ Dl1ea c,O de ?2@ Dl!ell0('e ?10@ Dl3in- ?4@ Dl1,eet0('e ?5@ Dl!,a t.alle ( ?3@ Dl3in-/n5o ?2@ Dl1,eet=i%ibilit( ?3@ Dl!,a t/te# ?29@ Dl3in-/n5o0('e ?3@ Dl1iGeRe' e%ent% ?2@ Dl!,a t3ocation ?3@ Dl3in-0('e ?2@ Dl1o t"et,od ?2@ Dl!,a t&ictu e&lace#ent ?7@ Dl3ocation/n0able ?9@ Dl1o t"et,odOld ?2@ Dl!,a t&ictu e0('e ?3@ Dl3oo-At ?2@ Dl1o tO de ?2@ Dl!,a t1'lit0('e ?4@ Dl"ail1(%te# ?3@ Dl1o tO ientation ?2@ Dl!,a t0('e ?73@ Dl"a -e 1t(le ?12@ Dl1o t0('e ?2@ Dl!li'boa d$o #at ?33@ Dl"ou%e>utton ?3@ Dl1'ecial!ell%=alue ?4@ Dl!olo /nde6 ?2@ Dl"ou%e&ointe ?4@ Dl1ub%c ibe0o$o #at ?2@ Dl!o##and2nde line% ?3@ Dl"1A''lication ?7@ Dl1u##a (!olu#n ?2@ Dl!o##ent)i%'la("ode ?3@ DlObject1iGe ?3@ Dl1u##a (Re'o t0('e ?2@ Dl!on%olidation$unction ?11@ DlO3E0('e ?3@ Dl1u##a (Row ?2@ Dl!o'(&ictu e$o #at ?2@ DlO3E=e b ?2@ Dl0ab&o%ition ?2@ Dl! eato ?1@ DlO de ?2@ Dl0e6t&a %ing0('e ?2@ Dl!ut!o'("ode ?2@ DlO ientation ?4@ Dl0e6tOuali5ie ?3@

Dl!=E o ?7@ Dl)ata3abel&o%ition ?11@ Dl)ata3abel%0('e ?6@ Dl)ata1e ie%)ate ?4@ Dl)ata1e ie%0('e ?4@ Dl)elete1,i5t)i ection ?2@ Dl)i ection ?4@ Dl)i%'la(>lan-%A% ?3@ Dl)i%'la(1,a'e% ?3@ Dl)=Ale t1t(le ?3@ Dl)=0('e ?8@ DlEdition$o #at ?4@ DlEditionO'tion%O'tion ?8@ DlEdition0('e ?2@ DlEnable!ancelRe( ?3@

Dl&age> ea- ?2@ Dl&age> ea-E6tent ?2@ Dl&ageO ientation ?2@ Dl&a'e 1iGe ?42@ Dl&a a#ete )ata0('e ?20@ Dl&a a#ete 0('e ?3@ Dl&a%te1'ecialO'e ation ?5@ Dl&a%te0('e ?6@ Dl&atte n ?20@ Dl&,oneticAlign#ent ?4@ Dl&,onetic!,a acte 0('e ?4@ Dl&ictu eA''ea ance ?2@ Dl&ictu e!onve to 0('e ?13@ Dl&ivot$ield!alculation ?9@ Dl&ivot$ield)ata0('e ?3@

Dl0ic-3abelO ientation ?5@ Dl0ic-3abel&o%ition ?4@ Dl0ic-"a - ?4@ Dl0i#e2nit ?3@ Dl0oolba & otection ?5@ Dl0 endline0('e ?6@ Dl2nde line1t(le ?5@ Dl=Align ?5@ Dl4>A0e#'late ?4@ Dl4indow1tate ?3@ Dl4indow0('e ?5@ Dl4indow=iew ?2@ DlD3""ac o0('e ?3@ DlLe%+o.ue%% ?3@

able 1'-'. %##itional enu&s 4or Excel 8.9 Dl!#d0('e ?4@ DlAt#l0('e ?4@ Dl1ou ce0('e ?7@ Dl!olu#n)ata0('e ?10@ Dl3a(out$o #0('e ?2@ Dl1ubtototal3ocation0('e ?2@ Dl!ube$ield0('e ?2@ Dl&ivot$o #at0('e ?22@ Dl4eb$o #atting ?3@ Dl)i%'la(2nit ?9@ DlOue (0('e ?6@ Dl4eb1election0('e ?3@ able 1'-5. %##itional enu&s 4or Excel 19 DlA abic"ode% ?4@ Dl/#'o t)ataA% ?2@ DlRobu%t!onnect ?3@ Dl!alculated"e#be 0('e ?2@ Dl/n%e t$o #atO igin ?2@ Dl1ea c,4it,in ?2@ Dl!alculation/nte u'tRe( ?3@ Dl3in-1tatu% ?11@ Dl1#a t0ag)i%'la("ode ?3@ Dl!alculation1tate ?3@ Dl&ivot!ell0('e ?10@ Dl1o t)ataO'tion ?2@ Dl!o u't3oad ?3@ Dl&ivot0able"i%%ing/te#% ?3@ Dl1'ea-)i ection ?2@ Dl)ata3abel1e'a ato ?1@ Dl&ivot0able=e %ion3i%t ?3@ Dl2'date3in-% ?3@ DlE o !,ec-% ?7@ Dl& intE o % ?4@ DlAeb ew"ode% ?4@ DlRange=alue)ata0('e ?3@

$3%* The ,BA Ob1ect Bro"ser


"ic o%o5t doe% %u''l( a tool 5o viewing t,e object%* ' o'e tie%* #et,od%* event%* and enu#% in an object #odel. /t i% called t,e "ic o%o5t Object > ow%e * and it i% acce%%ible 5 o# t,e =iew #enu in t,e =>A /)E ?o ,it t,e $2 -e(@. $igu e 15;1 %,ow% t,e "ic o%o5t Object > ow%e . Figure 1'-1. he -icroso4t 3bject .rowser

0,e to'#o%t d o';down li%t bo6 let% u% %elect an object #odel 5o viewingH in t,e ca%e o5 $igu e 15;1* we a e viewing t,e E6cel object #odel. 0,e %econd li%t bo6 i% 5o %ea c,ing t,e object #odel. On t,e le5t* we 5ind a li%t o5 t,e cla%%e% in t,e object #odel. 0,e e i% one cla%% 'e object and one cla%% 'e enu#. 0,e ig,t;,and li%t bo6 %,ow% t,e ' o'e tie%* #et,od%* and event% o5 t,e object t,at i% %elected in t,e !la%%e% li%t bo6. 0,e te6t bo6 at t,e botto# give% %o#e in5o #ation about t,e %elected ite#. 0,e Object > ow%e i% ce tainl( a u%e5ul tool* and (ou will ' obabl( want to %'end %o#e ti#e e6'e i#enting wit, it. ?&e ,a'% it% be%t 5eatu e i% t,at it i% ea%il( acce%%ible 5 o# t,e /)E.@ Aoweve * it give% onl( a 5lat* one; di#en%ional view o5 t,e object #odel. $o t,i% ea%on* / ,ave w itten an object b ow%e t,at ' ovide% a two;di#en%ional view o5 an object #odel. /n 5act* #an( o5 t,e 5igu e% in t,i% boo- a e %c een %,ot% ta-en 5 o# #( object b ow%e . $o #o e in5o #ation on t,i% b ow%e * 'lea%e %ee t,e cou'on in t,e bac- o5 t,i% boo-.

Chapter $8% The Application Ob1ect


A% we di%cu%%ed in !,a'te 15* t,e #ajo it( o5 t,e action in t,e E6cel object #odel e%t% in t,e %i6 object%: A''lication* !,a t* &ivot0able* Range* 4o -boo-* and 4o -%,eet. /n t,i% boo-* we will concent ate on t,e 5ollowing object%* along wit, %o#e o5 t,ei c,ild en: A''lication !,a t !o##and>a % )ialog% .lobal +a#e% Range 1,eet% 4indowE4indow% 4o -boo-E4o -boo-% 4o -%,eet 4o -1,eet$unction% 0,i% con%titute% t,e va%t #ajo it( o5 t,e E6cel object #odel. 4it, t,i% -nowledge* (ou %,ould be able to ' og a# #o%t E6cel ta%-% and be in a 'o%ition to ea%il( 'ic- u' an( additional in5o #ation 5 o# t,e E6cel ,el' 5ile% t,at (ou #ig,t need 5o le%% co##on ' og a##ing ta%-%. A% (ou #ig,t i#agine* %eve al o5 t,e%e object% a e co#'licated enoug, to de%e ve a co#'lete c,a'te * %o

we will devote t,i% c,a'te to di%cu%%ing %o#e o5 t,e ' o'e tie% and #et,od% o5 t,e A''lication object it%el5* along wit, %o#e o5 it% %i#'le c,ild en. $igu e 16;1 %,ow% t,e A''lication object* w,ic, %it% ato' t,e E6cel object #odel and e' e%ent% E6cel it%el5* and it% c,ild en. Eac, object i% ' eceded b( an icon t,at indicate% w,et,e it i% a collection object ?t,e little E1F ba%-et@ o a noncollection object ?t,e little oval@.
E1F

This 8ig%re an( others li2e it +as ta2en 8rom a program calle( 6b*ect 3o(el !ro+ser. 7or more on this) please chec2 o%t m' +eb site at http:JJ+++.romanpress.com.

Figure 15-1. he Excel %!!lication object an# its chil#ren ;the tag DvXE &eans that the object is new in version X o4 Excel< $igu e 16;2 %,ow% all c,ild en o5 t,e A''lication object* including t,o%e t,at a e #a -ed a% ,idden in t,e E6cel object #odel. 0,e%e latte object% a e #a -ed wit, an D t, oug, t,e icon. 0,e object% in $igu e 16;2 t,at a e #a -ed ?O55ice 2.2@ actuall( belong to t,e "ic o%o5t O55ice object #odel* but a e included ,e e becau%e t,e( a e acce%%ible 5 o# t,e E6cel object #odel and a e %o#eti#e% u%ed w,en ' og a##ing t,e E6cel #odel. 0,e e i% al%o one object t,at belong% to t,e =i%ual >a%ic E6ten%ibilit( #odel. /t i% #a -ed a% ?=>/)E 5.3@. Figure 15-2. he Excel %!!lication object along with its hi##en chil#ren

$8%$ Properties and 7ethods of the Application Ob1ect


0,e A''lication object ,a% a w,o''ing 268 ' o'e tie% and #et,od%* %,own in 0able 16;1. able 15-1. %!!lication object &e&bers )i%'la(Recent$ile% On)ouble!lic)i%'la(1c oll>a % OnEnt ( )i%'la(1tatu%>a OnRe( )ouble!licOnRe'eat )u##(1 On1,eetActivate )u##(10 On1,eet)eactivate
A2B

I)e5ault IEvaluate I$ind$ileSv9Z IRun2 I4aitSv9Z I41$unction

Activate"ic o%o5tA'' )u##(101Sv9Z On0i#e Active!ell )u##(11 On2ndo Active!,a t )u##(12Sv9Z On4indow Active)ialog )u##(13Sv10Z O'e ating1(%te# Active"enu>a )u##(14Sv10Z O ganiGation+a#e Active& inte )u##(2 &a ent Active1,eet )u##(3 &at, Active4indow )u##(4 &at,1e'a ato Active4o -boo)u##(5 &ivot0able1election Add!,a tAuto$o #at )u##(6 & eviou%1election% Add!u%to#3i%t )u##(7 & oduct!odeSv9Z Add/n% )u##(8 & o#'t$o 1u##a (/n5o Ale t>e5o eOve w iting )u##(9 Ouit Alt1ta tu'&at, Edit)i ectl(/n!ell Range An%we 4iGa dSv9Z EnableAni#ation% Read(Sv10Z A''lication EnableAuto!o#'lete Recent$ile% A%-0o2'date3in-% Enable!ancelRe( Reco d"ac o A%%i%tant EnableEvent% Reco dRelative Auto!o ect Enable1ound Re5e ence1t(le Auto$o #atA%Lou0('eRe'laceA('e lin-%Sv10Z Enable0i'4iGa d Regi%te ed$unction% Auto#ation1ecu it(Sv10Z E o !,ec-ingO'tion%Sv10Z Regi%te D33 Auto&e centEnt (Sv9Z Evaluate Re'eat AutoRecove Sv10Z E6cel4/ntl"ac o1,eet% Re'lace$o #atSv10Z >uild E6cel4"ac o1,eet% Re%et0i'4iGa d !alculate E6ecuteE6cel4"ac o Roll[oo# !alculate>e5o e1ave E6tend3i%tSv9Z Row% !alculate$ullSv9Z $eatu e/n%tallSv9Z R0)Sv10Z !alculate$ullRebuildSv10Z $ile!onve te % Run !alculation $ile)ialogSv10Z 1ave !alculation/nte u'tRe(Sv10Z $ile$ind 1ave4o -%'ace !alculation1tateSv10Z $ile1ea c, 1c een2'dating !alculation=e %ionSv9Z $ind$ile 1election !alle $ind$o #atSv10Z 1endRe(% !an&la(1ound% $i6ed)eci#al 1et)e5ault!,a t !anReco d1ound% $i6ed)eci#al&lace% 1,eet% !a'tion .ene ate.et&ivot)ataSv10Z 1,eet%/n+ew4o -boo!ell) agAnd) o' .et!u%to#3i%t!ontent% 1,o tcut"enu% !ell% .et!u%to#3i%t+u# 1,ow!,a t0i'+a#e% !enti#ete %0o&oint% .etO'en$ilena#e 1,ow!,a t0i'=alue% !,a t% .et&,oneticSv9Z 1,ow1ta tu')ialogSv10Z !,ec-Abo tSv10Z .et1aveA%$ilena#e 1,ow0ool0i'% !,ec-1'elling .oto 1,ow4indow%/n0a%-ba Sv9Z !li'boa d$o #at% Aeig,t 1#a t0agRecogniGe %Sv10Z !olo >utton% Ael' 1'eec,Sv10Z !olu#n% Ain%tanceSv10Z 1'ellingO'tion%Sv10Z !O"Add/n%Sv9Z AwndSv10Z 1tanda d$ont !o##and>a % !o##and2nde line% !on%t ain+u#e ic /gno eRe#oteRe7ue%t% /nc,e%0o&oint% /n'ut>o6 1tanda d$ont1iGe 1ta tu'&at, 1tatu%>a

!ont ol!,a acte % !onve t$o #ula !o'(Object%4it,!ell% ! eato !u %o !u %o "ove#ent !u%to#3i%t!ount !ut!o'("ode )ataEnt ("ode ))EA''Retu n!ode ))EE6ecute ))E/nitiate ))E&o-e ))ERe7ue%t ))E0e #inate )eci#al1e'a ato Sv10Z )e5ault$ile&at, )e5ault1ave$o #at )e5ault1,eet)i ection )e5ault4ebO'tion%Sv9Z )elete!,a tAuto$o #at )elete!u%to#3i%t )ialog% )ialog1,eet% )i%'la(Ale t% )i%'la(!li'boa d4indow )i%'la(!o##ent/ndicato )i%'la(E6cel4"enu% )i%'la($o #ula>a )i%'la($ull1c een )i%'la($unction0ool0i'%Sv10Z )i%'la(/n5o4indow )i%'la(/n%e tO'tion%Sv10Z )i%'la(+ote/ndicato )i%'la(&a%teO'tion%Sv10Z
E2F

/nte active /nte national /nte %ect /te ation 3anguage1etting%Sv9Z 3a ge>utton% 3e5t 3ib a (&at, "ac oO'tion% "ail3ogo55 "ail3ogon "ail1e%%ion "ail1(%te# "a'&a'e 1iGeSv10Z "at,!o' oce%%o Available "a6!,ange "a6/te ation% "e#o ($ ee "e#o (0otal "e#o (2%ed "enu>a % "odule% "ou%eAvailable "oveA5te Retu n "oveA5te Retu n)i ection +a#e +a#e% +etwo -0e#'late%&at, +ew4o -boo-Sv10Z +e6t3ette O)>!E o % O)>!0i#eout O3E)>E o %Sv9Z On!alculate On)ata

0e#'late%&at, 0,i%!ellSv10Z 0,i%4o -boo0,ou%and%1e'a ato Sv10Z 0oolba % 0o' 0 an%ition"enuRe( 0 an%ition"enuRe(Action 0 an%ition+avigRe(% 2/3anguage 2ndo 2nion 2%ableAeig,t 2%able4idt, 2%edObject%Sv10Z 2%e !ont ol 2%e 3ib a (&at,Sv9Z 2%e +a#e 2%e1(%te#1e'a ato %Sv10Z =alue =>E =e %ion =i%ible =olatile 4ait 4atc,e%Sv10Z 4idt, 4indow% 4indow%$o &en% 4indow1tate 4o -boo-% 4o -%,eet$unction 4o -%,eet%

GgI in(icates a global member.

O5 cou %e* t,e e a e 5a too #an( #e#be % to di%cu%% even t,e #ajo it( in a non e5e ence boo-* %o we will 'ic- out a 5ew o5 t,e #o e inte e%ting and u%e5ul #e#be %. 0,e i#'o tant 'oint i% t,at (ou can u%e 0able 16;1 to 5ind a #e#be t,at %uit% a 'a ticula 'u 'o%e and t,en c,ec- t,e E6cel ,el' 5ile% 5o #o e in5o #ation i5 it i% not cove ed in t,i% boo-. 4e will al%o di%cu%% additional ' o'e tie% and #et,od% o5 t,e A''lication object t, oug,out t,e e#ainde o5 t,e boo-* ,o'e5ull( at ti#e% w,en t,e di%cu%%ion will be #o e elevant. /n t,e ,o'e o5 #a-ing ou di%cu%%ion a bit #o e %t uctu ed* we will t ( to b ea- t,e #e#be % in 0able 16;1 into %e'a ate g ou'%. +ote* ,oweve * t,at t,i% i% in #an( ca%e% a bit a bit a (.

15.1.1 -e&bers that ,eturn +hil#ren


"an( o5 t,e #e#be % o5 t,e A''lication object a e de%igned %i#'l( to gain acce%% to a c,ild

object o5 t,e A''lication object. $o in%tance* t,e 4o -boo-% ' o'e t( %i#'l( etu n% t,e 4o -boo-% collection object* w,ic, e' e%ent% all o5 t,e cu entl( o'en 4o -boo- object% ?i.e.* wo -boo-%@. 4e will di%cu%% #an( o5 t,e%e object% at t,e ' o'e ti#e* but it i% wo t, ta-ing a looat t,e #e#be % t,at etu n t,e%e object% now. 0able 16;2 %,ow% t,e 48 #e#be % o5 t,e A''lication object t,at etu n c,ild object%. able 15-2. -e&bers that return chil# objects Name ReturnType Active!ell Range Active!,a t !,a t Active)ialog )ialog1,eet Active"enu>a "enu>a Active4indow 4indow Active4o -boo4o -booAdd/n% Add/n% A''lication A''lication Auto!o ect Auto!o ect AutoRecove AutoRecove !ell% Range !,a t% 1,eet% !olu#n% Range )e5ault4ebO'tion% )e5ault4ebO'tion% )ialog% )ialog% )ialog1,eet% 1,eet% E o !,ec-ingO'tion% E o !,ec-ingO'tion% E6cel4/ntl"ac o1,eet% 1,eet% E6cel4"ac o1,eet% 1,eet% $ind$o #at !ell$o #at /nte %ect Range "enu>a % "enu>a % "odule% "odule% +a#e% +a#e% +e6t3ette 4o -booO)>!E o % O)>!E o % O3E)>E o % O3E)>E o % &a ent A''lication Range Range Recent$ile% Recent$ile%

Re'lace$o #at Row% R0) 1,eet% 1,o tcut"enu% 1#a t0agRecogniGe % 1'eec, 1'ellingO'tion% 0,i%!ell 0,i%4o -boo0oolba % 2nion 2%edObject% 4atc,e% 4indow% 4o -boo-% 4o -%,eet$unction 4o -%,eet%

!ell$o #at Range R0) 1,eet% "enu 1#a t0agRecogniGe % 1'eec, 1'ellingO'tion% Range 4o -boo0oolba % Range 2%edObject% 4atc,e% 4indow% 4o -boo-% 4o -%,eet$unction 1,eet%

0,e e a e %o#e 'oint% wo t, noting in 0able 16;2. $i %t* t,e e a e %eve al #e#be % t,at begin wit, t,e wo d 8Active.8 /t %,ould co#e a% no %u ' i%e t,at t,e%e #e#be % etu n t,e co e%'onding cu entl( active object. $o in%tance* t,e Active1,eet #e#be etu n% t,e cu entl( active wo -%,eet o c,a t* de'ending u'on w,ic, i% active at t,e ti#e. ?+ote t,at t,e e i% no 1,eet object. 1,eet% a e eit,e wo -%,eet% o %tand;alone c,a t%. 4e will di%cu%% t,i% i%%ue in detail in !,a'te 18.@ Ob%e ve al%o t,at o5ten t,e na#e o5 a #e#be i% t,e %a#e a% t,e na#e o5 t,e object t,at t,e #e#be etu n%. $o in%tance* t,e Add/n% ' o'e t( etu n% t,e Add/n% collection* t,e A''lication ' o'e t( etu n% t,e A''lication object* and t,e 4indow% ' o'e t( etu n% t,e 4indow% collection. 0,e notable e6ce'tion% to t,i% ule a e:
C

0,e 0,i%4o ->oo- ' o'e t( etu n% t,e 4o -boo- object containing t,e cu entl( unning code. One u%e o5 t,i% ' o'e t( i% in dete #ining t,e location ?co#'lete 'at, and 5ilena#e@ o5 t,e wo -boo- on t,e u%e B% co#'ute * w,ic, i% done b( w iting:
ThisHor8boo8.>0ll9a5e

1eve al object ' o'e tie%* %uc, a% !ell%* !olu#n%* and Row%* etu n a Range object. 0,i% i% becau%e t,e e a e no !ell* !olu#n* o Row object% in t,e E6cel object #odel. /n%tead* eac, o5 t,e%e 8object%8 i% actuall( a Range object. ?/ncidentall(* a %i#ila t,ing ,a''en% in t,e 4o d object #odel. /n 'a ticula * t,e e a e no !,a acte * 4o d* o 1entence object%. Rat,e * t,e%e a e Range object% in t,e 4o d object #odel a% well.@

15.1.2 -e&bers that %44ect the (is!lay


0,e e a e %eve al #e#be % t,at a55ect t,e di%'la( o5 ce tain ite#%: "isplayAlerts property F,GW BooleanH

4,en Tr0e* E6cel di%'la(% va iou% wa ning #e%%age% ?%uc, a% a con5i #ation #e%%age t,at ' ecede% t,e deletion o5 a wo -%,eet@ w,ile a #ac o i% unning. /5 (ou do not want a #ac o to be di%tu bed* t,en %et t,i% to >alse:
A33lication.Dis3la Alerts = >alse

0,e de5ault value o5 t,i% ' o'e t( i% Tr0e. "isplay(omment ndicator property F,GW LongH 0,i% ' o'e t( a55ect% t,e wa( t,at E6cel indicate% t,e ' e%ence o5 a co##ent in an un%elected cell. /t can be an( one o5 t,e con%tant% in t,e 5ollowing enu#:
'n05 #lCo55entDis3la Mode xlCo55ent2ndicator*nl = %1 onl xl9o2ndicator = 7 xlCo55entAnd2ndicator = 1 and co55ent 'nd 'n05 . Dis3la . Dis3la . Dis3la . itsel6 indicator indicator

1etting )i%'la(!o##ent/ndicato to eit,e xlCo55ent2ndicator*nl o xlCo55entAnd2ndicator %et% t,e value o5 t,e )i%'la(+ote/ndicato ' o'e t( ?de%c ibed late in t,i% %ection@ to Tr0e* w,ile %etting )i%'la(!o##ent/ndicato to xl9o2ndicator c,ange% )i%'la(+ote/ndicato to >alse. "isplay&ormulaBar property F,GW BooleanH 0,i% ' o'e t( dete #ine% w,et,e t,e 5o #ula ba i% di%'la(ed. /t% de5ault value i% Tr0e. "isplay&ull/creen property F,GW BooleanH 0,i% ' o'e t( dete #ine% w,et,e E6cel i% in 5ull;%c een #ode. ?+ote t,at di%'la(ing E6cel in 5ull;%c een #ode i% not t,e %a#e a% #a6i#iGing E6celB% a''lication window.@ "isplay?ote ndicator property F,GW BooleanH /5 t,i% ' o'e t( i% Tr0e ?it% de5ault value@ t,en cell% containing note% di%'la( cell ti'% and contain note indicato % ?w,ic, a e %#all dot% in t,e u''e ; ig,t co ne o5 a cell@. 1etting )i%'la(+ote/ndicato to >alse al%o %et% )i%'la(!o##ent/ndicato to xl9o2ndicator* w,ile %etting )i%'la(+ote/ndicato to Tr0e %et% )i%'la(!o##ent/ndicato to xlCo55ent2ndicator*nl .

15.1." -e&bers that Enable Excel Features


1eve al A''lication #e#be % enable o di%able ce tain E6cel 5eatu e%: Auto&ormatAsIou!ype,eplaceJyperlin)s property F,GW BooleanH 1et to Tr0e to ,ave E6cel auto#aticall( 5o #at ,('e lin- te6t a% a ,('e lin-. 1et to >alse to tu n o55 t,i% o5ten;anno(ing 5eatu e o5 E6cel. EnableAnimations property F,GW BooleanH

0,i% ' o'e t( dete #ine% w,et,e ani#ated in%e tion and deletion i% enabled. 4,en ani#ation i% enabled* in%e ted wo -%,eet ow% and colu#n% a''ea %lowl( and deleted wo -%,eet ow% and colu#n% di%a''ea %lowl(. 0,e de5ault value i% >alse. EnableAuto(omplete property F,GW BooleanH 0,i% ' o'e t( dete #ine% w,et,e E6celB% Auto!o#'lete 5eatu e i% enabledH it% de5ault value i% Tr0e. Enable(ancelKey property F,GW LongH 0,i% ' o'e t( cont ol% ,ow E6cel ,andle% t,e !t l ;> ea- o E%c -e( co#bination% du ing a unning ' ocedu e. /t can be one o5 t,e 5ollowing #l'nableCancelPe con%tant%:
'n05 #l'nableCancelPe xlDisabled = 7 xl2nterr03t = 1 xl'rror?andler = 2 'nd 'n05

0,e #eaning% o5 t,e%e con%tant% 5ollow: xlDisabled 0 a''ing i% di%abled ?t,e -e(%t o-e% a e igno ed@. xl2nterr03t 0,e unning ' ocedu e i% inte u'ted b( t,e di%'la( o5 a dialog bo6 t,at enable% t,e u%e to eit,e debug o end t,e ' ocedu e. 0,i% i% t,e de5ault value. xl'rror?andler 0,e -e(%t o-e inte u't i% %ent to t,e unning ' ocedu e a% an e o t,at i% t a''able b( an e o ,andle u%ing t,e *n 'rror :oTo %tate#ent. 0,e e o code i% 18. +ote t,at t,i% ' o'e t( can be dange ou% and %,ould be u%ed wit, g eat ci cu#%'ection. /n 'a ticula * i5 (ou %et t,e ' o'e t( to xlDisabled* t,en t,e e i% no wa( to inte u't an in5inite loo'. 1i#ila l(* i5 (ou %et t,e ' o'e t( to xl'rror?andler but (ou e o ,andle etu n% u%ing t,e "es05e %tate#ent* t,e e i% no wa( to %to' non%el5;te #inating code. $o t,e%e ea%on%* E6cel alwa(% e%et% t,e Enable!ancelRe( ' o'e t( to xl2nterr03t w,eneve E6cel etu n% to t,e idle %tate and t,e e i% no code unning. EnableE#ents property F,GW BooleanH 0,i% ' o'e t( i% Tr0e ?it% de5ault value@ i5 event% a e enabled 5o t,e A''lication object. ?$o #o e on t,i%* %ee !,a'te 11.@ Enable/ound property F,GW BooleanH 0,i% ' o'e t( enable% and ?#e ci5ull(@ di%able% %ound% 5o "ic o%o5t O55ice. 0,e de5ault value i% >alse.

15.1.$ Event-,elate# -e&bers


/t i% 'o%%ible to a%%ign #ac o% to ce tain event%. ?0,e%e a e %'ecial event%<not t,e event% t,at we di%cu%%ed in !,a'te 11.@ $o in%tance* we can a%%ign a #ac o to 'la( w,eneve a 'a ticula -e( i% ' e%%ed. 0,i% i% done b( invo-ing t,e OnRe( #et,od 5o t,e A''lication object. 3et u% de%c ibe two o5 t,e #o e u%e5ul event% t,at can be a%%igned a #ac o. 15.1.$.1 3n@ey &etho# 0,e %(nta6 5o t,e OnRe( #et,od i%:
A33lication.*nPe (5ey= Procedure)

w,e e 5ey i% t,e -e( o -e( co#bination ?w itten a% a %t ing@ t,at will e6ecute t,e #ac o and Procedure i% t,e na#e o5 t,at #ac o. +ote t,at we can alte t,e no #al be,avio o5 E6cel b( a%%igning a -e( co#bination to t,e 5ey 'a a#ete t,at ,a% a no #al E6cel e%'on%e ?%uc, a% !t l;1 5o %ave@. /5 we a%%ign an e#'t( %t ing to t,e Procedure 'a a#ete * t,en E6cel will o#it it% no #al e%'on%e ?%o not,ing will ,a''en@. /5 we o#it t,e Procedure 'a a#ete * t,en E6cel will etu n t,e -e( co#bination to it% no #al 5unction. 0o illu%t ate* t,e 5ollowing code will di%able t,e !t l;o -e( co#bination* w,ic, no #all( di%'la(% t,e O'en dialog bo6:
A33lication.*nPe "Wo"=""

0,e 5ollowing code etu n% t,e !t l;o -e( co#bination to it% no #al E6cel 5unction:
A33lication.*nPe "Wo"

0,e 5ey a gu#ent can %'eci5( a %ingle -e( o an( -e( co#bined wit, one o #o e o5 Alt* !t l* o 1,i5t. +o #al al',anu#e ic -e(% a e denoted b( t,e#%elve%* a% in 8a*8 8A*8 81.8 0able 16;3 %,ow% ,ow to ente %'ecial -e(%. $o in%tance* t,e $2 -e( i% denoted b( 8`$2a8* and t,e Ente -e( i% denoted eit,e b( 8`E+0ERa8 o 8b8. able 15-". S!ecial @eys 4or the @ey Para&eter +ey Co&e >ac-%'ace `>A!R1&A!Ea o `>1a > ea`>REARa !a'% 3oc`!A&13O!Ra !lea `!3EARa )elete o )el `)E3E0Ea o `)E3a )own A ow `)O4+a End `E+)a Ente ?nu#e ic -e('ad@ `E+0ERa Ente b ?tilde@ E%c `E1!A&Ea o `E1!a Ael' `AE3&a Ao#e `AO"Ea /n% `/+1ER0a

3e5t A ow +u# 3oc&age )own &age 2' Retu n Rig,t A ow 1c oll 3oc0ab 2' A ow $1 t, oug, $15

`3E$0a `+2"3O!Ra `&.)+a `&.2&a `RE02R+a `R/.A0a `1!RO333O!Ra `0A>a `2&a `$1a t, oug, `$15a

0o co#bine -e(% wit, 1,i5t* !t l* o Alt* u%e t,e 5ollowing ' e5i6e%: 1,i5t !t l Alt F ?'lu% %ign@ Y ?ca et@ J ?'e cent %ign@

$o in%tance* to denote t,e Alt;$2 -e( co#bination* w ite 8J`$2a8. 0o denote !t l;1,i5t;Ente * w ite 8YF`E+0ERa8. /n o de to u%e one o5 t,e c,a acte % F* Y* J* `* a* o b wit,out ,aving it inte ' eted a% a %'ecial -e(* %i#'l( enclo%e t,e c,a acte in b ace%. $o in%tance* to ea%%ign t,e ` -e(* we would a%%ign t,e Re( 'a a#ete to 8``a8. 15.1.$.2 3n i&e &etho# 0,i% #et,od i% u%ed to un a ' ocedu e at a %'eci5ic ti#e o a5te a %'eci5ic a#ount o5 ti#e ,a% 'a%%ed. 0,e %(nta6 i%:
A33lication.*nTi5e(,arliestTime= Procedure= "atestTime= Schedule)

O5 cou %e* t,e Procedure 'a a#ete i% t,e na#e o5 t,e #ac o to un. 0,e ,arliestTime 'a a#ete i% t,e ti#e (ou want t,e #ac o to be un. 0o %'eci5( a ti#e* we u%e t,e Ti5e+al0e 5unction. $o in%tance* t,e 5ollowing code e6ecute% t,e #ac o test in t,e ThisHor8boo8 code #odule o5 t,e boo). wo -boo- at 3:58 &.".:
A33lication.*nTi5e Ti5e+al0e("B-&D ;M")= G "d-IexcelIboo81.xls!ThisHor8boo8.test" "atestTime i% an o'tional 'a a#ete t,at %'eci5ie% t,e late%t ti#e at w,ic, t,e ' ocedu e can begin unning. 4e can u%e t,e Ti5e+al0e 5unction to %'eci5( a ti#e 5o t,i% 'a a#ete * o we can %et "atestTime to ,arliestTime 'lu% %o#e additional ti#e. $o in%tance* t,e 5ollowing code e7ui e% t,at E6cel un t,e #ac o no late t,an 30 %econd% 5ollowing 3:58 &.".: A33lication.*nTi5e Ti5e+al0e("B-&D ;M")= G "d-IexcelIboo81.xls!ThisHor8boo8.test"= G Ti5e+al0e("B-&D ;M") A B7

0,e "atestTime 'a a#ete #a( be u%e5ul* %ince i5 E6cel i% bu%( ? unning anot,e ' ocedu e* 5o in%tance@* t,en e6ecution o5 t,e #ac o denoted b( Procedure will be dela(ed. /5 (ou do not want t,e #ac o to be un a5te a ce tain ti#e* t,en %et t,e "atestTime 'a a#ete .

/5 (ou want to clea a ' eviou%l( %et On0i#e #ac o* (ou can call t,e ' ocedu e wit, t,e Schedule 'a a#ete %et to >alse. Ot,e wi%e* t,e 'a a#ete can be o#itted* %ince it% de5ault value i% Tr0e. +ote t,at t,e 9ow 5unction etu n% t,e cu ent ti#e. 0,u%* to %c,edule a #ac o 5o a ce tain a#ount o5 ti#e 5 o# t,e ' e%ent* we can %et ,arliestTime to:
9ow A Ti5e+al0e(time)

15.1.' +alculation-,elate# -e&bers


0,e A''lication object ,a% %eve al #e#be % elated to calculation. 15.1.'.1 +alculate &etho# 0,i% #et,od calculate% all o'en wo -boo-%* a %'eci5ic wo -%,eet in a wo -boo-* o a %'eci5ied ange o5 cell% on a wo -%,eet* de'ending u'on ,ow it i% a''lied. 4,en a''lied to t,e A''lication object* a% in:
A33lication.Calc0late

E6cel will calculate all o'en wo -boo-%. 4,en a''lied to a %'eci5ic wo -%,eet* a% in:
Hor8sheets(1).Calc0late

E6cel will calculate t,at wo -%,eet. 4,en a''lied to a %'eci5ic ange* a% in:
Hor8sheets(1)."ows(2).Calc0late

E6cel will calculate t,e cell% in t,at ange. +ote t,at %ince !alculate i% a global #et,od* we can %i#'l( w ite:
Calc0late

in 'lace o5:
A33lication.Calc0late

15.1.'.2 +alculateFull,ebuil# &etho# 0,i% #et,od calculate% all data and ebuild% all de'endencie% ?5o #ula% t,at e5e to ot,e cell%@ in all o'en wo -boo-%. 0,i% #et,od a''lie% onl( to t,e A''lication object. 15.1.'." +alculation !ro!erty ;,H0 )ong< 0,i% ' o'e t( %et% E6celB% calculation #ode and can be %et to an( o5 t,e 5ollowing con%tant%:
'n05 #lCalc0lation xlCalc0lationMan0al = %L1B& xlCalc0lationA0to5atic = %L17& xlCalc0lationSe5ia0to5atic = 2 'nd 'n05

0,e de5ault value i% xlCalc0lationA0to5atic. A% i% t('ical* t,e docu#entation doe% not e6'lain t,e te # %e#iauto#atic ?at lea%t / could not 5ind an e6'lanation@. Aoweve * t,e e i% an o'tion in E6celB% !alculation tab unde t,e O'tion% dialog t,at allow% u% to %'eci5( auto#atic calculation e6ce't 5o data table%H t,i% i% w,at i% #eant b( %e#iauto#atic. 15.1.'.$ +alculate.e4oreSave !ro!erty ;,H0 .oolean< 0,i% ' o'e t( i% Tr0e i5 wo -boo-% a e calculated be5o e t,e( a e %aved to di%-. 0,i% i% elevant onl( w,en t,e !alculation ' o'e t( i% %et to xlMan0al. 15.1.'.' +hec1%bort &etho# 0,i% #et,od %to'% ecalculation in E6cel. /t% %(nta6 i%:
A33lication.Chec8Abort(kee abortran!e)

w,e e kee abortran!e i% a ange t,at i% e6e#'t 5 o# t,e #et,odH t,at i%* ecalcualtion %till ta-e% 'lace in t,i% ange.

15.1.5 File-,elate# -e&bers


3et u% ta-e a b ie5 loo- at t,e #e#be % t,at a e elated to 5ile o'e ation%. 15.1.5.1 (e4aultFilePath !ro!erty ;,H0 String< 0,i% ' o'e t( etu n% o %et% t,e de5ault 'at, t,at "ic o%o5t E6cel u%e% w,en it o'en% o %ave% 5ile%. 0,i% %etting can al%o be c,anged b( t,e u%e in t,e .ene al tab o5 t,e O'tion% dialog. 15.1.5.2 (e4aultSaveFor&at !ro!erty ;,H0 )ong< 0,i% ' o'e t( etu n% o %et% t,e de5ault 5o #at 5o %aving 5ile%. 0,e de5ault 5o t,i% ' o'e t( i% xlHor8boo89or5al* indicating t,e no #al wo -boo- 5o #at 5o t,e cu ent ve %ion o5 E6cel. 0,e 'o%%ible value% 5o t,i% ' o'e t( a e t,e #L>ile>or5at con%tant% %,own in 0able 16;4. able 15-$. X)FileFor&at constants 6lAdd/n ?18@ 6lE6cel5 ?39@ 6l4N3 ?40@ 6l!1= ?6@ 6lE6cel7 ?39@ 6l4N3$N3 ?41@ 6l!1="ac ?22@ 6lE6cel9795 ?43@ 6l4R1 ?5@ 6l!1="1)O1 ?24@ 6lAt#l ?44@Sv9Z 6l4R1A33 ?31@ 6l!1=4indow% ?23@ 6l/ntlAdd/n ?26@ 6l4R1$"0 ?30@ 6l!u ent&lat5o #0e6t ?;4158@ 6l/ntl"ac o ?25@ 6l4R3 ?15@ 6l)>$2 ?7@ 6l1L3R ?2@ 6l4R3$"3 ?32@ 6l)>$3 ?8@ 6l0e#'late ?17@ 6l4R4 ?38@ 6l)>$4 ?11@ 6l0e6t"ac ?19@ 6l4R1 ?4@ 6l)/$ ?9@ 6l0e6t"1)O1 ?21@ 6l4o -boo-+o #al ?;4143@ 6lE6cel2 ?16@ 6l0e6t& inte ?36@ 6l4o -%2$a Ea%t ?28@ 6lE6cel2$a Ea%t ?27@ 6l0e6t4indow% ?20@ 6l4O1 ?34@ 6lE6cel3 ?29@ 6l2nicode0e6t ?42@Sv9Z 6lD"31' ead%,eet ?46@Sv10Z 6lE6cel4 ?33@ 6l4ebA c,ive ?45@Sv10Z 6lE6cel44o -boo- ?35@ 6l4N24)1 ?14@

15.1.5." File(ialog !ro!erty 0,i% ' o'e t( ' og a##aticall( o'en% a 5ile; elated dialog bo6 and etu n% a $ile)ialog object. /t ta-e% a% 'a a#ete one o5 t,e 5ollowing con%tant% to indicate t,e t('e o5 dialog:
5so>ileDialo4>ile;ic8er 5so>ileDialo4>older;ic8er 5so>ileDialo4*3en 5so>ileDialo4SaveAs

0o illu%t ate* t,e 5ollowing code:


. *3en dialo4 Hith A33lication.>ileDialo4(5so>ileDialo4>older;ic8er) .Show . Dis3la 3ath Ms41ox .Selected2te5s(1) 'nd Hith

allow% t,e u%e to %elect a 5olde and t,en di%'la(% t,e 5olde B% co#'lete 'at,. 15.1.5.$ &etho# w,o%e 0,i% %(nta6 Fin#File #et,od* i%:

A33lication.>ind>ile

di%'la(% t,e O'en dialog bo6. /5 a 5ile i% o'ened %ucce%%5ull( b( t,e u%e * t,e #et,od etu n% Tr0e. /5 t,e u%e cancel% t,e dialog bo6* t,e #et,od etu n% >alse. 15.1.5.' :et3!enFilena&e &etho# 0,i% #et,od di%'la(% t,e O'en dialog bo6 and get% a 5ilena#e o 5ilena#e% 5 o# t,e u%e but doe% not o'en t,e 5ile%. /t% %(nta6 i%:
A33lication.:et*3en>ilena5e(FileFilter= G Filter1ndex= Title= )uttonText= (ultiSelect)

0,e o'tional FileFilter 'a a#ete i% a %t ing t,at %'eci5ie% w,at to 'ut in t,e 8$ile% o5 t('e8 d o';down li%t bo6 in t,e O'en dialog. /n ot,e wo d%* it %'eci5ie% 5ile 5ilte ing c ite ia. 0,i% %t ing i% in two 'a t%* o5 t,e 5o #:
descri3tion= 6ile6ilter

0,e 5i %t 'a t i% t,e de%c i'tion o5 t,e 5ile t('e* and t,e %econd 'a t i% t,e "1;)O1 wildca d 5ile 5ilte %'eci5ication. 0,e two 'a t% a e %e'a ated b( a co##a. +ote t,at t,e 5i %t 'a t i% t,e %t ing t,at a''ea % in t,e 8$ile% o5 t('e8 d o';down bo6 in t,e O'en dialog bo6. 0,u%* t,e 5i %t 'a t al%o include% t,e wildca d 5ile;5ilte %'eci5ication. &e ,a'% a 5ew e6a#'le% will ,el' cla i5(: !ext files Text >iles (R.txt)=R.txt Lotus files Lot0s 1%2%B (R.w8Q)= R.w8Q Add- n files Add%2n >iles (R.xla)=R.xla

/t i% al%o 'o%%ible to u%e #ulti'le wildca d 5ile 5ilte %* a% in:


1ac803 >iles (R.xl8) R.ba8)= R.xl8) R.ba8

?+ote t,e %e#icolon%.@ /5 t,e FileFilter a gu#ent i% o#itted* t,e de5ault i%:
All >iles (R.R)=R.R

+ote t,at FileFilter can con%i%t o5 #o e t,an one 5ilte %'eci5ication* %e'a ated b( co##a%* a% in:
Deb04.;rint A33lication.:et*3en>ilena5e( G "Text >iles (R.txt)=R.txt= G 1ac803 >iles (R.xl8) R.ba8)= R.xl8) R.ba8")

/n t,i% ca%e* t,e o'tional Filter1ndex 'a a#ete %'eci5ie% w,ic, o5 t,e 5ilte % a''ea % in t,e 8$ile% o5 t('e8 d o';down li%t bo6. $o in%tance* t,e 5ollowing will cau%e t,e %econd 5ilte ?bac-u' 5ile%@ to a''ea in t,e 8$ile% o5 t('e8 d o';down li%t bo6:
Deb04.;rint A33lication.:et*3en>ilena5e( G "Text >iles (R.txt)=R.txt= G 1ac803 >iles (R.xl8) R.ba8)= R.xl8) R.ba8"= 2)

0,e o'tional Title 'a a#ete %'eci5ie% t,e title o5 t,e dialog bo6. /5 t,i% a gu#ent i% o#itted* t,e title i% O'en. 0,e )uttonText 'a a#ete i% igno ed b( 4indow%* but u%ed on t,e "acinto%,. 0,e o'tional (ultiSelect ' o'e t( i% %et to Tr0e to allow #ulti'le 5ilena#e% to be %elected and >alse to allow onl( one 5ilena#e to be %elected. 0,e de5ault value i% >alse. 0o %elect #ulti'le 5ile% 5 o# t,e O'en dialog* t,e u%e #u%t ,old down t,e !t l o 1,i5t -e(. 0,e #et,od etu n% t,e %elected 5ilena#e o t,e na#e ente ed b( t,e u%e . 0,e etu ned na#e #a( al%o include a 'at, %'eci5ication. /5 t,e (ultiSelect 'a a#ete i% Tr0e* t,e etu n value i% an a a( o5 t,e %elected 5ilena#e% ?even i5 onl( one 5ilena#e i% %elected@. 0,e #et,od etu n% >alse i5 t,e u%e cancel% t,e dialog bo6. 4,en (ultiselect i% Tr0e* we can dete #ine t,e nu#be o5 5ile% %elected b( t,e u%e b( u%ing t,e <1o0nd 5unction to get t,e u''e bound 5o t,e etu ned a a(* a% in:
905>iles = <1o0nd(A33lication.:et*3en>ilena5e(M0ltiSelect-=Tr0e))

+ote 5inall( t,at t,i% #et,od #a( c,ange t,e cu ent d ive o 5olde . 15.1.5.5 :etSave%sFilena&e &etho# 0,i% #et,od i% %i#ila to t,e .etO'en$ilena#e #et,od* but in%tead di%'la(% t,e 1ave A% dialog bo6 and get% a 5ilena#e 5 o# t,e u%e wit,out %aving an( 5ile%. 0,e %(nta6 i%:
A33lication.:etSaveAs>ilena5e(1nitialFilename= G FileFilter= Filter1ndex= Title= )uttonText)

0,e o'tional 1nitialFilename 'a a#ete %'eci5ie% t,e 5ilena#e t,at i% 'laced in t,e 8$ile na#e8 te6t bo6 on t,e 1ave A% dialog. /5 t,i% a gu#ent i% o#itted* E6cel u%e% t,e na#e o5 t,e active wo -boo-. 0,e ot,e 'a a#ete % ?and etu n value%@ a e t,e %a#e a% 5o t,e .etO'en$ilena#e #et,od. A% wit, .etO'en$ilena#e* t,i% #et,od #a( c,ange t,e cu ent d ive o 5olde .

15.1.5.6 ,ecentFiles !ro!erty ;,ea#-3nly< 0,i% ' o'e t( etu n% a Recent$ile% collection t,at e' e%ent% t,e li%t o5 ecentl( u%ed 5ile%. 0,e e a e two inte e%ting a%'ect% to t,e Recent$ile% collection. $i %t* it ,a% a "a6i#u# ' o'e t( t,at etu n% o can be %et to t,e #a6i#u# nu#be o5 5ile% allowed in t,e ecentl( u%ed 5ile% li%t t,at a''ea % on E6celB% $ile #enu. 0,i% nu#be #u%t be an intege between 0 and 9* inclu%ive. 0,u%* t,e code:
Ms41ox A33lication."ecent>iles.Maxi505

di%'la(% t,e cu ent value. 1econd* we can ' int a li%t o5 t,e 5ilena#e% o5 t,e #o%t ecentl( u%ed 5ile% a% 5ollow% ?o5 cou %e* (ou #a( want to do #o e t,an ' int t,i% li%t@:
Di5 r6 As "ecent>ile >or 'ach r6 2n A33lication."ecent>iles Deb04.;rint r6.9a5e 9ext

+ote t,at t,e Recent$ile% collection contain% Recent$ile object%* and not %i#'l( t,e na#e% o5 t,e ecentl( u%ed 5ile%* a% one #ig,t e6'ect. 15.1.5.7 Save0or1s!ace &etho# 0,i% #et,od %ave% t,e cu ent wo -%'ace. /t% %(nta6 i%:
A33lication.SaveHor8s3ace(Filename)

w,e e Filename i% an o'tional 5ilena#e 5o t,e xlw 5ile.

15.1.6 -e&bers that %44ect the +urrent State o4 Excel 0,e 5ollowing #e#be % ,ave an e55ect on t,e cu ent %etting% o5 E6cel:
(opy$b%ectsWith(ells property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* object% ?%uc, a% e#bedded cont ol% o %,a'e%@ a e cut* co'ied* e6t acted* and %o ted along wit, cell%. (ursor property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e a''ea ance o5 t,e #ou%e 'ointe . /t can be one o5 t,e 5ollowing #lMo0se;ointer con%tant%:
'n05 #lMo0se;ointer xlDe6a0lt = %L1LB xl9orthwestArrow = 1 xlHait = 2 xl21ea5 = B 'nd 'n05

/t i% con%ide ed good ' og a##ing ' actice to %et t,e #ou%e 'ointe to xlHait i5 (ou code will ta-e #o e t,an a %econd o %o to co#'lete. O5 cou %e* (ou will need to etu n t,e #ou%e 'ointe to it% ' eviou% %tate w,en t,e ' ocedu e te #inate%. 0,e ' o'e wa( to

do t,i% i% to %ave t,e o iginal !u %o ' o'e t( value be5o e c,anging it* %o it can be e%et to it% o iginal value. (ut(opyMode property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e %tatu% o5 !ut o !o'( #ode. 0,e !ut!o'("ode ' o'e t( can be %et to eit,e Tr0e o >alse. On t,e &!* t,e%e ,ave t,e %a#e e55ect ?but di55e on t,e "acinto%,@H na#el(* to cancel !ut o !o'( #ode and e#ove t,e #oving bo de t,at %u ound% t,e egion to be cut o co'ied. 0,e !ut!o'("ode ' o'e t( can etu n >alse* indicating t,at E6cel i% in neit,e !ut no !o'( #ode* o el%e one o5 t,e two value% 5 o# t,e 5ollowing enu#:
'n05 #lC0tCo3 Mode xlCo3 = 1 xlC0t = 2 'nd 'n05 . Co3 5ode . C0t 5ode

"ataEntryMode property F,GW LongH 0,i% ' o'e t( etu n% o %et% )ata Ent ( #ode. 4,en in )ata Ent ( #ode* data can be ente ed onl( in t,e cell% in t,e cu entl( %elected ange. 0,e ' o'e t( can a%%u#e an( o5 t,e 5ollowing con%tant value%: xl*n )ata Ent ( #ode i% on. xl*66 )ata Ent ( #ode i% o55. xlStrict )ata Ent ( #ode i% on* and ' e%%ing E%c will not tu n it o55. Edit"irectly n(ell property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e ?w,ic, i% it% de5ault value@* E6cel allow% editing in cell%. Ot,e wi%e* it doe% not allow editing in t,e cell% ?but (ou can %till edit in t,e 5o #ula ba @. &ixed"ecimal property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* all nu#e ic data ente ed will be 5o #atted wit, t,e nu#be o5 5i6ed deci#al 'lace% %et b( t,e $i6ed)eci#al&lace% ' o'e t(. 0,e de5ault value o5 t,i% ' o'e t( i% >alseH t,e value o5 t,e $i6ed)eci#al&lace% ' o'e t( i% igno ed. &ixed"ecimalPlaces property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e nu#be o5 5i6ed deci#al 'lace% u%ed w,en t,e $i6ed)eci#al ' o'e t( i% %et to Tr0e. $o e6a#'le* i5 t,e $i6ed)eci#al& o'e t( i% Tr0e and $i6ed)eci#al&lace% i% %et to 3* an ent ( o5 100 in a cell will be di%'la(ed a% 0.1.

nteracti#e property F,GW BooleanH 4,en t,i% ' o'e t( i% %et to >alse* E6cel will bloc- all in'ut 5 o# t,e -e(boa d and #ou%e except 5o in'ut to dialog bo6e% t,at a e di%'la(ed b( code. 0,i% will ' event t,e u%e 5 o# inte 5e ing wit, t,e cu entl( unning #ac o. 0,e de5ault value o5 t,e /nte active ' o'e t( i% Tr0e. O5 cou %e* con%ide able ca e #u%t be ta-en wit, t,i% ' o'e t(. $o in%tance* i5 (ou 5o get to e%et t,e ' o'e t( to Tr0e* o i5 (ou code te #inate% une6'ectedl(* t,e u%e #a( need to e%ta t E6cel. +ote t,at t,e Alt;$4 -e( co#bination will wo - to %,ut down E6cel* but t,e u%e will not be able to %ave an( wo -. >e ca e5ul wit, t,i% oneC Mo#eAfter,eturn property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* t,e active cell will be #oved a% %oon a% t,e Ente -e( i% ' e%%ed. 0,e "oveA5te Retu n)i ection ' o'e t( i% u%ed to %'eci5( t,e di ection in w,ic, t,e active cell will be #oved. /5 %et to >alse* t,e active cell e#ain% unc,anged a5te t,e Ente -e( i% ' e%%ed. Mo#eAfter,eturn"irection property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e di ection in w,ic, t,e active cell i% #oved w,en t,e u%e ' e%%e% Ente i5 t,e "oveA5te Retu n ' o'e t( i% %et to Tr0e. /t can a%%u#e an( one o5 t,e 5ollowing value%:
'n05 #lDirection xl<3 = %L1N2 xlTo"i4ht = %L1N1 xlToLe6t = %L1&E xlDown = %L121 'nd 'n05

,eference/tyle property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e %t(le ?A1 %t(le o R1!1 %t(le@ in w,ic, E6cel di%'la(% cell e5e ence% and ow and colu#n ,eading%. /t can be one o5 t,e 5ollowing #l"e6erenceSt le con%tant%:
'n05 #l"e6erenceSt le xl"1C1 = %L1&7 xlA1 = 1 'nd 'n05

/creen'pdating property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* %c een u'dating i% tu ned on. 1ince t,i% #a( %low down %o#e di%'la(;inten%ive ' ocedu e% con%ide abl(* (ou #a( want to te#'o a il( tu n o55 %c een u'dating. /heets n?ewWor)boo) property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e nu#be o5 %,eet% t,at E6cel auto#aticall( in%e t% into new wo -boo-%. /how(hart!ip?ames property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* E6cel c,a t% %,ow c,a t ti' na#e%.

/how(hart!ipValues property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* E6cel c,a t% %,ow c,a t ti' value%. /how!ool!ips property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* 0ool0i'% a e tu ned on. /tandard&ont property F,GW /tringH 0,i% ' o'e t( etu n% o %et% t,e na#e o5 t,e %tanda d 5ont. +ote t,at t,e c,ange doe% not ta-e e55ect until E6cel i% e%ta ted. /tandard&ont/i7e property F,GW LongH 0,i% ' o'e t( etu n% o %et% t,e %tanda d 5ont %iGe* in 'oint%. 0,e c,ange doe% not ta-e e55ect until E6cel i% e%ta ted. /tartupPath property F,ead-$nly /tringH 0,i% ' o'e t( etu n% t,e co#'lete 'at, o5 t,e %ta tu' 5olde * e6cluding t,e 5inal %e'a ato . !emplatesPath property F,ead-$nly /tringH 0,i% ' o'e t( etu n% t,e 'at, w,e e te#'late% a e %to ed.

15.1.7 -e&bers that Pro#uce %ctions


1eve al #e#be % o5 t,e A''lication object 'e 5o # %o#e %o t o5 action. 15.1.7.1 +onvertFor&ula &etho# 0,i% #et,od conve t% cell e5e ence% in a 5o #ula between t,e A1 and R1!1 e5e ence %t(le%. /t can al%o conve t between elative and ab%olute e5e ence%. /t% %(nta6 i%:
A33lication.Convert>or50la(Formula= From%e*erenceStyle= To%e*erenceStyle= G To-bsolute= %elati+eTo)

0,e Formula 'a a#ete i% a %t ing containing t,e 5o #ula to conve t. /t #u%t be a valid 5o #ula* beginning wit, an e7ual %ign. 0,e From%e*erenceStyle 'a a#ete #u%t be one o5 t,e 5ollowing con%tant%:
'n05 #l"e6erenceSt le xl"1C1 = %L1&7 xlA1 = 1 'nd 'n05

0,e o'tional To%e*erenceStyle 'a a#ete i% t,e e5e ence %t(le into w,ic, to conve t t,e 5o #ula. /t i% al%o one o5 t,e #l"e6erenceSt le con%tant%. /5 we o#it t,i% a gu#ent* t,e e5e ence %t(le i% not c,anged.

0,e o'tional To-bsolute 'a a#ete %'eci5ie% t,e conve ted e5e ence t('e and can be one o5 t,e 5ollowing #l"e6erenceT 3e con%tant%:
'n05 #l"e6erenceT 3e xlAbsol0te = 1 xlAbs"ow"elCol05n = 2 xl"el"owAbsCol05n = B xl"elative = L 'nd 'n05

/5 t,i% a gu#ent i% o#itted* t,e e5e ence t('e i% not c,anged. $inall(* t,e o'tional %elati+eTo 'a a#ete i% a Range object containing a %ingle cell. 0,i% cell i% u%ed to dete #ine elative e5e ence%.H t,at i%* we can t,in- o5 t,e 5o #ula a% being 'laced in t,i% cell and %o all elative e5e ence% a e wit, e%'ect to t,i% cell. 0o illu%t ate* con%ide t,e 5ollowing code:
s>or50la = "=D2" Deb04.;rint A33lication.Convert>or50la(s>or50la= G xlA1= xl"1C1= xl"elative= "an4e("CB")) Deb04.;rint A33lication.Convert>or50la(s>or50la= G xlA1= xl"1C1= xl"el"owAbsCol05n= "an4e("CB"))

0,e %econd line conve t% 5 o# A1 notation to R1!1 notation* a%%u#ing t,at t,e 5o #ula i% in cell !3. Aence* t,e out'ut i%:
= "U%1VCU1V

%ince )2 i% one colu#n to t,e ig,t and one ow u' 5 o# cell !3. 0,e t,i d line o5 code conve t% A1 notation to R1!1 notation* but u%e% an ab%olute colu#n e5e ence and %o ' oduce%:
= "U%1VCL

%ince colu#n 4 i% one colu#n to t,e ig,t o5 colu#n 3. 15.1.7.2 Evaluate &etho# 0,i% #et,od conve t% an E6cel na#e to an object o a value. /t% %(nta6 i%:
A33lication.'val0ate(9a5e)

?0,i% #et,od al%o a''lie% to !,a t* )ialog1,eet* and 4o -%,eet object%.@ 0,e Name 'a a#ete i% t,e na#e o5 t,e object. /t can be an( o5 t,e 5ollowing t('e% o5 na#e: An A.-style reference
Name can be an( A1;%t(le e5e ence to a single cell. 0,e e5e ence i% con%ide ed to be

ab%olute. 0o illu%t ate* con%ide t,e 5ollowing code* eac, line o5 w,ic, 'u 'o t% to 'lace t,e wo d "a ( in cell A1:
"an4e("A1").+al0e = "Mar " A1.+al0e = "Mar " 'val0ate("A1").+al0e = "Mar "

UA1V.+al0e = "Mar "

0,e 5i %t line u%e% t,e Range #et,od. 0,e %econd line will ' oduce an e o becau%e E6cel con%ide % A1 a va iable at,e t,an a cell e5e ence. 0,e t,i d line u%e% t,e Evaluate #et,od to conve t t,e na#e o5 a cell to a Range object. 0,e 5ou t, line i% %,o t,and 5o t,e t,i d line. A range
Name can be an( ange 5o #ed b( u%ing t,e ange o'e ato ?colon@* inte %ect o'e ato

?%'ace@* and union o'e ato ?co##a@ wit, e5e ence%. 0,e Evaluate #et,od will etu n t,e co e%'onding Range object. 0o illu%t ate* con%ide t,e 5ollowing code:
'val0ate("12-CL").Select 'val0ate("12-CL= D&->N").Select 'val0ate("12-CL 11->2").Select U12-CL 11->2V.Select

0,e 5i %t line %elect% t,e ange >2:!4. 0,e %econd line %elect% t,e union o5 t,e two ectangula ange% >2:!4 and )5:$6. 0,e t,i d line %elect% t,e inte %ection o5 t,e two ectangula ange% >2:!4 >1:$2. 0,e 5ou t, line i% %,o t,and 5o t,e t,i d line. A "efined ?ame
Name can be an( de5ined na#e. $o in%tance* i5 we na#e a ange test* t,en t,e

5ollowing code %elect% t,at ange:


'val0ate("test").Select

?/ncidentall(* / ,ave ,ad %o#e incon%i%tent e%ult% u%ing t,e %(nta6 UtestV.Select. /t %ee#% to wo - %o#e but not all o5 t,e ti#e.@ 4e can al%o u%e 5o #ula na#e%. $o in%tance* t,e 5ollowing code di%'la(% t,e %u# o5 t,e value% in cell% >2 t, oug, >5:
Ms41ox 'val0ate("S<M(12-1&)")

+ote t,at e6te nal e5e ence% ? e5e ence% to ot,e wo -boo-%@ can be u%ed a% well* a% in:
Hor8boo8s("1**P2.#LS").Sheets("M Sheet").'val0ate("A1").Select

A% we ,ave %een* u%ing %7ua e b ac-et% i% e7uivalent to calling t,e Evaluate #et,od wit, a %t ing a gu#ent. 17ua e b ac-et% ,ave t,e advantage o5 ' oducing #o e conci%e code* but t,e( cannot be u%ed wit, %t ing va iable%. $o in%tance* we can w ite:
Di5 s>or50la As Strin4 s>or50la = "S<M(12-1&)" Ms41ox 'val0ate(s>or50la)

>ut t,e code:


Ms41ox Us>or50laV

will %i#'l( di%'la( t,e %t ing 12"?>2:>5@* a% it would wit,out t,e %7ua e b ac-et%. 15.1.7." :oto &etho#

0,i% #et,od %elect% a given ange in an( wo -boo-. ?/t can al%o %elect a =i%ual >a%ic ' ocedu e.@ 0,e %(nta6 i%:
A33lication.:oto(%e*erence= Scroll)

0,e o'tional %e*erence 'a a#ete %'eci5ie% t,e de%tination. /t can be a Range object* a %t ing t,at contain% a cell e5e ence in R1!1;%t(le notation* o a %t ing t,at contain% a =i%ual >a%ic ' ocedu e na#e. /5 t,e a gu#ent i% o#itted* t,e de%tination i% t,e de%tination u%ed in t,e ' eviou% call to .o0o. 0,e o'tional Scroll 'a a#ete %,ould be %et to Tr0e to %c oll t, oug, t,e window %o t,at t,e u''e ;le5t co ne o5 t,e de%tination a''ea % in t,e u''e ;le5t co ne o5 t,e window. 0,e de5ault i% >alse* w,ic, #ean% t,e de%tination will not #ove i5 it wa% vi%ible wit,in t,e window* o el%e it will a''ea at t,e botto# o5 t,e window i5 it wa% not vi%ible. $o e6a#'le* to %elect t,e ange >5:!6 in t,e active wo -%,eet* we can w ite:
A33lication.:oto "e6erence-="an4e("1&-CN")

o:
A33lication.:oto "e6erence-=""&C2-"NCB"

0,e .o0o #et,od al%o wo -% in conjunction wit, t,e & eviou%1election% a a(. /n 'a ticula * t,e A''lication object ,a% a & eviou%1election% ' o'e t( t,at etu n% an a a( o5 Range object% e5e encing t,e ' eviou% 5ou ange% %elected. 0,e %(nta6 i%:
A33lication.;revio0sSelections(2ndex)

w,e e 1ndex i% a nu#be between 1 and 4. Eac, ti#e t,e u%e %elect% a ange o cell eit,e b( u%ing t,e +a#e bo6 o t,e .o 0o co##and ?on t,e Edit #enu@* o t,e .oto #et,od i% called in code* t,e cu ent ange ?be5o e t,e action ta-e% 'lace@ i% added to t,e to' ?inde6 1@ o5 t,e & eviou%1election% a a( and t,e ot,e ite#% in t,e a a( a e #oved down one inde6 value. ?0,e ite# in 'o%ition 4* o5 cou %e* d o'% out o5 t,e a a(.@ A% a %i#'le illu%t ation* con%ide t,e code:
A33lication.:oto ActiveCell.+al0e A33lication.:oto ActiveCell.+al0e Sheet1."an4e("A1") = 1 Sheet2."an4e("A1") = 2

w,ic, 5ill% t,e 5i %t cell on eac, o5 two %,eet%* u%ing t,e .o0o #et,od to add t,e cell ange% to t,e & eviou%1election% a a(. +ow t,e 5ollowing line will alte nate between t,e two cell% w,en e6ecuted e'eatedl(:
A33lication.:oto A33lication.;revio0sSelections(1)

+ote t,at t,e .o0o #et,od di55e % 5 o# t,e 1elect #et,od in %eve al wa(%:
C C

>ot, #et,od% %elect t,e given ange* but t,e 1elect #et,od doe% not activate t,e %,eet u'on w,ic, t,e new %election i% #ade ?i5 it i% not al ead( active@. 0,e 1elect #et,od doe% not ,ave a Scroll a gu#ent.

C C

0,e 1elect #et,od doe% not add t,e cu ent %election to t,e & eviou%1election% a a(. 0,e 1elect #et,od ,a% a %e lace a gu#ent.

15.1.7.$ =uit &etho# 0,i% #et,od clo%e% E6cel. +ote t,at t,e >e5o e!lo%e event will 5i e w,en t,e Luit #et,od i% e6ecuted. ?0,i% event ,a% a Cancel 'a a#ete t,at can be %et to cancel t,e 7uit o'e ation.@ 4e di%cu%%ed wo -boo- event% ?including >e5o e!lo%e@ in !,a'te 11. +ote t,at i5 t,e e a e an( un%aved o'en wo -boo-% w,en t,e Ouit #et,od i% invo-ed* E6cel will di%'la( t,e u%ual dialog bo6 a%-ing t,e u%e w,et,e ,e o %,e want% to %ave t,e c,ange%. 4e can ' event t,i% eit,e b( e6'licitl( %aving all wo -boo-% ?u%ing t,e 1ave #et,od@ be5o e invo-ing t,e Ouit #et,od o b( %etting t,e )i%'la(Ale t% ' o'e t( to >alse. Aoweve * in t,e latte ca%e* an( un%aved data will be lo%t wit,out wa ningC /t i% al%o i#'o tant to note t,at E6cel c,ec-% t,e 1aved ' o'e t( o5 a wo -boo- in o de to dete #ine w,et,e to ' o#'t 5o %aving. 0,u%* i5 we %et t,e 1aved ' o'e t( to Tr0e but do not %ave t,e wo -boo-* E6cel will 7uit wit,out ' o#'ting to %ave t,e wo -boo- ?and wit,out %aving t,e wo -boo-@.

15.1.8 -iscellaneous -e&bers


Ae e a e %o#e additional #e#be % o5 t,e A''lication object. 15.1.8.1 +ellFor&atI Fin#For&at an# ,e!laceFor&at object 0,e !ell$o #at object wo -% in conjunction wit, t,e $ind$o #at and Re'lace$o #at ' o'e tie% o5 t,e A''lication object to ' og a##aticall( 5ind and e'lace cell 5o #atting. 1'eci5icall(* t,e new $ind$o #at and Re'lace$o #at ' o'e tie% o5 t,e A''lication object eac, etu n a uni7ue !ell$o #at object. 4e can %et t,e 5o #atting ' o'e tie% o5 eit,e o5 t,e%e !ell$o #at object% and t,en u%e t,e Re'lace #et,od o5 t,e Range object to e'lace t,e 5o #atting in t,e !ell$o #at object etu ned b( t,e $ind$o #at ' o'e t(* wit, t,e 5o #atting in t,e !ell$o #at object etu ned b( t,e Re'lace$o #at ' o'e t(. $o e6a#'le* t,e 5ollowing code e'lace% cell% t,at ,ave been 5o #atted a% bold wit, bold italic 5o #atting. +ote t,at now,e e in t,e code i% a !ell$o #at object e6'licitl( decla ed.
S0b 'xa53leGCell>or5at() . "e3lace 1old with 1old 2talic Hith A33lication.>ind>or5at .Clear .>ont.1old = Tr0e 'nd Hith Hith A33lication."e3lace>or5at .Clear .>ont.1old = Tr0e .>ont.2talic = Tr0e 'nd Hith Cells."e3lace Search>or5at-=Tr0e= "e3lace>or5at-=Tr0e 'nd S0b

0,e !ell$o #at object ,a% a nu#be o5 5o #at; elated ' o'e tie%. 0,e%e a e li%ted ,e e. ?0,e !ell$o #at object ,a% a %ingle #et,od na#ed !lea * w,ic, clea % all 5o #atting.@ 0,e%e a e u%ed ju%t a% we u%ed t,e $ont ' o'e t( in t,e ' eviou% code.
C C C C C C C C C C C C C C C

Add/ndent >o de % $ont $o #ulaAidden Ao iGontalAlign#ent /ndent3evel /nte io 3oc-ed "e ge!ell% +u#be $o #at +u#be $o #at3ocal O ientation 1, in-0o$it =e ticalAlign#ent 4 a'0e6t

15.1.8.2 /n!ut.ox &etho# 4e ,ave al ead( di%cu%%ed t,e =>A 2n30t1ox 5unction* w,ic, i% u%ed to etu n in'ut 5 o# t,e u%e . 0,e /n'ut>o6 #et,od o5 t,e A''lication object al%o etu n% u%e in5o #ation* but ,a% t,e advantage o5 being able to validate t,e etu n t('e and to etu n E6cel 5o #ula%* object%* and e o value%. 0,e %(nta6 5o t,e nputBox method i%:
A33lication.2n30t1ox(Prom t= Title= De*ault= G "e*t= To = 6el File= 6el Context1d= Ty e)

0,e 'a a#ete % a e a% 5ollow% ?note t,at all o5 t,e 'a a#ete % a e o'tional e6ce't t,e Prom t 'a a#ete @:
Prom t

0,e #e%%age to be di%'la(ed in t,e dialog bo6H it can be a %t ing* nu#be * date* o >oolean value.
Title

0,e ca'tion 5o t,e dialog bo6. 0,e de5ault ca'tion i% /n'ut.


De*ault

0,e value t,at will a''ea in t,e te6t bo6 w,en t,e dialog bo6 i% di%'la(ed. /5 t,i% a gu#ent i% o#itted* t,e te6t bo6 will be e#'t(.
"e*t and To

0,e u''e ;le5t co ne o5 t,e dialog bo6 in 'oint%* #ea%u ed 5 o# t,e u''e ;le5t co ne o5 t,e %c een.
6el File and 6el Context1D

0,e na#e o5 t,e Ael' 5ile and t,e conte6t /) 5o a ,el' to'ic to invo-e w,en t,e u%e ,it% t,e Ael' button on t,e in'ut bo6. /5 t,e%e a gu#ent% a e o#itted* t,en no Ael' button i% included on t,e in'ut bo6 dialog.
Ty e

0,e data t('e t,at can be ente ed into t,e te6t bo6 b( t,e u%e ?and t,u% t,e etu n t('e o5 t,e #et,od@. /t can be one o a sum of t,e value% in 0able 16;5. 4,en t,e value i% a %u# o5 %eve al nu#be %* t,en an( o5 t,e co e%'onding data t('e% i% acce'table. /t 5ollow% t,at 5o #ula% a e always acce'table. 0,e de5ault value i% 2 5o 0e6t. able 15-'. *alues 4or the /n!ut.ox -etho#>s y!e Para&eter Meaning A 5o #ula A nu#be 0e6t ?a %t ing@ A logical value ?0 ue o $al%e@ A e5e ence to a %ingle cell An e o value* %uc, a% U+EA An a a( o5 value%

Value 0 1 2 4 8 16 64

2n5o tunatel(* t,e t('e c,ec-ing done b( t,e /n'ut>o6 #et,od doe% not %ee# to be ve ( accu ate. 0o illu%t ate* t,e /n'ut>o6 %tate#ent:
"an4e("A1").+al0e = A33lication.2n30t1ox( G ;ro53t-="'nter data"= T 3e-=7)

%,ould acce't onl( 5o #ula% and not te6t. Aoweve * ente ing t,e te6t 8te%t8 %i#'l( 'ut% t,i% te6t in cell A1. ?0,e ,el' docu#entation doe% %a( t,at w,en Ty e i% 0* /n'ut>o6 etu n% t,e 5o #ula a% te6t and an( e5e ence% in t,e 5o #ula a e etu ned a% A1;%t(le e5e ence%.@ +ote t,at w,en Ty e i% e7ual to 8* t,e /n'ut>o6 #et,od etu n% a Range object t,at e5e % to t,e cell in t,e e5e ence. 0,e e5o e* we #u%t u%e t,e Set %tate#ent to a%%ign t,i% object to a va iable o5 t('e Range* a% in:
Di5 rn4 as +ariant Set rn4 = A33lication.2n30t1ox( G ;ro53t-="'nter Cell "e6erence"= T 3e-=D)

/5 we o#it t,e Set %tate#ent* t,e va iable i% %et to t,e value in t,e ange* at,e t,an t,e Range object it%el5. ?/5 we ,ad decla ed t,e rn! va iable to be o5 t('e Range* t,en t,e ' eceding code* wit,out t,e Set %tate#ent* would e%ult in t,e e o #e%%age* 8Object va iable o 4it, blocva iable not %et.8@ 4,en Ty e i% e7ual to 64* t,e u%e i% e6'ected to ente a ectangula cell ange t,at will be t eated a% a two;di#en%ional a a(. $o in%tance* con%ide a wo -%,eet a% %,own in $igu e 16;3. Figure 15-". /llustration o4 y!e J 5$

0,e code:
Di5 a As +ariant a = A33lication.2n30t1ox( G ;ro53t-="'nter Arra "= T 3e-=NL) Deb04.;rint a(B=2)

will acce't t,e in'ut:


A1-1N

a5te w,ic, a(B=2) will e7ual 56. A% a 5inal e6a#'le* i5 we e%'ond to t,e code:
Di5 a As +ariant a = A33lication.2n30t1ox( ;ro53t-="'nter >or50la"= T 3e-=1) "an4e("D1").>or50la = a

wit, a 5o #ula* E6cel doe% not 'ut t,e 5o #ula in t,e cell )1 ?it 'ut% onl( t,e nu#be @* even t,oug, 1 i% a %u# o5 1 and 0. /n ot,e wo d%* we %,ouldnBt ta-e t,e %u# %tate#ent too lite all(. 15.1.8." Selection !ro!erty 0,i% ' o'e t( %i#'l( etu n% t,e cu entl( %elected object in t,e active window. $o in%tance* i5 a cell i% %elected* t,e ' o'e t( etu n% a Range object denoting t,i% cell. 0,e 1election ' o'e t( etu n% 9othin4 i5 not,ing i% %elected. +ote t,at t,e ' o'e t( al%o a''lie% to a 4indow object and etu n% t,e cu ent %election in t,at window. 15.1.8.$ Status.ar !ro!erty ;,H0 String< 0,i% u%e5ul ' o'e t( etu n% o %et% t,e te6t in E6celB% %tatu% ba . 0o etu n cont ol o5 t,e %tatu% ba to E6cel* %i#'l( %et t,i% ' o'e t( to >alse. ?1i#ila l(* t,i% ' o'e t( will etu n >alse i5 E6cel cu entl( ,a% cont ol ove t,e %tatu% ba .@ 15.1.8.' /ntersect &etho# 0,i% #et,od etu n% a Range object t,at e' e%ent% t,e ectangula inte %ection o5 two o #o e ange%. 0,e %(nta6 i%:
A33lication.2ntersect(-r!#= -r!$= ...)

w,e e -r!#* -r!$* . . . a e t,e Range object% w,o%e ange% we wi%, to inte %ect. At lea%t two ,ange object% #u%t be %'eci5ied. $o in%tance* t,e 5ollowing line %elect% t,e inte %ection* w,ic, i% t,e ange >2:)5:

A33lication.2ntersect("an4e("A1-D&")= "an4e("12->E")).Select

15.1.8.5 2nion &etho# 0,i% #et,od i% t,e analog o5 t,e /nte %ect #et,od* but etu n% t,e union o5 two o #o e ange%. 0,e %(nta6 i%:
A33lication.<nion(-r!#= -r!$= ...)

w,e e -r!#* -r!$* . . . a e t,e Range object% w,o%e ange% we wi%, to join toget,e . At lea%t two Range object% #u%t be %'eci5ied. $o in%tance* t,e 5ollowing code %elect% bot, ectangula egion% A1:)5 and >2:$9:
A33lication.<nion("an4e("A1-D&")= "an4e("12->E")).Select

$8%' Children of the Application Ob1ect


$igu e 16;4 %,ow% t,e c,ild en o5 t,e A''lication object. ?0,i% e'eat% $igu e 16;1.@ Figure 15-$. he Excel %!!lication object an# its chil#ren

4e will di%cu%% #an( o5 t,e c,ild en o5 t,e A''lication object* including A''Event%* !,a t* Range* 1,eet%* 4o -boo-* and 4o -%,eet* in late c,a'te %. ?4e ,ave al ead( di%cu%%ed t,e )ialog% object.@ $o now* let u% di%cu%% %o#e o5 t,e 8%#alle 8 c,ild en.

15.2.1 Na&e 3bjects an# the Na&es +ollections

A +a#e object e' e%ent% a de5ined na#e 5o a ange o5 cell%. 0,e e a e two t('e% o5 na#e% in E6cel: built;in na#e% %uc, a% ;rintGArea and cu%to# na#e% c eated b( t,e u%e o b( code. +a#e object% a e -e't in %eve al +a#e% collection%. 0,e e i% a +a#e% collection 5o t,e A''lication object* a% well a% +a#e% collection% 5o eac, 4o -boo- and 4o -%,eet object. 0,e e a e a va iet( o5 wa(% to c eate a new +a#e object. 4e can add a +a#e object to a +a#e% collection b( calling t,e collectionB% Add #et,od o we can u%e t,e ! eate+a#e% #et,od o5 t,e Range object ?di%cu%%ed in !,a'te 19@. $o in%tance* t,e 5ollowing code c eate% a +a#e object t,at e5e % to a ange on 1,eet1 o5 >oo-1. 0,e +a#e object i% added to t,e wo -boo-B% +a#e% collection* but not to 1,eet1B% +a#e% collection:
Hor8boo8s("1oo81.xls").9a5es.Add 9a5e-="H8189a5e" "e6ersTo-="=Sheet1!CAC1-C1C1"

+ote t,e u%e o5 a %,eet 7uali5ie in t,e %e*ersTo 'a a#ete and t,e %'eci5ication o5 an ab%olute add e%%. /5 t,e ab%olute o'e ato ?V@ i% not u%ed* t,e ange will be de5ined elative to t,e active cell. 0,e 5ollowing code add% a +a#e object to t,e +a#e% collection o5 1,eet1 and 1,eet2:
Hor8boo8s("1oo81.xls").Hor8sheets("Sheet1") G .9a5es.Add 9a5e-="H8Sheet19a5e"= G "e6ersTo-="=Sheet1!CAC1-C1C1" Hor8boo8s("1oo81.xls").Hor8sheets("Sheet2"). G 9a5es.Add 9a5e-="H8Sheet29a5e"= G "e6ersTo-="=Sheet2!CAC1-C1C1"

+ote t,at t,i% code will al%o add t,e +a#e object% to t,e wo -boo-B% +a#e% collection. 0,e 5ollowing code %et% t,e 5ont 5o t,e ange H8Sheet19a5e to bold5ace:
Sheet1.9a5es("H8Sheet19a5e")."e6ersTo"an4e.>ont.1old = Tr0e

+ote t,at t,e e i% no +a#e% collection 5o a given Range object* even t,oug, a Range object can ,ave #o e t,an one na#e. 0,e be%t we can do i% et ieve t,e 5i %t na#e 5o a ange object b( u%ing t,e +a#e ' o'e t( ?%ee t,e di%cu%%ion in !,a'te 19@. 3et u% eview %o#e o5 t,e ' o'e tie% and #et,od% o5 t,e +a#e object: "elete method 0,i% #et,od* w,o%e %(nta6 i%:
Name/bject.Delete

delete% t,e +a#e object 5 o# t,e +a#e% collection% in w,ic, it e%ide%. /t doe% not delete t,e actual ange. ?ame property 0,i% ' o'e t( etu n% o %et% t,e na#e o5 t,e +a#e object.

,efers!o property 0,i% ' o'e t( etu n% o %et% t,e 5o #ula t,at de5ine% a na#ed ange* in A1;%t(le notation* beginning wit, an e7ual %ign. ,efers!o,.(. property 0,i% ' o'e t( etu n% o %et% t,e 5o #ula t,at de5ine% a na#ed ange* in R1!1;%t(le notation* beginning wit, an e7ual %ign. ,efers!o,ange property 0,i% ' o'e t( etu n% t,e Range object e5e ed to b( t,e na#ed ange. /t i% ead;onl(. ?1ee t,e ' eviou% e6a#'le code.@ Value property 0,i% ' o'e t( al%o etu n% o %et% t,e 5o #ula t,at de5ine% a na#ed ange* in A1;%t(le notation* beginning wit, an e7ual %ign. 0,u%* it i% e7uivalent to t,e Re5e %0o ' o'e t(. Visible property 0,i% ' o'e t( etu n% o %et% t,e vi%ibilit( o5 t,e na#ed ange.

15.2.2 he 0in#ows +ollection an# 0in#ow 3bjects


O5 cou %e* a 4indow object e' e%ent% an E6cel window. 0,e 4indow% collection o5 t,e A''lication object i% t,e collection o5 4indow object% 5o all cu entl( o'en window% in t,e cu entl( unning ve %ion o5 E6cel. ?1i#ila l(* t,e 4indow% collection 5o a 4o -boo- object contain% onl( t,e window% in t,e wo -boo-.@ 0,e A ange #et,od o5 t,e 4indow% collection i% u%ed to a ange t,e cu ent window%. 0,e %(nta6 i%:
Windows/bject.Arran4e(-rran!eStyle= G -cti+eWorkbook= Sync6ori&ontal= SyncVertical)

0,e o'tional -rran!eStyle 'a a#ete can be one o5 t,e 5ollowing #lArran4eSt le con%tant%:
'n05 #lArran4eSt xlArran4eSt xlArran4eSt xlArran4eSt xlArran4eSt 'nd 'n05 le le+ertical = %L1NN le?oriMontal = %L12D leTiled = 1 leCascade = F

. De6a0lt

4e can %et t,e -cti+eWorkbook 'a a#ete to Tr0e to a ange onl( t,e vi%ible window% o5 t,e active wo -boo-. 0,e de5ault value i% >alse* in w,ic, ca%e all window% a e a anged. 4,en -cti+eWorkbook i% 0 ue* t,e e#aining 'a a#ete % a e evaluated ?ot,e wi%e t,e( a e igno ed@. Sync6ori&ontal can be %et to Tr0e to %(nc, oniGe t,e ,o iGontal %c olling window% o5 t,e active wo -boo-. /n ot,e wo d%* all window% %c oll at t,e %a#e ti#e w,en one window i% %c olled ,o iGontall(. 0,e de5ault value i% $al%e. 1i#ila l(* t,e SyncVertical 'a a#ete

%'eci5ie% ve tical %c olling %(nc, oniGation. 0,u%* t,e 5ollowing code tile% t,e vi%ible window% and enable% ,o iGontal %c olling %(nc, oniGation:
ActiveHor8boo8.Hindows.Arran4e G Arran4eSt le-=xlArran4eSt leTiled= G S nc?oriMontal-=Tr0e

0o c eate a new window* we u%e t,e +ew4indow #et,od o5 t,e 4o -boo- object* a% in:
ThisHor8boo8.9ewHindow

in w,ic, ca%e a co'( o5 t,e active window i% c eated. 0,i% #et,od al%o a''lie% to an e6i%ting 4indow object and c eate% a co'( o5 t,e window to w,ic, it i% a''lied. 0,e Windows collection ,a% a %'ecial ' o'e t( wit, e%'ect to inde6ing* na#el(* t,e active window i% alwa(%:
Hindows(1)

0,e 58 #e#be % o5 t,e 4indow object a e %,own in 0able 16;6. able 15-5. -e&bers o4 the 0in#ow object I)i%'la(Rig,t0o3e5tSv9Z EnableRe%iGe 1c oll4o -boo-0ab% Activate $ eeGe&ane% 1elected1,eet% Activate+e6t . idline!olo 1election Activate& eviou% . idline!olo /nde6 1#all1c oll Active!ell Aeig,t 1'lit Active!,a t /nde6 1'lit!olu#n Active&ane 3a ge1c oll 1'litAo iGontal Active1,eet 3e5t 1'litRow A''lication +ew4indow 1'lit=e tical !a'tion On4indow 0abRatio !lo%e &ane% 0o' ! eato &a ent 0('e )i%'la($o #ula% &oint%0o1c een&i6el%DSv9Z 2%ableAeig,t )i%'la(. idline% &oint%0o1c een&i6el%LSv9Z 2%able4idt, )i%'la(Aeading% & intOut =iew )i%'la(Ao iGontal1c oll>a & int& eview =i%ible )i%'la(Outline Range$ o#&ointSv9Z =i%ibleRange )i%'la(Rig,t0o3e5t Range1election 4idt, )i%'la(=e tical1c oll>a 1c oll!olu#n 4indow+u#be )i%'la(4o -boo-0ab% 1c oll/nto=iewSv9Z 4indow1tate )i%'la([e o% 1c ollRow [oo#

15.2." he 0or1sheetFunction 3bject


0,e 4o -%,eet$unction object i% etu ned b( t,e 4o -%,eet$unction ' o'e t( o5 t,e A''lication object. 0,e %ole 'u 'o%e o5 t,e 4o -%,eet$unction object i% to ' ovide acce%% to E6celB% wo -%,eet 5unction%. $o in%tance* t,e 5ollowing code illu%t ate% t,e u%e o5 t,e 4o -%,eet$unction object to acce%% E6celB% Min 5unction:

Di5 rn4 As "an4e Di5 rMin As Sin4le Set rn4 = Hor8sheets("Sheet1")."an4e("A1-D17") rMin = A33lication.Hor8sheet>0nction.Min(rn4)

Chapter $9% The -orkbook Ob1ect


/n t,i% c,a'te * we di%cu%% t,e 4o -boo- object and t,e 4o -boo-% collection. $igu e 17;1 %,ow% t,e 'o tion o5 t,e E6cel object #odel t,at elate% di ectl( to wo -boo-%. Figure 16-1. he 0or1boo1 object

$9%$ The -orkbooks Collection


0,e A''lication object ,a% a 4o -boo-% ' o'e t( t,at etu n% a 4o -boo-% collection* w,ic, contain% all o5 t,e 4o -boo- object% 5o t,e cu entl( o'en in%tance o5 E6cel. $o in%tance* t,e 5ollowing code di%'la(% t,e nu#be o5 o'en wo -boo-%:
Di5 wbs As Hor8boo8s Set wbs = A33lication.Hor8boo8s Ms41ox wbs.Co0nt

3et u% loo- at a 5ew o5 t,e ' o'e tie% and #et,od% o5 t,e 4o -boo-% collection.

16.1.1 %## -etho#


0,e Add #et,od c eate% a new wo -boo-* w,ic, i% t,en added to t,e 4o -boo-% collection. 0,e new wo -boo- beco#e% t,e active wo -boo-. 0,e %(nta6 i%:
Workbooks/bject.Add(Tem late)

w,e e t,e o'tional Tem late 'a a#ete dete #ine% ,ow t,e new wo -boo- i% c eated. /5 t,i% a gu#ent i% a %t ing %'eci5(ing t,e na#e o5 an e6i%ting E6cel te#'late 5ile* t,e new wo -boo- i% c eated wit, t,at 5ile a% a te#'late. A% (ou #a( -now* a te#'late i% an E6cel wo -boo- t,at #a( contain content ?%uc, a% ow and colu#n label%@* 5o #atting* and #ac o% and ot,e cu%to#iGation% ?#enu% and toolba %* 5o in%tance@. 4,en (ou ba%e a new wo -boo- on a te#'late* t,e new wo -boo- eceive% t,e content* 5o #atting* and cu%to#iGation 5 o# t,e te#'late. 0,e Tem late a gu#ent can al%o be one o5 t,e 5ollowing con%tant%:

'n05 #lH1ATe53late xlH1ATHor8sheet = %L1NF xlH1ATChart = %L17E xlH1AT'xcelLMacroSheet = B xlH1AT'xcelL2ntlMacroSheet = L 'nd 'n05

/n t,i% ca%e* t,e new wo -boo- will contain a %ingle %,eet o5 t,e %'eci5ied t('e. /5 t,e Tem late a gu#ent i% o#itted* E6cel will c eate a new wo -boo- wit, t,e nu#be o5 blan- %,eet% %et b( t,e A''lication objectB% 1,eet%/n+ew4o -boo- ' o'e t(.

16.1.2 +lose -etho#


0,e !lo%e #et,od clo%e% all o'en wo -boo-%. 0,e %(nta6 i% %i#'l(:
Worksbooks/bject.Close

16.1." +ount Pro!erty


"o%t collection object% ,ave a !ount ' o'e t(* and t,e 4o -boo-% collection i% no e6ce'tion. 0,i% ' o'e t( %i#'l( etu n% t,e nu#be o5 cu entl( o'en wo -boo-%.

16.1.$ /te& Pro!erty


0,e /te# ' o'e t( etu n% a 'a ticula wo -boo- in t,e 4o -boo-% collection. $o in%tance:
Hor8boo8s.2te5(1)

etu n% t,e 4o -boo- object a%%ociated wit, t,e 5i %t wo -boo- in t,e 4o -boo-% collection. 1ince t,e /te# ' o'e t( i% t,e de5ault ' o'e t(* we can al%o w ite t,i% a%:
Hor8boo8s(1)

+ote t,at we cannot el( on t,e 5act t,at a ce tain wo -boo- will ,ave a ce tain inde6. ?0,i% a''lie% to all collection%.@ 0,u%* to e5e to a 'a ticula wo -boo-* (ou %,ould alwa(% u%e it% na#e* a% in:
Hor8boo8s("1oo81.xls")

/t i% i#'o tant to note t,at i5 a u%e c eate% a new wo -boo- na#ed* %a(* >oo-2* u%ing t,e +ew #enu ite# on t,e $ile #enu* t,en we #a( e5e to t,i% wo -boo- in code b( w iting:
Hor8boo8s("1oo82")

but t,e code:


Hor8boo8s("1oo82.xls")

will gene ate an e o ?%ub%c i't out o5 ange@ until t,e wo -boo- i% actuall( %aved to di%-.

16.1.' 3!en -etho#


0,i% #et,od o'en% an e6i%ting wo -boo-. 0,e at,e co#'le6 %(nta6 i%:

Workbooks/bject.*3en(FileName= 7 date"inks= %ead/nly= G Format= Password= Write%esPassword= 1!nore%ead/nly%ecommended= G /ri!in= Delimiter= ,ditable= Noti*y= Con+erter= -ddTo(%7)

"o%t o5 t,e%e 'a a#ete % a e a el( u%ed ?%eve al o5 t,e# elate to o'ening te6t 5ile%* 5o in%tance@. 4e di%cu%% t,e #o%t co##onl( u%ed 'a a#ete % and e5e t,e eade to t,e ,el' 5ile% 5o #o e in5o #ation. +ote t,at all o5 t,e 'a a#ete % a e o'tional e6ce't FileName.
FileName i% t,e 5ile na#e o5 t,e wo -boo- to be o'ened. 0o o'en t,e wo -boo- in ead;onl( #ode* %et t,e %ead/nly 'a a#ete to Tr0e.

/5 a 'a%%wo d i% e7ui ed to o'en t,e wo -boo-* t,e Password 'a a#ete %,ould be %et to t,i% 'a%%wo d. /5 a 'a%%wo d i% e7ui ed but (ou do not %'eci5( t,e 'a%%wo d* E6cel will a%- 5o it. 0,e -ddTo(ru 'a a#ete %,ould be %et to Tr0e to add t,i% wo -boo- to t,e li%t o5 ecentl( u%ed 5ile%. 0,e de5ault value i% >alse.

16.1.5 3!en ext -etho#


0,i% #et,od will load a te6t 5ile a% a new wo -boo-. 0,e #et,od will 'a %e t,e te6t data and 'lace it in a %ingle wo -%,eet. 0,e at,e co#'le6 %(nta6 i%:
Workbooks/bject.*3enText(Filename= /ri!in= Start%ow= G DataTy e= Text8uali*ier= Consecuti+eDelimiter= Tab= G Semicolon= Comma= S ace= /ther= /therChar= Field1n*o)

+ote 5i %t t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional e6ce't t,e FileName 'a a#ete . 0,e Filename 'a a#ete %'eci5ie% t,e 5ilena#e o5 t,e te6t 5ile to be o'ened. 0,e /ri!in 'a a#ete %'eci5ie% t,e o igin o5 t,e te6t 5ile and can be one o5 t,e 5ollowing #l;lat6or5 con%tant%:
'n05 #l;lat6or5 xlMacintosh = 1 xlHindows = 2 xlMSD*S = B 'nd 'n05

+ote t,at t,e xlHindows value %'eci5ie% an A+1/ te6t 5ile* w,e ea% t,e xlMSD*S con%tant %'eci5ie% an A1!// 5ile. /5 t,i% a gu#ent i% o#itted* t,e cu ent %etting o5 t,e $ile O igin o'tion in t,e 0e6t /#'o t 4iGa d will be u%ed. 0,e Start%ow 'a a#ete %'eci5ie% t,e ow nu#be at w,ic, to %ta t 'a %ing te6t 5 o# t,e te6t 5ile. 0,e de5ault value i% 1. 0,e o'tional DataTy e 'a a#ete %'eci5ie% t,e 5o #at o5 t,e te6t in t,e 5ile and can be one o5 t,e 5ollowing #lText;arsin4T 3e con%tant%:
'n05 #lText;arsin4T 3e xlDeli5ited = 1 xl>ixedHidth = 2 'nd 'n05 . De6a0lt

0,e Text8uali*ier 'a a#ete i% t,e te6t 7uali5ie . /t can be one o5 t,e 5ollowing #lText,0ali6ier con%tant%:
'n05 #lText,0ali6ier xlText,0ali6ier9one = %L1L2 xlText,0ali6ierDo0ble,0ote = 1 xlText,0ali6ierSin4le,0ote = 2 'nd 'n05

. De6a0lt

0,e Consecuti+eDelimiter 'a a#ete %,ould be %et to Tr0e 5o E6cel to con%ide con%ecutive deli#ite % a% one deli#ite . 0,e de5ault value i% >alse. 0,e e a e %eve al 'a a#ete % t,at e7ui e t,at DataTy e be xlDeli5ited. 4,en an( one o5 t,e%e 'a a#ete % i% %et to Tr0e* it indicate% t,at E6cel %,ould u%e t,e co e%'onding c,a acte a% t,e te6t deli#ite . 0,e( a e de%c ibed ,e e ?all de5ault value% a e >alse@:
Tab

1et to Tr0e to u%e t,e tab c,a acte a% t,e deli#ite .


Semicolon

1et to Tr0e to u%e a %e#icolon a% t,e deli#ite .


Comma

1et to Tr0e to u%e a co##a a% t,e deli#ite .


S ace

1et to Tr0e to u%e a %'ace a% t,e deli#ite .


/ther

1et to Tr0e to u%e a c,a acte t,at i% %'eci5ied b( t,e /therChar a gu#ent a% t,e deli#ite . 4,en /ther i% 0 ue* /therChar %'eci5ie% t,e deli#ite c,a acte . /5 /therChar contain% #o e t,an one c,a acte * onl( t,e 5i %t c,a acte i% u%ed. 0,e Field1n*o 'a a#ete i% an a a( containing 'a %e in5o #ation 5o t,e individual %ou ce colu#n%. 0,e inte ' etation o5 Field1n*o de'end% on t,e value o5 DataTy e. 4,en DataTy e i% xlDeli5ited* t,e Field1n*o a gu#ent %,ould be an a a( w,o%e %iGe i% t,e %a#e a% o %#alle t,an t,e nu#be o5 colu#n% o5 conve ted data. 0,e 5i %t ele#ent o5 a two ele#ent a a( i% t,e colu#n nu#be ?%ta ting wit, t,e nu#be 1@* and t,e %econd ele#ent i% one o5 t,e 5ollowing nu#be % t,at %'eci5ie% ,ow t,e colu#n i% 'a %ed: Value 1 2 3 4 5 6 7 8 9 .ene al 0e6t ")L date )"L date L") date "L) date )L" date L)" date 1-i' t,e colu#n Des ription

/5 a two;ele#ent a a( 5o a given colu#n i% #i%%ing* t,en t,e colu#n i% 'a %ed wit, t,e .ene al %etting. $o in%tance* t,e 5ollowing value 5o Field1n*o cau%e% t,e 5i %t colu#n to be 'a %ed a% te6t and t,e t,i d colu#n to be %-i''ed:
Arra (Arra (1= 2)= Arra (B= E))

All ot,e colu#n% will be 'a %ed a% gene al data. 0o illu%t ate* con%ide a te6t 5ile wit, t,e 5ollowing content%:
"Sohn"="S5ith"="Serial "ecord"=1J2JED ">red"=":w nn"="Serials *rder De3t"=2J2JED "Mar "="Davis"="'n4lish De3t"=BJ&JED "David"="Sohns"="Che5istr De3t"=LJLJED

0,e code:
Hor8boo8s.*3enText G >ile9a5e-="d-IexcelIte53.txt"= G *ri4in-=xlMSD*S= G Start"ow-=1= G DataT 3e-=xlDeli5ited= G Text,0ali6ier-=xlText,0ali6ierDo0ble,0ote= G Consec0tiveDeli5iter-=Tr0e= G Co55a-=Tr0e= G >ield2n6o-=Arra (Arra (1= 2)= G Arra (2= 2)= Arra (B= 2)= Arra (L= N))

' oduce% t,e wo -%,eet %,own in $igu e 17;2. +ote t,at t,e cell% in colu#n ) a e 5o #atted a% date%. Figure 16-2. % co&&a-#eli&ite# text 4ile o!ene# in Excel

On t,e ot,e ,and* i5 DataTy e i% xl>ixedHidth* t,e 5i %t ele#ent o5 eac, two;ele#ent a a( %'eci5ie% t,e %ta ting c,a acte 'o%ition in t,e colu#n ?0 being t,e 5i %t c,a acte @ and t,e %econd ele#ent %'eci5ie% t,e 'a %e o'tion ?1;9@ 5o t,e e%ulting colu#n* a% de%c ibed ea lie . 0o illu%t ate* con%ide t,e te6t 5ile w,o%e content% a e a% 5ollow%:
7%12&%NDE 2%&2B%LDE B%L2L%NNL

L%12&%1N7

0,e code:
Hor8boo8s.*3enText G >ile9a5e-="d-IexcelIte53.txt"= G *ri4in-=xlMSD*S= G Start"ow-=1= G DataT 3e-=xl>ixedHidth= G >ield2n6o-=Arra (Arra (7= 2)= G Arra (1= E)= Arra (2= 2)= Arra (&= E)= G Arra (N= 2))

' oduce% t,e wo -%,eet in $igu e 17;3. ?+ote ,ow we included a a(% to %-i' t,e ,(',en%.@ Figure 16-". % 4ixe#-wi#th text 4ile o!ene# in Excel

$inall(* it i% i#'o tant to ob%e ve t,at t,e te6t 5ile i% o'ened in E6cel* but not conve ted to an E6cel wo -boo- 5ile. 0o do %o* we can invo-e t,e 1aveA% #et,od* a% in:
A33lication.ActiveSheet.SaveAs G >ile9a5e-="d-IexcelIte53.xls"= G >ile>or5at-=xlHor8boo89or5al

$9%' The -orkbook Ob1ect


A 4o -boo- object e' e%ent% an o'en E6cel wo -boo-. A% we ,ave di%cu%%ed* 4o -booobject% a e %to ed in a 4o -boo-% collection. 0,e 4o -boo- object ,a% a total o5 103 ' o'e tie% and #et,od%* a% %,own in 0able 17;1. able 16-1. -e&bers o4 the 0or1boo1 object I!ode+a#e $ull+a#e Re5 e%,All I& intOutSv9Z $ull+a#e2R3EncodedSv10Z RejectAll!,ange% I& otectSv10Z Aa%"aile ReloadA%Sv9Z IReadOnl(Reco##endedSv10Z Aa%&a%%wo d Re#ove&e %onal/n5o #ationSv10Z I1aveA%Sv10Z Aa%Routing1li' Re#ove2%e Acce'tAll!,ange% Aig,lig,t!,ange%On1c een Re'l( Acce't3abel%/n$o #ula% Aig,lig,t!,ange%O'tion% Re'l(All Activate A0"3& ojectSv9Z Re'l(4it,!,ange%Sv10Z Active!,a t /%Addin Re%et!olo % Active1,eet /%/n'lace Revi%ion+u#be Add0o$avo ite% Ree'!,angeAi%to ( Route

A''lication Aut,o Auto2'date$ e7uenc( Auto2'date1ave!,ange% > ea-3in-Sv10Z >uiltin)ocu#ent& o'e tie% !alculation=e %ionSv9Z !an!,ec-/nSv10Z !,ange$ileAcce%% !,angeAi%to ()u ation !,ange3in!,a t% !,ec-/nSv10Z !lo%e !ode+a#e !olo % !o##and>a % !o##ent% !on5lictRe%olution !ontaine ! eate>ac-u' ! eato !u%to#)ocu#ent& o'e tie% !u%to#=iew% )ate1904 )elete+u#be $o #at )ialog1,eet% )i%'la() awingObject% )u##(16Sv10Z )u##(17Sv10Z EnableAutoRecove Sv10Z EndReviewSv10Z Envelo'e=i%ibleSv9Z E6cel4/ntl"ac o1,eet% E6cel4"ac o1,eet% E6clu%iveAcce%% $ile$o #at $ollowA('e lin$o wa d"aile

Re(wo d% Routed 3in-/n5o Routing1li' 3in-1ou ce% RunAuto"ac o% 3i%t!,ange%On+ew1,eet 1ave "aile 1aveA% "e ge4o -boo1ave!o'(A% "odule% 1aved "ulti2%e Editing 1ave3in-=alue% +a#e %bltSv9Z +a#e% 1end$o ReviewSv10Z +ew4indow 1end"ail On1ave 1end"aile On1,eetActivate 1et3in-On)ata On1,eet)eactivate 1et&a%%wo dEnc ('tionO'tion%Sv10Z O'en3in-% 1,eet% &a ent 1,ow!on5lictAi%to ( &a%%wo dSv10Z 1,ow&ivot0able$ield3i%tSv10Z &a%%wo dEnc ('tionAlgo it,#Sv10Z 1#a t0agO'tion%Sv10Z &a%%wo dEnc ('tion$ile& o'e tie%Sv10Z 1t(le% &a%%wo dEnc ('tionRe(3engt,Sv10Z 1ubject &a%%wo dEnc ('tion& ovide Sv10Z 0e#'lateRe#oveE6t)ata &at, 0itle &e %onal=iew3i%t1etting% 2n' otect &e %onal=iew& int1etting% 2n' otect1,a ing &ivot!ac,e% 2'date$ o#$ile &ivot0able4iGa d 2'date3in&o%t 2'date3in-%Sv10Z & eci%ionA%)i%'la(ed 2'dateRe#oteRe5e ence% & intOut 2%e !ont ol & int& eview 2%e 1tatu% & otect =>A1ignedSv9Z & otect1,a ing =>& oject & otect1t uctu e 4ebO'tion%Sv9Z & otect4indow% 4eb&age& eviewSv9Z &ubli%,Object%Sv9Z 4indow% &u ge!,angeAi%to (+ow 4o -%,eet% ReadOnl( 4 ite&a%%wo dSv10Z ReadOnl(Reco##ended 4 iteRe%e ved Rec,ec-1#a t0ag%Sv10Z 4 iteRe%e ved>(

1eve al o5 t,e #e#be % li%ted in 0able 17;1 e6i%t %olel( to etu n t,e c,ild en o5 t,e 4o -booobject. 0,e c,ild en a e %,own in $igu e 17;4. Figure 16-$. +hil#ren o4 the 0or1boo1 object

0able 17;2 give% t,e #e#be % o5 t,e 4o -boo- object t,at etu n c,ild en. able 16-2. -e&bers o4 0or1boo1 that return chil#ren Name ReturnType Active!,a t !,a t A''lication A''lication !,a t% 1,eet% !u%to#=iew% !u%to#=iew% )ialog1,eet% 1,eet% E6cel4/ntl"ac o1,eet% 1,eet% E6cel4"ac o1,eet% 1,eet% "aile "aile "odule% 1,eet% +a#e% +a#e% +ew4indow 4indow &ivot!ac,e% &ivot!ac,e% &ubli%,Object% &ubli%,Object% Routing1li' Routing1li' 1,eet% 1,eet% 1#a t0agO'tion% 1#a t0agO'tion% 1t(le% 1t(le% 4ebO'tion% 4ebO'tion% 4indow% 4indow% 4o -%,eet% 1,eet% 0,e e a e a 5ew ite#% wo t, noting about 0able 17;2. $i %t* t,e Active1,eet ' o'e t( #a( etu n eit,e a !,a t object o a 4o -%,eet object* de'ending u'on w,at t('e o5 object i% cu entl( active. 1econd* t,e !,a t%* 1,eet%* and 4o -%,eet% ' o'e tie% all etu n a ?di55e ent@ 1,eet% collection. /n 'a ticula * t,e !,a t% object etu n% t,e 1,eet% collection t,at contain% all o5 t,e c,a t %,eet% in t,e wo -boo-. ?0,i% doe% not include c,a t% t,at a e e#bedded in wo -%,eet%.@ 0,e 4o -%,eet% ' o'e t( etu n% t,e 1,eet% collection o5 all wo -%,eet% in t,e wo -boo-. $inall(* t,e 1,eet% ' o'e t( etu n% t,e 1,eet% collection o5 all wo -%,eet% and c,a t %,eet%. 0,i% i% a elativel( a e e6a#'le o5 a collection t,at contain% object% o5 #o e t,an one t('e. +ote t,at t,e e i% no 1,eet object in t,e E6cel object #odel.

3et u% loo- at a 5ew o5 t,e #o e co##onl( u%ed #e#be % 5 o# 0able 17;1.

16.2.1 %ctivate -etho#


0,i% #et,od activate% t,e wo -boo-. 0,e %(nta6 i% %t aig,t5o wa d* a% in:
Hor8boo8s("M Hor81oo8").Activate

+ote t,at 4o -boo-% i% global* %o we do not need to 7uali5( it wit, t,e A33lication -e(wo d.

16.2.2 +lose -etho#


0,e !lo%e #et,od clo%e% t,e wo -boo-. /t% %(nta6 i%:
Workbook/bject.Close(Sa+eChan!es= FileName= %outeWorkbook)

+ote t,at t,e !lo%e #et,od o5 t,e 4o -boo- object ,a% t, ee 'a a#ete %* unli-e t,e !lo%e #et,od o5 t,e 4o -boo-% object* w,ic, ,a% none. 0,e o'tional Sa+eChan!es 'a a#ete i% u%ed to %ave c,ange% to t,e wo -boo- be5o e clo%ing. /n 'a ticula * i5 t,e e a e no c,ange% to t,e wo -boo-* t,e a gu#ent i% igno ed. /t i% al%o igno ed i5 t,e wo -boo- a''ea % in ot,e o'en window%. On t,e ot,e ,and* i5 t,e e a e c,ange% to t,e wo -boo- and it doe% not a''ea in an( ot,e o'en window%* t,e a gu#ent ta-e% e55ect. /n t,i% ca%e* i5 Sa+eChan!es i% Tr0e* t,e c,ange% a e %aved. /5 t,e e i% not (et a 5ilena#e a%%ociated wit, t,e wo -boo- ?t,at i%* i5 it ,a% not been ' eviou%l( %aved@* t,en t,e na#e given in FileName i% u%ed. /5 FileName i% al%o o#itted* E6cel will ' o#'t t,e u%e 5o a 5ilena#e. /5 Sa+eChan!es i% >alse* c,ange% a e not %aved. $inall(* i5 t,e Sa+eChan!es a gu#ent i% o#itted* E6cel will di%'la( a dialog bo6 a%-ing w,et,e t,e c,ange% %,ould be %aved. /n %,o t* t,i% #et,od be,ave% a% (ou would ,o'e. 0,e o'tional %outeWorkbook e5e % to outing i%%ue%H we e5e t,e inte e%ted eade to t,e E6cel =>A ,el' 5ile 5o #o e in5o #ation. /t i% i#'o tant to note t,at t,e !lo%e #et,od c,ec-% t,e 1aved ' o'e t( o5 t,e wo -boo- to dete #ine w,et,e o not to ' o#'t t,e u%e to %ave c,ange%. /5 we %et t,e 1aved ' o'e t( to Tr0e* t,en t,e !lo%e #et,od will %i#'l( clo%e t,e wo -boo- wit, no wa ning and wit,out %aving an( un%aved c,ange%.

16.2." (is!lay(rawing3bjects Pro!erty


0,i% ' o'e t( etu n% o %et% a value indicating ,ow %,a'e% a e di%'la(ed. /t can be one o5 t,e 5ollowing #lDis3la Sha3es con%tant%:
'n05 #lDis3la Sha3es #lDis3la Sha3es = %L17L xl;laceholders = 2 xl?ide = B 'nd 'n05

16.2.$ FileFor&at Pro!erty ;,ea#-3nly )ong <


0,i% ' o'e t( etu n% t,e 5ile 5o #at o t('e o5 t,e wo -boo-. /t can be one o5 t,e 5ollowing #l>ile>or5at con%tant%:

'n05 #l>ile>or5at xlAdd2n = 1D xlCS+ = N xlCS+Mac = 22 xlCS+MSD*S = 2L xlCS+Hindows = 2B xlC0rrent;lat6or5Text = %L1&D xlD1>2 = F xlD1>B = D xlD1>L = 11 xlD2> = E xl'xcel2 = 1N xl'xcel2>ar'ast = 2F xl'xcelB = 2E xl'xcelL = BB xl'xcelLHor8boo8 = B& xl'xcel& = BE xl'xcelF = BE xl'xcelEFE& = LB xl?t5l = LL xl2ntlAdd2n = 2N xl2ntlMacro = 2& xlS@LP = 2 xlTe53late = 1F xlTextMac = 1E xlTextMSD*S = 21 xlText;rinter = BN xlTextHindows = 27 xl<nicodeText = L2 xlHebArchive = L& xlHS2HD1 = 1L xlHSB = L7 xlHSB>SB = L1 xlHP1 = & xlHP1ALL = B1 xlHP1>MT = B7 xlHPB = 1& xlHPB>MB = B2 xlHPL = BD xlHPS = L xlHor8boo89or5al = %L1LB xlHor8s2>ar'ast = 2D xlH,1 = BL xl#MLS3readsheet = LN 'nd 'n05

16.2.' Na&eI FullNa&eI an# Path Pro!erties


0,e +a#e ' o'e t( etu n% t,e na#e o5 t,e wo -boo-* t,e &at, ' o'e t( etu n% t,e 'at, to t,e wo -boo- 5ile* and $ull+a#e etu n% t,e 5ull( 7uali5ied ?'at, and 5ilena#e@ o5 t,e wo -boo- 5ile. All o5 t,e%e ' o'e tie% a e ead;onl(. +ote t,at u%ing t,e &at, ' o'e t( wit,out a 7uali5ie i% e7uivalent to:
A33lication.;ath

and t,u% etu n% t,e 'at, to E6cel it%el5 ? at,e t,an to a wo -boo-@.

16.2.5 CasPasswor# Pro!erty ;,ea#-3nly .oolean<

0,i% ead;onl( ' o'e t( i% Tr0e i5 t,e wo -boo- ,a% 'a%%wo d ' otection. +ote t,at a 'a%%wo d can be a%%igned a% one o5 t,e 'a a#ete % to t,e 1aveA% #et,od.

16.2.6 Precision%s(is!laye# Pro!erty ;,H0 .oolean<


4,en t,i% ' o'e t( i% Tr0e* calculation% in t,e wo -boo- will be done u%ing onl( t,e ' eci%ion o5 t,e nu#be % a% t,e( a e di%'la(ed* at,e t,an a% t,e( a e %to ed. /t% de5ault value i% >alseH calculation% a e ba%ed on t,e value% o5 nu#be % a% t,e( a e %to ed.

16.2.7 Print3ut -etho#


0,e & intOut #et,od ' int% an enti e wo -boo-. ?0,i% #et,od a''lie% to a ,o%t o5 ot,e object% a% well* %uc, a% Range* 4o -%,eet* and !,a t.@ 0,e %(nta6 i%:
Workbook/bject.;rint*0t(From= To= Co ies= G Pre+iew= -cti+ePrinter= PrintToFile= Collate)

+ote t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional. 0,e From 'a a#ete %'eci5ie% t,e 'age nu#be o5 t,e 5i %t 'age to ' int* and t,e To 'a a#ete %'eci5ie% t,e la%t 'age to ' int. /5 o#itted* t,e enti e object ? ange* wo -%,eet* etc.@ i% ' inted. 0,e Co ies 'a a#ete %'eci5ie% t,e nu#be o5 co'ie% to ' int. 0,e de5ault i% 1. 1et Pre+iew to Tr0e to invo-e ' int ' eview at,e t,an ' inting i##ediatel(. 0,e de5ault i% >alse.
-cti+ePrinter %et% t,e na#e o5 t,e active ' inte . On t,e ot,e ,and* %etting PrintToFile

to 0 ue cau%e% E6cel to ' int to a 5ile. E6cel will ' o#'t t,e u%e 5o t,e na#e o5 t,e out'ut 5ile. ?2n5o tunatel(* t,e e i% no wa( to %'eci5( t,e na#e o5 t,e out'ut 5ile in code.@ 0,e Collate 'a a#ete %,ould be %et to Tr0e to collate #ulti'le #ulti'age co'ie%.

16.2.8 PrintPreview -etho#


0,i% #et,od invo-e% E6celB% ' int ' eview 5eatu e. /t% %(nta6 i%:
Workbook/bject.;rint;review

+ote t,at t,e & int& eview #et,od a''lie% to t,e %a#e %et o5 object% a% t,e & intOut #et,od.

16.2.19 Protect -etho#


0,i% #et,od ' otect% a wo -boo- %o t,at it cannot be #odi5ied. /t% %(nta6 i%:
Workbook/bject.;rotect(Password= Structure= Windows)

0,e #et,od al%o a''lie% to c,a t% and wo -%,eet%* wit, a di55e ent %(nta6. 0,e o'tional Password 'a a#ete %'eci5ie% a 'a%%wo d ?a% a ca%e;%en%itive %t ing@. /5 t,i% a gu#ent i% o#itted* t,e wo -boo- will not e7ui e a 'a%%wo d to un' otect it.

1et t,e o'tional Structure 'a a#ete to Tr0e to ' otect t,e %t uctu e o5 t,e wo -boo-<t,at i%* t,e elative 'o%ition o5 t,e %,eet% in t,e wo -boo-. 0,e de5ault value i% >alse. 1et t,e o'tional Windows 'a a#ete to Tr0e to ' otect t,e wo -boo- window%. 0,e de5ault i% >alse.

16.2.11 ,ea#3nly Pro!erty ;,ea#-3nly .oolean<


0,i% ' o'e t( i% Tr0e i5 t,e wo -boo- ,a% been o'ened a% ead;onl(.

16.2.12 ,e4resh%ll -etho#


0,i% #et,od e5 e%,e% all e6te nal data ange% and 'ivot table% in t,e wo -boo-. 0,e %(nta6 i%:
Workbook/bject."e6reshAll

16.2.1" Save -etho#


0,i% #et,od %i#'l( %ave% an( c,ange% to t,e wo -boo-. /t% %(nta6 i%:
Workbook/bject.Save

16.2.1$ Save%s -etho#


0,i% #et,od %ave% c,ange% to a wo -boo- in t,e %'eci5ied 5ile. 0,e %(nta6 i%:
ex ression.SaveAs(Filename= FileFormat= Password= Write%esPassword= G %ead/nly%ecommended= Create)acku = -ccess(ode= Con*lict%esolution= G -ddTo(ru= TextCodePa!e= TextVisual"ayout)

0,e Filename 'a a#ete %'eci5ie% t,e 5ilena#e to u%e 5o t,e newl( %aved di%- 5ile. /5 a 'at, i% not included* E6cel will u%e t,e cu ent 5olde . 0,e FileFormat 'a a#ete %'eci5ie% t,e 5ile 5o #at to u%e w,en %aving t,e 5ile. /t% value i% one o5 t,e #l>ile>or5at con%tant% de%c ibed in ou di%cu%%ion o5 t,e $ile$o #at ' o'e t(. 0,e Password 'a a#ete %'eci5ie% t,e 'a%%wo d to u%e w,en %aving t,e 5ile and can be %et to an( ca%e;%en%itive %t ing o5 u' to 15 c,a acte %. 0,e Write%esPassword i% a %t ing t,at %'eci5ie% t,e w ite; e%e vation 'a%%wo d 5o t,i% 5ile. /5 a 5ile i% %aved wit, a w ite; e%e vation 'a%%wo d and t,i% 'a%%wo d i% not %u''lied w,en t,e 5ile i% ne6t o'ened* t,e 5ile will be o'ened a% ead;onl(. 4e can %et t,e %ead/nly%ecommended 'a a#ete to Tr0e to di%'la( a #e%%age w,en t,e 5ile i% o'ened* eco##ending t,at t,e 5ile be o'ened a% ead;onl(. 1et t,e Create)acku 'a a#ete to Tr0e to c eate a bac-u' 5ile.

0,e -ccess(ode and Con*lict%esolution 'a a#ete % e5e to %,a ing i%%ue%. 4e e5e t,e inte e%ted eade to t,e E6cel =>A ,el' 5ile 5o detail%.

1et t,e -ddTo(ru 'a a#ete to Tr0e to add t,e wo -boo- to t,e li%t o5 ecentl( u%ed 5ile%. 0,e de5ault value i% >alse. 0,e e#aining 'a a#ete % a e not u%ed in t,e 2.1. Engli%, ve %ion o5 E6cel.

16.2.1' Save+o!y%s -etho#


0,i% #et,od %ave% a co'( o5 t,e wo -boo- to a 5ile but doe% not #odi5( t,e o'en wo -boo- it%el5. 0,e %(nta6 i%:
Workbook/bject.SaveCo3 As(Filename)

w,e e Filename %'eci5ie% t,e 5ilena#e 5o t,e co'( o5 t,e o iginal 5ile.

16.2.15 Save# Pro!erty ;,H0 .oolean<


0,i% ' o'e t( i% Tr0e i5 no c,ange% ,ave been #ade to t,e %'eci5ied wo -boo- %ince it wa% la%t %aved. +ote t,at t,i% ' o'e t( i% eadEw ite* w,ic, #ean% we can %et t,e ' o'e t( to Tr0e even i5 t,e wo -boo- ,a% been c,anged %ince it wa% la%t %aved. A% di%cu%%ed ea lie * we can %et t,i% ' o'e t( to Tr0e* t,en clo%e a #odi5ied wo -boo- wit,out being ' o#'ted to %ave t,e cu ent c,ange%.

$9%* Children of the -orkbook Ob1ect


$igu e 17;5 %,ow% t,e c,ild en o5 t,e Wor)boo) object. ?0,i% i% a e'eat o5 $igu e 17;4.@ Figure 16-'. +hil#ren o4 the 0or1boo1 object

3et u% ta-e a 7uic- loo- at %o#e o5 t,e%e c,ild en. ?4e will di%cu%% t,e 4indow* 4o -%,eet* and 4o -boo-Event% object% late in t,e boo-.@

16.".1 he +usto&*iew 3bject


0,e !u%to#=iew% ' o'e t( etu n% t,e !u%to#=iew% collection. Eac, !u%to#=iew object in t,i% collection e' e%ent% a cu%to# view o5 t,e wo -boo-. !u%to#=iew object% a e ' ett( %t aig,t5o wa d* %o we will ju%t con%ide an e6a#'le. 3oo- at t,e %,eet %,own in $igu e 17;6.

Figure 16-5. Exa&!le o4 the +usto&*iew object

+ow %u''o%e we u%e t,e Auto5ilte co##and to 5ilte on t,e (ea * a% %,own in $igu e 17;7. Figure 16-6. % 4iltere# view

0,e 5ollowing code will give t,i% cu%to# view t,e na#e View.331:
ThisHor8boo8.C0sto5+iews.Add "+iew1EED"

+ow we can di%'la( t,i% view at an( ti#e wit, t,e code:
ThisHor8boo8.C0sto5+iews!+iew1EED.Show

o:
str+iew = "+iew1EED" ActiveHor8boo8.C0sto5+iews(str+iew).Show

16.".2 he Na&es +ollection


A% wit, t,e A''lication object* t,e 4o -boo- object ,a% a +a#e% ' o'e t( t,at etu n% a +a#e% collection. 0,i% collection e' e%ent% t,e +a#e object% a%%ociated wit, t,e wo -boo-. $o detail% on +a#e object%* %ee !,a'te 16.

16."." he Sheets +ollection


0,e 1,eet% ' o'e t( etu n% a 1,eet% collection t,at contain% a 4o -%,eet object 5o eac, wo -%,eet and a !,a t object 5o eac, c,a t%,eet in t,e wo -boo-. 4e will di%cu%% 4o -%,eet object% and !,a t object% late in t,e boo-.

16.".$ he Styles +ollection an# the Style 3bject


A 1t(le object e' e%ent% a %et o5 5o #atting o'tion% 5o a ange. Eac, wo -boo- ,a% a 1t(le% collection containing all 1t(le object% 5o t,e wo -boo-.

0o a''l( a %t(le to a ange* we %i#'l( w ite:


"an4e*bTect.St le = St le9a5e

w,e e StyleName i% t,e na#e o5 a %t(le. 0o c eate a 1t(le object* u%e t,e Add #et,od* w,o%e %(nta6 i%:
Hor8boo8*bTect.Add(Name= )ased/n)

+ote t,at t,e Add #et,od etu n% t,e newl( c eated 1t(le object. 0,e Name 'a a#ete %'eci5ie% t,e na#e o5 t,e %t(le* and t,e o'tional )ased/n 'a a#ete %'eci5ie% a Range object t,at e5e % to a cell w,o%e %t(le i% u%ed a% a ba%i% 5o t,e new %t(le. /5 t,i% a gu#ent i% o#itted* t,e newl( c eated %t(le i% ba%ed on t,e +o #al %t(le. +ote t,at* acco ding to t,e docu#entation* i5 a %t(le wit, t,e %'eci5ied na#e al ead( e6i%t%* t,e Add #et,od will ede5ine t,e e6i%ting %t(le ba%ed on t,e cell %'eci5ied in )ased/n. ?Aoweve * on #( %(%te#* E6cel i%%ue% an e o #e%%age in%tead* %o (ou %,ould c,ec- t,i% ca e5ull(.@ 0,e ' o'e tie% o5 t,e 1t(le object e5lect t,e va iou% 5o #atting 5eatu e%* %uc, a% 5ont na#e* 5ont %iGe* nu#be 5o #at* align#ent* and %o on. 0,e e a e al%o %eve al built;in %t(le%* %uc, a% +o #al* !u enc(* and &e cent. 0,e%e built;in %t(le% can be 5ound in t,e 1t(le na#e bo6 o5 t,e 1t(le dialog bo6 ?unde t,e $o #at #enu@. 0o illu%t ate* t,e 5ollowing code c eate% a %t(le and t,en a''lie% it to an a bit a ( ange o5 t,e cu ent wo -%,eet:
Di5 st As St le . Delete st le i6 it exists >or 'ach st 2n ActiveHor8boo8.St les 26 st.9a5e = "1ordered" Then st.Delete 9ext . Create st le Hith ActiveHor8boo8.St les.Add(9a5e-="1ordered") .1orders(xlTo3).LineSt le = xlDo0ble .1orders(xl1otto5).LineSt le = xlDo0ble .1orders(xlLe6t).LineSt le = xlDo0ble .1orders(xl"i4ht).LineSt le = xlDo0ble .>ont.1old = Tr0e .>ont.9a5e = "arial" .>ont.SiMe = BN 'nd Hith . A33l st le A33lication.ActiveSheet."an4e("A1-1B").St le = "1ordered"

$9%2 Example+ Sorting Sheets in a -orkbook


3et u% add a new utilit( to ou 1RD2til% a''lication. /5 (ou wo - wit, wo -boo-% t,at contain #an( %,eet% ?wo -%,eet% and c,a t%,eet%@* t,en (ou #a( want to %o t t,e %,eet% in al',abetical o de .

0,e ba%i% 5o t,e code to o de t,e %,eet% i% t,e "ove #et,od o5 t,e 4o -%,eet and !,a t object%. /t% %(nta6 i%:
Sheets/bject.Move()e*ore= -*ter)

O5 cou %e* to u%e t,i% #et,od e55ectivel(* we need a %o ted li%t o5 %,eet na#e%. 0,e 5i %t %te' i% to aug#ent t,e )ata1,eet wo -%,eet 5o 1RD2til% b( adding a new ow 5o t,e new utilit(* a% %,own in $igu e 17;8. ?0,e o de o5 t,e ow% in t,i% )ata1,eet i% ba%ed on t,e o de in w,ic, we want t,e ite#% to a''ea in t,e cu%to# #enu.@ Figure 16-7. %ug&enting the (ataSheet wor1sheet

+e6t* we in%e t a new code #odule called basSortSheets* w,ic, will contain t,e code to i#'le#ent t,i% utilit(. 4e %,all include two ' ocedu e% in basSortSheets. 0,e 5i %t ' ocedu e ve i5ie% t,at t,e u%e eall( want% to %o t t,e %,eet%. /5 %o* it call% t,e %econd ' ocedu e* w,ic, doe% t,e wo -. 0,e 5i %t ' ocedu e i% %,own in E6a#'le 17;1. /t di%'la(% t,e dialog bo6 %,own in $igu e 17;8. Exa&!le 16-1. he SortSheets Proce#ure
S0b SortSheets() 26 Ms41ox("Sort the sheets in this wor8boo8Q"= G vb*PCancel A vb,0estion= "Sort Sheets") = vb*P Then SortAllSheets 'nd 26 'nd S0b

0,e action ta-e% 'lace in t,e ' ocedu e %,own in E6a#'le 17;2. 0,e ' ocedu e 5i %t collect% t,e %,eet na#e% in an a a(* t,en 'lace% t,e a a( in a new wo -%,eet. /t t,en u%e% t,e 1o t #et,od ?a''lied to a Range object* di%cu%%ed in !,a'te 19@ to %o t t,e na#e%. 0,en* it e5ill% t,e a a( and 5inall(* eo de % t,e %,eet% u%ing t,e "ove #et,od. Exa&!le 16-2. he Sort%llSheets Proce#ure
S0b SortAllSheets() . Sort wor8sheets Di5 Di5 Di5 Di5 Di5 Di5 wb As Hor8boo8 ws As Hor8sheet rn4 As "an4e cSheets As 2nte4er sSheets() As Strin4 i As 2nte4er

Set wb = ActiveHor8boo8 . :et tr0e di5ension 6or arra cSheets = wb.Sheets.Co0nt "eDi5 sSheets(1 To cSheets)

. >ill arra with wor8sheet na5es >or i = 1 To cSheets sSheets(i) = wb.Sheets(i).9a5e 9ext . Create new sheet and 30t na5es in 6irst col05n Set ws = wb.Hor8sheets.Add >or i = 1 To cSheets ws.Cells(i= 1).+al0e = sSheets(i) 9ext . Sort col05n ws.Col05ns(1).Sort Pe 1-=ws.Col05ns(1)= G *rder1-=xlAscendin4 . "e6ill arra >or i = 1 To cSheets sSheets(i) = ws.Cells(i= 1).+al0e 9ext . Delete extraneo0s sheet A33lication.Dis3la Alerts = >alse ws.Delete A33lication.Dis3la Alerts = Tr0e . "eorder sheets b 5ovin4 each one to the end >or i = 1 To cSheets wb.Sheets(sSheets(i)).Move A6ter-=wb.Sheets(cSheets) 9ext 'nd S0b

Once t,e code i% in%e ted* (ou can %ave t,e /,4'tilsAxls wo -boo- a% an add;in. )onBt 5o get to unload t,e add;in 5i %t* o E6cel will co#'lain.

Chapter $<% The -orksheet Ob1ect


A 4o -%,eet object e' e%ent% an E6cel wo -%,eet. $igu e 18;1 %,ow% t,at 'o tion o5 t,e E6cel object #odel t,at elate% di ectl( to wo -%,eet%. Figure 17-1. he 0or1sheet object

$<%$ Properties and 7ethods of the -orksheet Ob1ect


0able 18;1 %,ow% t,e #e#be % o5 t,e 4o -%,eet object. able 17-1. -e&bers o4 the 0or1sheet 3bject I!,ec-1'ellingSv9Z ) o')own% & eviou% I!ode+a#e EnableAuto$ilte & intOut I)i%'la(Rig,t0o3e5tSv9Z Enable!alculation & int& eview IEvaluate EnableOutlining & otect I&a%te1'ecialSv10Z Enable&ivot0able & otect!ontent% I& intOutSv9Z Enable1election & otect) awingObject% I& otectSv10Z Evaluate & otectionSv10Z I1aveA%Sv10Z $ilte "ode & otection"ode Activate . ou'>o6e% & otect1cena io% A''lication . ou'Object% Oue (0able% A c% A&age> ea-% Range Auto$ilte A('e lin-% Rectangle% Auto$ilte "ode /nde6 Re%etAll&age> ea-% >utton% 3abel% Row% !alculate 3ine% 1aveA% !ell% 3i%t>o6e% 1cena io%

!,a tObject% !,ec->o6e% !,ec-1'elling !i cle/nvalid !i cula Re5e ence !lea A ow% !lea !i cle% !ode+a#e !olu#n% !o##ent% !on%olidation$unction !on%olidationO'tion% !on%olidation1ou ce% !o'( ! eato !u%to#& o'e tie%Sv10Z )elete )i%'la(Auto#atic&age> ea-% )i%'la(&age> ea-% )i%'la(Rig,t0o3e5t ) awingObject% ) awing%

"ailEnvelo'eSv10Z "ove +a#e +a#e% +e6t O3EObject% On!alculate On)ata On)ouble!licOnEnt ( On1,eetActivate On1,eet)eactivate O'tion>utton% Outline Oval% &age1etu' &a ent &a%te &a%te1'ecial &ictu e% &ivot0able% &ivot0able4iGa d

1c i't%Sv9Z 1c ollA ea 1c oll>a % 1elect 1et>ac-g ound&ictu e 1,a'e% 1,owAll)ata 1,ow)ata$o # 1#a t0ag%Sv10Z 1'inne % 1tanda dAeig,t 1tanda d4idt, 0abSv10Z 0e6t>o6e% 0 an%itionE6'Eval 0 an%ition$o #Ent ( 0('e 2n' otect 2%edRange =i%ible =&age> ea-%

"an( o5 t,e #e#be % in 0able 18;1 e6i%t %olel( to etu n t,e c,ild en o5 t,e Wor)sheet object. 0,e%e #e#be % and t,ei etu n t('e% a e %,own in 0able 18;2. able 17-2. -e&bers hat ,eturn 3bjects Name ReturnType A''lication Auto$ilte Range Range Range !o##ent% !u%to#& o'e tie% A&age> ea-% A('e lin-% +a#e% Outline &age1etu' &ivot0able & otection Oue (0able% Range Range 1,a'e%

A''lication Auto$ilte !ell% !i cula Re5e ence !olu#n% !o##ent% !u%to#& o'e tie% A&age> ea-% A('e lin-% +a#e% Outline &age1etu' &ivot0able4iGa d & otection Oue (0able% Range Row% 1,a'e%

1#a t0ag% 0ab 2%edRange =&age> ea-%

1#a t0ag% 0ab Range =&age> ea-%

3et u% di%cu%% %o#e o5 t,e #e#be % in 0able 18;1. Acti#ate method 0,i% #et,od activate% t,e wo -%,eet* a% in:
ThisHor8boo8.Hor8sheets("Sheet1").Activate Auto&ilterMode property

0,i% ' o'e t( i% Tr0e i5 t,e Auto$ilte d o';down a ow% a e cu entl( di%'la(ed on t,e wo -%,eet. ?Al%o %ee t,e $ilte "ode ' o'e t(* di%cu%%ed late in t,i% %ection@. +ote t,at we can %et t,i% ' o'e t( to >alse to e#ove t,e a ow%* but we cannot %et it to Tr0e. 0o di%'la( t,e Auto$ilte a ow%* we u%e t,e Auto$ilte #et,od* w,ic, i% di%cu%%ed in !,a'te 19. (alculate method 0,i% #et,od calculate% all cell% in t,e wo -%,eet. ?+ote t,at t,e #et,od a''lie% to wo -boo-% and %'eci5ic ange% a% well.@ 0,e %(nta6 i% %i#'l(:
Worksheet/bject.Calc0late

(ode?ame property 0,i% ' o'e t( etu n% t,e code na#e 5o t,e wo -%,eet ?it al%o a''lie% to wo -boo- and c,a t object%@. 0,e code na#e can be u%ed in 'lace o5 an( e6' e%%ion t,at etu n% t,e wo -%,eet. 0,e code na#e can al%o be %et in t,e & o'e tie% window. /t i% e5e ed to a% ?na#e@ to di%tingui%, it 5 o# t,e +a#e ' o'e t(. 0o illu%t ate* %u''o%e t,at we ,ave a wo -%,eet w,o%e code na#e i% 1,eet!ode+a#e and w,o%e na#e i% 1,eet+a#e. 0,en t,e 5ollowing a e e7uivalent:
Hor8sheets("Sheet9a5e").Activate SheetCode9a5e.Activate

+ote t,at w,en we 5i %t c eate a wo -%,eet* t,e na#e and code na#e a e t,e %a#e. 0,e two na#e% can t,en be c,anged inde'endentl(. Aoweve * t,e code na#e can be c,anged onl( at de%ign ti#eH it cannot be c,anged wit, code at un ti#e. (opy method 0,e !o'( #et,od ,a% #ulti'le %(nta6e%. 0o co'( a wo -%,eet* we u%e t,e %(nta6:
Worksheet/bject.Co3 ()e*ore= -*ter)

w,e e t,e o'tional )e*ore 'a a#ete i% t,e %,eet be5o e w,ic, t,e co'ied %,eet will be 'laced and t,e -*ter 'a a#ete i% t,e %,eet a5te w,ic, t,e co'ied %,eet will be 'laced. ?Onl( one o5 )e*ore o -*ter i% allowed at one ti#e.@

+ote t,at i5 neit,e )e*ore no -*ter i% %'eci5ied* E6cel will co'( t,e wo -%,eet to a new wo -boo-. 0o illu%t ate* t,e 5ollowing code co'ie% t,e active wo -%,eet and 'lace% t,e co'( at t,e end o5 t,e li%t o5 cu ent wo -%,eet%:
ActiveSheet.Co3 A6ter-=Hor8sheets(Hor8sheets.Co0nt)

"elete method 0,i% #et,od %i#'l( delete% t,e wo -%,eet. 0,e %(nta6 i%:
Worksheet/bject.Delete

Enable(alculation property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* E6cel auto#aticall( ecalculate% t,e wo -%,eet w,en nece%%a (. Ot,e wi%e* t,e u%e #u%t e7ue%t a ecalculation. +ote t,at w,en t,i% ' o'e t( i% 5i %t %et to Tr0e* E6cel will do a ecalculation. E#aluate method 0,e Evaluate #et,od conve t% an E6cel na#e to an object o a value. 4e di%cu%%ed t,e detail% o5 t,i% #et,od in !,a'te 16. &ilterMode property F,ead-$nly BooleanH 0,i% ' o'e t( i% Tr0e i5 t,e wo -%,eet i% in 5ilte #ode. 0,u%* 5o in%tance* i5 t,e Auto$ilte a ow% a e di%'la(ed but no 5ilte ing ,a% ta-en 'lace* t,en Auto$ilte "ode i% Tr0e w,e ea% $ilte "ode i% >alse. Once 5ilte ing i% actuall( 'e 5o #ed* t,en $ilte "ode i% Tr0e. &ut anot,e wa(* t,e $ilte "ode ' o'e t( indicate% w,et,e t,e e a e ,idden ow% due to 5ilte ing. Mo#e method 0,e "ove #et,od #ove% t,e wo -%,eet to anot,e location in t,e wo -boo-. 0,e %(nta6 i%:
Worksheet/bject.Move()e*ore= -*ter)

w,e e t,e 'a a#ete % ,ave t,e %a#e #eaning a% in t,e !o'( #et,od* di%cu%%ed ea lie in t,i% %ection. ?ame property F,GW /tringH 0,i% ' o'e t( etu n% o %et% t,e na#e o5 t,e wo -%,eet* a% a %t ing. ?ames property 0,i% ' o'e t( etu n% t,e +a#e% collection e' e%enting all t,e wo -%,eet;%'eci5ic na#e%. $o #o e on +a#e object%* %ee !,a'te 16. Paste/pecial method

0,i% #et,od 'a%te% t,e content% o5 t,e !li'boa d onto t,e wo -%,eet* u%ing a %'eci5ied 5o #at. 0,e #o%t co##onl( u%ed %(nta6 i% %i#'l(:
Worksheet/bject.;asteS3ecial(Format)

w,e e Format %'eci5ie% t,e 5o #at o5 t,e data to 'a%te* a% a %t ing. $o in%tance* t,e 5ollowing code 'a%te% data in 4o d docu#ent 5o #at ?a%%u#ing t,at it e6i%t% on t,e !li'boa d@:
ActiveSheet.;asteS3ecial "Microso6t Hord Doc05ent"

0o lea n t,e %(nta6 o5 ot,e Format %t ing%* (ou can co'( t,e de%i ed object and t,en c,ec- E6celB% &a%te 1'ecial dialog bo6. +ote t,at we #u%t %elect t,e de%tination ange be5o e u%ing t,e &a%te1'ecial #et,od. Print$ut method 0,e & intOut #et,od ' int% a wo -%,eet. ?0,e #et,od al%o a''lie% to 4o -boo- and Range object%.@ 0,e %(nta6 i%:
Worksheet/bject.;rint*0t(From= To= Co ies= G Pre+iew= -cti+ePrinter= PrintToFile= Collate)

+ote t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional. 0,e From 'a a#ete %'eci5ie% t,e 'age nu#be o5 t,e 5i %t 'age to ' int* and t,e 0o 'a a#ete %'eci5ie% t,e la%t 'age to ' int. /5 o#itted* t,e enti e object ? ange* wo -%,eet* etc.@ i% ' inted. 0,e Co ies 'a a#ete %'eci5ie% t,e nu#be o5 co'ie% to ' int. 0,e de5ault i% 1. 1et Pre+iew to Tr0e to invo-e ' int ' eview at,e t,an ' inting i##ediatel(. 0,e de5ault i% >alse.
-cti+ePrinter %et% t,e na#e o5 t,e active ' inte .

1etting PrintToFile to Tr0e cau%e% E6cel to ' int to a 5ile. E6cel will ' o#'t t,e u%e 5o t,e na#e o5 t,e out'ut 5ile. ?2n5o tunatel(* t,e e i% no wa( to %'eci5( t,e na#e o5 t,e out'ut 5ile in code.@ 0,e Collate 'a a#ete %,ould be %et to Tr0e to collate #ulti'le #ulti'age co'ie%. PrintPre#iew method 0,i% #et,od invo-e% E6celB% ' int ' eview 5eatu e 5o t,e wo -%,eet. /t% %(nta6 i%:
Worksheet/bject.;rint;review

Protect method 0,i% #et,od ' otect% a wo -%,eet 5 o# #odi5ication. /t% %(nta6 i%:
Worksheet/bject.;rotect(Password= Drawin!/bjects= G

Contents= Scenarios= 7ser1nter*ace/nly)

?+ote t,at t,e %(nta6 va ie% 5 o# t,e %a#e #et,od o5 t,e 4o -boo- object.@ 0,e o'tional Password 'a a#ete i% a %t ing t,at %'eci5ie% a ca%e;%en%itive 'a%%wo d 5o t,e wo -%,eet. 0,e o'tional Drawin!/bjects 'a a#ete %,ould be %et to Tr0e to ' otect %,a'e%. 0,e de5ault value i% >alse. 0,e o'tional Contents 'a a#ete %,ould be %et to Tr0e* t,e de5ault* to ' otect t,e cell% in t,e wo -%,eet. 0,e o'tional Scenarios 'a a#ete %,ould be %et to Tr0e* t,e de5ault* to ' otect %cena io%. 0,e Protect #et,od allow% inde'endent ' otection o5 cell% 5 o# c,ange% b( t,e u%e and b( code. /n 'a ticula * i5 7ser1nter*ace/nly i% %et to Tr0e* t,en t,e u%e cannot #a-e c,ange% to t,e wo -%,eet* but c,ange% can be #ade t, oug, code. On t,e ot,e ,and* i5 7ser1nter*ace/nly i% >alse ?t,e de5ault@* t,en neit,e t,e u%e no t,e ' og a##e can alte t,e wo -%,eet. +ote t,at it i% not t,e #ac o% t,e#%elve% t,at a e ' otected* a% t,e ,el' docu#entation %ee#% to indicate. Rat,e * t,e wo -%,eet i% ' otected 5 o# t,e effect o5 t,e #ac o%. +ote al%o t,at i5 t,e 7ser1nter*ace/nly a gu#ent i% %et to Tr0e w,en ' otecting a wo -%,eet and t,en t,e wo -boo- i% %aved* t,e enti e wo -%,eet ?not ju%t t,e inte 5ace@ will be ' otected w,en t,e wo -boo- i% eo'ened. 0o un' otect t,e wo -%,eet but eenable u%e inte 5ace ' otection* we #u%t ea''l( t,e & otect #et,od wit, 7ser1nter*ace/nly %et to Tr0e. ProtectionMode property F,ead-$nlyH 0,i% ' o'e t( i% Tr0e i5 u%e ;inte 5ace;onl( ' otection i% tu ned on ?via t,e & otect #et,od@. /t% de5ault value i% >alse. /a#eAs method 0,i% #et,od %ave% c,ange% to t,e wo -%,eet in a di55e ent 5ile. /t% %(nta6 i%:
Worksheet/bject.SaveAs(Filename= FileFormat= Password= G Write%esPassword= %ead/nly%ecommended= Create)acku = G -ddTo(ru= TextCodePa!e= TextVisual"ayout)

0,e Filename 'a a#ete %'eci5ie% t,e 5ilena#e to u%e 5o t,e newl( %aved di%- 5ile. /5 a 'at, i% not included* E6cel will u%e t,e cu ent 5olde . 0,e FileFormat 'a a#ete %'eci5ie% t,e 5ile 5o #at to u%e w,en %aving t,e 5ile. /t% value i% one o5 t,e #l>ile>or5at con%tant% de%c ibed in ou di%cu%%ion o5 t,e $ile$o #at ' o'e t( in !,a'te 17. 0,e Password 'a a#ete %'eci5ie% t,e 'a%%wo d to u%e w,en %aving t,e 5ile and can be %et to an( ca%e;%en%itive %t ing o5 u' to 15 c,a acte %.

0,e Write%esPassword 'a a#ete i% a %t ing t,at %'eci5ie% t,e w ite; e%e vation 'a%%wo d 5o t,i% 5ile. /5 a 5ile i% %aved wit, a w ite; e%e vation 'a%%wo d and t,i% 'a%%wo d i% not %u''lied w,en t,e 5ile i% ne6t o'ened* t,e 5ile will be o'ened a% ead;onl(. 4e can %et t,e %ead/nly%ecommended 'a a#ete to Tr0e to di%'la( a #e%%age w,en t,e 5ile i% o'ened* eco##ending t,at t,e 5ile be o'ened a% ead;onl(. 1et t,e Create)acku 'a a#ete to Tr0e to c eate a bac-u' 5ile.

1et t,e -ddTo(ru 'a a#ete to Tr0e to add t,e wo -boo- to t,e li%t o5 ecentl( u%ed 5ile%. 0,e de5ault value i% >alse. 0,e e#aining 'a a#ete % a e not u%ed in t,e 2.1. Engli%, ve %ion o5 E6cel. /crollArea property 0,i% ' o'e t( etu n% o %et% t,e ange w,e e %c olling and cell %election i% allowed. 0,e value %,ould be an A1;%t(le ange e5e ence. $o in%tance* t,e code:
ActiveSheet.ScrollArea = "A1-1277"

allow% cell %election and %c olling onl( wit,in t,e ange A1:>200. 0o e#ove an( e%t iction% on cell %election and %c olling* %et t,i% ' o'e t( to an e#'t( %t ing* a% in:
ActiveSheet.ScrollArea = ""

+ote t,at %etting t,e %c oll a ea ,a% not,ing to do wit, 5 eeGing 'ane%. /elect method 0,i% #et,od %elect% t,e wo -%,eet. 0,i% i% not t,e %a#e a% #a-ing it active t, oug, t,e Activate #et,od. /n 5act* %eve al %,eet% can be %elected at one ti#e ?to delete t,e#* 5o in%tance@. 0,e %(nta6 i%:
Worksheet/bject.Select("e3lace)

w,e e %e lace i% %et to Tr0e to e'lace t,e cu ent %election wit, t,e %'eci5ied wo -%,eet* at,e t,an including t,e wo -%,eet in t,e cu ent %election. /etBac)groundPicture method 0,i% #et,od %et% t,e bac-g ound g a',ic 5o a wo -%,eet ?o c,a t@. 0,e %(nta6 i%:
Worksheet/bject.Set1ac84ro0nd;ict0re(FileName)

w,e e FileName i% t,e na#e o5 t,e g a',ic 5ile to u%e 5o t,e bac-g ound. /how"ata&orm method 0,i% #et,od di%'la(% t,e data 5o # a%%ociated wit, t,e wo -%,eet. +ote t,at 5o t,e 1,ow)ata$o # #et,od to wo - wit,out gene ating an e o * E6cel #u%t be able to dete #ine t,at t,e cu ent %election i% 'a t o5 a li%t. $o in5o #ation on t,e u%e o5 data 5o #%* %ee t,e E6cel 8 ,el' to'ic 8.uideline% 5o c eating a li%t on a wo -%,eet8 o t,e E6cel 9 ,el' to'ic 8About data 5o #%.8

0,e %(nta6 o5 t,i% #et,od i% %i#'l(:


Worksheet/bject.ShowData>or5

+ote t,at t,e ' ocedu e in w,ic, t,e 1,ow)ata$o # #et,od i% called will 'au%e w,ile t,e data 5o # i% di%'la(ed. 4,en t,e data 5o # i% clo%ed* t,e ' ocedu e will e%u#e at t,e line 5ollowing t,e call to 1,ow)ata$o #. ?/n ot,e wo d%* t,e data 5o # i% modal.@ $igu e 18;2 illu%t ate% t,e data 5o # 5o a wo -%,eet. Figure 17-2. % #ata 4or&

'nprotect method 0,i% #et,od e#ove% ' otection 5 o# a wo -%,eet. /t ,a% no e55ect i5 t,e wo -%,eet i% not ' otected. 0,e %(nta6 i%:
Worksheet/bject.<n3rotect(Password)

w,e e Password i% t,e 'a%%wo d u%ed to ' otect t,e wo -%,eet ?i5 an(@. /5 we o#it t,i% a gu#ent 5o a %,eet t,at i% 'a%%wo d;' otected* E6cel will ' o#'t t,e u%e 5o t,e 'a%%wo d. 'sed,ange property 0,i% o%ten%ibl( ve ( u%e5ul ' o'e t( etu n% a Range object t,at e' e%ent% t,e %#alle%t ectangula egion t,at enco#'a%%e% an( cu entl( u%ed cell%. 2n5o tunatel(* t,e 2%edRange ' o'e t( ,a% ,ad a at,e oc-( ,i%to ( in 'a%t ve %ion% o5 E6cel* and #( e6'e ience i% t,at t,e ' oble#% ,ave not been co#'letel( e%olved in E6cel 97. ?2n5o tunatel(* / -now o5 no %ingle te%t to c,ec- t,e eliabilit( o5 t,i% ' o'e t(* and / ,ave not (et u%ed E6cel 9 long enoug, to #a-e a de5initive %tate#ent about t,i% ve %ion.@ 0,u%* / %t ongl( %ugge%t t,at (ou u%e t,i% #et,od wit, caution* 5o it %o#eti#e% %ee#% to include cell% t,at once ,ad content% but ,ave %ince been co#'letel( clea ed.

At t,e end o5 !,a'te 19* we will give an e6a#'le 5unction t,at can be u%ed to co#'ute t,e co ect u%ed ange. Visible property 0,i% ' o'e t( etu n% Tr0e i5 t,e wo -%,eet i% vi%ible and >alse ot,e wi%e. Aoweve * in addition to %etting t,i% ' o'e t( to Tr0e o >alse* we can al%o %et t,i% ' o'e t( to xl+er ?idden* in w,ic, ca%e t,e onl( wa( to #a-e t,e wo -%,eet vi%ible i% b( %etting t,i% ' o'e t( to Tr0e in code. Aence* t,e u%e cannot #a-e t,e wo -%,eet vi%ible.

$<%' Children of the -orksheet Ob1ect


3et u% di%cu%% a 5ew o5 t,e c,ild en o5 t,e 4o -%,eet object. Ot,e % will be di%cu%%ed in late c,a'te %. (omments 0,e !o##ent% ' o'e t( etu n% t,e !o##ent% collection* w,ic, con%i%t% o5 all !o##ent object% ?co##ent%@ in t,e wo -%,eet. 4e will di%cu%% t,e !o##ent object in !,a'te 19. !he ?ames collection 4e di%cu%%ed t,e +a#e% collection and +a#e object% in !,a'te 16* and %o we e5e t,e eade to t,at ea lie di%cu%%ion. !he $utline ob%ect 0o illu%t ate E6cel outlining u%ing code* con%ide t,e wo -%,eet %,own in $igu e 18;3. Ou goal i% to ' oduce t,e outline in $igu e 18;4. Figure 17-". /llustrating Excel outlines

Figure 17-$. he en# result

0,e 5i %t %te' in obtaining t,e outline in $igu e 18;4 i% to %et t,e ' o'e tie% o5 t,e Outline object 5o t,i% wo -%,eet. 0,e Outline ' o'e t( o5 t,e 4o -%,eet object etu n% an Outline object* %o we begin wit,:
Hith ActiveSheet.*0tline .S055ar "ow = xl1elow .A0to5aticSt les = >alse 'nd Hith

1etting t,e 1u##a (Row ' o'e t( to xl1elow tell% E6cel t,at ou %u##a ( ow% ?t,e %ubtotal and total ow%@ lie below t,e detailed data. 0,u%* E6cel will 'lace t,e e6'an%ionEcont action button% ?t,e %#all button% di%'la(ing #inu% %ign% in $igu e 18;4@ at t,e a'' o' iate ow%. 1etting Auto#atic1t(le% to >alse ' event% E6cel 5 o# ta#'e ing wit, ou 5o #atting. Ot,e wi%e* E6cel would e#ove t,e bold5acing on t,e %u##a ( ow%. A% (ou can %ee in $igu e 18;4* we want to #a-e t,e 5ollowing g ou'ing%:
"ows 2%L "ows F%E "ows 2%11

$o t,i%* we u%e t,e . ou' #et,od o5 t,e Range object. /n 'a ticula * t,e 5ollowing code acco#'li%,e% t,e de%i ed g ou'ing* e%ulting in $igu e 18;4:
Hith ActiveSheet ."ows("2-L").:ro03 ."ows("F-E").:ro03 ."ows("2-11").:ro03 'nd Hith

+ote t,at t,e 1u##a (!olu#n ' o'e t( o5 t,e Outline object gove n% t,e location o5 t,e e6'an%ionEcont action button% w,en colu#n% g ou'ed. 0o e6'and o colla'%e level%* t,e u%e can clic- t,e %#all nu#be ed button% at t,e to' o5 t,e le5t#o%t colu#n in $igu e 18;4. !lic-ing on button nu#be 9 e%ult% in all level% above 9 being co#'letel( e6'anded and all level% below and including 9 being co#'letel( cont acted. 0,u%* all ow% at level 9 and above a e #ade vi%ible* but no level% below 9 a e vi%ible.

0,e %a#e t,ing can be acco#'li%,ed u%ing t,e /howLe#els #et,od o5 t,e $utline object* w,o%e %(nta6 i%:
*0tline*bTect.ShowLevels(%ow"e+els= Column"e+els)

$o in%tance* t,e code:


ActiveSheet.*0tline.ShowLevels 2

i% e7uivalent to clic-ing on t,e button labeled 2 and ,a% t,e e55ect o5 %,owing all level% above and including level 2* a% 'ictu ed in $igu e 18;5. Figure 17-'. 3utline colla!se# to level 2

!he Page/etup ob%ect 0,e &age1etu' object e' e%ent% t,e 'age 5o #atting ?%uc, a% #a gin% and 'a'e %iGe@ o5 an E6cel wo -%,eet. Eac, o5 t,e 'age;5o #atting o'tion% i% %et b( %etting a co e%'onding ' o'e t( o5 t,e Page/etup object. 0,e &age1etu' ' o'e t( o5 t,e Wor)sheet object etu n% t,e wo -%,eetB% &age1etu' object. 0,e ' o'e tie% and #et,od% o5 t,e &age1etu' object a e %,own in 0able 18;3. ?All o5 t,e ite#% in 0able 18;3 a e ' o'e tie% e6ce't t,e & intOualit( #et,od.@ "o%t o5 t,e #e#be % in 0able 18;3 a e %el5;e6'lanato ( ?and ,old no eal %u ' i%e%@* %o we will not di%cu%% t,e#. able 17-". -e&bers o4 the PageSetu! 3bject A''lication $it0o&age%4ide & intE o %Sv10Z >lac-And4,ite $oote "a gin & int. idline% >otto#"a gin Aeade "a gin & intAeading% !ente $oote 3e5t$oote & int+ote% !ente $oote &ictu eSv10Z 3e5t$oote &ictu eSv10Z & intOualit( !ente Aeade 3e5tAeade & int0itle!olu#n% !ente Aeade &ictu eSv10Z 3e5tAeade &ictu eSv10Z & int0itleRow% !ente Ao iGontall( 3e5t"a gin Rig,t$oote !ente =e ticall( O de Rig,t$oote &ictu eSv10Z !,a t1iGe O ientation Rig,tAeade ! eato &a'e 1iGe Rig,tAeade &ictu eSv10Z ) a5t &a ent Rig,t"a gin $i %t&age+u#be & intA ea 0o'"a gin $it0o&age%0all & int!o##ent% [oo# 0o illu%t ate* t,e 5ollowing code %et% %o#e o5 t,e ' o'e tie% o5 t,e active wo -%,eet:

Hith ActiveSheet.;a4eSet03 .Le6tMar4in = A33lication.2nchesTo;oints(1) ."i4htMar4in = A33lication.2nchesTo;oints(1) .;rintTitle"ows = "A1" .;a3erSiMe = xl;a3erLetter 'nd Hith

+ote t,e u%e o5 t,e 2nchesTo;oints 5unction* w,ic, i% e7ui ed i5 we want to e6' e%% unit% in inc,e%* %ince #o%t o5 t,e 5o #atting ' o'e tie% e7ui e #ea%u e#ent in 'oint%. Re5e ing to $igu e 18;6* t,e & int0itleRow% ' o'e t( will cau%e t,e wo d Re'o t* w,ic, lie% in cell A1* to a''ea on eac, 'age o5 t,e ' intout. Figure 17-5. % wor1sheet an# the Print itle,ows !ro!erty

$<%* Protection in Excel @P


E6cel D& int oduce% %o#e additional ' otection 5eatu e%* beginning wit, t,e & otection object* w,ic, we di%cu%% ne6t.

17.".1 he Protection 3bject


4,en (ou ' otect a wo -%,eet* E6cel 'e #it% (ou to %'eci5( t,at ce tain o'e ation% a e %till 'e #itted on unloc-ed cell%. At t,e u%e level* t,i% i% done t, oug, t,e & otection dialog bo6. At t,e ' og a##ing level* it i% done t, oug, t,e ' o'e tie% o5 t,e & otection object. "o%t o5 t,e%e ' o'e tie% ?li%ted below@ a e %el5;e6'lanato (. +ote t,at all o5 t,e%e ' o'e tie% e6ce't AllowEditRange% a e >oolean.
C C C C C C C C C C C C

Allow)eleting!olu#n% Allow)eletingRow% AllowEditRange% Allow$ilte ing Allow$o #atting!ell% Allow$o #atting!olu#n% Allow$o #attingRow% Allow/n%e ting!olu#n% Allow/n%e tingA('e lin-% Allow/n%e tingRow% Allow1o ting Allow2%ing&ivot0able%

$o e6a#'le* i5 t,e Allow1o ting ' o'e t( i% Tr0e* t,en u%e % can %till %o t unloc-ed cell% in a ' otected wo -%,eet. 0,e AllowEditRange% ' o'e t( etu n% an AllowEditRange% object* di%cu%%ed %e'a atel( in t,e te6t.

17.".2 he %llowE#it,ange 3bject


0,e AllowEditRange object allow% a %'eci5ied ange o5 cell% on a wo -%,eet to be 'a%%wo d ' otected 5 o# editing. Once a ange ,a% been ' otected in t,i% wa(* and the entire wor)sheet has been protected* an( atte#'t at editing cell% in t,at ange will e7ui e t,e 'a%%wo d. Ae e i% %o#e code t,at a%%ign% a 'a%%wo d to a ange on t,e active wo -%,eet. /t al%o de#on%t ate% t,e u%e o5 t,e AllowEditRange% collection.
S0b ;rotect"an4e() Di5 ws As Hor8sheet Di5 i As 2nte4er Set ws = A33lication.ActiveSheet . "e5ove 3rotection ws.<n3rotect . Delete all c0rrent 3rotection ran4es .Ms41ox ws.;rotection.Allow'dit"an4es.Co0nt >or i = 1 To ws.;rotection.Allow'dit"an4es.Co0nt Deb04.;rint ws.;rotection.Allow'dit"an4es(i).Title ws.;rotection.Allow'dit"an4es(i).Delete 9ext . Add a 3rotection ran4e ws.;rotection.Allow'dit"an4es.Add G Title-="?eadin4s"= G "an4e-="an4e("A1-AL")= G ;assword-="hide" . ;rotect sheet (else 3rotection ran4e is not enabled) ws.;rotect 'nd S0b

0,e ' o'e tie% o5 t,e AllowEditRange object a e: ,ange Retu n% o %et% t,e ange a%%ociated wit, t,e AllowEditRange object. !itle Retu n% o %et% t,e title ?i.e. na#e@ o5 t,e ange a%%ociated wit, t,e AllowEditRange object. 'sers Retu n% t,e collection o5 2%e Acce%%Object% a%%ociated wit, t,e AllowEditRange object. $o #o e on t,i%* %ee t,e %ection on t,e 2%e Acce%% object.

0,e #et,od% o5 t,e AllowEditRange object a e: (hangePassword !,ange% t,e 'a%%wo d a%%ociated wit, t,e AllowEditRange object. "elete )elete% t,e AllowEditRange object. 'nprotect 2n' otect% t,e wo -boo-.

17."." he 2ser%ccess 3bjects


2%e Acce%% object% allow ce tain u%e % to acce%% a ' otected ange wit,out e7ui ing t,e 'a%%wo d. $o in%tance* i5 (ou u%e na#e i% ste#e* t,en t,e 5ollowing code will allow (ou to acce%% ' otected ange%:
S0b Add<ser() Di5 ws As Hor8sheet Di5 0a As <serAccess Set ws = A33lication.ActiveSheet . 9*T'- Sheet 50st be 0n3rotected 6or this code to wor8! ws.<n3rotect Set 0a = ws.;rotection.Allow'dit"an4es(1).<sers.Add("steve"= Tr0e) 'nd S0b

+ote t,at t,e wo -%,eet #u%t be un' otected 5o t,i% code to un wit,out e o . 0,e 2%e Acce%% object ,a% but t, ee #e#be %: t,e AllowEdit >oolean ' o'e t(* t,e ead;onl( +a#e ' o'e t(* and t,e )elete #et,od. 0,e 2%e Acce%%3i%t collection ,old% t,e cu ent 2%e Acce%% object%.

$<%2 Example+ Printing Sheets


4e can now i#'le#ent t,e & int1,eet% 5eatu e o5 ou /,4'tils a''lication. Recall t,at at t,e ' e%ent ti#e* t,i% & int utilit(* located in t,e PrintAutl add;in* %i#'l( di%'la(% a #e%%age bo6. 0o i#'le#ent t,i% 5eatu e* we want t,e utilit( to 5i %t di%'la( a dialog bo6* a% %,own in $igu e 18;7. 0,e li%t bo6 contain% a li%t o5 all %,eet% in t,e active wo -boo-. 0,e u%e can %elect one o #o e %,eet% and ,it t,e & int button to ' int t,e%e %,eet%. Figure 17-6. Print sheets #ialog

0,e %te'% to c eate t,e ' int utilit( a e a% 5ollow%: all t,e action ta-e% 'lace in t,e PrintAxls wo -boo-* %o o'en t,i% wo -boo-. 4,en t,e c,ange% a e 5ini%,ed* (ou will need to %ave PrintAxls a% PrintAutl a% well. /5 PrintAutl i% loaded* t,e onl( wa( to unload it i% to unload t,e add;in /,4'tilsAxla ?i5 it i% loaded@ and clo%e t,e wo -boo- /,4'tilsAxls ?i5 it i% o'en@.

17.$.1 +reate the 2serFor&


! eate t,e dialog %,own in $igu e 18;7 in t,e PrintAxls wo -boo-. +a#e t,e dialog dl!PrintSheets and %et it% !a'tion ' o'e t( to 8& int 1,eet%.8 0,en c,ange t,e ;rintSheets ' ocedu e to:
;0blic S0b ;rintSheets() dl4;rintSheets.Show 'nd S0b

0,e dl!PrintSheets dialog ,a% two co##and button% and one li%t bo6:
dl4;rintSheets.Show

17.$.1.1 )ist box &lace a li%t bo6 on t,e 5o # a% in $igu e 18;7. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 18;4. able 17-$. Non#e4ault Pro!erties o4 the )ist .ox Property Value +a#e l%t1,eet% 0ab/nde6 0 "ulti1elect 5 #"ulti1electE6tended 4,en t,e !ancel ' o'e t( o5 t,e cmdCancel button i% %et to Tr0e* t,e button i% 8clic-ed8 w,en t,e u%e ,it% t,e E%ca'e -e(. 0,u%* t,e E%ca'e -e( will di%#i%% t,e ' int dialog. 0,e "ulti1elect ' o'e t( i% %et to 6r5M0ltiSelect'xtended %o t,at t,e u%e can u%e t,e !ont ol -e( to %elect #ulti'le ?'o%%ibl( noncon%ecutive@ ent ie% and t,e %,i5t -e( to %elect #ulti'le con%ecutive ent ie%. 0,e 0ab/nde6 ' o'e t( dete #ine% not onl( t,e o de in w,ic, t,e cont ol% a e vi%ited a% t,e u%e ,it% t,e 0ab -e(* but al%o dete #ine% w,ic, cont ol ,a% t,e initial 5ocu%. 1ince we want t,e initial 5ocu% to be on t,e li%t bo6* we %et it% tab inde6 to 0. 17.$.1.2 Print button &lace a co##and button on t,e 5o # a% in $igu e 18;7. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 18;5.

+a#e Accele ato !a'tion 0ab/nde6

able 17-'. Non#e4ault Pro!erties o4 the Print .utton Property Value c#d& int & & int 1

17.$.1." +ancel button &lace anot,e co##and button on t,e 5o # a% in $igu e 18;7. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 18;6. able 17-5. Non#e4ault Pro!erties o4 the +ancel .utton Property Value c#d!ancel ! !ancel 2 0 ue

+a#e Accele ato !a'tion 0ab/nde6 !ancel

17.$.2 +reate the +o#e .ehin# the 2serFor&


+ow it i% ti#e to c eate t,e code be,ind t,e%e cont ol%. 17.$.2.1 he (eclarations section 0,e )ecla ation% %ection o5 t,e dl!PrintSheets 2%e $o # %,ould contain decla ation% o5 t,e #odule;level va iable%* a% %,own in E6a#'le 18;1. Exa&!le 17-1. -o#ule-)evel *ariable (eclarations
*3tion 'x3licit Di5 cSheets As 2nte4er Di5 sSheet9a5es() As Strin4

17.$.2.2 +ancel button co#e 0,e !ancel button code i% %,own in E6a#'le 18;2. Exa&!le 17-2. he c&#+ancelG+lic1 Event Can#ler
;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b

17.$.2." Print button co#e 0,e & int button call% t,e #ain ' int ' ocedu e and t,en unload% t,e 5o #H it% %ou ce code i% %,own in E6a#'le 18;3. Exa&!le 17-". he c&#PrintG+lic1 Event Can#ler

;rivate S0b c5d;rintGClic8() ;rintSelectedSheets <nload Me 'nd S0b

17.$.2.$ he For&>s /nitiali?e event 0,e /nitialiGe event o5 t,e 2%e $o # i% t,e 'lace to 5ill t,e li%t bo6 wit, a li%t o5 %,eet%. Ou a''lication u%e% a #odule;level a a(* sSheetNames* to ,old t,e %,eet na#e% and a #odule;level intege va iable* cSheets* to ,old t,e %,eet countH bot, we e de5ined in E6a#'le 18;1. 4e 5ill t,e%e va iable% in t,e /nitialiGe event and t,en u%e t,e a a( to 5ill t,e li%t* a% E6a#'le 18;4 %,ow%. 0,e va iable% a e u%ed again in t,e #ain ' int ' ocedu e* w,ic, i% w,( we ,ave decla ed t,e# at t,e #odule level. +ote t,e u%e o5 t,e "eDi5 %tate#ent to edi#en%ion t,e a a(%. 0,i% i% nece%%a ( %ince we do not -now at t,e out%et ,ow #an( %,eet% t,e e a e in t,e wo -boo-. Exa&!le 17-$. he 2serFor&>s /nitiali?e Event Proce#ure
;rivate S0b <ser>or5G2nitialiMe() Di5 ws As *bTect .Hor8sheet "eDi5 sSheet9a5es(1 To 17) lstSheets.Clear cSheets = 7 >or 'ach ws 2n ActiveHor8boo8.Sheets cSheets = cSheets A 1 . "edi5ension arra s i6 necessar 26 <1o0nd(sSheet9a5es) $ cSheets Then "eDi5 ;reserve sSheet9a5es(1 To cSheets A &) 'nd 26 . Save na5e o6 sheet sSheet9a5es(cSheets) = ws.9a5e . Add sheet na5e to list box lstSheets.Add2te5 sSheet9a5es(cSheets) 9ext 'nd S0b

17.$.2.' he PrintSheets !roce#ure 0,e #ain ' inting ' ocedu e i% %,own in E6a#'le 18;5. +ote t,at we ,ave been ca e5ul to deal wit, two %'ecial ca%e%. $i %t* t,e e #a( not be an( %,eet% in t,e wo -boo-. 1econd* t,e u%e #a( ,it t,e & int button wit,out %electing an( %,eet% in t,e li%t bo6. /t i% i#'o tant to note al%o t,at li%t bo6e% a e 0;ba%ed* #eaning t,at t,e 5i %t ite# i% ite# 0. Aoweve * ou a a(% a e 1;ba%ed ?t,e 5i %t ite# i% ite# 1@* %o we #u%t ta-e t,i% into account w,en we #ove 5 o# a %election to an a a( #e#be H to wit: %election i co e%'ond% to a a( inde6 iF1. Exa&!le 17-'. he PrintSelecte#Sheets Proce#ure
S0b ;rintSelectedSheets() Di5 i As 2nte4er Di5 b9oneSelected As 1oolean

b9oneSelected = Tr0e 26 cSheets = 7 Then Ms41ox "9o sheets in this wor8boo8."= vb'xcla5ation 'xit S0b 'lse >or i = 7 To lstSheets.ListCo0nt % 1 26 lstSheets.Selected(i) Then b9oneSelected = >alse . List box is 7%based= arra s are 1%based ActiveHor8boo8.Sheets(sSheet9a5es(i A 1)).;rint*0t 'nd 26 9ext 'nd 26 26 b9oneSelected Then Ms41ox "9o sheets have been selected 6ro5 the list box."= vb'xcla5ation 'nd 26 'nd S0b

Chapter $=% The 6ange Ob1ect


0,e Range object i% one o5 t,e wo -,o %e object% in t,e E6cel object #odel. 1i#'l( 'ut* to wo - wit, a 'o tion o5 an E6cel wo -%,eet* we gene all( need to 5i %t identi5( t,at 'o tion a% a Range object. A% "ic o%o5t 'ut% it* a Range object 8Re' e%ent% a cell* a ow* a colu#n* a %election o5 cell% containing one o #o e contiguou% bloc-% o5 cell%* o a 3;) ange.8 0able 19;1 %,ow% t,e 158 #e#be % o5 t,e Range object. able 18-1. -e&bers o4 the ,ange 3bject End &,onetic Enti e!olu#n &,onetic%Sv9Z Enti eRow &ivot!ellSv10Z E o %Sv10Z &ivot$ield $ill)own &ivot/te# $ill3e5t &ivot0able $illRig,t & ecedent% $ill2' & e5i6!,a acte $ind & eviou% $ind+e6t & intOut $ind& eviou% & int& eview $ont Oue (0able $o #at!ondition% Range $o #ula ReadingO de $o #ulaA a( Re#ove1ubtotal $o #ulaAidden Re'lace $o #ula3abel Re%iGe $o #ula3ocal Row $o #ulaR1!1 Row)i55e ence% $o #ulaR1!13ocal RowAeig,t

I)e5ault I+ewEnu# I&a%te1'ecialSv10Z I& intOutSv9Z Activate Add!o##ent Add/ndent Add e%% Add e%%3ocal Advanced$ilte AllowEditSv10Z A''lication A''l(+a#e% A''l(Outline1t(le% A ea% Auto!o#'lete Auto$ill Auto$ilte Auto$it Auto$o #at

AutoOutline >o de A ound >o de % !alculate !ell% !,a acte % !,ec-1'elling !lea !lea !o##ent% !lea !ontent% !lea $o #at% !lea +ote% !lea Outline !olu#n !olu#n)i55e ence% !olu#n% !olu#n4idt, !o##ent !on%olidate !o'( !o'($ o#Reco d%et !o'(&ictu e !ount ! eate+a#e% ! eate&ubli%,e ! eato !u entA a( !u entRegion !ut )ata1e ie% )elete )e'endent% )ialog>o6 )i ect)e'endent% )i ect& ecedent% )i t(Sv10Z EditionO'tion%

$unction4iGa d .oal1ee. ou' Aa%A a( Aa%$o #ula Aeig,t Aidden Ao iGontalAlign#ent A('e lin-% /)Sv9Z /ndent3evel /n%e t /n%e t/ndent /nte io /te# Nu%ti5( 3e5t 3i%tAeade Row% 3i%t+a#e% 3ocation/n0able 3oc-ed "e ge "e geA ea "e ge!ell% +a#e +avigateA ow +e6t +ote0e6t +u#be $o #at +u#be $o #at3ocal O55%et O ientation Outline3evel &age> ea&a ent &a %e &a%te1'ecial

Row% Run 1elect 1et&,oneticSv9Z 1,ow 1,ow)e'endent% 1,ow)etail 1,owE o % 1,ow& ecedent% 1, in-0o$it 1#a t0ag%Sv10Z 1o t 1o t1'ecial 1ound+ote 1'ea-Sv10Z 1'ecial!ell% 1t(le 1ub%c ibe0o 1ubtotal 1u##a ( 0able 0e6t 0e6t0o!olu#n% 0o' 2ng ou' 2n"e ge 2%e1tanda dAeig,t 2%e1tanda d4idt, =alidation =alue =alue2 =e ticalAlign#ent 4idt, 4o -%,eet 4 a'0e6t

Ou 'lan in t,i% c,a'te i% 5i %t to e6'lo e wa(% o5 de5ining Range object%. 0,en we will di%cu%% #an( o5 t,e ' o'e tie% and #et,od% o5 t,i% object* a% indicated in 0able 19;1. A% we ,ave #entioned* ou goal i% not to cove all a%'ect% o5 t,e E6cel object #odel* but to cove t,e #ain 'o tion% o5 t,e #odel and to ' ovide (ou wit, a %u55icient 5oundation %o t,at (ou can 'ic- u' w,ateve el%e (ou #a( need b( u%ing t,e ,el' %(%te#.

$=%$ The 6ange Ob1ect as a Collection


0,e Range object i% at,e unu%ual in t,at it o5ten act% li-e a collection object a% well a% a noncollection object. $o in%tance* it ,a% an /te# #et,od and a !ount ' o'e t(. On t,e ot,e ,and* t,e Range object ,a% #an( #o e noncollection;t('e #e#be % t,an i% t('ical o5 collection object%. /n 'a ticula * t,e ave age #e#be count a#ong all ot,e collection object% i% 19* w,e ea% t,e Range object ,a% 158 #e#be %.

/ndeed* t,e Range object %,ould be t,oug,t o5 a% a collection object t,at can ,old ot,e Range object%. 0o illu%t ate* con%ide t,e 5ollowing code:
Di5 rn4 as "an4e Set rn4 = "an4e("A1"= "C&").Cells Ms41ox rn4.Co0nt Set rn4 = "an4e("A1"= "C&")."ows Ms41ox rn4.Co0nt Set rn4 = "an4e("A1"= "C&").Col05ns Ms41ox rn4.Co0nt

. dis3la s 1& . dis3la s &

. dis3la s B

/n t,i% code* we alte natel( %et rn! to t,e collection o5 all cell%* ow%* and colu#n% o5 t,e ange A1:!5. /n eac, ca%e* Ms41ox e'o t% t,e co ect nu#be o5 ite#% in t,e collection. +ote t,at t,e E6cel #odel doe% not ,ave a cell* ow* o colu#n object. Rat,e * t,e%e object% a e Range object%H t,at i%* t,e #e#be % o5 rn! a e Range object%. 4,en we do not %'eci5( t,e #e#be t('e* a Range object act% li-e a collection o5 cell%. 0o illu%t ate* ob%e ve t,at t,e code:
Di5 rn4 As "an4e Set rn4 = "an4e("A1"= "C&") Ms41ox rn4.Co0nt Ms41ox rn4(N).+al0e . row%5aTor order

di%'la(% t,e nu#be o5 cell% in t,e ange and t,en t,e value o5 cell 6 in t,at ange ?counted in ow #ajo o de H t,at i%* %ta ting wit, t,e 5i %t ow and counting 5 o# le5t to ig,t@. Al%o* t,e code:
Di5 Di5 Set >or rn4 As "an4e oCell As "an4e rn4 = "an4e("A1"= "C&") 'ach oCell 2n rn4 Deb04.;rint oCell.+al0e 9ext

will c(cle t, oug, eac, cell in t,e ange rn!* ' inting cell value% in t,e /##ediate window.

$=%' (efining a 6ange Ob1ect


A% witne%% to t,e i#'o tance o5 t,e Range object* t,e e a e a total o5 113 #e#be % ?' o'e tie% and #et,od%@ t, oug,out t,e E6cel object #odel t,at etu n a Range object. 0,i% nu#be d o'% to 51 i5 we count onl( distinct #e#be na#e%* a% %,own in 0able 19;2. ?$o in%tance* >otto#Rig,t!ell i% a ' o'e t( o5 21 di55e ent object%* a% i% 0o'3e5t!ell.@ able 18-2. Excel -e&bers hat ,eturn a ,ange 3bject I)e5ault End Range Active!ell Enti e!olu#n Range1election >otto#Rig,t!ell Enti eRow Re5e %0oRange !ell% $ind Re%iGe !,anging!ell% $ind+e6t Re%ultRange !i cula Re5e ence $ind& eviou% Row)i55e ence% !olu#n)i55e ence% .et&ivot)ata RowRange !olu#nRange /nte %ect Row% !olu#n% /te# 1ou ceRange !u entA a( 3abelRange 1'ecial!ell% !u entRegion 3ocation 0ableRange1 )ata>od(Range "e geA ea 0ableRange2

)ata3abelRange )ataRange

+e6t O55%et

0,i%!ell 0o'3e5t!ell

)e'endent% )e%tination )i ect)e'endent% )i ect& ecedent%

&ageRange &ageRange!ell% & ecedent% & eviou%

2nion 2%edRange =i%ibleRange

3et u% ta-e a loo- at %o#e o5 t,e #o e ' o#inent wa(% to de5ine a Range object.

18.2.1 ,ange Pro!erty


0,e Range ' o'e t( a''lie% to t,e A''lication* Range* and 4o -%,eet object%. +ote t,at:
A33lication."an4e

i% e7uivalent to:
ActiveSheet."an4e

4,en Range i% u%ed wit,out 7uali5ication wit,in t,e code #odule o5 a wo -%,eet* t,en it i% a''lied to t,at %,eet. 4,en Range i% u%ed wit,out 7uali5ication in a code #odule 5o a wo -boo-* t,en it a''lie% to t,e active wo -%,eet in t,at wo -boo-. 0,u%* 5o e6a#'le* i5 t,e 5ollowing code a''ea % in t,e code #odule 5o 1,eet2:
Hor8sheets(1).Activate "an4e("D1").+al0e = "test"

t,en it% e6ecution 5i %t activate% 1,eet1* but %till 'lace% t,e wo d 8te%t8 in cell )1 o5 1,eet2. >ecau%e t,i% #a-e% code di55icult to ead* / %ugge%t t,at (ou alwa(% 7uali5( (ou u%e o5 t,e Range ' o'e t(. 0,e Range ' o'e t( ,a% two di%tinct %(nta6e%. 0,e 5i %t %(nta6 i%:
object."an4e(Name)

w,e e Name i% t,e na#e o5 t,e ange. /t #u%t be an A1;%t(le e5e ence and can include t,e ange o'e ato ?a colon@* t,e inte %ection o'e ato ?a %'ace@* o t,e union o'e ato ?a co##a@. An( dolla %ign% in Name a e igno ed. 4e can al%o u%e t,e na#e o5 a na#ed ange. 0o illu%t ate* ,e e a e %o#e e6a#'le%:
"an4e("A2") "an4e("A2-1B") "an4e("A2->B A1-D&") "an4e("A2->B= A1-D&")

. An intersection . A 0nion

O5 cou %e* we can u%e t,e !onve t$o #ula #et,od to conve t a 5o #ula 5 o# R1!1 %t(le to A1 %t(le be5o e a''l(ing t,e Range ' o'e t(* a% in:
"an4e(A33lication.Convert>or50la(""2C&-"NCE"= xl"1C1= xlA1))

$inall(* i5 Test"an4e i% t,e na#e o5 a ange* t,en we #a( w ite:


"an4e(A33lication.9a5es("Test"an4e"))

o : "an4e(A33lication.9a5es!
Test"an4e) to etu n t,i% ange.

0,e %econd %(nta6 5o t,e Range ' o'e t( i%:


object."an4e(Cell#= Cell$)

Ae e Cell# i% t,e cell in t,e u''e ;le5t co ne o5 t,e ange and Cell$ i% t,e cell in t,e lowe ig,t co ne * a% in:
"an4e("DL"= ">D")

Alte nativel(* Cell# and Cell$ can be Range object% t,at e' e%ent a ow o colu#n. $o in%tance* t,e 5ollowing etu n% t,e Range object t,at e' e%ent% t,e %econd and t,i d ow% o5 t,e active %,eet:
"an4e("ows(2)= "ows(B))

/t i% i#'o tant to note t,at w,en t,e Range ' o'e t( i% a''lied to a Range object* all e5e ence% a e elative to t,e u''e ;le5t co ne cell in t,at ange. $o in%tance* i5 rn! e' e%ent% t,e %econd colu#n in t,e active %,eet* t,en:
rn4."an4e("A2")

i% t,e %econd cell in t,at colu#n* and not cell A2 o5 t,e wo -%,eet. Al%o* t,e e6' e%%ion:
rn4."an4e("12")

e' e%ent% t,e ?ab%olute@ cell !2* becau%e t,i% cell i% in t,e %econd colu#n and %econd ow 5 o# cell >1 ?w,ic, i% t,e u''e ;le5t cell in t,e ange rn! @.

18.2.2 +ells Pro!erty


0,e E6cel object #odel doe% not ,ave an o55icial !ell% collection no a !ell object. +eve t,ele%%* t,e cell% ' o'e t( act% a% t,oug, it etu n% %uc, a collection a% a Range object. $o in%tance* t,e 5ollowing code etu n% D:
"an4e("A1-1L").Cells.Co0nt

/ncidentall(* Cells.Co0nt etu n% 1N=FFF=21N = 2&N R N&&BN. 0,e !ell% ' o'e t( a''lie% to t,e A''lication* Range* and 4o -%,eet object% ?and i% global@. 4,en a''lied to t,e 4o -%,eet object* it etu n% t,e Range object t,at e' e%ent% all o5 t,e cell% on t,e wo -%,eet. "o eove * t,e 5ollowing a e e7uivalent:
Cells A33lication.Cells ActiveSheet.Cells

4,en a''lied to a Range object* t,e !ell% ' o'e t( %i#'l( etu n% t,e %a#e object* and ,ence doe% not,ing. 0,e %(nta6:
Cells(i=T)

etu n% t,e Range object e' e%enting t,e cell at ow i and colu#n j. 0,u%* 5o in%tance:

Cells(1=1)

i% e7uivalent to:
"an4e("A1")

One advantage o5 t,e !ell% ' o'e t( ove t,e Range #et,od i% t,at t,e !ell% ' o'e t( can acce't intege va iable%. $o in%tance* t,e 5ollowing code %ea c,e% t,e 5i %t 100 ow% o5 colu#n 4 5o t,e 5i %t cell containing t,e wo d 8te%t.8 /5 %uc, a cell i% 5ound* it i% %elected. /5 not* a #e%%age i% di%'la(ed:
Di5 r As Lon4 >or r = 1 To 177 26 Cells(r= L).+al0e = "test" Then Cells(r= L).Select 'xit >or 'nd 26 9ext 26 r = 171 then Ms41ox "9o s0ch cell."

/t i% al%o 'o%%ible to co#bine t,e Range and !ell% ' o'e tie% in a u%e5ul wa(. $o e6a#'le* con%ide t,e 5ollowing code:
Di5 r As Lon4 Di5 rn4 As "an4e Hith ActiveSheet >or r = 1 To 177 26 Cells(r= r).+al0e $( "" Then Set rn4 = ."an4e(.Cells(1= 1)= .Cells(r= r)) 'xit >or 'nd 26 9ext 'nd Hith rn4.Select

0,i% code %ea c,e% t,e diagonal cell% ?cell% wit, t,e %a#e ow and colu#n nu#be @ until it 5ind% a none#'t( cell. /t t,en %et% rn4 to e5e to t,e ange con%i%ting o5 t,e ectangle w,o%e u''e ;le5t co ne i% cell A1 and w,o%e lowe ; ig,t co ne i% t,e cell 5ound in t,i% %ea c,.

18.2." +olu&nI +olu&nsI ,owI an# ,ows Pro!erties


0,e E6cel object #odel doe% not ,ave an o55icial !olu#n% o Row% collection. Aoweve * t,e !olu#n% ' o'e t( doe% etu n a collection o5 Range object%* eac, o5 w,ic, e' e%ent% a colu#n. 0,u%:
ActiveSheet.Col05ns(i)

i% t,e Range object t,at e5e % to t,e it, colu#n o5 t,e active wo -%,eet ?and i% a collection o5 t,e cell% in t,at colu#n@. 1i#ila l(:
ActiveSheet."ows(i)

e5e % to t,e it, ow o5 t,e active wo -%,eet. 0,e !olu#n% and Row% ' o'e tie% can al%o be u%ed wit, a Range object. &e ,a'% t,e %i#'le%t wa( to t,in- o5 rn4.Col05ns i% a% t,e collection o5 all colu#n% in t,e wo -%,eet reindexed %o t,at colu#n 1 i% t,e le5t#o%t colu#n t,at inte %ect% t,e ange rn!. 0o %u''o t t,i% %tate#ent* con%ide t,e 5ollowing code* w,o%e e%ult% a e %,own in $igu e 19;1:

Di5 i As 2nte4er Di5 rn4 As "an4e Set rn4 = "an4e("D1-'1= :1-21") rn4.Select Ms41ox ">irst col05n in ran4e is " K rn4.Col05n Ms41ox "Col05n co0nt is " K rn4.Col05ns.Co0nt >or i = %(rn4.Col05n % 2) To rn4.Col05ns.Co0nt A 1 rn4.Col05ns(i).Cells(1= 1).+al0e = i 9ext . Dis3la s L . Dis3la s 2

Figure 18-1. % noncontiguous range

+ote t,at t,e ange rn! i% %elected in $igu e 19;1 ?and include% cell )1@. 0,e !olu#n ' o'e t( o5 a Range object etu n% t,e le5t#o%t colu#n t,at inte %ect% t,e ange. ?1i#ila l(* t,e Row ' o'e t( etu n% t,e to'#o%t ow t,at inte %ect% t,e ange.@ Aence* t,e 5i %t #e%%age bo6 will di%'la( t,e nu#be 4. +ow* 5 o# t,e 'oint o5 view o5 rn!* Col05ns(1) i% colu#n nu#be 4 o5 t,e wo -%,eet ?colu#n )@. Aence* Col05ns(7) i% colu#n nu#be 3 o5 t,e wo -%,eet ?colu#n !@ w,ic,* incidentall(* i% not 'a t o5 rn!. /ndeed* t,e 5i %t colu#n o5 t,e wo -%,eet i% colu#n nu#be
%(rn4.Col05n % 2)

w,ic, i% ' eci%el( w,( we %ta ted t,e >or loo' at t,i% value. +e6t* ob%e ve t,at:
rn4.Col05ns.Co0nt

i% e7ual to 2 ?w,ic, i% t,e nu#be di%'la(ed b( t,e %econd #e%%age bo6@. 0,i% i% a bit une6'ected. Aoweve * 5o %o#e ea%on* "ic o%o5t de%igned t,e !ount ' o'e t( o5 r n!.Col05ns to etu n t,e nu#be o5 colu#n% t,at inte %ect onl( t,e leftmost area in t,e ange* w,ic, i% a ea )1:E1. ?4e will di%cu%% a ea% a bit late .@ $inall(* note t,at:
rn4.Col05ns(B)

i% colu#n $* w,ic, doe% not inte %ect t,e ange at all.

A% anot,e illu%t ation* con%ide t,e ange %elected in $igu e 19;2. 0,i% ange i% t,e union >4:!5* E2:E7. Figure 18-2. he range as a union

0,e code:
Di5 rn4 As "an4e Set rn4 = "an4e("1L-C&= '2-'F") Ms41ox rn4.Col05ns(1).Cells(1= 1).+al0e

di%'la(% a #e%%age bo6 containing t,e x %,own in cell >4 in $igu e 19;2 becau%e t,e inde6e% in t,e !ell% ' o'e t( a e ta-en elative to t,e u''e cell in t,e le5t#o%t a ea in t,e ange. +ote t,at we can u%e eit,e intege % o c,a acte % ?in 7uote%@ to denote a colu#n* a% in:
Col05ns(&)

and:
Col05ns("'")

4e can al%o w ite* 5o in%tance:


Col05ns("A-D")

to denote colu#n% A t, oug, ). 1i#ila l(* we can denote #ulti'le ow% a% in:
"ows("1-B")

1ince a %(nta6 %uc, a%:


Col05ns("C-D"= ":-?")

doe% not wo -* t,e 2nion #et,od i% o5ten u%e5ul in connection wit, t,e !olu#n% and Row% #et,od%. $o in%tance* t,e code:
Di5 rn4 As "an4e Set rn4 = <nion("ows(B)= "ows(&)= "ows(F)) rn4.Select

%elect% t,e t,i d* 5i5t,* and %event, ow% o5 t,e wo -%,eet containing t,i% code o o5 t,e active wo -%,eet i5 t,i% code i% in a wo -boo- o %tanda d code #odule.

18.2.$ 344set Pro!erty


0,e O55%et ' o'e t( i% u%ed to etu n a ange t,at i% o55%et 5 o# a given ange b( a ce tain nu#be o5 ow% andEo colu#n%. 0,e %(nta6 i%:
%an!e/bject.*66set(%ow/**set= Column/**set)

w,e e %ow/**set i% t,e nu#be o5 ow% and Column/**set i% t,e nu#be o5 colu#n% b( w,ic, t,e ange i% to be o55%et. +ote t,at bot, o5 t,e%e 'a a#ete % a e o'tional wit, de5ault value 0* and bot, can be eit,e 'o%itive* negative* o 0. $o in%tance* t,e 5ollowing code %ea c,e% t,e 5i %t 100 cell% to t,e i##ediate ig,t o5 cell )2 5o an e#'t( cell ?i5 (ou ti e o5 t,e #e%%age bo6e%* %i#'l( ' e%% !t l;> ea- to ,alt #ac o e6ecution@:
Di5 Di5 Set >or rn4 As "an4e i As 2nte4er rn4 = "an4e("D2") i = 1 To 177 26 rn4.*66set(7= i).+al0e = "" Then Ms41ox ">o0nd e53t cell at o66set " K i K " 6ro5 cell D2" 'nd 26 9ext

$=%* Additional 7embers of the 6ange Ob1ect


3et u% now ta-e a 7uic- loo- at %o#e additional #e#be % o5 t,e Range object. ?&lea%e e5e to 0able 19;1 5o an indication o5 w,ic, #e#be % a e di%cu%%ed in t,i% %ection.@

18.".1 %ctivate -etho#


0,e Activate #et,od will activate ?o %elect@ t,e ange to w,ic, it i% a''lied. 0,e Activate #et,od a''lie% to a va iet( o5 ot,e object% be%ide% t,e Range object* %uc, a% t,e 4indow object* t,e 4o -%,eet object* and t,e 4o -boo- object.

18.".2 %##+o&&ent -etho#


0,i% #et,od add% a !o##ent object ?i.e.* a co##ent@ to t,e single-cell ange. /t% %(nta6 i%:
%an!e/bject.AddCo55ent(Text)

w,e e Text i% t,e te6t o5 t,e co##ent. $o in%tance* t,e code:


Di5 Di5 Set Set rn4 As "an4e c As Co55ent rn4 = "an4e("12") c = rn4.AddCo55ent("This is a co55ent")

add% a co##ent to cell >2 wit, t,e te6t 80,i% i% a co##ent.8 +ote t,at i5 %an!e/bject con%i%t% o5 #o e t,an a %ingle cell* a unti#e e o e%ult%.

18."." %##ress Pro!erty ;,ea#-3nly String<


0,e Add e%% ' o'e t( etu n% t,e ange e5e ence o5 t,e Range object a% a %t ing. 0,e %(nta6 i%:
%an!e/bject.Address(%ow-bsolute= Column-bsolute= G %e*erenceStyle= ,xternal= %elati+eTo) %ow-bsolute i% %et to Tr0e ?t,e de5ault@ to etu n t,e ow 'a t o5 t,e e5e ence a% an ab%olute e5e ence. Column-bsolute i% %et to Tr0e ?t,e de5ault@ to etu n t,e colu#n 'a t o5 t,e

e5e ence a% an ab%olute e5e ence.


%e*erenceStyle can be one o5 t,e #l"e6erenceSt le con%tant% xlA1 o xl"1C1. 0,e de5ault value i% xlA1.

1et t,e ,xternal 'a a#ete to Tr0e to etu n an e6te nal e5e ence<t,at i%* a e5e ence t,at i% 7uali5ied b( t,e wo -boo- and wo -%,eet na#e% and i% t,u% valid out%ide t,e cu ent wo -%,eet. 0,e de5ault value o5 >alse etu n% a e5e ence t,at i% not 7uali5ied and i% t,e e5o e valid onl( wit,in t,e cu ent wo -%,eet. $inall(* t,e %elati+eTo 'a a#ete i% u%ed w,en %ow-bsolute and Column-bsolute a e >alse and %e*erenceStyle i% xl"1C1. /n t,i% ca%e* we #u%t include a e5e ence 'oint ?a cell@ to u%e 5o t,e elative add e%%e%. 3et u% con%ide %o#e e6a#'le% to ,el' cla i5( t,i% ' o'e t(:
Set rn4 = "an4e("12") rn4.Address("e6erenceSt le-=xlA1) . "et0rns rn4.Address("e6erenceSt le-=xlA1= G 'xternal-=Tr0e) . "et0rns U1oo81VSheet1!C1C2 rn4.Address("e6erenceSt le-=xl"1C1) . "et0rns rn4.Address("owAbsol0te-=>alse= G Col05nAbsol0te-=>alse= "e6erenceSt le-=xlA1) . "et0rns rn4.Address("owAbsol0te-=>alse= G Col05nAbsol0te-=>alse= "e6erenceSt le-=xl"1C1= G "elativeTo-="an4e("D1")) . "et0rns Set rn4 = "an4e("12-D&") rn4.Address("e6erenceSt le-=xlA1) . "et0rns rn4.Address("e6erenceSt le-=xl"1C1) . "et0rns rn4.Address("owAbsol0te-=>alse= G Col05nAbsol0te-=>alse= "e6erenceSt le-=xlA1) . "et0rns rn4.Address("owAbsol0te-=>alse= G Col05nAbsol0te-=>alse= "e6erenceSt le-=xl"1C1= G "elativeTo-="an4e("D1")) . "et0rns 2V-"ULVC C1C2

"2C2 12 "U1VCU%2V C1C2-CDC& "2C2-"&CL 12-D& "U1VCU%

18.".$ %utoFill -etho#


0,i% i#'o tant #et,od 'e 5o #% an auto5ill on t,e cell% in t,e ange. /t% %(nta6 i%:
%an!e/bject.A0to>ill(Destination= Ty e)

Ae e Destination i% t,e Range object w,o%e cell% a e to be 5illed. 0,e de%tination #u%t include t,e %ou ce ange<t,at i%* t,e ange t,at contain% t,e data to u%e 5o t,e auto5ill. 0,e o'tional Ty e 'a a#ete %'eci5ie% t,e 5ill t('e. /t can be one o5 t,e 5ollowing #lA0to>illT 3e con%tant% ?note t,at t,e E6cel docu#entation e5e % to a none6i%tent #l>illT 3e enu#@:

'n05 #lA0to>illT 3e xl>illDe6a0lt = 7 xl>illCo3 = 1 xl>illSeries = 2 xl>ill>or5ats = B xl>ill+al0es = L xl>illDa s = & xl>illHee8da s = N xl>illMonths = F xl>ill@ears = D xlLinearTrend = E xl:rowthTrend = 17 'nd 'n05

/5 t,i% a gu#ent i% xl>illDe6a0lt o i% o#itted* E6cel will atte#'t to %elect t,e #o%t a'' o' iate 5ill t('e* ba%ed on t,e %ou ce data. 0o illu%t ate* con%ide t,e code:
"an4e("A1-11").A0to>ill "an4e("A1-P1")

w,ic, auto5ill% cell% !1 t, oug, R1 u%ing t,e %ou ce data in cell% A1 and >1. /5 A1 contain% 1 and >1 contain% 2* t,en t,i% code will 5ill t,e de%tination cell% wit, con%ecutive intege % %ta ting at 3 ?in cell !1@. +ote t,at cell% A1 and >1 a e included in t,e de%tination ange. A% anot,e illu%t ation* con%ide t,e wo -%,eet in $igu e 19;3* w,e e cell >1 contain% t,e 5o #ula:
=A1RA1

Figure 18-". 0or1sheet to auto4ill range .1F.'

0,e code:
"an4e("11").A0to>ill "an4e("11-1&")

will ' oduce t,e out'ut %,own in $igu e 19;4. Figure 18-$. %uto4illing .1F.' in Figure 18-"

4e %,ould #ention one %ou ce o5 'otential ' oble#% wit, t,e Auto$ill #et,od. A''a entl(* w,en Auto$ill i% e6ecuted* t,e 5o #ula in t,e %ou ce cell i% co'ied* with changes* to ot,e cell%. Aoweve * t,e #alue o5 t,e %ou ce cell i% al%o co'ied* but without changes. 0,u%* i5 autocalculation i% o55* t,e 5o #ula% in t,e auto5illed cell% will be co ect but t,e value% will be inco ect. 0o 5i6 t,i%* ju%t invo-e t,e !alculate #et,od.

18.".' %utoFilter -etho#


0,e Auto$ilte #et,od ,a% two %(nta6e%* co e%'onding to two di%tinct 5unction%. 0,e %(nta6:
%an!e/bject.A0to>ilter

%i#'l( toggle% t,e di%'la( o5 t,e Auto$ilte d o';down a ow% 5o t,e colu#n% t,at a e involved in t,e ange. 0,e %(nta6:
%an!e/bject.A0to>ilter(Field= Criteria#= / erator= Criteria$)

di%'la(% t,e Auto$ilte a ow% and 5ilte % a li%t u%ing t,e Auto$ilte 5eatu e. 0,e o'tional Field 'a a#ete i% t,e o55%et ?a% an intege * counting 5 o# t,e le5t@ o5 t,e 5ield on w,ic, t,e 5ilte i% ba%ed ?t,e le5t#o%t 5ield i% 5ield one@. 0,e o'tional Criteria# 'a a#ete i% t,e c ite ia ?a% a %t ing@. 4e can u%e 8T8 to 5ind blan5ield%* o 8SZ8 to 5ind nonblan- 5ield%. /5 t,i% a gu#ent i% o#itted* t,e c ite ia i% All. /5 / erator ?%ee t,e 5ollowing e6a#'le@ i% %et to xlTo3172te5s* t,en Criteria# %'eci5ie%* a% an intege * t,e nu#be o5 ite#% to di%'la( ?t,i% nu#be need not be e7ual to 10@. 0,e / erator 'a a#ete can be one o5 t,e 5ollowing #lA0to>ilter*3erator con%tant%:
'n05 #lA0to>ilter*3erator xlAnd = 1 xl*r = 2 xlTo3172te5s = B xl1otto5172te5s = L xlTo317;ercent = & xl1otto517;ercent = N 'nd 'n05

/5 t,i% 'a a#ete i% %et to xlAnd o xl*r* t,en we #u%t u%e Criteria# and Criteria$ to con%t uct t,e co#'ound c ite ia. 0o illu%t ate* con%ide t,e wo -%,eet %,own in $igu e 19;5. Figure 18-'. % wor1sheet be4ore auto4iltering 0,e code:
"an4e("A1-1&").A0to>ilter 2= "1EEF"= xl*r= "1EED"

will 5ilte t,e ange A1:>5 to %,ow onl( t,o%e ow% in t,e ange 5o eit,e t,e (ea 1997 o 1998. +ote t,at it ,a% no e55ect on t,e e#aining ow% o5 t,e wo -%,eet. Aence* t,e e%ult will be t,e wo -%,eet in $igu e 19;5 wit, ow% 3 and 4 #i%%ing. Recall t,at t,e Auto$ilte "ode ' o'e t( o5 t,e 4o -%,eet object i% Tr0e i5 t,e Auto$ilte d o' down a ow% a e cu entl( di%'la(ed on t,e wo -%,eet. +ote t,at we can %et t,i% ' o'e t( to >alse to e#ove t,e a ow%* but we cannot %et it to Tr0e. ?0o di%'la( t,e Auto$ilte a ow%* u%e t,e Auto$ilte #et,od.@ Recall al%o t,at t,e $ilte "ode ' o'e t( i% Tr0e i5 t,e wo -%,eet i% in 5ilte #ode. 0,u%* 5o in%tance* i5 t,e Auto$ilte a ow% a e di%'la(ed but no 5ilte ing ,a% ta-en 'lace* t,en Auto$ilte "ode i% Tr0e* w,e ea% $ilte "ode i% >alse. Once 5ilte ing i% actuall( 'e 5o #ed* t,en $ilte "ode i% Tr0e.

18.".5 %utoFit -etho#


0,i% #et,od c,ange% t,e widt, o5 t,e colu#n% o t,e ,eig,t o5 t,e ow% ?de'ending u'on t,e t('e o5 ange@ to obtain t,e be%t 5it 5o t,e angeB% content%. 0,e %(nta6 i%:
%an!e/bject.A0to>it

w,e e %an!e/bject e5e % to a Range object t,at con%i%t% o5 eit,e one o #o e ow% o one o #o e colu#n% ?but not bot,@. Ot,e wi%e* t,e #et,od gene ate% an e o . /5 t,e ange con%i%t% o5 colu#n%* t,en t,e colu#n widt, i% adju%ted. /5 t,e ange con%i%t% o5 ow%* t,en t,e ow ,eig,t i% adju%ted.

18.".6 %utoFor&at -etho#


0,i% #et,od auto#aticall( 5o #at% a ange u%ing a ' ede5ined 5o #at. 0,e %(nta6 i%:
%an!e/bject.A0to>or5at(Format= Number= Font= G -li!nment= )order= Pattern= Width)

All 'a a#ete % o5 t,i% #et,od a e o'tional. 0,e Format 'a a#ete can be one o5 t,e 5ollowing #l"an4eA0to>or5at con%tant%:
'n05 #l"an4eA0to>or5at xl"an4eA0to>or5atBD'66ects1 = 1B xl"an4eA0to>or5atBD'66ects2 = 1L xl"an4eA0to>or5atAcco0ntin41 = L xl"an4eA0to>or5atAcco0ntin42 = & xl"an4eA0to>or5atAcco0ntin4B = N xl"an4eA0to>or5atAcco0ntin4L = 1F xl"an4eA0to>or5atClassic1 = 1 xl"an4eA0to>or5atClassic2 = 2 xl"an4eA0to>or5atClassicB = B xl"an4eA0to>or5atClassic;ivotTable = B1 xl"an4eA0to>or5atColor1 = F xl"an4eA0to>or5atColor2 = D xl"an4eA0to>or5atColorB = E xl"an4eA0to>or5atList1 = 17 xl"an4eA0to>or5atList2 = 11 xl"an4eA0to>or5atListB = 12 xl"an4eA0to>or5atLocal>or5at1 = 1& xl"an4eA0to>or5atLocal>or5at2 = 1N xl"an4eA0to>or5atLocal>or5atB = 1E xl"an4eA0to>or5atLocal>or5atL = 27 xl"an4eA0to>or5at9one = %L1L2 xl"an4eA0to>or5at;T9one = L2 xl"an4eA0to>or5at"e3ort1 = 21 xl"an4eA0to>or5at"e3ort17 = B7 xl"an4eA0to>or5at"e3ort2 = 22 xl"an4eA0to>or5at"e3ortB = 2B xl"an4eA0to>or5at"e3ortL = 2L xl"an4eA0to>or5at"e3ort& = 2& xl"an4eA0to>or5at"e3ortN = 2N xl"an4eA0to>or5at"e3ortF = 2F xl"an4eA0to>or5at"e3ortD = 2D xl"an4eA0to>or5at"e3ortE = 2E xl"an4eA0to>or5atSi53le = %L1&L xl"an4eA0to>or5atTable1 = B2 xl"an4eA0to>or5atTable17 = L1 xl"an4eA0to>or5atTable2 = BB xl"an4eA0to>or5atTableB = BL xl"an4eA0to>or5atTableL = B& xl"an4eA0to>or5atTable& = BN

xl"an4eA0to>or5atTableN xl"an4eA0to>or5atTableF xl"an4eA0to>or5atTableD xl"an4eA0to>or5atTableE 'nd 'n05

= = = =

BF BD BE L7

+ote t,at t,e con%tant% #a -ed a% not used a e not u%ed in t,e 2.1. Engli%, ve %ion o5 E6cel. 0,e ot,e 'a a#ete % a e >oolean and %,ould be %et to Tr0e ?t,e de5ault value%@ to include t,e co e%'onding 5o #at 5eatu e* a% 5ollow%:
Number

/nclude nu#be 5o #at%


Font

/nclude 5ont 5o #at%

-li!nment

/nclude align#ent
)order

/nclude bo de 5o #at%
Pattern

/nclude 'atte n 5o #at%


Width

/nclude colu#n widt, and ow ,eig,t in t,e auto5o #at +ote t,at i5 t,e ange i% a %ingle cell* t,e Auto$o #at #et,od al%o 5o #at% t,e cu ent egion containing t,e cell. ?0,e !u entRegion ' o'e t( and t,e cu ent egion a e di%cu%%ed in detail late in t,i% %ection.@ &ut anot,e wa(* t,e 5ollowing two %tate#ent% a e e7uivalent:
Cells("A1").A0to>or5at Cells("A1").C0rrent"e4ion.A0to>or5at

18.".7 .or#er%roun# -etho#


0,i% #et,od add% a bo de to a ange and o'tionall( %et% t,e !olo * 3ine1t(le* and 4eig,t ' o'e tie% 5o t,e bo de . 0,e %(nta6 i%:
%an!e/bject.1orderAro0nd("ineStyle= Wei!ht= Color1ndex= Color)

0,e "ineStyle 'a a#ete can be one o5 t,e 5ollowing #lLineSt le con%tant% ?note t,at t,e E6cel docu#entation e5e % to a none6i%tent #l1orderLineSt le enu#@:
'n05 #lLineSt le xlLineSt le9one = %L1L2 xlDo0ble = %L11E xlDot = %L11D xlDash = %L11& xlContin0o0s = 1 xlDashDot = L xlDashDotDot = & xlSlantDashDot = 1B 'nd 'n05

. the de6a0lt

0,e o'tional Wei!ht 'a a#ete %'eci5ie% t,e bo de weig,t and can be one o5 t,e 5ollowing #l1orderHei4ht con%tant%:
'n05 #l1orderHei4ht xlMedi05 = %L1BD xl?airline = 1 xlThin = 2 xlThic8 = L 'nd 'n05

. the de6a0lt

+ote t,at t,e Wei!ht ' o'e t( i% igno ed unle%% t,e "ineStyle i% xlContin0o0s o o#itted. 0,e o'tional Color1ndex 'a a#ete %'eci5ie% t,e bo de colo * eit,e a% an inde6 into t,e cu ent colo 'alette o a% one o5 t,e 5ollowing #lColor2ndex con%tant%:

'n05 #lColor2ndex xlColor2ndex9one = %L1L2 xlColor2ndexA0to5atic = %L17& 'nd 'n05

0,e o'tional Color 'a a#ete al%o %'eci5ie% t,e bo de colo a% an R.> value. +ote t,at (ou %,ould %'eci5( at #o%t one o5 t,e colo 'a a#ete %. 0,e tec,ni7ue 5o clea ing a bo de i% a bit une6'ected. $o in%tance* %u''o%e we ,ave %et a bo de wit,:
rn4.1orderAro0nd LineSt le-=xlDash

0o clea t,i% bo de * we #ig,t natu all( t (:


rn4.1orderAro0nd LineSt le-=xlLineSt le9one

but t,i% doe% not,ing. /n%tead* we #u%t w ite:


rn4.1orders.LineSt le = xlLineSt le9one

w,ic, clea % t,e bo de % a ound eac, cell in t,e ange %e'a atel(.

18.".8 +alculate -etho#


0,i% #et,od ?w,ic, al%o a''lie% to t,e 4o -boo- and 4o -%,eet object%@ calculate% all cell% in t,e %'eci5ied ange. $o in%tance* t,e code:
Hor8sheets(1)."ows(2).Calc0late

will calculate all o5 t,e cell% in t,e %econd ow o5 t,e 5i %t wo -%,eet.

18.".19 +lear -etho#s


E6cel ,a% %eve al clea #et,od%. /n 'a ticula * t,e !lea #et,od clea % all content%* 5o #ula%* and 5o #atting 5 o# t,e cell% in t,e given ange. 0,e !lea !ontent% #et,od clea % onl( t,e content% ?value% andEo 5o #ula%@ 5 o# t,e cell% in t,e ange and leave% t,e 5o #atting intact. 0,e !lea $o #at% #et,od clea % onl( t,e 5o #atting 5 o# t,e cell% in t,e ange.

18.".11 +olu&n(i44erences an# ,ow(i44erences -etho#s


0,e !olu#n)i55e ence% #et,od etu n% a Range object t,at e' e%ent% all t,e cell% in t,e ange w,o%e content% a e di55e ent 5 o# ce tain co#'a i%on cell% ?t,e e i% one co#'a i%on cell in eac, colu#n@. 0,e %(nta6 i%:
%an!e/bject.Col05nDi66erences(Com arisonCell)

w,e e Com arisonCell i% a ange object t,at e' e%ent% a %ingle cell. 0,e 'u 'o%e o5 Com arisonCell i% %i#'l( to identi5( t,e row w,o%e cell% contain t,e co#'a i%on value%. 0o illu%t ate* con%ide t,e 5ollowing code* w,o%e e%ult% a e %,own in $igu e 19;6:
Di5 rn4 As "an4e= rn42 As "an4e Set rn4 = "an4e("A1-DN") Set rn42 = G rn4.Col05nDi66erences(Co53arison-="an4e("A1")) rn42.Select

Figure 18-5. he result o4 the +olu&n(i44erences &etho#

1ince t,e Com arisonCell i% cell A1* t,e 5i %t cell o5 eac, colu#n in t,e ange contain% t,e co#'a i%on value. 0,u%* t,e cell% t,at do not contain an 868 a e %elected in colu#n A* t,e nonblan- cell% a e %elected in colu#n >* t,e cell% t,at do not contain a 8(8 a e %elected in colu#n ! and t,e cell% t,at do not contain an 8a8 a e %elected in colu#n ). 0,e Row)i55e ence% #et,od i% t,e analog 5o ow% o5 t,e !olu#n)i55e ence% #et,od.

18.".12 +olu&n0i#th an# ,owCeight Pro!erties


0,e !olu#n4idt, ' o'e t( etu n% o %et% t,e widt, o5 t,e colu#n% in t,e %'eci5ied ange. 0,e etu n value i% in unit%* eac, o5 w,ic, e7ual% t,e widt, o5 one c,a acte in t,e +o #al %t(le. $o ' o'o tional 5ont%* t,e widt, o5 t,e c,a acte 808 ?Ge o@ i% u%ed. +ote t,at i5 t,e colu#n% in t,e ange do not all ,ave t,e %a#e widt,* t,e !olu#n4idt, ' o'e t( etu n% 90ll. 0,e RowAeig,t ' o'e t( etu n% t,e ,eig,t o5 all t,e ow% in t,e ange* #ea%u ed in 'oint%. +ote t,at i5 t,e ow% in t,e ange do not all ,ave t,e %a#e ,eig,t* t,e RowAeig,t ' o'e t( etu n% 90ll.

18.".1" 0i#thI CeightI o!I an# )e4t Pro!erties


0,e%e ' o'e tie% etu n value% 5o t,e enti e ange* in 'oint%. $o in%tance* t,e 0o' ' o'e t( etu n% t,e di%tance* in 'oint%* 5 o# t,e to' o5 ow 1 to t,e to' o5 t,e 5i %t ?le5t#o%t@ a ea o5 t,e ange. +ote t,at w,en a''lied to a colu#n* t,e 4idt, ' o'e t( etu n% t,e widt,* in 'oint%* o5 t,e colu#n. Aoweve * t,e elation%,i' between 4idt, and !olu#n4idt, can %ee# a bit %t ange at 5i %t. $o in%tance* t,e 5ollowing code %,ow% t,at a colu#n o5 !olu#n4idt, 1 ,a% 4idt, 9.6 but a colu#n o5 !olu#n4idt, 2 ,a% 4idt, 15. ?/n #( ca%e* t,e +o #al %t(le i% 10 'oint A ial.@ Aoweve * i5 t,e !olu#n4idt, ' o'e t( eall( #ea%u e% t,e widt, o5 a colu#n in unit% and t,e 4idt, ' o'e t( eall( #ea%u e% t,e widt, o5 t,e %a#e colu#n in 'oint%* t,en doubling one o5 t,e%e ' o'e tie% %,ould double t,e ot,e C
Col05ns("A").Col05nHidth = 1 Ms41ox Col05ns("A").Hidth . Dis3la s E.N

Col05ns("A").Col05nHidth = 2 Ms41ox Col05ns("A").Hidth . Dis3la s 1& Col05ns("A").Col05nHidth = 17 Ms41ox Col05ns("A").Hidth . Dis3la s &D.2

$o tunatel(* a little ,ig,;%c,ool algeb a eveal% t,e t ut, ,e e. /t a''ea % t,at t,e 4idt, ' o'e t( include% 'adding on t,e 5a ig,t and t,e 5a le5t o5 t,e enti e g ou' o5 c,a acte % ?ne6t to t,e colu#n bounda ie%@. 0o %u''o t t,i% conclu%ion* letB% do a little algeb a* w,ic, (ou can %-i' i5 it u'%et% (ou. A%%u#e 5o a #o#ent t,at t,e 4idt, ' o'e t( include% not ju%t t,e %u# o5 t,e widt,% o5 t,e

!olu#n4idt, c,a acte %* but al%o an additional 'oint% o5 'adding on eac, %ide o5 t,e enti e g ou' o5 c,a acte %. 0,u%* t,e 5o #ula 5o 4idt, i%:
Hidth = 2R3 A Col05nHidthRw

w,e e w i% t,e t ue widt, o5 a %ingle 808 c,a acte * in 'oint%. 0,u%* 'lugging in t,e value% 5 o# t,e 5i %t two e6a#'le% in t,e ' eviou% code give%:
E.N = 2R3 A 1Rw 1& = 2R3 A 2Rw

1ubt acting t,e 5i %t e7uation 5 o# t,e %econd give%:


&.L = w

1ub%tituting t,i% into t,e 5i %t e7uation and %olving 5o


3 = 2.1

give%:

0,u%* t,e 5o #ula 5o a +o #al %t(le o5 10 'oint A ial i%:


Hidth = L.2 A Col05nHidthR&.L

+ow* 5o a !olu#n4idt, o5 10* t,i% give%:


Hidth = L.2 A 17R&.L = &D.2

Eu e-aC ?!,ec- t,e t,i d e6a#'le in t,e ' eviou% code.@ 0,u%* we ,ave ve i5ied ?but not eall( ' oved@ t,at t,e 4idt, ' o'e t( #ea%u e% not ju%t t,e widt, o5 eac, c,a acte but include% %o#e 'adding on t,e %ide% o5 t,e colu#n<in t,i% ca%e 2.1 'oint% o5 'adding on eac, %ide.

18.".1$ +onsoli#ate -etho#


0,i% #et,od co#bine% ?o con%olidate%@ data 5 o# #ulti'le ange% ?'e ,a'% on #ulti'le wo -%,eet%@ into a %ingle ange on a %ingle wo -%,eet. /t% %(nta6 i%:
%an!e/bject.Consolidate(Sources= Function= G To %ow= "e*tColumn= Create"inks) Sources i% t,e %ou ce o5 t,e con%olidation. /t #u%t be an a a( o5 e5e ence% in R1!1;%t(le

notation. 0,e e5e ence% #u%t include t,e 5ull 'at, o5 t,e ange% to be con%olidated. ?1ee t,e 5ollowing e6a#'le.@ Function i% t,e 5unction u%ed to co#bine t,e data. /t can be one o5 t,e 5ollowing #lConsolidation>0nction con%tant%. ?0,e de5ault value i% xlAvera4e.@
'n05 #lConsolidation>0nction xlAvera4e = %L17N xlCo0nt = %L112 xlCo0nt905s = %L11B xlMax = %L1BN xlMin = %L1BE xl;rod0ct = %L1LE xlStDev = %L1&& xlStDev; = %L1&N xlS05 = %L1&F xl<n8nown = 1777 xl+ar = %L1NL xl+ar; = %L1N&

'nd 'n05 To %ow %,ould be %et to Tr0e to con%olidate t,e data ba%ed on colu#n title% in t,e to' ow o5 t,e con%olidation ange%. 1et t,e 'a a#ete to >alse ?t,e de5ault@ to con%olidate data b( 'o%ition. /n ot,e wo d%* i5 To %ow i% Tr0e* E6cel will co#bine colu#n% wit, t,e %a#e ,eading* even i5

t,e( a e not in t,e %a#e 'o%ition.

"e*tColumn %,ould be %et to Tr0e to con%olidate t,e data ba%ed on ow title% in t,e le5t colu#n o5 t,e con%olidation ange%. 1et t,e 'a a#ete to >alse ?t,e de5ault@ to con%olidate data b(

'o%ition.

Create"inks %,ould be %et to Tr0e to ,ave t,e con%olidation u%e wo -%,eet lin-%. 1et t,e 'a a#ete to >alse ?t,e de5ault@ to ,ave t,e con%olidation co'( t,e data.

0o illu%t ate* con%ide t,e wo -%,eet% in $igu e 19;7 and $igu e 19;8 ?note t,e o de o5 t,e colu#n%@. Figure 18-6. Sheet2 be4ore consoli#ation

Figure 18-7. Sheet" be4ore consoli#ation

0,e code:
Hor8sheets("Sheet1")."an4e("A1").Consolidate G So0rces-=Arra ("Sheet2!"1C1-"BCB"= G "SheetB!"1C1-"BCB")= >0nction-=xlS05

will ' oduce t,e e%ult% %,own in $igu e 19;9 ?on 1,eet1@. Figure 18-8. Sheet1 a4ter consoli#ation with o!,ow set to False

On t,e ot,e ,and* %etting t,e 0o'Row ' o'e t( to Tr0e:


Hor8sheets("Sheet1")."an4e("A1").Consolidate G So0rces-=Arra ("Sheet2!"1C1-"BCB"= G "SheetB!"1C1-"BCB")= >0nction-=xlS05= G To3"ow-=Tr0e

' oduce% t,e e%ult% %,own in $igu e 19;10* %ince t,e data i% co#bined ba%ed on t,e na#e% in t,e 5i %t ow. Figure 18-19. Sheet1 with o!,ow set to rue

18.".1' +o!y an# +ut -etho#s


A% a''lied to t,e Range object* t,e !o'( #et,od ,a% t,e %(nta6:
%an!e/bject.Co3 (Destination)

w,e e Destination i% a Range object t,at %'eci5ie% t,e new ange to w,ic, t,e %'eci5ied ange will be co'ied. /5 t,i% a gu#ent i% o#itted* E6cel will co'( t,e ange to t,e !li'boa d. $o in%tance* t,e code:
"an4e("A1-CB").Co3 "an4e("D&")

co'ie% t,e ange A1:!3 to a ange o5 li-e %iGe w,o%e u''e ;le5t co ne i% cell )5. +ote t,at t,e %a#e ule% a''l( ,e e a% w,en co'(ing u%ing E6celB% u%e inte 5ace. /n 'a ticula * i5 t,e de%tination i% #o e t,an a %ingle cell* t,en it #u%t ,ave t,e %a#e di#en%ion% a% t,e %ou ce ange o el%e an e o will occu . 0,e !ut #et,od ,a% %i#ila %(nta6:
%an!e/bject.C0t(Destination)

and cut% t,e ange at,e t,an co'(ing it.

18.".15 +o!yFro&,ecor#set -etho#


$o t,o%e eade % 5a#ilia wit, )AO* !o'($ o#Reco d%et i% a ve ( 'owe 5ul #et,od t,at co'ie% t,e content% o5 a )AO Reco d%et object onto a wo -%,eet* beginning at t,e u''e ;le5t co ne o5 t,e %'eci5ied ange. +ote t,at i5 t,e Reco d%et object contain% 5ield% wit, O3E object% in t,e#* t,i% #et,od 5ail%. 0o illu%t ate* con%ide t,e 5ollowing code* w,ic, e7ui e% t,at a e5e ence to "ic o%o5t )AO i% %et in t,e Re5e ence% dialog bo6 in t,e E6cel =>A 0ool% #enu:
Di5 rs As "ecordset Set rs = G D1'n4ine.*3enDatabase("d-IexcelIexcel.5db"). G *3en"ecordset("*bTects") "an4e("A1").Co3 >ro5"ecordset(rs= 17= 17)

0,i% code o'en% an Acce%% databa%e na#ed d5@excel@excelAmdb* c eate% a eco d%et ba%ed on t,e table na#ed *bTects* and t,en co'ie% t,e 5i %t 10 colu#n% o5 t,e 5i %t 10 ow% o5 t,e eco d%et to t,e cu ent wo -%,eet* %ta ting at cell A1. +ote t,at* in gene al* co'(ing begin% at t,e cu ent ow o5 t,e Reco d%et object ?w,ic, in ou e6a#'le i% t,e 5i %t ow* %ince we o'ened t,e eco d%et anew@.

18.".16 +reateNa&es -etho#


0,i% #et,od c eate% ange na#e% ba%ed on te6t label% in %'eci5ied cell%. 0,e %(nta6 i%:
%an!e/bject.Create9a5es(To = "e*t= )ottom= %i!ht)

0,e 'a a#ete % a e o'tional and ,ave t,e de5ault value o5 >alse. /5 one o5 t,e 'a a#ete % i% %et to Tr0e* t,en t,e co e%'onding ow ?To o )ottom@ o colu#n ?"e*t o %i!ht@ i% u%ed to %u''l( t,e na#e% 5o t,e na#ed ange%. /5 all o5 t,e 'a a#ete % a e >alse* t,en E6cel t ie% to gue%% t,e location o5 t,e na#e%. ?/ would gene all( advi%e again%t letting an a''lication gue%% at an(t,ing.@ 0o illu%t ate* t,e 5ollowing code* w,en a''lied to t,e %,eet in $igu e 19;8* will de5ine t, ee na#ed ange%:
"an4e("A1-CB").Create9a5es To3-=Tr0e

$o in%tance* t,e ange A2:A3 will be na#ed No,n.

18.".17 +urrent,egion Pro!erty


0,i% u%e5ul ' o'e t( etu n% a Range object t,at e' e%ent% t,e cu ent egion* w,ic, i% t,e egion bound b( t,e clo%e%t e#'t( ow% and colu#n%. 0o illu%t ate* t,e 5ollowing code* w,en a''lied to t,e %,eet in $igu e 19;11* %elect% t,e ectangula egion A2:!4:
ActiveCell.C0rrent"e4ion.Select

Figure 18-11. /llustrating +urrent,egion

18.".18 (elete -etho#


0,i% #et,od delete% t,e cell% in t,e ange. /t% %(nta6 i%:
%an!e/bject.Delete(Shi*t)

0,e o'tional Shi*t 'a a#ete %'eci5ie% ,ow to %,i5t cell% to e'lace t,e deleted cell%. /t can be one o5 t,e 5ollowing con%tant%:
'n05 #lDeleteShi6tDirection xlShi6t<3 = %L1N2 xlShi6tToLe6t = %L1&E 'nd 'n05

/5 t,i% a gu#ent i% o#itted* t,en E6cel gue%%e% ba%ed on t,e %,a'e o5 t,e ange. /n view o5 t,i%* / would advi%e alwa(% including t,e a gu#ent. ?A''lication% %,ould not gue%%C@

18.".29 (e!en#ents an# (irect(e!en#ents Pro!erties


0,e )e'endent% ' o'e t( etu n% a Range object t,at e' e%ent% all cell% containing all t,e de'endent% o5 a cell. 0o illu%t ate* con%ide $igu e 19;12* w,e e we ,ave di%'la(ed t,e unde l(ing 5o #ula% in eac, cell. Figure 18-12. /llustrating the (e!en#ents !ro!erty

0,e 5ollowing code %elect% cell% >1* >2* !4* and !6:
"an4e("A1").De3endents.Select

+ote t,at !6 i% not a di ect de'endent o5 A1. >( cont a%t* t,e 5ollowing line %elect% t,e di ect de'endent% o5 cell A1* w,ic, a e cell% >1* >2* and !4:
"an4e("A1").DirectDe3endents.Select

18.".21 Prece#ents an# (irectPrece#ents Pro!erties


0,e%e ' o'e tie% wo - ju%t li-e t,e )e'endent% and )i ect)e'endent% ' o'e tie%* but in t,e eve %e di ection. $o in%tance* e5e ing to $igu e 19;12* t,e line:
"an4e("CN").;recedents.Select

%elect% t,e cell% >1 and A1* w,e ea% t,e line:
"an4e("CN").Direct;recedents.Select

%elect% t,e cell >1.

18.".22 En# Pro!erty


0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e cell at t,e 8end8 o5 t,e egion t,at contain% t,e %ou ce ange b( #i#ic-ing a -e(%t o-e co#bination ?%ee t,e 5ollowing code@. 0,e %(nta6 i%:
%an!e/bject.'nd(Direction)

w,e e %an!e/bject %,ould be a e5e ence to a %ingle cell and Direction i% one o5 t,e 5ollowing con%tant%. ?0,e -e(%t o-e co#bination i% al%o given in t,e 5ollowing code.@
'n05 #lDirection xl<3 = %L1N2 xlTo"i4ht = %L1N1 xlToLe6t = %L1&E xlDown = %L121 'nd 'n05 . . . . Ctrl%<3 Ctrl%"i4ht Ctrl%Le6t Ctrl%Down

0,u%* 5o in%tance* t,e code:


"an4e("CL").'nd(xlTo"i4ht).Select

%elect% t,e ig,t#o%t cell in Row 4 5o w,ic, all cell% between t,at cell and cell !4 a e none#'t( ?t,at i%* t,e cell i##ediatel( to t,e le5t o5 t,e 5i %t e#'t( cell in ow 4 5ollowing cell !4@.

18.".2" Entire+olu&n an# Entire,ow Pro!erties

0,e Enti e!olu#n ' o'e t( etu n% a Range object t,at e' e%ent% t,e colu#n o colu#n% t,at contain t,e %'eci5ied ange. 0,e Enti eRow ' o'e t( etu n% a Range object t,at e' e%ent% t,e ow o ow% t,at contain t,e %'eci5ied ange. $o in%tance* t,e code:
"an4e("A1-AB").'ntire"ow.Select

%elect% t,e 5i %t t, ee ow% o5 t,e cu ent wo -%,eet.

18.".2$ Fill -etho#s


0,e E6cel object #odel ,a% 5ou $ill #et,od%: $ill)own* $ill2'* $ill3e5t* and $illRig,t. A% e6'ected* t,e%e #et,od% wo - %i#ila l(* %o we will de%c ibe onl( $ill)own. 0,e $ill)own #et,od 5ill% down 5 o# t,e to' cell o cell% in t,e %'eci5ied ange to t,e botto# o5 t,e ange. 0,e content%* 5o #ula%* and 5o #atting o5 t,e cell o cell% in t,e to' ow o5 a ange a e co'ied into t,e e%t o5 t,e ow% in t,e ange. 0,e %(nta6 i%:
%an!e/bject.>illDown

$o in%tance* t,e code:


"an4e("1B-D&").>illDown

will du'licate t,e value% o5 cell% >3 t, oug, >5 in cell% !3 t, oug, !5 and )3 t, oug, )5.

18.".2' Fin# -etho#


0,e $ind #et,od etu n% t,e 5i %t cell in a given ange t,at %ati%5ie% a c ite ion. +ote t,at t,e $ind #et,od etu n% 9othin4 i5 no #atc, i% 5ound. /n an( ca%e* it doe% not a55ect t,e %election o t,e active cell. 0,e %(nta6 o5 t,e $ind #et,od i%:
%an!e/bject.>ind(What= -*ter= "ook1n= "ook-t= G Search/rder= SearchDirection= (atchCase= (atch)yte)

+ote t,at all o5 t,e 'a a#ete % e6ce't What a e o'tional. 0,e What 'a a#ete i% t,e data to %ea c, 5o and can be a %t ing o an( ot,e valid E6cel data t('e ?nu#be * date* etc.@. 0,e -*ter 'a a#ete i% t,e cell a5te w,ic, t,e %ea c, %,ould begin. ?0,i% would be t,e active cell w,en doing a %ea c, 5 o# t,e u%e inte 5ace.@ 0,u%* t,e cell e5e ed to b( -*ter i% t,e la%t cell %ea c,ed. /5 t,e -*ter a gu#ent i% o#itted* t,e %ea c, %ta t% a5te t,e cell in t,e u''e ;le5t co ne o5 t,e ange. 0,e "ook1n 'a a#ete i% one o5 t,e 5ollowing con%tant%:
'n05 #l>indLoo82n xl+al0es = %L1NB xlCo55ents = %L1LL xl>or50las = %L12B 'nd 'n05

0,e "ook-t 'a a#ete i% one o5 t,e 5ollowing con%tant% t,at dete #ine% w,et,e t,e 4,at value

#u%t #atc, t,e cellB% enti e content% o ju%t an( 'a t o5 t,e cellB% content%:
'n05 #lLoo8At xlHhole = 1 xl;art = 2 'nd 'n05

0,e Search/rder 'a a#ete i% one o5 t,e 5ollowing #lSearch*rder con%tant%:


'n05 #lSearch*rder xl1 "ows = 1 xl1 Col05ns = 2 'nd 'n05

0,e SearchDirection 'a a#ete i% one o5 t,e 5ollowing #lSearchDirection con%tant%:


'n05 #lSearchDirection xl9ext = 1 xl;revio0s = 2 'nd 'n05 . De6a0lt

0,e (atchCase 'a a#ete %,ould be %et to Tr0e to do a ca%e;%en%itive %ea c,H ot,e wi%e* t,e %ea c, will be ca%e;in%en%itive. ?0,e (atch)yte 'a a#ete i% u%ed onl( in t,e $a Ea%t ve %ion o5 "ic o%o5t E6cel. 1ee t,e ,el' docu#entation 5o detail%.@ 0,e e a e %eve al t,ing% to note about t,e $ind #et,od:
C

C C C

0,e value% o5 t,e "ook1n * "ook-t* Search/rder* (atchCase* and (atch)yte 'a a#ete % ?but not t,e SearchDirection 'a a#ete @ a e %aved eac, ti#e t,e $ind #et,od i% invo-ed and a e t,en eu%ed 5o t,e ne6t call to t,i% #et,od. +ote al%o t,at %etting t,e%e a gu#ent% c,ange% t,e co e%'onding %etting% in E6celB% $ind dialog bo6* and* conve %el(* c,anging t,e %etting% in t,e $ind dialog bo6 c,ange% t,e value% o5 t,e%e 'a a#ete %. 0,i% i#'lie% t,at we cannot el( on t,e value% o5 t,e%e 'a a#ete %* %ince t,e u%e #a( ,ave c,anged t,e# t, oug, t,e $ind dialog bo6. Aence* it i% i#'o tant to %'eci5( each o5 t,e%e a gu#ent% 5o eac, call to t,e $ind #et,od. 0,e $ind+e6t and $ind& eviou% #et,od% ?de%c ibed in t,e ne6t %ection@ can be u%ed to e'eat a %ea c,. 4,en a %ea c, eac,e% t,e end o5 t,e %'eci5ied %ea c, ange* it w a'% a ound to t,e beginning o5 t,e ange. /5 (ou do not want t,i% be,avio * con%ide u%ing a di55e ent ange. 0o 5ind cell% t,at #atc, #o e co#'licated %ea c, c ite ia* %uc, a% t,o%e involving wildca d #atc,e%* we #u%t u%e a #o e #anual a'' oac,* %uc, a% c(cling t, oug, t,e cell% in t,e ange wit, a >or 'ach loo' and u%ing t,e Li8e o'e ato . $o in%tance* t,e 5ollowing code %ea c,e% 5o all cell% in t,e ange A1:!5 w,o%e content% begin wit, an 8A8 and %et% t,e 5ont 5o t,e%e cell% to bold ?note t,e u%e o5 t,e evaluation o'e ato to denote t,e ange A1:!5@:
Di5 c As "an4e >or 'ach c 2n UA1-C&V 26 c.+al0e Li8e "AR" Then c.>ont.1old = Tr0e 'nd 26 9ext

18.".25 Fin#Next an# Fin#Previous -etho#s


0,e $ind+e6t #et,od continue% a %ea c, t,at wa% %ta ted wit, t,e $ind #et,od* etu ning t,e ne6t cell t,at #atc,e% t,e c ite ia. 0,e %(nta6 i%:
%an!e/bject.>ind9ext(-*ter)

0,e -*ter 'a a#ete #u%t be %'eci5ied o t,e %ea c, will begin at t,e u''e ;le5t co ne o5 t,e

ange. 0,u%* $ind+e6t i% t,e %a#e a% $ind* e6ce't t,at it u%e% all o5 t,e 'a a#ete % ?e6ce't -*ter@ t,at we e %et b( t,e ' eviou% u%e o5 t,e $ind #et,od. 0o continue t,e %ea c, 5 o# t,e la%t cell 5ound* u%e t,at cell a% t,e -*ter a gu#ent. $o in%tance* t,e 5ollowing code %ea c,e% 5o all cell% in t,e to' ow t,at contain t,e value 0 and e#ove% t,e value:
Di5 c As "an4e Di5 s>irst?it As Strin4 . Address o6 6irst hit Hith "ows(1) Set c = .>ind(7= Loo82n-=xl+al0es) 26 9ot c 2s 9othin4 Then s>irst?it = c.Address Do . Chan4e cell contents c.+al0e = "" . 6ind next cell Set c = .>ind9ext(c) Loo3 Hhile 9ot c 2s 9othin4 'nd 26 'nd Hith

0,e $ind& eviou% #et,od ,a% t,e %(nta6:


%an!e/bject.>ind;revio0s()e*ore)

and wo -% ju%t li-e t,e $ind+e6t #et,od* but %ea c,e% bac-wa d in t,e ange %ta ting wit, t,e cell before t,e cell e5e ed to b( t,e )e*ore 'a a#ete ?wit, w a' a ound 5 o# t,e beginning o5 t,e ange to t,e end@.

18.".26 For&ula an# For&ula,1+1 Pro!erties


0,e $o #ula ' o'e t( etu n% o %et% t,e 5o #ula o value 5o eac, cell in t,e ange. 0,e 5o #ula #u%t be e6' e%%ed in A1;%t(le notation* and #u%t include a leading e7ual %ign. $o in%tance* t,e line:
"an4e("A1").>or50la = "=S05(A2-AB)"

%et% t,e 5o #ula in cell A1. 0,e line:


"an4e("A1-C1").>or50la = "=S05(A2-AB)"

'lace% t,e 5o #ula in cell% A1:!1* but becau%e t,e 5o #ula u%e% elative e5e ence%* t,e%e e5e ence% will be alte ed a% u%ual. /5 we want to 'ut t,e e6act %a#e 5o #ula in eac, cell* we #u%t u%e an a a(* a% in:
"an4e("A1-C1").>or50la = G Arra ("=S05(A2-AB)"= "=S05(A2-AB)"= "=S05(A2-AB)")

4e can al%o etu n an a a( u%ing t,e $o #ula ' o'e t(. 0o illu%t ate* con%ide t,e wo -%,eet in $igu e 19;13. 0,e code:
Di5 a As +ariant a = "an4e("A1-C2").>or50la

%et% t,e =a iant va iable a to an a a(* %o t,at* 5o in%tance* a(2=B) = F. +ote t,at t,e $o #ula ' o'e t( etu n% a =a iant* %o t,at a #u%t be decla ed a% a =a iant.

Figure 18-1". /llustrating the For&ula !ro!erty /5 a cell contain% a con%tant* t,e $o #ula ' o'e t( etu n% t,at con%tant. 4e can al%o a%%ign a con%tant to a cell b( w iting* 5o e6a#'le:
"an4e("A1").>or50la = 1

/5 t,e cell i% e#'t(* t,en t,e $o #ula ' o'e t( etu n% an e#'t( %t ing. /5 t,e cell contain% a 5o #ula* t,en t,e $o #ula #et,od etu n% t,e 5o #ula a% a %t ing* a% it would be di%'la(ed in t,e 5o #ula ba ?including t,e e7ual %ign@. /5 we %et t,e $o #ula ' o'e t( ?o t,e =alue ' o'e t(@ o5 a cell to a date* t,en E6cel c,ec-% to %ee w,et,e t,at cell i% al ead( 5o #atted wit, one o5 t,e date o ti#e 5o #at%. /5 not* E6cel u%e% t,e de5ault %,o t date 5o #at. 0,e $o #ulaR1!1 ' o'e t( i% t,e analog to t,e $o #ula ' o'e t( but acce't% and etu n% 5o #ula% in R1!1 %t(le.

18.".27 For&ula%rray Pro!erty


0,e $o #ulaA a( ' o'e t( etu n% o %et% an a a( 5o #ula* w,ic, #u%t be in R1!1 %t(le* 5o a ange. 0o illu%t ate* con%ide t,e wo -%,eet %,own in $igu e 19;14. 0,e code:
"an4e("AE-C11").>or50laArra = "=A1-CB A A&-CF"

' oduced t,e value% in cell% A9:!11 in $igu e 19;14. 0,e 5o #ula on t,e le5t %a(% to add t,e content% o5 eac, cell in t,e u''e #o%t 3;b(;3 a a( to t,e co e%'onding cell in t,e #iddle 3;b(;3 a a(* and 'lace t,e e%ult in t,e co e%'onding cell in t,e lowe 3;b(;3 a a(. Figure 18-1$. /llustrating the For&ula%rray !ro!erty

+ote al%o t,at t,e code:


Deb04.;rint "an4e("AE").>or50laArra

' int% t,e a a( 5o #ula:


=A1-CB A A&-CF

18.".28 For&ulaCi##en Pro!erty ;,H0 .oolean<


0,i% ' o'e t( etu n% o %et% t,e Aidden %tate ?Tr0e o >alse@ 5o t,e 5o #ula in t,e cell to w,ic, it i% a''lied. 0,i% i% e7uivalent to %etting t,e Aidden c,ec- bo6 in t,e & otection tab o5 t,e $o #at !ell% dialog.

+ote t,at t,i% i% not t,e %a#e a% t,e Aidden ' o'e t(* w,ic, a''lie% to ange% t,at con%i%t o5 enti e ow% ?o enti e colu#n%@ and dete #ine% w,et,e o not t,o%e ow% ?o colu#n%@ a e ,idden 5 o# view.

18."."9 CasFor&ula Pro!erty ;,ea#-3nly<


0,i% ' o'e t( etu n% Tr0e i5 all cell% in t,e ange contain 5o #ula%H it etu n% >alse i5 none o5 t,e cell% in t,e ange contain% a 5o #ula and 90ll ot,e wi%e.

18."."1 Cori?ontal%lign&ent Pro!erty


0,e Ao iGontalAlign#ent ' o'e t( etu n% o %et% t,e ,o iGontal align#ent o5 all cell% in t,e ange. 0,e value can be one o5 t,e 5ollowing con%tant%:
'n05 #l?Ali4n xl?Ali4n"i4ht = %L1&2 xl?Ali4nLe6t = %L1B1 xl?Ali4nS0sti6 = %L1B7 xl?Ali4nDistrib0ted = %L11F .6or >ar 'ast 'xcel xl?Ali4nCenter = %L17D xl?Ali4n:eneral = 1 xl?Ali4n>ill = & xl?Ali4nCenterAcrossSelection = F 'nd 'n05

+ote e%'eciall( t,e xl?Ali4nCenterAcrossSelection con%tant* w,ic, i% ve ( u%e5ul 5o aligning a title ac o%% #ulti'le cell%.

18."."2 /n#ent)evel Pro!erty an# /nsert/n#ent -etho#


0,e /ndent3evel ' o'e t( etu n% o %et% t,e le5t indent 5o eac, cell in t,e ange and can be an( intege between 0 and 15. All ot,e %etting% cau%e an e o . & e%u#abl(* indent% a e u%e5ul 5o aligning t,e content% o5 cell% o 5o 5o #atting te6t. $o in%tance* to %et t,e indent level o5 cell A1 to 10* we can w ite:
"an4e("A1").2ndentLevel = 17

2n5o tunatel(* t,e docu#entation doe% not %'eci5( ,ow big an indent unit i%* but we can %till u%e indent unit% in a elative wa(. & e%u#abl(* an indent level o5 2 i% twice t,at o5 an indent level o5 1. An alte native i% to u%e t,e /n%e t/ndent #et,od* wit, t,e %(nta6:
%an!e/bject.2nsert2ndent(1nsert-mount)

w,e e 1nsert-mount i% an intege between 0 and 15. Aoweve * in t,i% ca%e* t,e /n%e tA#ount 'a a#ete %'eci5ie% t,e a#ount to change t,e cu ent indent 5o t,e ange.

18."."" /nsert -etho#


0,i% #et,od in%e t% a cell o ange o5 cell% into t,e wo -%,eet* %,i5ting e6i%ting cell% to #a-e oo#. 0,e %(nta6 i%:
%an!e/bject.2nsert(Shi*t)

w,e e Shi*t can be one o5 t,e #l2nsertShi6tDirection con%tant%:


'n05 #l2nsertShi6tDirection xlShi6tTo"i4ht = %L1N1

xlShi6tDown = %L121 'nd 'n05

/5 t,e Shi*t a gu#ent i% o#itted* E6cel will decide u'on t,e %,i5t di ection ba%ed on t,e %,a'e o5 t,e ange. ?A% wit, ot,e ca%e% w,en E6cel will gue%%* / eco##end again%t allowing it to do %o.@

18."."$ )oc1e# Pro!erty


0,i% ' o'e t( etu n% t,e 3oc-ed %tatu% o5 t,e cell% in t,e ange o can be u%ed to loc- t,e ange. 0,e ' o'e t( etu n% 90ll i5 t,e ange contain% bot, loc-ed and unloc-ed cell%.

18."."' -erge-,elate# -etho#s an# Pro!erties


/t i% 7uite co##on to c eate a #e ged cell ?t,at i%* a %ingle cell c eated b( co#bining %eve al adjacent cell%@ 5o u%e a% a title o ,eading* 5o in%tance. 0,e "e ge #et,od c eate% a #e ged cell 5 o# t,e %'eci5ied ange. 0,e %(nta6 i%:
%an!e/bject.Mer4e(-cross)

w,e e -cross i% an o'tional =a iant t,at %,ould be %et to Tr0e to #e ge t,e cell% in eac, ow o5 t,e ange into a %ingle cell 'e ow o >alse ?t,e de5ault@ to #e ge all cell% in all ow% into a single cell. +ote t,at w,en t,e individual cell% contain data* t,e #e ged cell will contain onl( t,e data 5 o# t,e u''e ;le5t cell. Aence* t,e data in all ot,e cell% will be lo%t. 0,e 2n"e ge #et,od %e'a ate% a #e ged a ea into individual cell%. /t% %(nta6 i%:
%an!e/bject.<nMer4e

+ote t,at a% long a% %an!e/bject contain% an( o5 t,e cell% wit,in a #e ged ange* even i5 it doe% not contain all #e ged cell% o i5 it contain% additional cell% not in t,e #e ged a ea* t,e #et,od will un#e ge t,e #e ged ange. +ote t,at calling t,e 2n"e ge #et,od on a ange t,at doe% not contain #e ged cell% ,a% no e55ect and doe% not ' oduce a unti#e e o . 0,e "e geA ea ' o'e t( a''lie% onl( to ange% t,at con%i%t o5 a %ingle cell ?ot,e wi%e an e o occu %@. 0,e ' o'e t( etu n% a Range object e' e%enting t,e #e ged ange containing t,at cell ?o t,e cell it%el5 i5 it i% not 'a t o5 a #e ged ange@.

0,e "e ge!ell% ' o'e t( etu n% Tr0e i5 t,e %'eci5ied ange i% contained wit,in a #e ged ange o5 cell%. 0,e ' o'e t( etu n% 90ll i5 t,e %'eci5ied ange contain% cell% t,at a e wit,in a #e ged ange a% well a% cell% t,at lie out%ide t,e #e ged ange.

18."."5 Next an# Previous Pro!erties


4,en a''lied to a Range object* t,e +e6t ' o'e t( etu n% t,e cell t,at would be #ade active b( %t i-ing t,e
TA1

-e(* alt,oug, it doe% not actuall( %elect t,at cell. 0,u%* on an un' otected %,eet* t,i% ' o'e t( etu n% t,e cell i##ediatel( to t,e ig,t o5 t,e u''e ;le5t cell in t,e ange. On a ' otected %,eet* t,i% ' o'e t( etu n% t,e ne6t unloc)ed cell.
1i#ila l(* t,e & eviou% ' o'e t( e#ulate% t,e 1,i5t;0ab -e( b( etu ning t,e a'' o' iate cell ?al%o wit,out %electing t,e cell@.

18."."6 Nu&berFor&at Pro!erty

0,i% ' o'e t( etu n% o %et% t,e nu#be ;5o #atting %t ing 5o t,e cell% in t,e ange. +ote t,at t,e ' o'e t( will etu n 90ll i5 t,e cell% in t,e ange do not all ,ave t,e %a#e nu#be 5o #at. One o5 t,e %i#'le%t wa(% to dete #ine t,e de%i ed 5o #atting %t ing i% to eco d an E6cel #ac o and u%e t,e $o #at dialog. Lou can t,en in%'ect t,e #ac o code 5o t,e co ect 5o #atting %t ing.

18."."7 Parse -etho#


0,i% #et,od 'a %e% t,e data in a colu#n ?o 'o tion t,e eo5@ and di%t ibute% t,e content% o5 t,e ange to 5ill adjacent colu#n%. 0,e %(nta6 i%:
%an!e/bject.;arse(Parse"ine= Destination)

w,e e %an!e/bject can be no #o e t,an one colu#n wide. 0,e Parse"ine 'a a#ete i% a %t ing containing le5t and ig,t b ac-et% to indicate w,e e t,e data in t,e cell% in t,e colu#n %,ould be %'lit. $o e6a#'le* t,e %t ing:
UxxxV UxxxV

cau%e% t,e &a %e #et,od to in%e t t,e 5i %t t, ee c,a acte % 5 o# eac, cell into t,e 5i %t colu#n o5 t,e de%tination ange* %-i' t,e 5ou t, c,a acte * and t,en in%e t t,e ne6t t, ee c,a acte % into t,e %econd colu#n. An( additional c,a acte % ?be(ond t,e 5i %t %i6@ a e not included in t,e de%tination. 0,i% #a-e% t,e &a %e #et,od #o%t u%e5ul 5o 'a %ing 5i6ed;lengt, data ?eac, cell ,a% data o5 t,e %a#e lengt,@. 0,e Destination 'a a#ete i% a Range object t,at e' e%ent% t,e u''e ;le5t co ne o5 t,e de%tination ange 5o t,e 'a %ed data. /5 t,i% a gu#ent i% o#itted* E6cel will 'a %e t,e data in 'laceH t,at i%* it will u%e t,e %ou ce colu#n a% t,e 5i %t de%tination colu#n.

18."."8 PasteS!ecial -etho#


0,i% #et,od 'a%te% data 5 o# t,e !li'boa d into t,e %'eci5ied ange. 0,e %(nta6 i%:

%an!e/bject.;asteS3ecial(Paste= / eration= Ski )lanks= Trans ose)

0,e Paste 'a a#ete indicate% w,at will be 'a%ted and i% one o5 t,e 5ollowing #l;asteT 3e con%tant%:
'n05 #l;asteT 3e xl;aste+al0es = %L1NB xl;asteCo55ents = %L1LL xl;aste>or50las = %L12B xl;aste>or5ats = %L122 xl;asteAll = %L17L xl;asteAll'xce3t1orders = N 'nd 'n05

. De6a0lt

0,e o'tional / eration 'a a#ete %'eci5ie% a 'a%te o'e ation and can be one o5 t,e 5ollowing #l;asteS3ecial*3eration con%tant%:
'n05 #l;asteS3ecial*3eration xl;asteS3ecial*3eration9one = %L1L2 xl;asteS3ecial*3erationAdd = 2 xl;asteS3ecial*3erationS0btract = B xl;asteS3ecial*3erationM0lti3l = L xl;asteS3ecial*3erationDivide = & 'nd 'n05 . De6a0lt

0,e Ski )lanks 'a a#ete %,ould be %et to Tr0e to %-i' 'a%ting blan- cell% 5 o# t,e !li'boa d. 0o illu%t ate* %u''o%e t,at t,e cell on t,e !li'boa d t,at i% de%tined to be 'a%ted into cell )5 i% blan-. /5 Ski )lanks i% >alse ?t,e de5ault@* t,en w,ateve i% in )5 be5o e t,e 'a%te o'e ation will be ove w itten w,en t,e blan- cell i% 'a%ted* %o )5 will t,en be e#'t(. Aoweve * i5 Ski )lank i% Tr0e* t,e blan- cell will not be 'a%ted into )5 and %o t,e content% o5 )5 will not be di%tu bed. 0,e o'tional Trans ose 'a a#ete can be %et to Tr0e to t an%'o%e ow% and colu#n% w,en t,e ange i% 'a%ted. 0,e de5ault value i% >alse.

18.".$9 Print3ut -etho#


0,e & intOut #et,od ' int% a ange. ?0,i% #et,od a''lie% to a ,o%t o5 ot,e object% a% well* %uc, a% 4o -%,eet* 4o -boo-* and !,a t.@ 0,e %(nta6 i%:
%an!e/bject.;rint*0t(From= To= Co ies= Pre+iew= G -cti+ePrinter= PrintToFile= Collate)

+ote t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional. 0,e From 'a a#ete %'eci5ie% t,e 'age nu#be o5 t,e 5i %t 'age to ' int* and t,e To 'a a#ete %'eci5ie% t,e la%t 'age to ' int. /5 o#itted* t,e enti e object ? ange* wo -%,eet* etc.@ i% ' inted. 0,e Co ies 'a a#ete %'eci5ie% t,e nu#be o5 co'ie% to ' int. 0,e de5ault i% 1. 1et Pre+iew to Tr0e to invo-e ' int ' eview at,e t,an ' inting i##ediatel(. 0,e de5ault i% >alse. -cti+ePrinter %et% t,e na#e o5 t,e active ' inte .

1etting PrintToFile to Tr0e cau%e% E6cel to ' int to a 5ile. E6cel will ' o#'t t,e u%e 5o t,e na#e o5 t,e out'ut 5ile. ?2n5o tunatel(* t,e e i% no wa( to %'eci5( t,e na#e o5 t,e out'ut 5ile in codeC@ 0,e Collate 'a a#ete %,ould be %et to Tr0e to collate #ulti'le #ulti'age co'ie%.

18.".$1 PrintPreview -etho#


0,i% #et,od invo-e% E6celB% ' int ' eview 5eatu e 5o t,e given ange ?t,i% #et,od a''lie% to t,e %a#e li%t o5 object% a% t,e & intOut #et,od@. /t% %(nta6 i%:
%an!e/bject.;rint;review

18.".$2 ,e!lace -etho#


0,i% #et,od 5ind% and e'lace% %'eci5ied data in all cell% in a ange. /t ,a% no e55ect on t,e %election o t,e active cell. 0,e %(nta6 i%:
%an!e/bject."e3lace(What= %e lacement= "ook-t= G Search/rder= (atchCase= (atch)yte)

0,e What 'a a#ete i% t,e data to %ea c, 5o * and t,e %e lacement 'a a#ete i% t,e e'lace#ent data. 0,e%e data can be %t ing% o an( ot,e valid E6cel data t('e% ?nu#be %* date%* etc.@. 0,e "ook-t 'a a#ete i% one o5 t,e 5ollowing con%tant% t,at dete #ine% w,et,e t,e What value #u%t #atc, t,e cellB% enti e content% o ju%t an( 'a t o5 t,e cellB% content%:
'n05 #lLoo8At xlHhole = 1 xl;art = 2 'nd 'n05

0,e Search/rder 'a a#ete i% one o5 t,e 5ollowing #lSearch*rder con%tant%:


'n05 #lSearch*rder xl1 "ows = 1 xl1 Col05ns = 2 'nd 'n05

0,e (atchCase 'a a#ete %,ould be %et to Tr0e to do a ca%e;%en%itive %ea c, ?t,e de5ault i% >alse@. 0,e (atch)yte 'a a#ete i% u%ed onl( in t,e $a Ea%t ve %ion o5 "ic o%o5t E6cel. 1ee t,e ,el' docu#entation 5o detail%. +ote t,at t,e value% o5 t,e "ook-t* Search/rder* (atchCase* and (atch)yte 'a a#ete % a e %aved eac, ti#e t,e $ind #et,od i% invo-ed and t,en eu%ed 5o t,e ne6t call to t,i% #et,od. +ote al%o t,at %etting t,e%e a gu#ent% c,ange% t,e co e%'onding %etting% in E6celB% $ind dialog bo6* and conve %el(* c,anging t,e %etting% in t,e $ind dialog bo6 c,ange% t,e value% o5 t,e%e 'a a#ete %. 0,i% i#'lie% t,at we cannot el( on t,e value% o5 t,e%e 'a a#ete %* %ince t,e u%e #a( ,ave c,anged t,e# t, oug, t,e $ind dialog bo6. Aence* it i% i#'o tant to %'eci5( each o5 t,e%e a gu#ent% 5o eac, call to t,e $ind #et,od. /5 t,e content% o5 t,e What a gu#ent a e 5ound at lea%t once* t,e Re'lace #et,od etu n% Tr0e.

18.".$" Select -etho#


0,i% #et,od %elect% t,e given ange. Actuall(* t,e 1elect #et,od a''lie% to a w,o''ing 81 di55e ent E6cel object%. $o t,e Range object* it% %(nta6 i%:
%an!e/bject.Select

+ote t,at t,i% #et,od %elect% a ange o5 cell%* w,e ea% t,e Activate #et,od activate% a %ingle cell.

18.".$$ Shrin1 oFit Pro!erty


0,i% ' o'e t( can be %et to Tr0e to tell E6cel to %, in- t,e 5ont %iGe o5 all te6t in t,e ange %o t,at t,e te6t 5it% t,e available colu#n widt,. /t al%o etu n% Tr0e i5 1, in-0o$it i% %et 5o all cell% in t,e ange* >alse i5 it i% tu ned o55 5o all cell% in t,e ange* o 90ll i5 %o#e cell% ,ave 1, in-0o$it tu ned on and ot,e % ,ave 1, in-0o$it tu ned o55.

18.".$' Sort -etho#


0,i% #et,od %o t% a ange o t,e cu ent egion w,en t,e %'eci5ied ange contain% onl( one cell. /t can al%o be u%ed to %o t a 'ivot table. 0,e %(nta6 i%:
%an!e/bject.Sort(5ey#= /rder#= 5ey$= Ty e= /rder$= 5ey2= /rder2= G 6eader= /rderCustom= (atchCase= /rientation= Sort(ethod= G 1!noreControlCharacters= 1!noreDiacritics= 1!nore5ashida)

1o ting can ta-e 'lace ba%ed on u' to t, ee -e(%* denoted b( 5ey#* 5ey$* and 5ey2. 0,e%e 'a a#ete % can be e6' e%%ed a% te6t ?a ange na#e@ o a Range object. 0,e co e%'onding /rder 'a a#ete can be %et to one o5 t,e 5ollowing value%:
'n05 #lSort*rder xlAscendin4 = 1 xlDescendin4 = 2 'nd 'n05 . De6a0lt

0,e o'tional Ty e 'a a#ete i% u%ed onl( w,en %o ting 'ivot table%. 0,e o'tional 6eader 'a a#ete %'eci5ie% w,et,e t,e 5i %t ow contain% ,eade %* in w,ic, ca%e t,e( a e not included in t,e %o t. 0,e 6eader 'a a#ete can be one o5 t,e 5ollowing value%:
'n05 #l@es9o:0ess xl:0ess = 7 xl@es = 1 xl9o = 2 'nd 'n05

. De6a0lt

0,e o'tional /rderCustom 'a a#ete i% an intege o55%et into t,e li%t o5 cu%to# %o t o de %. Aoweve * "ic o%o5t %ee#% not to ,ave docu#ented t,i% 5u t,e * %o it %ee#% be%t to %i#'l( o#it t,i% a gu#ent* in w,ic, ca%e it i% a%%u#ed to be +o #al ?w,ic, %ound% good@. 0,e o'tional (atchCase 'a a#ete %,ould be %et to Tr0e to do a ca%e;%en%itive %o t and >alse ?t,e de5ault@ to do a %o t t,at i% not ca%e;%en%itive. $o in%tance* %u''o%e t,at cell A1 contain% t,e te6t 8AAA8 and cell A2 contain% t,e te6t 8aaa.8 0,e code:
"an4e("A1-A2").Sort Pe 1-=Cells(1= 1)= MatchCase-=Tr0e

will %wa' t,e content% o5 t,e%e two cell%* but t,e code:
"an4e("A1-A2").Sort Pe 1-=Cells(1= 1)= MatchCase-=>alse

will not. 0,e o'tional /rientation 'a a#ete dete #ine% w,et,e t,e %o t i% done b( ow o b( colu#n. /t can a%%u#e eit,e o5 t,e value% in t,e 5ollowing enu#:
'n05 #lSort*rientation xlSortCol05ns = 1

xlSort"ows = 2 'nd 'n05

$o in%tance:
"an4e("A1-12").Sort Pe 1-="ows(1)= *rientation-=xlSortCol05ns

%o t% t,e colu#n% in t,e ange A1:>2 u%ing t,e 5i %t ow 5o t,e %o t -e(. 0,e e%t o5 t,e 'a a#ete % a e not u%ed in t,e 2.1. Engli%, ve %ion o5 E6cel. 0,e Sort(ethod 'a a#ete i% not docu#ented* but it ,a% a de5ault value xl;in@in* w,ateve t,at #ean%.

18.".$5 S!ecial+ells -etho#


0,i% #et,od etu n% a Range object t,at e' e%ent% all t,e cell% t,at #atc, a %'eci5ied t('e and value. 0,e %(nta6 i%:
%an!e/bject.S3ecialCells(Ty e= Value)

0,e Ty e 'a a#ete %'eci5ie% t,e t('e o5 cell% to include 5 o# %an!e/bject. /t can be one o5 t,e 5ollowing #lCellT 3e con%tant%:
'n05 #lCellT 3e xlCellT 3eCo55ents = %L1LL xlCellT 3e>or50las = %L12B xlCellT 3eConstants = 2 xlCellT 3e1lan8s = L xlCellT 3eLastCell = 11 xlCellT 3e+isible = 12 'nd 'n05 .Cells with co55ents .Cells with 6or50las .Cells with constants .1lan8 cells .Last cell in ran4e .All visible cells

$o in%tance* t,e code:


"an4e("A1-D17").S3ecialCells(xlCellT 3e1lan8s).Select

%elect% all blan- cell% in t,e ange A1:)10. 0,e o'tional Value 'a a#ete a''lie% w,en t,e Ty e 'a a#ete i% eit,e xlCellT 3eConstants o xlCellT 3e>or50las and identi5ie% #o e %'eci5icall( t,e t('e o5 cell to etu n. /n t,e%e ca%e%* t,e Value 'a a#ete can be %et to one o5* o a %u# o5* t,e 5ollowing con%tant%:
'n05 #lS3ecialCells+al0e xl905bers = 1 xlText+al0es = 2 xlLo4ical = L xl'rrors = 1N 'nd 'n05

$o in%tance* t,e code:


"an4e("A1-D17").S3ecialCells(xlCellT 3eConstants= xlText+al0es).Select

%elect% onl( t,e cell% wit, te6t ?a% o''o%ed to nu#be %@ wit,in t,e ange A1:)10.

18.".$6 ext o+olu&ns -etho#


0,i% #et,od 'a %e% a colu#n ?o colu#n%@ o5 cell% t,at contain te6t into %eve al colu#n%. 0,e %(nta6 i%:

%an!e/bject.TextToCol05ns(Destination= DataTy e= G Text8uali*ier= Consecuti+eDelimiter= Tab= Semicolon= G Comma= S ace= /ther= /therChar= Field1n*o)

+ote t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional. 0,e Destination 'a a#ete i% a Range object t,at %'eci5ie% w,e e to 'ut t,e e%ult% o5 t,e conve %ion. /5 t,e Range object e' e%ent% #o e t,an a %ingle cell* t,en t,e %ta ting 'oint 5o t,e de%tination i% t,e u''e ;le5t cell in t,at ange. 0,e DataTy e 'a a#ete %'eci5ie% t,e 5o #at o5 t,e te6t to be %'lit into colu#n%. /t can be one o5 t,e 5ollowing #lText;arsin4T 3e con%tant%:
'n05 #lText;arsin4T 3e xlDeli5ited = 1 xl>ixedHidth = 2 'nd 'n05 . De6a0lt

0,e Text8uali*ier 'a a#ete i% t,e te6t 7uali5ie . /t can be one o5 t,e 5ollowing #lText,0ali6ier con%tant%:
'n05 #lText,0ali6ier xlText,0ali6ier9one = %L1L2 xlText,0ali6ierDo0ble,0ote = 1 xlText,0ali6ierSin4le,0ote = 2 'nd 'n05

. De6a0lt

0,e Consecuti+eDelimiter 'a a#ete %,ould be %et to Tr0e to ,ave E6cel con%ide con%ecutive deli#ite % a% one deli#ite . 0,e de5ault value i% >alse. 0,e e a e %eve al 'a a#ete % t,at e7ui e t,at t,e DataTy e be xlDeli5ited and* w,en %et to Tr0e* indicate t,at E6cel %,ould u%e t,e co e%'onding c,a acte a% t,e te6t deli#ite . 0,e( a e de%c ibed in t,e 5ollowing li%t ?all de5ault value% a e >alse@:
Tab

1et to Tr0e to u%e t,e tab c,a acte a% deli#ite .


Semicolon

1et to Tr0e to u%e a %e#icolon a% deli#ite .


Comma

1et to Tr0e to u%e a co##a a% deli#ite .


S ace

1et to Tr0e to u%e a %'ace a% deli#ite .


/ther

1et to Tr0e to u%e a c,a acte t,at i% %'eci5ied b( t,e /therChar a gu#ent a% deli#ite . 4,en /ther i% Tr0e* /therChar %'eci5ie% t,e deli#ite c,a acte . /5 /therChar contain% #o e t,an one c,a acte * onl( t,e 5i %t c,a acte i% u%ed.

0,e Field1n*o 'a a#ete i% an a a( containing 'a %e in5o #ation 5o t,e individual %ou ce colu#n%. 0,e inte ' etation o5 Field1n*o de'end% on t,e value o5 DataTy e. 4,en DataTy e i% xlDeli5ited* t,e Field1n*o a gu#ent %,ould be an a a( w,o%e %iGe i% t,e %a#e a% ?o %#alle t,an<%ee 0able 19;3@ t,e nu#be o5 colu#n% o5 conve ted data. 0,e 5i %t ele#ent o5 a two;ele#ent a a( i% t,e colu#n nu#be ?%ta ting wit, t,e nu#be 1@* and t,e %econd ele#ent i% one o5 t,e nu#be % in 0able 19;3 t,at %'eci5ie% ,ow t,e colu#n i% 'a %ed. able 18-". Fiel#/n4o *alues 4or xl(eli&ite# ext Des ription .ene al 0e6t ")L date )"L date L") date "L) date )L" date L)" date 1-i' t,e colu#n

Co&e 1 2 3 4 5 6 7 8 9

/5 a two;ele#ent a a( 5o a given colu#n i% #i%%ing* t,en t,e colu#n i% 'a %ed wit, t,e .ene al %etting. $o in%tance* t,e 5ollowing value 5o Field1n*o cau%e% t,e 5i %t colu#n to be 'a %ed a% te6t and t,e t,i d colu#n to be %-i''ed:
Arra (Arra (1= 2)= Arra (B= E))

All ot,e colu#n% will be 'a %ed a% gene al data. 0o illu%t ate* con%ide t,e %,eet %,own in $igu e 19;15. 0,e code:
"an4e("A1-AB").TextToCol05ns G Destination-="an4e("11")= G DataT 3e-=xlDeli5ited= G Consec0tiveDeli5iter-=Tr0e= Co55a-=Tr0e= G >ield2n6o-=Arra (Arra (1= 2)= Arra (2= B))

' oduce% t,e %econd and t,i d colu#n% o5 $igu e 19;15. +ote t,at t,e cell% in colu#n ! a e 5o #atted a% date%. Figure 18-1'. % wor1sheet with text to be !arse# in %1F%"

On t,e ot,e ,and* i5 DataTy e i% xl>ixedHidth* t,e 5i %t ele#ent o5 eac, two;ele#ent a a( %'eci5ie% t,e %ta ting c,a acte 'o%ition in t,e colu#n ?0 being t,e 5i %t c,a acte @ and t,e %econd ele#ent %'eci5ie% t,e 'a %e o'tion ?1;9@ 5o t,e e%ulting colu#n* a% de%c ibed ' eviou%l(. 0o illu%t ate* con%ide t,e wo -%,eet in $igu e 19;16. 0,e code:
"an4e("A1-AB").TextToCol05ns G Destination-="an4e("11")= G DataT 3e-=xl>ixedHidth= G >ield2n6o-=Arra (Arra (7= 2)= G Arra (1= E)= Arra (2= 2)= Arra (&= E)= G

Arra (N= 2))

'a %e% t,e 5i %t colu#n o5 $igu e 19;16 into t,e e#aining colu#n%. ?+ote ,ow we included a a(% to %-i' t,e ,(',en%.@ Figure 18-15. % wor1sheet with 4ixe#-wi#th #ata to be !arse# in %1F%"

18.".$7 *alue Pro!erty


0,e =alue ' o'e t( etu n% t,e value o5 t,e %'eci5ied cell. /5 t,e cell i% e#'t(* =alue etu n% an e#'t( %t ing. 0,i% can be te%ted in eit,e o5 t,e 5ollowing wa(%:
26 "an4e("A1") = "" Then . . .

o:
26 2s'53t ("an4e("A1")) Then . . .

/5 t,e Range object contain% #o e t,an one cell* t,e Value ' o'e t( etu n% a two;di#en%ional a a(. $o in%tance* e5e ing to $igu e 19;16* t,e code:
Di5 v As +ariant v = "an4e("A1-AB").+al0e Deb04.;rint 2sArra (v) Deb04.;rint v(2= 1) . row 2= col 1

will ' int:


Tr0e 2%LB&%NFD

18.".$8 0ra! ext Pro!erty


0,i% ' o'e t( etu n% o %et% t,e value t,at tell% E6cel w,et,e to w a' te6t in t,e cell%. /t will etu n 90ll i5 t,e %'eci5ied ange contain% %o#e cell% t,at w a' te6t and ot,e % t,at do not. +ote t,at E6cel will c,ange t,e ow ,eig,t o5 t,e ange* i5 nece%%a (* to acco##odate t,e te6t w,en w a''ed.

$=%2 Children of the 6ange Ob1ect


0,e c,ild en o5 t,e Range object a e %,own in $igu e 19;17. Figure 18-16. +hil#ren o4 the ,ange object

!o e%'onding to eac, o5 t,e%e c,ild en i% a ' o'e t( o5 t,e Range object t,at etu n% t,e c,ild. $o in%tance* t,e &ivot$ield ' o'e t( o5 t,e Range object etu n% a &ivot$ield c,ild object. 3et u% ta-e a loo- at t,e c,ild en o5 t,e Range object.

18.$.1 he %reas +ollection


An area i% a contiguou% ?t,at i%* connected@ bloc- o5 cell% in a wo -%,eet. 0,e e i% no A ea object in t,e E6cel object #odel. /n%tead* a ea% a e Range object%. Aoweve * eve ( ange i% #ade u' o5 one o #o e a ea%* and t,e collection o5 all Range object% t,at e' e%ent t,e%e a ea% i% t,e A ea% collection 5o t,e ange. 0o illu%t ate* con%ide $igu e 19;18* w,ic, i% t,e e%ult o5 calling t,e 5ollowing code:
Di5 rn4 As "an4e Set rn4 = ActiveSheet.Cells.S3ecialCells( G xlCellT 3eConstants= xl905bers) rn4.Select

+ote t,at t, ee di%tinct a ea% a e %elected. Figure 18-17. % range with three areas

4e can clea t,e %econd a ea b( w iting:


rn4.Areas(2).Clear

0,i% will clea t,e cell% !4 and )4. ?Areas i% a 1;ba%ed collection.@ /t %t i-e% #e a% a bit i%-( to e5e to an individual a ea b( inde6. Aoweve * it i% 'e 5ectl( %a5e to

c(cle t, oug, all a ea% u%ing a >or loo' %uc, a%:


Di5 rn4 As "an4e= r As "an4e Set rn4 = ActiveSheet.Cells.S3ecialCells( G xlCellT 3eConstants= xl905bers) >or 'ach r 2n rn4.Areas Deb04.;rint r.Cells.Co0nt 9ext

18.$.2 he .or#ers +ollection


Eve ( ange ,a% a %et o5 bo de %. $o in%tance* t,e botto# bo de con%i%t% o5 t,e botto# bo de % o5 all o5 t,e cell% t,at one would encounte b( loo-ing u' at t,e ange 5 o# t,e botto# o5 t,e wo -%,eet. ?/#agine #oving u' eac, colu#n o5 t,e %,eet until (ou encounte a cell in t,e ange.@ $o e6a#'le* t,e botto# bo de o5 t,e ange:
"an4e("a1-bL= d2-e2")

i% %,own a% a da - line in $igu e 19;19. Figure 18-18. /llustrating the .or#er object

0,e >o de % ' o'e t( o5 t,e Range object etu n% a >o de % collection 5o t,e ange. 0,i% collection contain% %eve al >o de object%* inde6ed b( t,e 5ollowing con%tant%:
'n05 #l1orders2ndex xlDia4onalDown = & xlDia4onal<3 = N xl'd4eLe6t = F xl'd4eTo3 = D xl'd4e1otto5 = E xl'd4e"i4ht = 17 xl2nside+ertical = 11 xl2nside?oriMontal = 12 'nd 'n05

?0,e E6cel ,el' docu#entation e5e % to t,e%e a% #l1orderT 3e con%tant%.@ 0o illu%t ate* t,e 5ollowing code %et% t,e inte io colo o5 t,e ange %,own in $igu e 19;19 to a g a( %cale and %et% t,e botto# bo de to t,ic- ed ?%,own a% blac- in t,e 5igu e@. +ote t,e u%e o5 ne%ted Hith %tate#ent%:
Hith "an4e("a1-bL= d2-e2") .2nterior.Color = ":1(1EN= 1EN= 1EN) Hith .1orders(xl'd4e1otto5) .Hei4ht = xlThic8 .Color = ":1(2&&= 7= 7) 'nd Hith 'nd Hith

$igu e 19;20 %,ow% t,e e%ult% o5 c,anging t,e con%tant xl'd4e1otto5 to xlDia4onalDown* w,ile $igu e 19;21 %,ow% t,e e%ult% o5 c,anging t,e con%tant to xl2nside+ertical. Figure 18-29. he xl(iagonal(own constant

Figure 18-21. he xl/nsi#e*ertical constant

18.$." he .or#er 3bject


0,e #o%t inte e%ting ' o'e tie% and #et,od% o5 t,e >o de object a e de%c ibed in t,i% %ection. 18.$.".1 +olor !ro!erty 0,i% ' o'e t( etu n% o %et% t,e ' i#a ( colo o5 t,e bo de . /t can al%o be a''lied to t,e >o de % collection to %et all ve tical and ,o iGontal line% 5o t,e bo de % at t,e %a#e ti#e. ?0,e ' o'e t( al%o a''lie% to $ont object% and /nte io object%.@ $o in%tance* t,e 5ollowing code ,a% t,e e55ect %,own in $igu e 19;22:
Hith "an4e("a1-bL= d2-e2") .2nterior.Color = ":1(1EN= 1EN= 1EN) Hith .1orders .Hei4ht = xlThic8 .Color = ":1(2&&= 7= 7) 'nd Hith 'nd Hith

Figure 18-22. %ssigning the +olors !ro!erty o4 the .or#ers collection

0o %et a colo value* we u%e t,e ":1 colo 5unction* w,ic, ,a% t,e 5o #:
":1(red= !reen= blue)

w,e e red* !reen* and blue a e intege % between 0 and 255* inclu%ive* t,at e' e%ent t,e %t engt, o5 t,e e%'ective colo co#'onent. 0able 19;4 give% %o#e co##on colo value%. able 18-$. So&e +o&&on +olors Re& Green 0 0 0 0 0 255

Color >lac>lue . een

,lue 0 255 0

!(an Red "agenta Lellow 4,ite

0 255 255 255 255

255 0 0 255 255

255 0 255 0 255

0o u%e a g a(%cale* %et t,e ed* g een* and blue co#'onent% e7uall(. $o in%tance:
":1(1EN= 1EN= 1EN)

will ' oduce a 25J g a(%cale. ?0,e la ge t,e nu#be %* t,e clo%e to w,ite.@ 2n5o tunatel(* E6cel ound% all g a(%cale %etting% to one o5 t,e 5ollowing:
C C C C C C

0J ?w,ite@ 25J 40J 50J 80J 100J ?blac-@

Lou can %ee t,i% b( unning t,e 5ollowing code:


Di5 r As 2nte4er >or r = 1 To 2& Cells(r= 1).2nterior.Color = G ":1(2&& % 17 R r= 2&& % 17 R r= 2&& % 17 R r) Cells(r= 2).+al0e = 2&& % 17 R r 9ext

/5 (ou want to u%e g a(%cale% o5ten* con%ide adding t,e 5ollowing con%tant decla ation% to a code #odule. ?0,e nu#be % on t,e ig,t a e R.> value%.@
;0blic ;0blic ;0blic ;0blic Const Const Const Const :ra :ra :ra :ra 2& L7 &7 D7 = = = = 12NB22&N EDNDE&7 DL21&7L BB&&LLB

18.$.".2 +olor/n#ex !ro!erty 0,i% ' o'e t( %et% t,e colo b( u%ing an inde6 into a colo 'alette. 0,e e i% no wa( to do ju%tice to t,i% in a blac- and w,ite boo-* %o / %ugge%t (ou ta-e a loo- at t,i% ' o'e t( in E6celB% ,el' docu#entation* w,e e t,e e i% a colo 'ictu e. Aoweve * (ou can %et t,i% ' o'e t( to one o5 t,e 5ollowing #lColor2ndex con%tant% a% well:
'n05 #lColor2ndex xlColor2ndex9one = %L1L2 xlColor2ndexA0to5atic = %L17& 'nd 'n05 . no interior 6ill . a0to5atic 6ill

18.$."." )ineStyle !ro!erty 0,e 3ine1t(le ' o'e t( etu n% o %et% t,e line %t(le 5o t,e bo de . /t can be one o5 t,e 5ollowing #lLineSt le con%tant%:
'n05 #lLineSt le xlLineSt le9one = %L1L2 xlDo0ble = %L11E xlDot = %L11D xlDash = %L11& xlContin0o0s = 1

xlDashDot = L xlDashDotDot = & xlSlantDashDot = 1B 'nd 'n05

0,e%e value% %'ea- ' ett( #uc, 5o t,e#%elve%. 18.$.".$ 0eight !ro!erty 0,e 4eig,t ' o'e t( etu n% o %et% t,e weig,t o5 t,e bo de . /t can be one o5 t,e 5ollowing #l1orderHei4ht con%tant%:
'n05 #l1orderHei4ht xlMedi05 = %L1BD xl?airline = 1 xlThin = 2 xlThic8 = L 'nd 'n05

18.$.$ he +haracters 3bject


0,e !,a acte % object e' e%ent% a contiguou% %e7uence o5 te6t c,a acte %. 0,e #ain 'u 'o%e o5 t,e !,a acte % object i% to #odi5( a portion o5 a te6t %t ing. 0,e %(nta6 i%:
%an!e/bject.Characters(start= len!th)

w,e e start i% t,e %ta t c,a acte nu#be and len!th i% t,e nu#be o5 c,a acte %. 0o illu%t ate* t,e 5ollowing code bold5ace% t,e 5i %t wo d in a cell:
Di5 rn4 As "an4e Set rn4 = "an4e("A1") rn4.Characters(1= 2nStr(rn4.+al0e= " ") % 1).>ont.1old = Tr0e

0,e e%ult i% %,own in $igu e 19;23. Figure 18-2". .ol#4acing the 4irst wor# o4 a cell

18.$.' he +o&&ent 3bject


Recall t,at t,e Add!o##ent #et,od o5 t,e Range object i% u%ed to add a co##ent to a ange. Once t,e co##ent ,a% been added* a co e%'onding !o##ent object i% c eated. Eac, co##ent object belong% to t,e !o##ent% collection o5 t,e 4o -%,eet object. 0o illu%t ate* t,e 5ollowing code c eate% a co##ent in cell A1 i5 it doe% not al ead( e6i%t. /t t,en %et% t,e te6t and #a-e% t,e co##ent vi%ible 5o a'' o6i#atel( t, ee %econd%. +ote t,e u%e o5 t,e Do'vents %tate#ent to en%u e t,at 4indow% ,a% t,e o''o tunit( to di%'la( t,e co##ent be5o e ente ing t,e Do loo'. ?Lou #ig,t want to t ( t,i% code wit,out t,e Do'vents %tate#ent. On #( %(%te#* t,e co##ent i% not di%'la(ed.@ +ote al%o t,at t,e Ti5er 5unction etu n% t,e nu#be o5 %econd% %ince #idnig,t ?%o t,e e i% a 'otential ' oble# i5 t,e t, ee;%econd inte val ,a''en% to occu at #idnig,t@.
Di5 t5 As Sin4le t5 = Ti5er 26 "an4e("A1").Co55ent 2s 9othin4 Then "an4e("A1").AddCo55ent "co55ent"

'nd 26 "an4e("A1").Co55ent.Text "Created- " K 9ow "an4e("A1").Co55ent.+isible = Tr0e Do'vents Do- Loo3 <ntil Ti5er % t5 ( B "an4e("A1").Co55ent.+isible = >alse

18.$.5 he Font 3bject


0,e $ont ' o'e t( o5 a Range object etu n% a $ont object. $ont object% a e u%ed to cont ol t,e c,a acte i%tic% o5 t,e 5ont ?5ont na#e* %iGe* colo * and %o on@ u%ed in t,e ange. 0,e ' o'e tie% o5 t,e $ont object a e %,own in 0able 19;5. able 18-'. Pro!erties o4 the Font 3bject $ont1t(le 1iGe /talic 1t i-et, oug, +a#e 1ub%c i't Outline$ont 1u'e %c i't &a ent 2nde line 1,adow

A''lication >ac-g ound >old !olo !olo /nde6 ! eato

Recall t,at t,e !,a acte % ' o'e t( can be u%ed to 5o #at portions o5 te6t.

18.$.6 he For&at+on#itions +ollection


E6cel allow% u% to a''l( conditional formatting to a cell ?o a ange o5 cell%@. A conditional format i% a 5o #at t,at i% a''lied i5 and onl( i5 ce tain condition% a e #et b( t,e content% o5 t,e cell. $o in%tance* we #a( want to #a-e a nu#be ed i5 it i% negative* blac- i5 it i% 'o%itive* o g een i5 it i% 0. 0,i% e7ui e% t, ee conditional 5o #at%. 0,e $o #at!ondition% ' o'e t( o5 a Range object etu n% a $o #at!ondition% collection t,at can contain u' to t, ee $o #at!ondition object%* eac, o5 w,ic, e' e%ent% a conditional 5o #at. 0,e Add #et,od o5 t,e $o #at!ondition% collection i% u%ed to add $o #at!ondition object% to t,e collection. Aoweve * atte#'ting to add #o e t,an t, ee %uc, object% will gene ate an e o . 0,e %(nta6 5o t,e Add #et,od i%:
FormatConditions/bject.Add(Ty e= / erator= Formula#= Formula$)

0,e e7ui ed Ty e 'a a#ete %'eci5ie% w,et,e t,e conditional 5o #at i% ba%ed on t,e value in t,e cell o an e6' e%%ion. /t can be eit,e o5 t,e 5ollowing #l>or5atConditionT 3e con%tant%:
'n05 #l>or5atConditionT 3e xlCell+al0e = 1 xl'x3ression = 2 'nd 'n05

4,en Ty e i% xlCell+al0e* t,e / erator 'a a#ete %'eci5ie% t,e o'e ato to u%e wit, t,at value. /5 Ty e i% xl'x3ression* t,e / erator a gu#ent i% igno ed. 0,e value o5 / erator i% one o5 t,e 5ollowing con%tant%:
'n05 #l>or5atCondition*3erator xl1etween = 1 xl9ot1etween = 2

xl'/0al = B xl9ot'/0al = L xl:reater = & xlLess = N xl:reater'/0al = F xlLess'/0al = D 'nd 'n05

/5 Ty e i% xlCell+al0e* t,en Formula# and Formula$ give t,e co#'a i%on value% u%ed wit, / erator and t,e cell value. +ote t,at Formula$ i% u%ed onl( wit, t,e xl1etween and xl9ot1etween con%tant%. $o e6a#'le* t,e 5ollowing code %et% t,e inte io colo o5 a cell in t,e ange A1:!4 to 25J g a(%cale i5 t,e nu#be i% between 0 and 10 ?inclu%ive@ and to w,ite ot,e wi%e. 0,e e%ult% a e %,own in $igu e 19;24. +ote t,at we 5i %t clea ed all conditional 5o #atting be5o e c eating new $o #at!ondition object%. +ote al%o t,at an e#'t( cell i% t eated a% i5 it contain% a 0.
Di5 rn4 As "an4e Di5 i As 2nte4er Set rn4 = "an4e("A1-CL") . Clear all existin4 6or5ats >or i = rn4.>or5atConditions.Co0nt To 1 Ste3 %1 rn4.>or5atConditions(i).Delete 9ext Hith rn4 .>or5atConditions.Add xlCell+al0e= xl1etween= 7= 17 .>or5atConditions(1).2nterior.Color = ":1(1EN= 1EN= 1EN) .>or5atConditions.Add xlCell+al0e= xl9ot1etween= 7= 17 .>or5atConditions(2).2nterior.Color = ":1(2&&= 2&&= 2&&) 'nd Hith

Figure 18-2$. % con#itionally 4or&atte# range

4,en Ty e i% xl'x3ression* Formula$ i% igno ed* and Formula# give% t,e 5o #ula o e6' e%%ion t,at dete #ine% t,e condition. 0,i% 'a a#ete can be a con%tant* a %t ing* a cell e5e ence* o a 5o #ula. 0o illu%t ate* t,e 5ollowing code %et% t,e inte io colo ba%ed on w,et,e cell% A1 and A2 contain t,e %a#e value:
Di5 rn4 As "an4e Di5 i As 2nte4er Set rn4 = "an4e("A1-A2") . Clear all existin4 6or5ats >or i = rn4.>or5atConditions.Co0nt To 1 Ste3 %1 rn4.>or5atConditions(i).Delete 9ext Hith rn4

.>or5atConditions.Add xl'x3ression= = G "an4e("A1").+al0e = "an4e("A2").+al0e .>or5atConditions(1).2nterior.Color = G ":1(7= 7= 2&&) .>or5atConditions.Add xl'x3ression= = G "an4e("A1").+al0e $( "an4e("A2").+al0e .>or5atConditions(2).2nterior.Color = G ":1(2&&= 7= 7) 'nd Hith

A% t,e ' eviou% e6a#'le% %,ow* t,e actual 5o #atting i% done b( %etting %o#e o5 t,e ' o'e tie% o5 c,ild en o5 t,e $o #at!ondition object. /n 'a ticula * t,e >o de %* $ont* and /nte io ' o'e tie% etu n c,ild object% o5 t,e %a#e na#e* w,o%e ' o'e tie% can be %et to indicate t,e de%i ed 5o #atting. +ote 5inall( t,at an e6i%ting $o #at!ondition object can be deleted u%ing t,e )elete #et,od o5 t,e $o #at!ondition% collection* and it can be c,anged u%ing t,e "odi5( #et,od o5 t,e $o #at!ondition object. 0,e "odi5( #et,od ,a% t,e %(nta6:
FormatCondition/bject.Modi6 (Ty e= / erator= Formula#= Formula$)

w,e e t,e 'a a#ete % a e identical to t,o%e o5 t,e Add #et,od.

18.$.7 he /nterior 3bject


0,e /nte io object e' e%ent% t,e c,a acte i%tic% o5 t,e inte io egion o5 a cell ?o ange o5 cell%@. 0,e /nte io object ,a% onl( a ,and5ul o5 ' o'e tie% ?and no #et,od%@* a% de%c ibed in t,i% %ection. 18.$.7.1 +olor an# +olor/n#ex !ro!erties 0,e%e ' o'e tie% a e analogou% to t,e ' o'e tie% b( t,e %a#e na#e o5 t,e >o de % object* di%cu%%ed ea lie . 0,e( %et t,e inte io o5 a cell ?o cell%@ to t,e colo %'eci5ied. 18.$.7.2 Pattern !ro!erty 0,i% ' o'e t( etu n% o %et% t,e inte io 'atte n. /t can be one o5 t,e 5ollowing #l;attern con%tant%:
'n05 #l;attern xl;attern+ertical = %L1NN xl;attern<3 = %L1N2 xl;attern9one = %L1L2 xl;attern?oriMontal = %L12D xl;attern:ra F& = %L12N xl;attern:ra &7 = %L12& xl;attern:ra 2& = %L12L xl;atternDown = %L121 xl;atternA0to5atic = %L17& xl;atternSolid = 1 xl;atternChec8er = E xl;atternSe5i:ra F& = 17 xl;atternLi4ht?oriMontal = 11 xl;atternLi4ht+ertical = 12 xl;atternLi4htDown = 1B xl;atternLi4ht<3 = 1L xl;attern:rid = 1& xl;atternCrissCross = 1N xl;attern:ra 1N = 1F xl;attern:ra D = 1D 'nd 'n05

+ote t,at t,i% ' ovide% anot,e wa( to acce%% g a(%cale%.

18.$.7." Pattern+olor an# Pattern+olor/n#ex !ro!erties 0,e%e ' o'e tie% %et t,e colo ?o colo inde6@ o5 t,e 'atte n u%ed to 5ill t,e inte io o5 a cell. $o #o e on %etting colo and colo inde6e%* 'lea%e %ee t,e di%cu%%ion o5 t,e !olo and !olo /nde6 ' o'e tie% o5 t,e >o de object.

18.$.8 he PivotFiel#I Pivot/te&I an# Pivot able 3bjects


0,e%e object% elate to &ivot0able object% and will be di%cu%%ed in !,a'te 20.

18.$.19 he =uery able 3bject


A Oue (0able object e' e%ent% a wo -%,eet table t,at i% built 5 o# data etu ned 5 o# an e6te nal data %ou ce* %uc, a% "ic o%o5t 1O3 1e ve o a "ic o%o5t Acce%% databa%e. 4e will not di%cu%% Oue (0able object% in t,i% boo-. ?0,e e a e bette wa(% to et ieve data 5 o# an e6te nal %ou ce.@

18.$.11 he *ali#ation 3bject


A =alidation object i% u%ed to en5o ce data validation on a cell o ange o5 cell%. 0,e =alidation ' o'e t( o5 t,e Range object etu n% a =alidation object* w,o%e ' o'e tie% can be etu ned o %et. +ote t,at t,e e i% no =alidation% collection. )ata validation involve% t, ee 'a t%: t,e actual validation* an in'ut #e%%age t,at can be di%'la(ed w,en a cell i% activated* and an e o #e%%age t,at can be di%'la(ed i5 t,e data ente ed i% invalid. 0,e #et,od% o5 t,e =alidation object a e Add* )elete* and "odi5(. 0o add validation to a ange* u%e t,e Add #et,od* w,o%e %(nta6 i%:
Validation/bject.Add(Ty e= -lertStyle= / erator= Formula#= Formula$)

+ote t,e %i#ila it( between t,e 'a a#ete % o5 t,e Add #et,od o5 t,e =alidation object and t,e Add #et,od o5 t,e $o #at!ondition% object. 0,e e7ui ed Ty e 'a a#ete %'eci5ie% t,e t('e o5 data allowed and can be one o5 t,e 5ollowing #lD+T 3e con%tant%:
'n05 #lD+T 3e xl+alidate2n30t*nl = 7 xl+alidateHhole905ber = 1 xl+alidateDeci5al = 2 xl+alidateList = B xl+alidateDate = L xl+alidateTi5e = & xl+alidateTextLen4th = N xl+alidateC0sto5 = F 'nd 'n05

0,e xl+alidate2n30t*nl con%tant cau%e% E6cel to t eat all data a% valid. 0,i% value %,ould be u%ed w,en we want to di%'la( an in'ut #e%%age ?de%c ibed late in t,i% %ection@* but not invo-e data validation. 0,e o'tional -lertStyle 'a a#ete %'eci5ie% t,e button% t,at will a''ea on t,e e o dialog bo6 t,at i% di%'la(ed i5 t,e data ente ed i% invalid. /t can be one o5 t,e 5ollowing #lD+AlertSt le con%tant%:
'n05 #lD+AlertSt le xl+alidAlertSto3 = 1 xl+alidAlertHarnin4 = 2

xl+alidAlert2n6or5ation = B 'nd 'n05

0,e #eaning% o5 t,e%e con%tant% a e a% 5ollow%: xl+alidAlert2n6or5ation OR and !ancel button% xl+alidAlertSto3 Ret ( and !ancel button% xl+alidAlertHarnin4 Le%* +o* and !ancel button% 0,e o'tional / erator 'a a#ete i% t,e o'e ato u%ed in t,e validation* and can be an( one o5 t,e #l>or5atCondition*3erator con%tant%:
'n05 #l>or5atCondition*3erator xl1etween = 1 xl9ot1etween = 2 xl'/0al = B xl9ot'/0al = L xl:reater = & xlLess = N xl:reater'/0al = F xlLess'/0al = D 'nd 'n05

0,e Formula# 'a a#ete %'eci5ie% t,e 5i %t 'a t o5 t,e data;validation e7uation and Formula$ %'eci5ie% t,e %econd 'a t w,en / erator i% xl1etween o xl9ot1etween. 0o unde %tand t,i% at,e co#'le6 object* it i% be%t to loo- at t,e co e%'onding dialog bo6e% in t,e E6cel u%e inte 5ace. $igu e 19;25 %,ow% t,e 1etting% tab o5 t,e =alidation dialog bo6. Figure 18-2'. he Settings tab o4 the (ata *ali#ation #ialog

0,i% dialog co e%'ond% to %etting:


T 3e-=xl+alidateHhole905ber *3erator-=xl1etween >or50la1-="&" >or50la2-="17" 24nore1lan8 = Tr0e

Lou can lea n #o e about t,e Ty e con%tant% b( clic-ing on t,e 9 button in t,e )ata =alidation dialog and t,en clic-ing on t,e Allow d o';down li%t bo6. +ote t,at t,e ot,e cont ol% on t,e tab in $igu e 19;25 will c,ange de'ending u'on t,e value %elected in t,e Allow d o';down bo6. 0,e /n'ut "e%%age tab i% %,own in $igu e 19;26. 0,e value% in t,i% dialog co e%'ond to ' o'e tie% o5 t,e =alidation object. /n 'a ticula * we ,ave:
Show2n30t = Tr0e 2n30tTitle = "2n30t-" 2n30tMessa4e = "2n30t a n05ber"

Figure 18-25. he /n!ut -essage tab o4 the (ata *ali#ation #ialog

$igu e 19;27 %,ow% t,e E o Ale t tab. 0,i% dialog co e%'ond% to t,e 5ollowing ' o'e tie% o5 t,e =alidation object:
Show'rror = Tr0e 'rrorTitle = "'rror-" 'rrorMessa4e = "This is an error"

Figure 18-26. he Error %lert tab o4 the (ata *ali#ation #ialog

4e can now 'ut all o5 t,e 'iece% toget,e to %,ow ,ow to %et u' data validation 5o a ange o5 cell%. +ote t,at t,e 5i %t o de o5 bu%ine%% i% to delete an( old validation:
Hith "an4e("A1-DL").+alidation .Delete .Add T 3e-=xl+alidateHhole905ber= G AlertSt le-=xl+alidAlertSto3= G *3erator-=xl1etween= G >or50la1-="&"= >or50la2-="17" .24nore1lan8 = Tr0e .Show2n30t = Tr0e .2n30tTitle = "2n30t-" .2n30tMessa4e = "2n30t a n05ber"

278

.Show'rror = Tr0e .'rrorTitle = "'rror-" .'rrorMessa4e = "This is a error" 'nd Hith

$=%3 Example+ 5etting the #sed 6ange


A% we #entioned in !,a'te 18* t,e 2%edRange #et,od %ee#% to ,ave %o#e ' oble#%* in t,at it doe% not alwa(% etu n w,at we would con%ide to be t,e currently u%ed ange* t,at i% t,e %#alle%t ectangula egion o5 cell% t,at contain% all cell% t,at cu entl( ,ave data. /n an( ca%e* i5 (ou* too* ,ave t ouble wit, t,e 2%edRange #et,od* t,e 5ollowing 5unction can be u%ed in it% 'lace. +ote t,at t,e 5unction :et<sed"an4e doe% a%%u#e t,at E6celB% 2%edRange #et,od etu n% a %u'e %et o5 t,e co ect u%ed ange. 0,e o'e ation o5 :et<sed"an4e i% %t aig,t5o wa d. A% it% %ou ce code in E6a#'le 19;1 %,ow%* t,e 5unction %ta t% wit, E6celB% u%ed ange* dete #ine% t,e coo dinate% ? ow and colu#n nu#be %@ o5 t,e u''e ;le5t and lowe ; ig,t co ne % o5 t,i% ange* and t,en ' oceed% to %, in- t,i% ange i5 it contain% ow% o colu#n% t,at a e blan-. 0,i% i% dete #ined b( u%ing t,e E6cel Co0ntA wo -%,eet 5unction* w,ic, count% t,e nu#be o5 none#'t( cell%. Exa&!le 18-1. he :et2se#,ange Function
>0nction :et<sed"an4e(ws As Hor8sheet) As "an4e . Ass05es that 'xcel.s <sed"an4e 4ives a s03erset . o6 the real 0sed ran4e. Di5 Di5 Di5 Di5 Di5 Di5 Di5 s As Strin4= x As 2nte4er rn4 As "an4e r1>ixed As 2nte4er= c1>ixed As 2nte4er r2>ixed As 2nte4er= c2>ixed As 2nte4er i As 2nte4er r1 As 2nte4er= c1 As 2nte4er r2 As 2nte4er= c2 As 2nte4er

Set :et<sed"an4e = 9othin4 . Start with 'xcel.s 0sed ran4e Set rn4 = ws.<sed"an4e . :et bo0ndin4 cells 6or 'xcel.s 0sed ran4e . That is= Cells(r1=c1) to Cells(r2=c2) r1 = rn4."ow r2 = rn4."ows.Co0nt A r1 % 1 c1 = rn4.Col05n c2 = rn4.Col05ns.Co0nt A c1 % 1 . Save existin4 val0es r1>ixed = r1 c1>ixed = c1 r2>ixed = r2 c2>ixed = c2 . Chec8 rows 6ro5 to3 down 6or all blan8s. . 26 6o0nd= shrin8 rows. >or i = 1 To r2>ixed % r1>ixed A 1 26 A33lication.Co0ntA(rn4."ows(i)) = 7 Then

. e53t row %% red0ce r1 = r1 A 1 'lse . none53t row= 4et o0t 'xit >or 'nd 26 9ext . "e3eat 6or col05ns 6ro5 le6t to ri4ht >or i = 1 To c2>ixed % c1>ixed A 1 26 A33lication.Co0ntA(rn4.Col05ns(i)) = 7 Then c1 = c1 A 1 'lse 'xit >or 'nd 26 9ext . "eset the ran4e Set rn4 = G ws."an4e(ws.Cells(r1= c1)= ws.Cells(r2= c2)) . Start r1>ixed c1>ixed r2>ixed c2>ixed a4ain = r1 = c1 = r2 = c2

. Do rows 6ro5 botto5 03 >or i = r2>ixed % r1>ixed A 1 To 1 Ste3 %1 26 A33lication.Co0ntA(rn4."ows(i)) = 7 Then r2 = r2 % 1 'lse 'xit >or 'nd 26 9ext . "e3eat 6or col05ns 6ro5 ri4ht to le6t >or i = c2>ixed % c1>ixed A 1 To 1 Ste3 %1 26 A33lication.Co0ntA(rn4.Col05ns(i)) = 7 Then c2 = c2 % 1 'lse 'xit >or 'nd 26 9ext Set :et<sed"an4e = G ws."an4e(ws.Cells(r1= c1)= ws.Cells(r2= c2)) 'nd >0nction

$=%8 Example+ Selecting Special Cells


0,e E6cel u%e inte 5ace doe% not ,ave a built;in #et,od 5o %electing wo -%,eet cell% ba%ed on va iou% c ite ia. $o in%tance* t,e e i% no wa( to %elect all cell% w,o%e value i% between 0 and 100* o all cell% t,at contain a date late t,an Nanua ( 1* 1998. 0,e e i% al%o no wa( to %elect onl( t,o%e cell% in a given colu#n w,o%e value i% di55e ent 5 o# t,e value o5 t,e ' eceding cell. 0,i% can be ve ( u%e5ul w,en (ou ,ave a %o ted colu#n and want to e6t act a %et o5 uni8ue value%* a% %,own in $igu e 19;28.

Figure 18-27. Selecting unique values

4e will develo' a %#all utilit( ?and add it to t,e 1RD2til% a''lication@ t,at can #a-e a %election ba%ed on %o#e %i#'le c ite ia. Lou #a( want to en,ance t,i% utilit( b( adding #o e c ite ia. 0,e 5i %t %te' i% to aug#ent t,e )ata1,eet 5o 1RD2til% b( adding a new ow 5o t,e new utilit(* a% %,own in $igu e 19;29. ?0,e o de o5 t,e ow% in t,i% )ata1,eet i% ba%ed on t,e o de in w,ic, we want t,e ite#% to a''ea in t,e cu%to# #enu.@ Figure 18-28. %ug&enting the (ataSheet wor1sheet

18.5.1 (esigning the 2tility


0o -ee' ou utilit( elativel( %i#'le* we will i#'le#ent t,e 5ollowing %election c ite ia:
C C C C

1elect cell i5 ' eceding cell i% di55e ent 1elect cell i5 ' eceding cell i% t,e %a#e 1elect e#'t( cell% 1elect none#'t( cell%

0,e %ea c, ange 5o t,e %election o'e ation* t,at i%* t,e a ea to w,ic, t,e %election c ite ia will be a''lied* i% t,e cu ent %election on t,e active wo -%,eet. +ote t,at we will need to ve i5( t,at t,i% i% a %election o5 wo -%,eet cell% and not* %a(* a c,a t. $o t,e 5i %t two c ite ia* t,i% ange #u%t be eit,e a %ingle ow o a %ingle colu#n o a 'o tion t,e eo5. $o t,e la%t two c ite ia* t,e %ea c, ange can be an( %election o5 cell%. A% a cou te%( to t,e u%e * i5 t,e cu ent %election i% ju%t a %ingle cell* t,e utilit( will de5ault to t,e u%ed ange 5o t,e la%t two c ite ia ?e#'t( o none#'t(@ and to t,e u%ed 'o tion o5 t,e column containing t,e active cell 5o t,e 5i %t two c ite ia ?%a#e and di55e ent@. A% a bonu%* we al%o include a 5eatu e t,at enla ge% t,e cu ent %election b( including t,e enti e ow ?o colu#n@ containing eac, %elected cell. $o in%tance* a''l(ing t,i% to t,e wo -%,eet in $igu e 19;28 will %elect ow% 12* 16* 18* 23* and 25.

18.5.2 (esigning the (ialog

+ow t,at ou ga#e 'lan ,a% be #a''ed out* we can de%ign and con%t uct t,e dialog. 0,e 5inal ' oduct i% %,own in $igu e 19;30. /t i% a 2%e $o # called dl!SelectS ecial* and it% !a'tion ' o'e t( %,ould be %et to 81elect 1'ecial.8 Figure 18-"9. Select S!ecial #ialog

A% to t,e o'e ation o5 t,e utilit(* t,e u%e will 5i %t %elect one o5 t,e #utuall( e6clu%ive o'tion% unde 1elect !ell% /5. 0,e actual %ea c, ange i% di%'la(ed at t,e botto# o5 t,e dialog. Ae e a e %o#e o5 t,e ,ig,lig,t% o5 t,i% 5o # de%ign. 4e %ugge%t (ou ead on be5o e c eating (ou own 5o #. 18.5.2.1 he Fra&e control A 5 a#e cont ol i% u%ed to g ou' ot,e cont ol%. 0,i% i% o5ten done ju%t to g ou' cont ol% t,at ,ave a %i#ila 'u 'o%e. Aoweve * in t,e ca%e o5 o'tion button%* it ,a% a #o e ' o5ound e55ect. +a#el(* t,e o'tion button% in a %ingle 5 a#e a e mutually exclusi#e* w,ic, #ean% t,at i5 t,e u%e %elect% one o'tion button* t,e ot,e % a e auto#aticall( un%elected. 0o en%u e t,at t,e o'tion button% a e eall( in%ide t,e 5 a#e and not #e el( on to' o5 it* #a-e %u e t,e 5 a#e i% %elected w,en (ou clic- on t,e $ptionButton cont ol icon in t,e 0oolbo6. 0,en c eate t,e o'tion button in%ide t,e 5 a#e. Al%o* i5 (ou decide to co'( and 'a%te t,e additional o'tion button%* #a-e %u e t,at t,e 5 a#e i% %elected w,en (ou c,oo%e t,e &a%te co##and. 18.5.2.2 +ontrol na&es 0,e cont ol na#e% we e c,o%en to con5o # to #( na#ing convention. 0,ei na#e% a e:
C C C C C C C C C C *raTy e ?5 a#e@ o tDi**erent o tSame o t,m ty cmdSelect cmdCancel cmd7ndo cmdCom lete%ows cmdCom leteColumns lblSearch%an!e

Lou will not need to %et #an( cont ol ' o'e tie% be(ond t,e +a#e ' o'e t( and t,e Accele ato ' o'e t( ?indicated 5o eac, cont ol in $igu e 19;30 b( an unde %co e in it% ca'tion@. >e %u e to %et t,e 4o d4 a' ' o'e t( o5 t,e lblSearch%an!e label to >alse %o t,at t,e label will occu'(

onl( a %ingle line. Al%o* %et t,e 0ab1to' ' o'e t( o5 lblSearch%an!e to >alse. 18.5.2." ab 3r#er /t i% i#'o tant w,eneve de%igning a cu%to# dialog to %et t,e tab o de o5 all cont ol% ' o'e l(. 0,e e i% not,ing le%% ' o5e%%ional t,an ,aving t,e 5ocu% ju#' a ound ando#l( w,en t,e u%e e'eatedl( ,it% t,e 0ab -e(C 0,e %i#'le%t wa( to %et t,e co ect tab o de i% to u%e t,e 0ab O de dialog bo6* available 5 o# t,e =iew #enu and %,own in $igu e 19;31. Lou can u%e t,i% dialog to get an ove all view o5 t,e cu ent tab o de and to c,ange t,at o de * i5 de%i ed. Re#e#be t,at t,e cont ol wit, tab o de 0 will eceive t,e 5ocu% w,en t,e dialog i% 5i %t di%'la(ed. Lou will need to di%'la( t,e 0ab O de dialog twice: once w,ile t,e enti e dialog i% %elected and once w,ile t,e 5 a#e cont ol i% %elected ?to %ee t,e tab o de % o5 t,e o'tion button%@. Figure 18-"1. he ab 3r#er #ialog

18.5.2.$ So&e 4inal ti!s 4e %,ould e#a - t,at t,e => edito B% $o #at #enu ,a% %o#e ve ( u%e5ul ite#% 5o aligning and e%iGing cont ol% on a 2%e $o # to give (ou 5o #% a #o e ' o5e%%ional loo-. Lou %,ould de5initel( do %o#e e6'lo ation o5 t,i% #enu. Anot,e u%e5ul t ic- i% to co'( and 'a%te cont ol%. 0,i% ' oduce% cont ol% o5 identical %iGe and ' e%e ve% ot,e ' o'e tie% a% well. ?O5 cou %e* %o#e ' o'e tie%* %uc, a% t,e +a#e ' o'e t( o t,e 'o%ition ' o'e tie%* a e not ' e%e ved.@

18.5." 0riting the +o#e


+ow t,at t,e dialog i% c eated* we can %ta t w iting t,e code. /n t,e bas(ain %tanda d #odule* 'lace t,e code t,at di%'la(% t,e 1elect 1'ecial dialog bo6. Aoweve * it i% 'o%%ible t,at t,e cu ent %election in t,e active wo -%,eet i% not a collection o5 cell%. /t could be a d awing object o c,a t* 5o in%tance. /n t,i% ca%e* we want to i%%ue a #e%%age %tating t,at t,e cu ent %election i% ina'' o' iate 5o t,e /elect/pecial utilit( and not to bot,e di%'la(ing t,e dialog. 0,e code in E6a#'le 19;2 ?w,ic, %,ould be %to ed in bas(ain@ will do t,e job. Exa&!le 18-2. he SelectS!ecial Proce#ure
S0b SelectS3ecial() . Chec8 6or valid selection 26 T 3e9a5e(Selection) $( ""an4e" Then Ms41ox "Selection 50st be a ran4e o6 wor8sheet cells."= vbCritical 'lse dl4SelectS3ecial.Show 'nd 26 'nd S0b

+ote t,at we u%e t,e T 3e9a5e 5unction. 4,en a''lied to an object* a% in:

T 3e9a5e(*bTect+ariable)

t,e 5unction will etu n t,e na#e o5 t,e object. +e6t* we need a cou'le o5 #odule;level decla ation%* %,own in E6a#'le 19;3* in t,e 5o #B% code #odule. Exa&!le 18-". #lgSelectS!ecial -o#ule-)evel (eclarations
*3tion 'x3licit . These are 0sed b 5ore than one 3roced0re Di5 rn4Search As "an4e Di5 rn4>or<ndo As "an4e

0,e /nitialiGe event o5 t,e 5o # i% t,e 'lace to initialiGe t,e cont ol%. A% E6a#'le 19;4 %,ow%* we 5i %t want to di%able %o#e co##and button% and 5ill t,e lblSearch%an!e label. 4e al%o can %et t,e #odule;level va iable% ,e e. Exa&!le 18-$. he /nitiali?e Event Proce#ure
;rivate S0b <ser>or5G2nitialiMe() c5dSelect.'nabled = >alse c5d<ndo.'nabled = >alse lblSearch"an4e.Ca3tion = "Search "an4e- 9othin4" Set rn4Search = Selection Set rn4>or<ndo = rn4Search 'nd S0b

0,e !lo%e button %i#'l( unload% t,e 5o #H it% %ou ce code i% %,own in E6a#'le 19;5. Exa&!le 18-'. he c&#+loseG+lic1 Event Proce#ure
;rivate S0b c5dCloseGClic8() <nload Me 'nd S0b

/ncidentall(* (ou can te%t out (ou ' og e%% %o 5a ?and late @ b( unning t,e /nitialiGe event. Nu%t 'lace t,e cu %o in t,i% event and ,it $5.

0,e 2ndo button etu n% t,e %election to it% o iginal %tate* w,ic, i% %aved in t,e #odule;level va iable rn!For7ndo. /t% %ou ce code i% %,own in E6a#'le 19;6. Exa&!le 18-5. he c&#2n#oG+lic1 Event Proce#ure
;rivate S0b c5d<ndoGClic8() 26 9ot rn4>or<ndo 2s 9othin4 Then rn4>or<ndo.Select c5d<ndo.'nabled = >alse 'nd 26 'nd S0b

0,e 5i %t t,ing t,e u%e will do a5te t,e dialog i% di%'la(ed i% c,oo%e an o'tion 5 o# t,e 5 a#e at t,e to'. 0,i% c,oice will dete #ine in 'a t t,e %ea c, ange. Al%o* %o#e c,oice% e7ui e a #o e e%t ictive %ea c, ange. 0o eact to t,e u%e B% c,oice* we call a ' ocedu e called :etSearch"an4e w,eneve an o'tion button i% %elected. 0,e code to ,andle t,e o'tion button% i% %,own in E6a#'le 19;7. Exa&!le 18-6. Event Can#lers 4or the 3!tion .uttons
;rivate S0b o3tDi66erentGClic8() :etSearch"an4e 'nd S0b ;rivate S0b o3t'53t GClic8() :etSearch"an4e 'nd S0b ;rivate S0b o3t9ot'53t GClic8() :etSearch"an4e 'nd S0b ;rivate S0b o3tSa5eGClic8() :etSearch"an4e 'nd S0b

0,e :etSearch"an4e ' ocedu e i% %,own in E6a#'le 19;8. Exa&!le 18-7. he :etSearch,ange Proce#ure
;rivate S0b :etSearch"an4e() . Set search ran4e based on choice o6 search t 3e. . 26 Di66erent or Sa5e= validate ran4e . 26 sin4le cell= chan4e to. % 0sed col05n 6or Di66erent or Sa5e 5atch . % 0sed ran4e 6or '53t or 9ot '53t 5atch . He 8now that rn4Search is a ran4e o6 cells. . Disables Select b0tton i6 not a valid ran4e. Di5 cCol05ns As 2nte4er= c"ows As 2nte4er c5dSelect.'nabled = Tr0e . Ma be te53orar

26 o3tDi66erent *r o3tSa5e Then . Search ran4e 50st be (3ortion o6) . a sin4le row or col05n cCol05ns = rn4Search.Col05ns.Co0nt

c"ows = rn4Search."ows.Co0nt 26 rn4Search.Areas.Co0nt ( 1 *r G (cCol05ns $( 1 And c"ows $( 1) Then lblSearch"an4e.Ca3tion = ""e/0ires (3ortion o6) sin4le col05n or row." c5dSelect.'nabled = >alse 'xit S0b 'nd 26 . 26 sin4le cell then ex3and to 0sed 3ortion o6 col05n 26 cCol05ns = 1 And c"ows = 1 Then Set rn4Search = A33lication.2ntersect( G rn4Search.'ntireCol05n= ActiveSheet.<sed"an4e) 'nd 26 'lse26 o3t'53t *r o3t9ot'53t Then . 26 selection is sin4le cell then ex3and to 0sed ran4e 26 rn4Search.Cells.Co0nt = 1 Then Set rn4Search = ActiveSheet.<sed"an4e 'nd 26 'nd 26 lblSearch"an4e.Ca3tion = "Search "an4e- " K G rn4Search.Address("owAbsol0te-=>alse= Col05nAbsol0te-=>alse) 'nd S0b

4,en t,e u%e ,it% t,e 1elect button* t,e action begin%* ba%ed on t,e u%e B% %election. 0,u%* we %,ould call a di55e ent ' ocedu e ba%ed on w,ic, o'tion button i% %elected. A5te t,e new %election i% #ade* t,e 1elect button i% di%abled. 1ince t,e !o#'leteRow% and !o#'lete!olu#n% 5eatu e% a e %till available* ,oweve * we do not want to di%#i%% t,e #ain dialog. 0,e code to ,andle t,e 1elect button i% %,own in E6a#'le 19;9. Exa&!le 18-8. he c&#SelectG+lic1 Event Proce#ure
;rivate S0b c5dSelectGClic8() . "ead o3tion b0ttons and . call a33ro3riate 3roced0re 26 o3tDi66erent Then Select26Di66erent 'lse26 o3tSa5e Then Select26Sa5e 'lse26 o3t'53t Then Select26'53t 'lse26 o3t9ot'53t Then Select269ot'53t 'nd 26 c5dSelect.'nabled = >alse 'nd S0b

0,e Select26Di66erent ' ocedu e i% %,own in E6a#'le 19;10. /t ba%icall( %ea c,e% t, oug, t,e rn!Search ange* loo-ing 5o cell% w,o%e content% di55e 5 o# t,e ' eviou% cell. 1ince we do not -now w,et,e t,e ange i% a colu#n o ow ?o 'o tion t,e eo5@* it i% ea%ie to u%e a double >or loo'. Aoweve * it would be a bit #o e e55icient to %'lit t,e code into two ca%e% ?cColumns T 1 and c%ows T 1@. +ote t,at t,e 5i %t cell need% a bit o5 %'ecial attention* %ince we want to include it in t,e %election. 0,e %election i% accu#ulated in a Range object va iable called rn!(atch* u%ing t,e <nion 5unction. Aoweve * we alwa(% need to con%ide t,e 'o%%ibilit( t,at rn!(atch i%

286

cu entl( e7ual to 9othin4* in w,ic, ca%e t,e <nion 5unction will ?un5o tunatel(@ etu n 9othin4. /n ot,e wo d%:
A33lication.<nion(So5ethin4= 9othin4) = 9othin4

Exa&!le 18-19. he Select/4(i44erent Proce#ure


;rivate S0b Select26Di66erent() Di5 rn4Match As "an4e Di5 vCell+al0e As +ariant Di5 v;revio0sCell+al0e As +ariant Di5 cMatches As 2nte4er Di5 oCell As *bTect Di5 c"ows As 2nte4er= cCol05ns As 2nte4er Di5 r As 2nte4er= c As 2nte4er . :et row and col05n co0nt (one o6 which is 1) cCol05ns = rn4Search.Col05ns.Co0nt c"ows = rn4Search."ows.Co0nt . Start search cMatches = 7 Set rn4Match = 9othin4 >or r = 1 To c"ows >or c = 1 To cCol05ns Set oCell = rn4Search.Cells(r= c) vCell+al0e = oCell.+al0e vCell+al0e = CStr(vCell+al0e) 26 r = 1 And c = 1 Then . 2ncl0de 6irst cell 26 rn4Match 2s 9othin4 Then Set rn4Match = oCell 'lse Set rn4Match = A33lication.<nion(rn4Match= oCell) 'nd 26 cMatches = cMatches A 1 . Save val0e 6or next co53arison v;revio0sCell+al0e = vCell+al0e 'lse . Do co53arison with 3revio0s cell vCell+al0e = rn4Search.Cells(r= c).+al0e vCell+al0e = CStr(vCell+al0e) 26 vCell+al0e $( v;revio0sCell+al0e Then 26 rn4Match 2s 9othin4 Then Set rn4Match = oCell 'lse Set rn4Match = A33lication.<nion(rn4Match= oCell) 'nd 26 cMatches = cMatches A 1 'nd 26 . Save val0e 6or next co53arion v;revio0sCell+al0e = vCell+al0e 'nd 26 9ext . col05n

Tea2m89 77l'D

9ext

. row

. Select the ran4e 26 cMatches ( 7 Then rn4Match.Select c5d<ndo.'nabled = >alse 'lse Ms41ox "9o 5atchin4 cells. Selection will not be chan4ed."= vb2n6or5ation c5d<ndo.'nabled = >alse 'nd 26 'nd S0b

0,e Select26Sa5e ' ocedu e* w,ic, i% %,own in E6a#'le 19;11* i% ve ( %i#ila to t,e Select26Di66erent ' ocedu e. One %igni5icant di55e ence i% t,at we do not include t,e 5i %t cell. Exa&!le 18-11. he Select/4Sa&e Proce#ure
;rivate S0b Select26Sa5e() Di5 Di5 Di5 Di5 Di5 Di5 Di5 rn4Match As "an4e vCell+al0e As +ariant v;revio0sCell+al0e As +ariant cMatches As 2nte4er oCell As *bTect c"ows As 2nte4er= cCol05ns As 2nte4er r As 2nte4er= c As 2nte4er

. :et row and col05n co0nt (one o6 which is 1) cCol05ns = rn4Search.Col05ns.Co0nt c"ows = rn4Search."ows.Co0nt . Start search cMatches = 7 Set rn4Match = 9othin4 >or r = 1 To c"ows >or c = 1 To cCol05ns Set oCell = rn4Search.Cells(r= c) vCell+al0e = oCell.+al0e vCell+al0e = CStr(vCell+al0e) 26 r = 1 And c = 1 Then . Save 6irst val0e 6or next co53arion v;revio0sCell+al0e = vCell+al0e 'lse . Do co53arison with 3revio0s cell vCell+al0e = rn4Search.Cells(r= c).+al0e vCell+al0e = CStr(vCell+al0e) 26 vCell+al0e = v;revio0sCell+al0e Then 26 rn4Match 2s 9othin4 Then Set rn4Match = oCell 'lse Set rn4Match = A33lication.<nion(rn4Match= oCell) 'nd 26 cMatches = cMatches A 1 'nd 26 . Save val0e 6or next co53arion

v;revio0sCell+al0e = vCell+al0e 'nd 26 9ext . col05n 9ext . row . Select the ran4e 26 cMatches ( 7 Then rn4Match.Select c5d<ndo.'nabled = >alse 'lse Ms41ox "9o 5atchin4 cells. Selection will not be chan4ed."= vb2n6or5ation c5d<ndo.'nabled = >alse 'nd 26 'nd S0b

0,e Select26'53t and Select269ot'53t Select26'53t i% %,own in E6a#'le 19;12.

' ocedu e% a e al#o%t identical.

Exa&!le 18-12. he Select/4E&!ty Proce#ure


;rivate S0b Select26'53t () Di5 rn4Match As "an4e Di5 cMatches As 2nte4er Di5 oCell As *bTect Di5 c"ows As 2nte4er= cCol05ns As 2nte4er Di5 r As 2nte4er= c As 2nte4er . :et row and col05n co0nt (one o6 which is 1) cCol05ns = rn4Search.Col05ns.Co0nt c"ows = rn4Search."ows.Co0nt . Start search cMatches = 7 Set rn4Match = 9othin4 >or r = 1 To c"ows >or c = 1 To cCol05ns Set oCell = rn4Search.Cells(r= c) 26 2s'53t (oCell) Then 26 rn4Match 2s 9othin4 Then Set rn4Match = oCell 'lse Set rn4Match = A33lication.<nion(rn4Match= oCell) 'nd 26 cMatches = cMatches A 1 'nd 26 9ext . col05n 9ext . row . Select the ran4e 26 cMatches ( 7 Then rn4Match.Select c5d<ndo.'nabled = >alse 'lse Ms41ox "9o 5atchin4 cells. Selection will not be chan4ed."= vb2n6or5ation c5d<ndo.'nabled = >alse

'nd 26 'nd S0b

0o get t,e Select269ot'53t


26 2s'53t (oCell) Then

' ocedu e* ju%t c,ange t,e line:

to:
26 9ot 2s'53t (oCell) Then

$inall(* t,e Co53leteCol05ns and Co53lete"ows ' ocedu e% a e called 5 o# t,e co e%'onding co##and;button !lic- event% and a e ve ( %i#ila . Co53leteCol05ns i% %,own in E6a#'le 19;13. Exa&!le 18-1". he c&#+o&!lete+olu&nsG+lic1 Proce#ure
;rivate S0b c5dCo53leteCol05nsGClic8() . >or each selected cell= select the entire col05n Di5 oCell As *bTect Di5 rn49ew As "an4e Set rn49ew = 9othin4 >or 'ach oCell 2n Selection 26 rn49ew 2s 9othin4 Then Set rn49ew = oCell.'ntireCol05n 'lse Set rn49ew = <nion(rn49ew= oCell.'ntireCol05n) 'nd 26 9ext rn49ew.Select c5d<ndo.'nabled = Tr0e 'nd S0b

0o get Co53lete"ows* ju%t e'lace Enti e!olu#n b( Enti eRow in two 'lace%.

Chapter '>% Pi ot Tables


/n t,i% c,a'te * we ta-e a loo- at 'ivot table% and ,ow to c eate and 5o #at t,e# u%ing code.

'>%$ Pi ot Tables
4,ile we a e a%%u#ing t,at t,e eade i% 5a#ilia wit, t,e ba%ic% o5 E6cel* it ' obabl( would not ,u t to eview t,e conce't o5 a 'ivot table ?o &ivot0able@ 7uic-l(. Pi#ot!ables a e one o5 t,e #o%t 'owe 5ul 5eatu e% in E6cel. 0,e( a e de%igned to acco#'li%, t, ee #ain ta%-%:
C C C

/#'o t e6te nal data Agg egate dataH 5o e6a#'le* %u#* count* o ave age t,e data )i%'la( t,e data in inte e%ting wa(%

&ivot0able% can u%e data 5 o# e6te nal %ou ce%* a% well a% 5 o# one o #o e E6cel table%. $o in%tance* t,e data 5o a &ivot0able can co#e 5 o# an Acce%% databa%e. Aoweve * %etting u' E6cel to i#'o t e6te nal data e7ui e% t,at t,e a'' o' iate data %ou ce d ive % be in%talled on t,e u%e B% co#'ute . "o eove * t,e e a e %igni5icant li#itation% on E6celB% abilit( to i#'o t data t, oug, &ivot0able%. $o in%tance* all %t ing% a e li#ited to a lengt, o5 255 c,a acte %* w,ic, #a-e% u%ing 1O3 to de5ine a data %ou ce #uc, #o e di55icult. All in all* i#'o ting data u%ing a &ivot0able can be ' oble#atic. $u t,e #o e* we alwa(% ,ave t,e o'tion o5 i#'o ting t,e e7ui ed data di ectl( to an E6cel wo -%,eet ?u%ing a va iet( o5 #o e %o',i%ticated #et,od%* %uc, a% )AO and t,e .etRow% #et,od@ and t,en c eating t,e &ivot0able 5 o# t,e wo -%,eet. Acco dingl(* we will e%t ict ou di%cu%%ion to u%ing E6cel data a% t,e &ivot0able %ou ce. 0able 20;1* w,ic, e' e%ent% %ale% 5 o# a 5ictitiou% 5a%t 5ood co#'an( t,at ,a% bot, co#'an( and 5 anc,i%e %to e%* %,ow% t,e 5i %t ,al5 o5 t,e data t,at we will u%e to build ou 'ivot table. 0,e actual %ou ce table i% an E6cel wo -%,eet t,at contain% twice t,e nu#be o5 ow% a% 0able 20;1* t,e additional ow% being t,e analogou% data 5o t,e (ea 1997. ?0,u%* t,e 5i %t colu#n in t,e e#ainde o5 t,e table contain% t,e (ea 1997.@ able 29-1. Source (ata 4or Pivot able ;4or 1887< Perio& Store Co&e Store City Store Type Transa tions 1 >O;1 >O10O+ !o#'an( 3881 1 >O;2 >O10O+ !o#'an( 3789 1 >O;3 >O10O+ !o#'an( 3877 1 >O;4 >O10O+ !o#'an( 3862 1 >O;5 >O10O+ $ anc,i%e 4013 1 >O;6 >O10O+ $ anc,i%e 3620 2 >O;1 >O10O+ !o#'an( 3948 2 >O;2 >O10O+ !o#'an( 3878 2 >O;3 >O10O+ !o#'an( 3911 2 >O;4 >O10O+ !o#'an( 3926 2 >O;5 >O10O+ $ anc,i%e 3990

-ear 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998

Sales V6*248.00 V5*722.00 V6*278.00 V6*123.00 V6*861.00 V5*039.00 V6*468.00 V6*301.00 V6*390.00 V6*438.00 V6*767.00

1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998

2 3 3 3 3 3 3 4 4 4 4 4 4 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 1 1 1 1 1 1 2 2 2

>O;6 >O;1 >O;2 >O;3 >O;4 >O;5 >O;6 >O;1 >O;2 >O;3 >O;4 >O;5 >O;6 3A;1 3A;2 3A;3 3A;4 3A;5 3A;6 3A;1 3A;2 3A;3 3A;4 3A;5 3A;6 3A;1 3A;2 3A;3 3A;4 3A;5 3A;6 3A;1 3A;2 3A;3 3A;4 3A;5 3A;6 +L;1 +L;2 +L;3 +L;4 +L;5 +L;6 +L;1 +L;2 +L;3

>O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ >O10O+ 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 3O1 A+.E3E1 +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR

$ anc,i%e !o#'an( !o#'an( !o#'an( !o#'an( $ anc,i%e $ anc,i%e !o#'an( !o#'an( !o#'an( !o#'an( $ anc,i%e $ anc,i%e $ anc,i%e !o#'an( !o#'an( $ anc,i%e $ anc,i%e $ anc,i%e $ anc,i%e !o#'an( !o#'an( $ anc,i%e $ anc,i%e $ anc,i%e $ anc,i%e !o#'an( !o#'an( $ anc,i%e $ anc,i%e $ anc,i%e $ anc,i%e !o#'an( !o#'an( $ anc,i%e $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e

3615 3936 3857 3898 3949 3617 3624 3853 3891 3892 3966 3595 3611 8259 9140 9727 9494 10644 10649 9066 9789 9814 9917 10617 10190 9531 9698 9771 10232 10561 10924 9310 9496 9596 10050 10440 10778 6390 7016 7293 7037 7815 6935 6954 7531 7486

V5*091.00 V6*307.00 V6*153.00 V6*319.00 V6*453.00 V5*052.00 V5*111.00 V6*021.00 V6*333.00 V6*289.00 V6*571.00 V4*945.00 V5*051.00 V29*267.00 V31*947.00 V35*405.00 V33*830.00 V39*971.00 V40*077.00 V32*595.00 V35*217.00 V35*455.00 V35*926.00 V39*424.00 V38*387.00 V33*966.00 V34*419.00 V34*494.00 V37*315.00 V39*141.00 V41*938.00 V33*202.00 V33*910.00 V34*500.00 V37*274.00 V38*304.00 V40*965.00 V19*890.00 V22*229.00 V24*077.00 V22*704.00 V26*962.00 V22*925.00 V22*389.00 V25*324.00 V24*753.00

1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 1998

2 2 2 3 3 3 3 3 3 4 4 4 4 4 4

+L;4 +L;5 +L;6 +L;1 +L;2 +L;3 +L;4 +L;5 +L;6 +L;1 +L;2 +L;3 +L;4 +L;5 +L;6

+E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR +E4 LORR

!o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e !o#'an( $ anc,i%e $ anc,i%e

7285 7749 6881 7256 7330 7212 7480 6771 6954 7086 7275 7121 7562 6569 6973

V24*112.00 V26*325.00 V23*123.00 V23*330.00 V24*258.00 V23*386.00 V24*619.00 V22*189.00 V23*188.00 V22*703.00 V24*245.00 V23*025.00 V25*329.00 V20*845.00 V23*220.00

0,e &e iod colu#n in 0able 20;1 i% t,e ti#e 'e iod. $o %i#'licit(* we con%ide onl( 5ou ti#e 'e iod%. 0,e 1to e !ode colu#n give% t,e %to e code* u%ed to uni7uel( identi5( a %to e. 0,e 1to e !it( give% t,e cit( in w,ic, t,e %to e i% located. 0,e 1to e 0('e colu#n indicate% w,et,e t,e %to e i% owned b( t,e co#'an( o i% 5 anc,i%ed. 0,e 0 an%action% colu#n give% t,e nu#be o5 t an%action% 5o t,at ti#e 'e iod. 0,e 1ale% colu#n give% t,e total %ale% 5o t,at %to e du ing t,at 'e iod. +ote t,at t,e e i% one* and onl( one* ow 5o eac, ti#e 'e iodE%to e code. ?/n databa%e language* t,e ti#e 'e iodE%to e code 5o #% a )ey 5o t,e data.@ Ou goal i% to c eate a &ivot0able 5 o# t,e data in 0able 20;1. O5 cou %e* be5o e c eating a &ivot0able* we need to identi5( t,e t('e o5 agg egate data in w,ic, we a e inte e%ted. !lea l(* we want total %ale% and t an%action count%. 0,e 7ue%tion i%: 8Ove w,at g ou'ing%98 0,e be%t a'' oac, i% 5i %t to identi5( t,e most refined ?o smallest@ g ou'ing 5o t,e agg egate data. /n t,i% ca%e* it i% %to e t('eE%to e locationEti#e 'e iod. $o e6a#'le* we want t,e total %ale% 5o all co#'an( %to e% in +ew Lo - du ing 'e iod 1. /n addition* we will want agg egate% 5o la ge g ou'ing%<5o e6a#'le* total %ale% 5o all co#'an( %to e% in +ew Lo - ove all 'e iod% and total %ale% 5o +ew Lo -. $inall(* we want %e'a ate total% 5o t,e (ea % 1998 and 1997.

'>%' The Pi otTable -izard


3et u% 5i %t wal- t, oug, t,e &ivot0able wiGa d to c eate ou &ivot0able. 0,en we will c eate t,e %a#e &ivot0able u%ing code. 0,e 5i %t %te' i% to %elect t,e %ou ce data and %ta t t,e wiGa d b( %electing &ivot0able Re'o t unde t,e )ata #enu. 0,i% will ' oduce t,e 5i %t wiGa d dialog* a% %,own in $igu e 20;1. ?0,e%e 5igu e% a e 5o E6cel 97 and 2000. 0,e E6cel D& wiGa d ,a% a %o#ew,at di55e ent a''ea ance.@ Figure 29-1. Ste! 1 in the Pivot able wi?ar#

+ote t,at t,i% dialog allow% u% to %elect t,e data %ou ce 5o t,e &ivot0able data. !lic-ing t,e +e6t button ' oduce% t,e dialog in $igu e 20;2. Figure 29-2. Ste! 2 in the Pivot able wi?ar#

1ince we %elected t,e co ect %ou ce ange be5o e %ta ting t,e wiGa d* E6cel ,a% co ectl( identi5ied t,at ange in $igu e 20;2* %o we can %i#'l( ,it t,e +e6t button* w,ic, ' oduce% t,e dialog in $igu e 20;3. Figure 29-". Ste! " in the Pivot able wi?ar#

0,i% dialog i% w,e e we 5o #at t,e &ivot0able b( deciding w,ic, colu#n% o5 t,e o iginal %ou ce table beco#e 'age% in t,e &ivot0able* w,ic, beco#e ow%* w,ic, beco#e colu#n%* and w,ic, beco#e data ?5o agg egation@. 0,e ' ocedu e i% to d ag t,e button% on t,e ig,t to t,e ' o'e location< ow* colu#n* 'age* o data. ?4e want one 'age 5o eac, o5 t,e two (ea %.@ $o ou e6a#'le* we d ag t,e button% to t,e location% %,own in $igu e 20;4. +ote t,at t,e onl( button not u%ed i% 1to e !ode. 0,i% 5ield i% t,e aggregate 5ield* t,at i%* we will %u# ove all %to e code%. Figure 29-$. Ste! $ in the Pivot able wi?ar#

!lic-ing t,e +e6t button ta-e% u% to t,e dialog in $igu e 20;5* w,e e we c,oo%e t,e location 5o t,e &ivot0able. 4e c,oo%e a new wo -%,eet. Figure 29-'. Ste! ' in the Pivot able wi?ar#

!lic-ing t,e $ini%, button ' oduce% t,e &ivot0able in $igu e 20;6. Figure 29-5. he Pivot able

+ote t,at t,e 'age button i% labeled Lea . 1electing one o5 All* 1998* o 1997 5 o# t,e d o';down li%t bo6 ne6t to t,i% button will con5ine t,e data to t,at %election. 0,u%* t,e 'ivot table ,a% t, ee 'age%: 1997* 1998* and co#bined ?o All@. +ote al%o t,at t,e colu#n% a e labeled b( 'e iod% and t,e ow% a e labeled b( bot, 1to e !it( and 1to e 0('e* a% e7ue%ted. /n addition* E6cel ,a% c eated a new 5ield called )ata t,at i% u%ed a% ow label%. /n t,i% ca%e* E6cel co ectl( gue%%ed t,at we want %u#%* but i5 E6cel ,ad gue%%ed inco ectl( we could #a-e a c,ange #anuall(. /n %u##a (* we can %ee t,at t,e #ain co#'onent% o5 a 'ivot table a e t,e 'age%* ow%* colu#n%* and data 5ield%. Rat,e t,an 'u %ue 5u t,e develo'#ent o5 t,i% &ivot0able u%ing t,e E6cel inte 5ace* let u% now %witc, to u%ing code.

'>%* The Pi otTable-izard 7ethod


0o c eate a &ivot0able t, oug, code* we u%e t,e &ivot0able4iGa d #et,od o5 t,e 4o -%,eet object o t,e &ivot0able object. !ont a ( to w,at (ou #ig,t a%%u#e* t,e &ivot0able4iGa d #et,od doe% not %ta t t,e &ivot0able wiGa d. Rat,e * it i% u%ed to c eate a &ivot0able w,en a''lied to t,e 4o -%,eet object o to #odi5( an e6i%ting &ivot0able w,en a''lied to t,e &ivot0able object. 0,e %(nta6 i%:
ex ression.;ivotTableHiMard(SourceTy e= SourceData= TableDestination= G TableName= %ow:rand= Column:rand= Sa+eData= 6as-utoFormat= G -utoPa!e= %eser+ed= )ack!round8uery= / timi&eCache= G Pa!eField/rder= a!eFieldWra Count= %eadData= Connection)

w,e e ex ression etu n% eit,e a 4o -%,eet object o a &ivot0able object. A% (ou #ig,t e6'ect* t,e 'a a#ete % o5 t,e &ivot0able4iGa d #et,od co e%'ond to %etting% in t,e &ivot0able wiGa d. On t,e ot,e ,and* t,e &ivot0able4iGa d #et,od cannot do eve (t,ing t,at t,e &ivot0able

wiGa d can do. $o in%tance* it cannot be u%ed to %'eci5( t,e ow* colu#n* and data 5ield%. ?4e will %ee ,ow to do t,at a bit late .@ &ut anot,e wa(* t,e &ivot0able4iGa d #et,od %et% t,e ' o'e tie% o5 an empty &ivot0able. 3et u% go ove %o#e o5 t,e #o e i#'o tant 'a a#ete % to t,e &ivot0able4iGa d #et,od. 0,e o'tional SourceTy e 'a a#ete %'eci5ie% t,e %ou ce o5 t,e &ivot0able data and can be one o5 t,e 5ollowing #l;ivotTableSo0rceT 3e con%tant%:
'n05 #l;ivotTableSo0rceT 3e xl;ivotTable = %L1LD xlDatabase = 1 xl'xternal = 2 xlConsolidation = B 'nd 'n05

0,e%e di ectl( co e%'ond to t,e 5i %t dialog o5 t,e &ivot0able wiGa d* a% %,own in $igu e 20;1. /5 we %'eci5( a value 5o SourceTy e* t,en we #u%t al%o %'eci5( a value 5o SourceData. /5 we %'eci5( neit,e * E6cel u%e% t,e %ou ce t('e xlDatabase and t,e %ou ce data 5 o# a na#ed ange called Database. /5 t,i% na#ed ange doe% not e6i%t* E6cel u%e% t,e cu ent egion i5 t,e cu ent %election i% in a ange o5 #o e t,an 10 cell% t,at contain data. Ot,e wi%e* t,e #et,od will 5ail. All in all* t,i% ule i% %u55icientl( co#'licated to wa ant alwa(% %'eci5(ing t,e%e 'a a#ete %. 0,e SourceData 'a a#ete %'eci5ie% t,e data 5o t,e &ivot0able. /t can be a Range object* an a a( o5 ange%* o a te6t con%tant t,at e' e%ent% t,e na#e o5 anot,e &ivot0able. $o e6te nal data* t,i% #u%t be a two;ele#ent a a(* t,e 5i %t ele#ent o5 w,ic, i% t,e connection %t ing %'eci5(ing t,e O)>! %ou ce 5o t,e data* and t,e %econd ele#ent o5 w,ic, i% t,e 1O3 7ue ( %t ing u%ed to get t,e data. 0,e TableDestination 'a a#ete i% a Range object %'eci5(ing w,e e t,e &ivot0able %,ould be 'laced. /t can include a wo -%,eet 7uali5ie to %'eci5( t,e wo -%,eet u'on w,ic, to 'lace t,e 'ivot table a% well. 0,e TableName 'a a#ete i% a %t ing t,at %'eci5ie% t,e na#e o5 t,e new &ivot0able. 0,e %ow:rand 'a a#ete %,ould be %et to Tr0e to %,ow g and total% 5o ow% in t,e &ivot0able. 1i#ila l(* t,e Column:rand 'a a#ete %,ould be %et to Tr0e to %,ow g and total% 5o colu#n% in t,e &ivot0able. 0,e Sa+eData 'a a#ete %,ould be %et to Tr0e to %ave data wit, t,e &ivot0able. /5 it i% >alse* t,en onl( t,e &ivot0able de5inition i% %aved.
6as-utoFormat i% %et to Tr0e to ,ave E6cel auto#aticall( 5o #at t,e &ivot0able w,eneve it i%

e5 e%,ed o w,eneve an( 5ield% a e #oved. 0,e Pa!eField/rder and Pa!eFieldWra Count 'a a#ete % a e #eaning5ul onl( w,en t,e e i% #o e t,an one 'age 5ield* in w,ic, ca%e t,e%e 'a a#ete % %'eci5( w,e e t,e 'age 5ield button% and conco#itant d o';down li%t bo6e% a e 'laced elative to one anot,e . 0,e Pa!eField/rder 'a a#ete can be eit,e xlDownThen*ver ?t,e de5ault@ o xl*verThenDown. $o in%tance* i5 t,e e we e t, ee 'age 5ield%* t,en t,e %etting:
;a4e>ield*rder = xlDownThen*ver ;a4e>ieldHra3Co0nt = 2

Tea2m99 77l'D

would a ange t,e 'age 5ield% a% in $igu e 20;7. 0,i% 'ivot table i% onl( 5o illu%t ation o5 t,e 'age 5ield o de . /t wa% c eated 5 o# t,e o iginal 'ivot table b( #oving t,e ow 5ield% to 'age 5ield%. +ote al%o t,at %etting Pa!eField/rder to xl*verThenDown would %i#'l( eve %e t,e 'o%ition% o5 1to e !it( and 1to e 0('e. Figure 29-6. /llustrating !age 4iel# or#er

0,e 5ollowing code o%ten%ibl( c eate% t,e &ivot0able in $igu e 20;6 at t,e location o5 t,e active cell:
ActiveSheet.;ivotTableHiMard G So0rceT 3e-=xlDatabase= G So0rceData-=".So0rce.!"1C1-"1L&CF"= G Table9a5e-="SalesKTrans"

/n 5act* t,e e%ult% o5 e6ecuting t,i% code a e %,own in $igu e 20;8. 0,e ea%on not,ing #uc, %ee#% to ,ave ,a''ened i% t,at* a% we #entioned ea lie * t,e &ivot0able4iGa d #et,od doe% not allow u% to %'eci5( w,ic, 5ield% a e 'age* ow* colu#n* and data 5ield%. 0,e table in $igu e 20;8 i% an e#'t( &ivot0able. Figure 29-7. %n e&!ty Pivot able

'>%2 The Pi otTable Ob1ect


0o unde %tand bette w,at #u%t be done ne6t* we #u%t di%cu%% t,e &ivot0able object and it% va iou% c,ild collection object%. /nvo-ing t,e &ivot0able4iGa d #et,od ,a% c eated a &ivot0able object na#ed 1ale%K0 an% 5o u%. All &ivot0able object% ,ave a &ivot$ield% collection* acce%%ed t, oug, t,e &ivot$ield% ' o'e t(. 0,u%* t,e code:
Di5 36 As ;ivot>ield >or 'ach 36 2n G ActiveSheet.;ivotTables("SalesKTrans").;ivot>ields Deb04.;rint 36.9a5e 9ext

' oduce% t,e 5ollowing li%t o5 'ivot 5ield%:

298

@ear ;eriod Store Code Store Cit Store T 3e Transactions Sales

+ow* eac, &ivot$ield object can ,ave a de%ignation t,at %'eci5ie% w,et,e t,i% 5ield i% to be u%ed a% a ow 5ield* a colu#n 5ield* a 'age 5ield* o a data 5ield. 0,i% de%ignation i% e5e ed to a% it% orientation. /t tu n% out t,at t,e e i% #o e t,an one wa( to %et t,e o ientation o5 a 'ivot 5ield. One a'' oac, i% to %et t,e 'ivot 5ieldB% O ientation ' o'e t(* and anot,e a'' oac, i% to u%e t,e Add$ield% #et,od. 2n5o tunatel(* neit,e o5 t,e%e #et,od% i% %u55icientl( docu#ented* %o %o#e e6'e i#entation i% in o de . A% to t,e O ientation ' o'e t( a'' oac,* con%ide t,e code in E6a#'le 20;1* w,ic, %et% bot, t,e O ientation and &o%ition ' o'e tie%. 4e will di%cu%% t,e %ubtletie% o5 t,i% code a5te (ou ,ave loo-ed at it. Exa&!le 29-1. he +reatePivotFiel#s Proce#ure
S0b Create;ivot>ields() . Ass05e so0rce 6or 3ivot table . is in sheet na5ed .So0rce. ActiveSheet.;ivotTableHiMard G So0rceT 3e-=xlDatabase= G So0rceData-=".So0rce.!"1C1-"1L&CF"= G Table9a5e-=";ivotTable1" Hith ActiveSheet.;ivotTables("SalesKTrans") Deb04.;rint "1e6ore all-" Show>ields .;ivot>ields("@ear").*rientation = xl;a4e>ield .;ivot>ields("@ear").;osition = 1 .;ivot>ields("Store Cit ").*rientation = .;ivot>ields("Store Cit ").;osition = 1 .;ivot>ields("Store T 3e").*rientation = .;ivot>ields("Store T 3e").;osition = 2 .;ivot>ields(";eriod").*rientation = Deb04.;rint "1e6ore data 6ields-" Show>ields Hith .;ivot>ields("Transactions") .*rientation = xlData>ield .;osition = 1 'nd Hith Hith .;ivot>ields("Sales") .*rientation = xlData>ield .;osition = 2 'nd Hith xl"ow>ield xl"ow>ield

xlCol05n>ield

Deb04.;rint "" Deb04.;rint "A6ter data 6ields-" Show>ields .;ivot>ields("Data").*rientation = xl"ow>ield .;ivot>ields("Data").;osition = B 'nd Hith 'nd S0b

0,e Show>ields ' ocedu e u%ed in Create;ivot>ields i% %,own in E6a#'le 20;2H it %i#'l( ' int% ?to t,e /##ediate window@ a li%t o5 all 'ivot 5ield% and i% ve ( u%e5ul 5o e6'e i#enting o debugging. Exa&!le 29-2. he ShowFiel#s Proce#ure
S0b Show>ields() Di5 36 As ;ivot>ield Deb04.;rint "R;ivot>ields-" >or 'ach 36 2n G ActiveSheet.;ivotTables("SalesKTrans").;ivot>ields Deb04.;rint 36.9a5e 9ext Deb04.;rint "R"ow>ields-" >or 'ach 36 2n G ActiveSheet.;ivotTables("SalesKTrans")."ow>ields Deb04.;rint 36.9a5e 9ext Deb04.;rint "RCol>ields-" >or 'ach 36 2n G ActiveSheet.;ivotTables("SalesKTrans").Col05n>ields Deb04.;rint 36.9a5e 9ext Deb04.;rint "RData>ields-" >or 'ach 36 2n G ActiveSheet.;ivotTables("SalesKTrans").Data>ields Deb04.;rint 36.9a5e 9ext 'nd S0b

Running (reatePi#ot&ields e%ult% in t,e 5ollowing di%'la( to t,e /##ediate window:


1e6ore allR;ivot>ields@ear ;eriod Store Code Store Cit Store T 3e Transactions Sales

R"ow>ieldsRCol>ieldsRData>ields1e6ore data 6ieldsR;ivot>ields@ear ;eriod Store Code Store Cit Store T 3e Transactions Sales R"ow>ieldsStore Cit Store T 3e RCol>ields;eriod RData>ieldsA6ter data 6ieldsR;ivot>ields@ear ;eriod Store Code Store Cit Store T 3e Transactions Sales Data R"ow>ieldsStore Cit Store T 3e Data RCol>ields;eriod RData>ieldsS05 o6 Transactions S05 o6 Sales

0,e 5i %t t,ing we notice 5 o# t,i% li%t i% t,at t,e %'ecial 'ivot 5ield called )ata i% c eated b( E6cel only after t,e 0 an%action% and 1ale% 5ield% a e a%%igned t,e xlData>ield o ientation. 0,i% %tate#ent i% 5u t,e %u''o ted b( t,e 5act t,at i5 we #ove t,e la%t two line% o5 code:
.;ivot>ields("Data").*rientation = xl"ow>ield .;ivot>ields("Data").;osition = B

to ju%t be5o e t,e Hith bloc- elated to t,e 0 an%action% 5ield* E6cel will i%%ue an e o #e%%age w,en we t ( to un t,e code* %to''ing at t,e line:
.;ivot>ields("Data").*rientation = xl"ow>ield

becau%e it cannot %et t,e O ientation ' o'e t( o5 t,e none6i%tent )ata 5ield. +e6t* we ob%e ve t,at* wit, e%'ect to Row* !olu#n* and &age 5ield%* E6cel %i#'l( add% t,e 'ivot 5ield% to t,e a'' o' iate collection%. Aoweve * wit, e%'ect to )ata 5ield%* E6cel c eate% new 5ield object% called 1u# o5 0 an%action% and 1u# o5 1ale% t,at a e con%ide ed data 5ield% but not 'ivot; table 5ield%C

301

29.$.1 Na&ing (ata Fiel#s


4e %,ould #a-e a 5ew e#a -% about na#ing data 5ield%. /t i% i#'o tant to note t,at i5 t,e na#e o5 a data 5ield ,a% not been c,anged but we #a-e a c,ange to t,e agg egate 5unction* %a( 5 o# S05 to Avera4e* t,en E6cel will auto#aticall( ena#e t,e data 5ield* in t,i% ca%e 5 o# Sum o* Sales to -+era!e o* SalesA Aoweve * once we %et a new na#e 5o t,e data 5ield* E6cel will not ena#e it w,en we c,ange t,e agg egate 5unction. 4e can ena#e a data 5ield %i#'l( b( %etting it% +a#e ' o'e t(. Aoweve * even t,oug, )ata 5ield% do not %ee# to belong to t,e &ivot$ield% collection* we cannot u%e t,e na#e o5 a 'ivot 5ield 5o a data 5ield. $o in%tance* we cannot ena#e Sum o* Transactions to Transactions* %ince t,i% i% al ead( ta-en b( t,e 'ivot 5ield. ?0 (ing to do %o will ' oduce an e o .@ 0,u%* in de%igning t,e %ou ce table 5o t,e 'ivot table* we %,ould c,oo%e a colu#n ,eading t,at we do not want to u%e in t,e 'ivot tableC

29.$.2 he +o&!lete +o#e


$o e5e ence* let u% 'ut toget,e t,e code e7ui ed to c eate t,e 'ivot table in $igu e 20;6H it i% %,own in E6a#'le 20;3. Exa&!le 29-". he +reatePivot Proce#ure
S0b Create;ivot() . Create 3ivot table at active cell . Ass05es that the so0rce table is in sheet called So0rce ActiveSheet.;ivotTableHiMard G So0rceT 3e-=xlDatabase= G So0rceData-=".Co53an 1oth.!"1C1-"1L&CF"= G Table9a5e-="SalesKTrans" . Assi4n 6ield orientations and data 6ields Hith ActiveSheet.;ivotTables("SalesKTrans") .;ivot>ields("@ear").*rientation = xl;a4e>ield .;ivot>ields("@ear").;osition = 1 .;ivot>ields("Store Cit ").*rientation = xl"ow>ield .;ivot>ields("Store Cit ").;osition = 1 .;ivot>ields("Store T 3e").*rientation = xl"ow>ield .;ivot>ields("Store T 3e").;osition = 2 .;ivot>ields(";eriod").*rientation = xlCol05n>ield Hith .;ivot>ields("Transactions") .*rientation = xlData>ield .;osition = 1 'nd Hith Hith .;ivot>ields("Sales") .*rientation = xlData>ield .;osition = 2 'nd Hith .;ivot>ields("Data").*rientation = xl"ow>ield G G

.;ivot>ields("Data").;osition = B 'nd Hith 'nd S0b

Anot,e a'' oac, to a%%igning o ientation 5o t,e 'ivot 5ield% i% to u%e t,e Add$ield% #et,od o5 t,e &ivot0able object. 4e can u%e t,i% #et,od 5o all but data 5ield%. 0,e %(nta6 i%:

Pi+otTable/bject.Add>ields(%owFields= G ColumnFields= Pa!eFields= -ddToTable)


0,e o'tional %owFields 'a a#ete can %'eci5( eit,e a %ingle 'ivot;5ield na#e o an a a( o5 'ivot;5ield na#e% to be added a% ow%* and %i#ila l( 5o t,e ColumnFields and Pa!eFields 'a a#ete %. /t i% i#'o tant to note t,at an( invocation o5 t,e Add$ield% #et,od will replace all e6i%ting 5ield% o5 t,e given t('e ? ow* colu#n* o 'age@ wit, t,e 5ield% de%ignated b( t,e 'a a#ete % o5 t,e #et,od. 0o inc e#ent at,e t,an e'lace e6i%ting 5ield%* we #u%t %et t,e -ddToTable 'a a#ete to Tr0e. 0,e alte native to (reatePi#ot %,own in E6a#'le 20;4 u%e% t,e Add$ield% #et,od 5o ow* colu#n* and 'age 5ield%. +ote t,at t,i% i% %,o te t,an t,e ' eviou% ' ocedu e. ?/t i% al%o t,e a'' oac, ta-en b( E6cel it%el5 w,en we eco d a #ac o t,at c eate% t,i% 'ivot table.@ Exa&!le 29-$. +reating a Pivot able 2sing the %##Fiel#s -etho#
S0b Create;ivot2() . Create 3ivot table at active cell . Ass05es that the so0rce table is in sheet called So0rce ActiveSheet.;ivotTableHiMard G So0rceT 3e-=xlDatabase= G So0rceData-=".So0rce.!"1C1-"1L&CF"= G Table9a5e-="SalesKTrans2" ActiveSheet.;ivotTables("SalesKTrans2").Add>ields G "ow>ields-=Arra ("Store Cit "= "Store T 3e")= G Col05n>ields-=";eriod"= G ;a4e>ields-="@ear" Hith ActiveSheet.;ivotTables("SalesKTrans2") Hith .;ivot>ields("Transactions") .*rientation = xlData>ield .;osition = 1 'nd Hith Hith .;ivot>ields("Sales") .*rientation = xlData>ield .;osition = 2 'nd Hith 'nd Hith 'nd S0b

'>%3 Properties and 7ethods of the Pi otTable Ob1ect

0,e #e#be % o5 t,e &ivot0able object a e %,own in 0able 20;2. 4eBll di%cu%% t,e #o%t i#'o tant o5 t,e%e #e#be % b( t,ei 5unction. able 29-2. -e&bers o4 the Pivot able 3bject I)e5ault E o 1t ing & int0itle%Sv9Z I&ivot1electSv10Z $o #atSv9Z Re5 e%,)ate Add)ata$ieldSv10Z .et)ata Re5 e%,+a#e Add$ield% .et&ivot)ataSv10Z Re5 e%,0able A''lication . and0otal+a#eSv9Z Re'eat/te#%OnEac,& inted&ageSv9Z !ac,e/nde6 Aa%Auto$o #at Row$ield% !alculated$ield% Aidden$ield% Row. and !alculated"e#be %Sv10Z /nne )etail RowRange !olu#n$ield% 3i%t$o #ula% 1ave)ata !olu#n. and "anual2'date 1election"ode !olu#nRange ")DSv10Z 1,ow!ell>ac-g ound$ o#O3A&Sv10Z ! eate!ube$ileSv10Z "e ge3abel% 1,ow&age"ulti'le/te#3abelSv10Z ! eato +a#e 1,ow&age% !ube$ield%Sv9Z +ull1t ing 1#all. idSv9Z )ata>od(Range &age$ieldO de 1ou ce)ata )ata$ield% &age$ield% 1ubtotalAidden&age/te#% )ata3abelRange &age$ield1t(le 0ableRange1 )ata&ivot$ieldSv10Z &age$ield4 a'!ount 0ableRange2 )i%'la(E#'t(!olu#nSv10Z &ageRange 0able1t(le )i%'la(E#'t(RowSv10Z &ageRange!ell% 0ag )i%'la(E o 1t ing &a ent 0otal%AnnotationSv9Z )i%'la(/##ediate/te#%Sv10Z &ivot!ac,e 2'date )i%'la(+ull1t ing &ivot$ield% =acated1t(le )u##(15Sv10Z &ivot$o #ula% =alue Enable)ata=alueEditingSv10Z &ivot1elect =e %ionSv10Z Enable) illdown &ivot1election =iew!alculated"e#be %Sv10Z Enable$ield)ialog &ivot1election1tanda dSv10Z =i%ible$ield% Enable$ield3i%tSv10Z &ivot0able4iGa d =i%ual0otal%Sv10Z Enable4iGa d & e%e ve$o #atting

29.'.1 ,eturning a Fiel#s +ollection


1eve al o5 t,e #e#be % o5 t,e &ivot0able object a e de%igned to etu n a 5ield% collection. 29.'.1.1 +olu&nFiel#s !ro!erty 0,i% ' o'e t( etu n% t,e collection o5 all colu#n 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.Col05n>ields
Alte nativel(* we can etu n %elected colu#n 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.Col05n>ields(1ndex) 304

w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1.2 (ataFiel#s !ro!erty 0,i% ' o'e t( etu n% t,e collection o5 all data 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.Data>ields
Alte nativel(* we can etu n %elected data 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.Data>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1." Ci##enFiel#s !ro!erty A% we will %ee* a 'ivot 5ield can be ,idden b( %etting it% o ientation to xl?idden. 0,e Aidden$ield% ' o'e t( etu n% t,e collection o5 all ,idden 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.?idden>ields
Alte nativel(* we can etu n %elected ,idden 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.?idden>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1.$ PageFiel#s !ro!erty 0,e &age$ield% ' o'e t( etu n% t,e collection o5 all 'age 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.;a4e>ields
Alte nativel(* we can etu n %elected 'age 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.;a4e>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1.' PivotFiel#s !ro!erty 0,e &ivot$ield% ' o'e t( etu n% t,e collection o5 all 'ivot 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.;ivot>ields
Alte nativel(* we can etu n %elected 'ivot 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.;ivot>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1.5 ,owFiel#s !ro!erty

0,e Row$ield% ' o'e t( etu n% t,e collection o5 all ow 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject."ow>ields
Alte nativel(* we can etu n %elected ow 5ield% u%ing t,e %(nta6:

Pi+otTable/bject."ow>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%. 29.'.1.6 *isibleFiel#s !ro!erty 0,e =i%ible$ield% ' o'e t( etu n% t,e collection o5 all vi%ible 5ield%* u%ing t,e %(nta6:

Pi+otTable/bject.+isible>ields
Alte nativel(* we can etu n %elected vi%ible 5ield% u%ing t,e %(nta6:

Pi+otTable/bject.+isible>ields(1ndex)
w,e e 1ndex i% eit,e a %ingle inde6 ?t,e inde6 nu#be o5 t,e de%i ed 5ield@ o an a a( o5 inde6e%.

29.'.2 otals-,elate# -e&bers


0,e &ivot0able object ,a% two ' o'e tie% t,at a55ect t,e di%'la( o5 total%. (olumn-rand property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* t,e &ivot0able %,ow% g and colu#n total%. ,ow-rand property F,GW BooleanH 4,en t,i% ' o'e t( i% Tr0e* t,e &ivot0able %,ow% g and ow total%. 0o illu%t ate* e5e ing to t,e 'ivot table in $igu e 20;6* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G Col05n:rand = >alse ActiveSheet.;ivotTables("SalesKTrans"). G "ow:rand = >alse

' oduce% t,e 'ivot table in $igu e 20;9* wit, no g and total%. Figure 29-8. No gran# totals

4e can al%o %u'' e%% t,e di%'la( o5 individual 'ivot;5ield total%* %uc, a% t,e total% 5o 1to e !it( in $igu e 20;9. 0,i% i% a ' o'e t( o5 t,e 'a ticula &ivot$ield object* %o we will di%cu%% it w,en we di%cu%% t,i% object late in t,e c,a'te . A% a ' eview* ,oweve * t,e di%'la( o5 5ield total% i% gove ned b( t,e 1ubtotal% ' o'e t( o5 t,e &ivot$ield object. $o in%tance* t,e 5ollowing code tu n% o55 all 5ield total% in $igu e 20;9:
Di5 i As 2nte4er >or i = 1 To 12 ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit ").S0btotals(i) = >alse 9ext

?0,e e a e 12 t('e% o5 total%* and we #u%t tu n t,e# all o55.@ 0,i% ' oduce% t,e 'ivot table in $igu e 20;10. Figure 29-19. No totals at all

Tea3m09 77l'D

29.'." ,eturning a Portion o4 a Pivot able


1eve al o5 t,e #e#be % o5 t,e &ivot0able object a e de%igned to etu n a 'o tion o5 t,e 'ivot table a% a Range object. 0,e( a e a% 5ollow%. 29.'.".1 +olu&n,ange !ro!erty 0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e colu#n a ea in t,e 'ivot table. $igu e 20 11 illu%t ate% t,e colu#n ange. Figure 29-11. he +olu&n,ange range

29.'.".2 (ata.o#y,ange !ro!erty 0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e &ivot0ableB% data a ea. $igu e 20;12 %,ow% t,e e%ult% o5 %electing t,e )ata>od(Range. Figure 29-12. he (ata.o#y,ange range

29.'."." (ata)abel,ange !ro!erty

308

0,i% ead;onl( ' o'e t( etu n% a Range object t,at e' e%ent% t,e label% 5o t,e &ivot0able data 5ield%. $igu e 20;13 illu%t ate% )ata3abelRange. Figure 29-1". he (ata)abel,ange range

29.'.".$ Page,ange an# Page,ange+ells !ro!erties 0,e &ageRange ' o'e t( etu n% a Range object t,at e' e%ent% t,e &ivot0ableB% 'age a ea. 0,i% i% t,e %#alle%t ectangula egion containing all 'age 5ield; elated cell%. 0,e &ageRange!ell% ' o'e t( etu n% a Range object t,at e' e%ent% ju%t t,e cell% in t,e &ivot0able containing t,e 'age;5ield button% and ite# d o';down li%t%. $igu e 20;14 and $igu e 20;15 illu%t ate t,e di55e ence. Figure 29-1$. he Page,ange range

Figure 29-1'. he Page,ange+ells range

29.'.".' ,ow,ange !ro!erty

0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e &ivot0ableB% ow a ea. $igu e 20;16 illu%t ate% t,e ow a ea. Figure 29-15. he ,ow,ange range

29.'.".5 able,ange1 !ro!erty 0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e enti e &ivot0able e6ce't t,e 'age 5ield%. 0,i% i% illu%t ated in $igu e 20;17. Figure 29-16. he able,ange1 range

29.'.".6 able,ange2 !ro!erty 0,i% ' o'e t( etu n% a Range object t,at e' e%ent% t,e enti e &ivot0able* including t,e 'age 5ield%. 0,i% i% illu%t ated in $igu e 20;18. Figure 29-17. he able,ange2 range

29.'.$ PivotSelect an# PivotSelection


0,e &ivot1elect #et,od %elect% 'a t o5 a &ivot0able. 0,e %(nta6 i%:

Pi+otTable/bject.;ivotSelect(Name= (ode)
0,e (ode 'a a#ete %'eci5ie% t,e %election #ode and can be one o5 t,e 5ollowing #l;TSelectionMode con%tant%:
'n05 #l;TSelectionMode xlDataAndLabel = 7 xlLabel*nl = 1 xlData*nl = 2 xl*ri4in = B xl1lan8s = L xl10tton = 1& xl>irst"ow = 2&N 'nd 'n05

. 'xcel E onl

0,e Name 'a a#ete %'eci5ie% t,e %election in w,at "ic o%o5t e5e % to a% 8%tanda d &ivot0able %election 5o #at.8 2n5o tunatel(* t,e docu#entation doe% not tell u% w,at t,i% #ean%* %a(ing in%tead* 8A %t ing e6' e%%ion u%ed to %'eci5( 'a t o5 a &ivot0able. 0,e ea%ie%t wa( to unde %tand t,e e7ui ed %(nta6 i% to tu n on t,e #ac o eco de * %elect cell% in t,e &ivot0able* and t,en %tud( t,e e%ulting code.8 0,e e i% #o e on t,i%* and we e5e t,e eade to t,e E6cel =>A ,el' docu#entation ?%ta t b( loo-ing u' t,e &ivot1elect to'ic@. 1o let u% con%ide %o#e e6a#'le%* all o5 w,ic, a e ba%ed on t,e 'ivot table in $igu e 20;10. Aoweve * to illu%t ate t,e +a#e ' o'e t( and to %,o ten t,e 5igu e% a bit* we will ena#e t,e data 5ield 81u# o5 0 an%action%8 to 80 an%8 and 81u# o5 1ale%8 to 81ale8 u%ing t,e 5ollowing code:
S0b "ena5e() . To shorten the na5es o6 the data 6ields ActiveSheet.;ivotTables("SalesKTrans"). G Data>ields("S05 o6 Transactions").9a5e = "Trans" ActiveSheet.;ivotTables("SalesKTrans"). G Data>ields("S05 o6 Sales").9a5e = "Sale" 'nd S0b

0,i% al%o e#',a%iGe% a 'oint we #ade ea lie . +a#el(* we would li-e to ena#e t,e 81u# o5 1ale%8 5ield to 81ale%8 but t,e e i% a colu#n in t,e %ou ce table b( t,at na#e* %o E6cel will not let u% u%e t,e na#e 5o a data 5ield. 0,u%* we a e %tuc- wit, 81ale.8 +ow bac- to bu%ine%%. 0,e 5ollowing code %elect% t,e enti e 'ivot table:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ""= xlDataAndLabel

0,e 5ollowing code %elect% t,e 1to e 0('e label a ea ?'ivot;5ield label and 'ivot;ite# label%@:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ".Store T 3e.UAllV"= xlLabel*nl

0,e 5ollowing code %elect% all data and label% elated to t,e !o#'an( 'ivot ite#:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect "Co53an "= xlDataAndLabel

0,e 5ollowing code %elect% t,e cell% %,own in $igu e 20;19:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect "Co53an 1*ST*9"= xlDataAndLabel

Figure 29-18. Selecting the co&!any label an# #ata 4or .oston

On t,e ot,e ,and* b( eve %ing t,e wo d% !o#'an( and >O10O+:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect "1*ST*9 Co53an "= xlDataAndLabel

we get t,e %election in $igu e 20;20* w,ic, doe% not include t,e !o#'an( labelC Figure 29-29. ,eversing the wor# or#er to select co&!any #ata 4or .oston only

0,e 5ollowing code %elect% cell E12 o5 t,e 'ivot table in $igu e 20;10:
ActiveSheet.;ivotTables("SalesKTrans").;ivotSelect G ".L*S A9:'L'S. >ranchise .Sale. .2."= xlData*nl

0,e 5ollowing code %elect% t,e label% and data 5o >o%ton and +ew Lo -:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ".Store Cit .U1*ST*9=.9'H @*"P.V"= xlDataAndLabel

/5 we e'lace t,e co##a wit, a colon:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ".Store Cit .U1*ST*9-.9'H @*"P.V"= xlDataAndLabel

t,en all ite#% 5 o# >o%ton to +ew Lo - ?t,at i%* all ite#%@ a e %elected. 0,e &ivot1election ' o'e t( etu n% o %et% t,e &ivot0able %election* again in %tanda d &ivot0able %election 5o #at. 1etting t,i% ' o'e t( i% e7uivalent to calling t,e &ivot1elect #et,od wit, t, e (ode a gu#ent %et to xlDataAndLabel.

29.'.' %##itional -e&bers o4 the Pivot able 3bject


3et u% ta-e a loo- at %o#e additional #e#be % o5 t,e &ivot0able object. 29.'.'.1 %##Fiel#s &etho# 4e ,ave %een t,i% #et,od in action ea lie in t,i% c,a'te . 29.'.'.2 +alculate#Fiel#s &etho# /t i% 'o%%ible to add calculated fields to a 'ivot table. 0,e%e a e 5ield% t,at a e not 'a t o5 t,e o iginal %ou ce data* but in%tead a e calculated 5 o# %ou ce 5ield% u%ing a 5o #ula. 0,e !alculated$ield% #et,od etu n% t,e !alculated$ield% collection o5 all calculated 5ield%. 0o add a new calculated 5ield* we u%e t,e Add #et,od o5 t,e !alculated$ield% collection. 0,e %(nta6 i%:

CalculatedFields/bject.Add(Name= Formula)

w,e e Name i% t,e na#e o5 t,e 5ield and Formula i% t,e 5o #ula 5o t,e 5ield. 0o illu%t ate* t,e 5ollowing code c eate% a calculated 5ield and di%'la(% it in t,e 'ivot table 5 o# $igu e 20;10. 0,e e%ult% a e %,own in $igu e 20;21.
Hith ActiveSheet.;ivotTables("SalesKTrans"). G Calc0lated>ields.Add("Avera4e"= G "= SalesJTransactions") .*rientation = xlData>ield .9a5e = "Av4 Chec8" .905ber>or5at = "OO.O" 'nd Hith

Figure 29-21. /llustrating a calculate# 4iel#

4e %,ould #a-e a b ie5 e#a - about t,e a it,#etic o5 t,e calculated 5ield. 0,e calculated 5ield i% co#'uted di ectl( 5 o# t,e data in t,e 'ivot table. 0,e %ou ce data i% not involved directly. 0,i% i% w,( we did not %'eci5( an agg egate 5unction 5o t,e calculated 5ield. ?1uc, a 5unction would ,ave been igno ed.@ 0,u%* 5o in%tance* t,e value in cell )7 i% obtained b( dividing t,e value in cell )6 b( t,e value in cell )5. $inall(* we note t,at t,e 3i%t$o #ula% #et,od can be u%ed to c eate a li%t o5 all calculated 5ield% on a %e'a ate wo -%,eet. 29.'.'." Errors-relate# !ro!erties 4,en t,e )i%'la(E o 1t ing ' o'e t( i% Tr0e* t,e &ivot0able di%'la(% a cu%to# e o %t ing in cell% t,at contain e o %. ?0,e de5ault value i% >alse.@ A% t,e E6cel ,el' 5ile ob%e ve%* t,i% ' o'e t( i% 'a ticula l( u%e5ul 5o %u'' e%%ing divide;b(;Ge o e o %.

0,e E o 1t ing ' o'e t( can be u%ed to %et t,e %t ing t,at i% di%'la(ed in cell% t,at contain e o % w,en )i%'la(E o 1t ing i% Tr0e. ?0,e de5ault value i% an e#'t( %t ing.@ 29.'.'.$ Null-relate# !ro!erties 4,en t,e )i%'la(+ull1t ing ' o'e t( i% Tr0e* t,e &ivot0able di%'la(% a cu%to# %t ing in cell% t,at contain null value%. ?0,e de5ault value i% Tr0e.@ 0,e +ull1t ing ' o'e t( can be u%ed to %et t,e cu%to# %t ing 5o %uc, cell%. ?0,e de5ault value i% an e#'t( %t ing.@ 29.'.'.' Enable(rill(own !ro!erty One o5 t,e 5eatu e% o5 a &ivot0able i% t,e "rill"own 5eatu e. 0o illu%t ate* i5 we double;clic- on a data cell* %uc, a% cell )5 in t,e 'ivot table o5 $igu e 20;10* E6cel will c eate a new wo -%,eet* a% %,own in $igu e 20;22. 0,i% wo -%,eet %,ow% t,e o iginal %ou ce ow% t,at cont ibute to t,e value in t,e double;clic-ed cell )5. ?+ote t,at t,e ow% a e co#'lete* alt,oug, t,e 1ale% colu#n doe% not cont ibute to cell )5.@ Figure 29-22. /llustrating (rill(own

>( now (ou ,ave ' obabl( gue%%ed t,at t,e ead;w ite >oolean ' o'e t( Enable) ill)own i% u%ed to enable o to di%able t,i% 5eatu e 5o t,e 'ivot table. 29.'.'.5 For&atting !ro!erties an# &etho#s 0,e ead;w ite Aa%Auto$o #at ' o'e t( i% Tr0e ?it% de5ault value@ i5 t,e &ivot0able i% auto#aticall( 5o #atted w,en it i% e5 e%,ed o w,en %o#e o5 it% 5ield% a e #oved. 0,e label% 5o t,e ow%* colu#n%* %ubtotal%* and total% in $igu e 20;6 a e not #e ged wit, adjacent blan- cell%. ?0,e bo de % a e adju%ted %o it #a( a''ea %o* ,oweve .@ 0o #e ge t,e label% wit, adjacent blan- cell%* we can %et t,e "e ge3abel% ' o'e t( to Tr0e. 0,e ead;w ite & e%e ve$o #atting ' o'e t( i% Tr0e ?it% de5ault value@ i5 &ivot0able 5o #atting i% ' e%e ved w,en t,e &ivot0able i% e5 e%,ed o ecalculated b( o'e ation% %uc, a% 'ivoting* %o ting* o c,anging 'age;5ield ite#%. 0,e 0able1t(le ' o'e t( etu n% o %et% t,e %t(le na#e ?a% a %t ing@ u%ed in t,e &ivot0able bod(. 0,e de5ault value i% a null %t ing* %o no %t(le i% a''lied. 29.'.'.6 ,e4reshing a !ivot table

4,en t,e "anual2'date ' o'e t( i% Tr0e* t,e &ivot0able i% ecalculated onl( at t,e u%e B% e7ue%t. 0,e de5ault value i% >alse. 0,e Re5 e%,0able #et,od e5 e%,e% t,e &ivot0able 5 o# t,e o iginal %ou ce data. 0,e #et,od etu n% Tr0e i5 it i% %ucce%%5ul. 0,e Re5 e%,)ate ' o'e t( etu n% t,e date on w,ic, t,e &ivot0able o 'ivot cac,e wa% la%t e5 e%,ed. /t i% ead;onl(. 0,e Re5 e%,+a#e ' o'e t( etu n% t,e u%e na#e o5 t,e 'e %on w,o la%t e5 e%,ed t,e &ivot0able data o 'ivot cac,e. 29.'.'.7 PageFiel#-relate# !ro!erties A% di%cu%%ed ea lie in t,e c,a'te * t,e &age$ieldO de ' o'e t( etu n% o %et% t,e o de in w,ic, 'age 5ield% a e added to t,e &ivot0able la(out. /t can be one o5 t,e 5ollowing #l*rder con%tant%: xlDownThen*ver o xl*verThenDown. 0,e de5ault con%tant i% xlDownThen*ver. Recall al%o t,at t,e &age$ield4 a'!ount ' o'e t( etu n% o %et% t,e nu#be o5 &ivot0able 'age 5ield% in eac, colu#n o ow. 0,e &age$ield1t(le ' o'e t( etu n% o %et% t,e %t(le u%ed in t,e 'age 5ield a ea. 29.'.'.8 Na&e !ro!erty 0,i% ' o'e t( etu n% o %et% t,e na#e o5 t,e 'ivot table a% a %t ing. 29.'.'.19 Save(ata !ro!erty ;,H0 .oolean< 4,en t,i% ' o'e t( i% Tr0e* it% de5ault value* data 5o t,e &ivot0able i% %aved wit, t,e wo -boo-. /5 it i% >alse* onl( t,e &ivot0able de5inition i% %aved. 29.'.'.11 ShowPages &etho# 0,i% #et,od c eate% a new &ivot0able 5o eac, ite# in t,e %'eci5ied 'age 5ield. Eac, new &ivot0able i% c eated on a new wo -%,eet. 0,e %(nta6 i%:

Pi+otTable/bject.Show;a4es(Pa!eField)
$o in%tance* i5 we a''l( t,i% #et,od to t,e 'ivot table in $igu e 20;10 wit, t,e code:
ActiveSheet.;ivotTables("SalesKTrans").Show;a4es "@ear"

we will get two new wo -%,eet%. One wo -%,eet* na#ed 1997* will contain t,e o iginal 'ivot table* but wit, t,e Lea 'age 5ield %et to 1997. 0,e ot,e wo -%,eet will contain t,e %a#e 'ivot table wit, t,e Lea 5ield %et to 1998. ?4e can %till c,ange t,e Lea 5ield on an( o5 t,e 'ivot table%. /n ot,e wo d%* eac, 'ivot table contain% t,e data 5o all o5 t,e Lea 5ield value%.@ 29.'.'.12 Source(ata !ro!erty 0,i% ead;onl( ' o'e t( etu n% t,e data %ou ce 5o t,e &ivot0able. $o in%tance* w,en t,at %ou ce i% a %ingle E6cel wo -%,eet* t,e 1ou ce)ata #et,od etu n% a %t ing de%c ibing t,e %ou ce ange. 0,e code:

316

deb04.3rint ActiveSheet.;ivotTables("SalesKTrans").So0rceData

etu n% t,e %t ing:


.Co53an 1oth.!"1C1-"1L&CF

'>%8 Children of the Pi otTable Ob1ect


0,e c,ild en o5 t,e &ivot0able object a e %,own in $igu e 20;23. Figure 29-2". +hil#ren o4 the Pivot able object

'>%9 The Pi ot:ield Ob1ect


0,e ' o'e tie% and #et,od% o5 t,e &ivot$ield object a e %,own in 0able 20;3. able 29-". -e&bers o4 the PivotFiel# 3bject !u ent&age+a#eSv9Z 3a(out&age> ea-Sv9Z

I)e5ault

Tea3m195Fly

Add&age/te#Sv10Z A''lication Auto1,ow Auto1,ow!ount Auto1,ow$ield Auto1,owRange Auto1,ow0('e Auto1o t Auto1o t$ield Auto1o tO de >a%e$ield >a%e/te# !alculated/te#% !alculation !a'tionSv9Z !,ild$ield !,ild/te#% ! eato !ube$ieldSv9Z !u ent&age !u ent&age3i%tSv10Z

)ataba%e1o tSv10Z )ataRange )ata0('e )elete ) ag0o!olu#n ) ag0o)ataSv9Z ) ag0oAide ) ag0o&age ) ag0oRow ) illed)ownSv9Z Enable/te#1electionSv10Z $o #ula $unction . ou'3evel Aidden/te#% Aidden/te#%3i%tSv10Z /%!alculated /%"e#be & o'e t(Sv10Z 3abelRange 3a(out>lan-3ineSv9Z 3a(out$o #Sv9Z

3a(out1ubtotal3ocationSv9Z "e#o (2%ed +a#e +u#be $o #at O ientation &a ent &a ent$ield &a ent/te#% &ivot/te#% &o%ition & o'e t(O de Sv10Z & o'e t(&a ent$ieldSv10Z 1e ve >a%ed 1,owAll/te#% 1ou ce+a#e 1tanda d$o #ulaSv10Z 1ubtotal+a#eSv9Z 1ubtotal% 0otal3evel% =alue =i%ible/te#%

3et u% ta-e a loo- at %o#e o5 t,e%e #e#be %.

29.6.1 %utoShow-,elate# -e&bers


0,e Auto1,ow #et,od i% u%ed to e%t ict t,e di%'la( o5 'ivot ite#% 5o a given 'ivot 5ield. 0,e %(nta6 i%:
;ivot>ield*bTect.A0toShow(T 3e= "an4e= Co0nt= >ield)

All 'a a#ete % a e e7ui ed 5o t,i% #et,od. 0,e Ty e 'a a#ete ,a% two 'o%%ible value%: xlA0to5atic activate% t,e e#aining 'a a#ete % and* t,e eb(* cau%e% t,e e%t iction% to ta-e e55ect* and xlMan0al di%able% t,e e#aining 'a a#ete % and cau%e% E6cel to e#ove an( e%t iction% cau%ed b( a ' eviou% call to t,i% #et,od wit, Ty e e7ual to xlA0to5atic. 0,e ot,e 'a a#ete % can be de%c ibed b( t,e 5ollowing %entence: e%t ict 'ivot ite#% to t,e to' ?%an!e TxlTo3@ o botto# ?%an!e Txl1otto5@ Count 'ivot ite#% ba%ed on t,e value in 'ivot 5ield Field. 0,u%* 5o in%tance* e5e ing to $igu e 20;10* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e").A0toShow G xlA0to5atic= xlTo3= 1= "Sale"

%,ow% t,e to' ?%an!e TxlTo3 and CountT1@ 1to e 0('e ba%ed on t,e value o5 1ale. 0,e e%ult i% %,own in $igu e 20;24.

Figure 29-2$. /llustrating %utoShow

A% (ou can %ee* t,e to' %ale% in >o%ton a e 5 o# t,e co#'an( %to e%* w,e ea% t,e to' %ale% in t,e ot,e citie% a e in 5 anc,i%e %to e%. 0,e %a#e code a% t,e ' eviou% but wit, Ty e %et to xlMan0al will e#ove t,e e%t iction% and e%to e t,e o iginal 'ivot table:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e").A0toShow G xlMan0al= xlTo3= 1= "Sale"

0,e 5ollowing ' o'e tie% a e a%%ociated wit, Auto1,ow: Auto/how(ount property 0,e ead;onl( Auto1,ow!ount ' o'e t( etu n% t,e nu#be o5 ite#% t,at a e auto#aticall( %,own in t,e 'ivot 5ield ?t,i% i% t,e Count 'a a#ete o5 t,e Auto1,ow #et,od@. Auto/how&ield property 0,i% ead;onl( ' o'e t( etu n% t,e na#e o5 t,e data 5ield u%ed to dete #ine w,ic, ite#% a e %,own ?t,i% i% t,e Field 'a a#ete o5 t,e Auto/how #et,od@. Auto/how,ange property 0,i% ead;onl( ' o'e t( etu n% xlTo3 o xl1otto5. 0,i% i% t,e value o5 t,e %an!e 'a a#ete o5 t,e Auto1,ow #et,od. Auto/how!ype property 0,i% ead;onl( ' o'e t( etu n% xlA0to5atic i5 Auto1,ow i% enabled 5o t,e 'ivot 5ield and xlMan0al i5 Auto1,ow i% di%abled.

29.6.2 Sorting Pivot Fiel#s


0,e Auto1o t #et,od %et% t,e automatic 5ield;%o ting ule% 5o t,e 'ivot 5ield. 0,e %(nta6 i%:

Pi+otField/bject.A0toSort(/rder= Field) 319

0,e /rder 'a a#ete %'eci5ie% t,e %o t o de and i% one o5 t,e 5ollowing con%tant%:
'n05 #lSort*rder xlAscendin4 = 1 xlDescendin4 = 2 'nd 'n05

/t can al%o be %et to xlMan0al to di%able auto#atic %o ting. 0,e Field 'a a#ete i% t,e na#e o5 t,e 5ield to u%e a% t,e %o t -e(. $o in%tance* e5e ing to $igu e 20;10* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e").A0toSort G xlAscendin4= "Sale"

%o t% b( 1ale and ' oduce% t,e e%ult% %,own in $igu e 20;25. +ote t,e o de o5 t,e 1to e 0('e ite#% 5o >o%ton a% co#'a ed to t,e ot,e citie%. Figure 29-2'. /llustrating %utoSort

0,e ead;onl( Auto1o t$ield ' o'e t( etu n% t,e na#e o5 t,e -e( 5ield and t,e Auto1o tO de ' o'e t( etu n% t,e %o t o de o5 t,e 'ivot 5ield ?xlAscendin4* xlDescendin4* o xlMan0al@.

29.6." he Fun#a&ental Pro!erties


0,e &ivot$ield object ,a% a ,and5ul o5 ba%ic ' o'e tie% t,at (ou will al#o%t alwa(% want to %et. 29.6.".1 Function !ro!erty 0,i% ' o'e t( a''lie% onl( to data 5ield% and etu n% o %et% t,e agg egate 5unction u%ed to %u##a iGe t,e 'ivot 5ield. /t can be one o5 t,e 5ollowing #lConsolidation>0nction con%tant%:
'n05 #lConsolidation>0nction

xl+ar; = %L1N& xl+ar = %L1NL xlS05 = %L1&F xlStDev; = %L1&N xlStDev = %L1&& xl;rod0ct = %L1LE xlMin = %L1BE xlMax = %L1BN xlCo0nt905s = %L11B xlCo0nt = %L112 xlAvera4e = %L17N xl<n8nown = 1777 'nd 'n05

. 'xcel E onl

29.6.".2 Nu&berFor&at !ro!erty 0,i% ' o'e t( a''lie% onl( to data 5ield% and etu n% o %et% t,e 5o #atting %t ing 5o t,e object. +ote t,at it will etu n 90ll i5 all cell% in t,e %'eci5ied ange do not ,ave t,e %a#e nu#be 5o #at. 0,i% i% a ead;w ite %t ing ' o'e t(. 29.6."." 3rientation !ro!erty 0,i% ' o'e t( etu n% o %et% t,e o ientation o5 t,e 'ivot 5ield. /t can be %et to one o5 t,e 5ollowing value%:
'n05 #l;ivot>ield*rientation xl?idden = 7 xl"ow>ield = 1 xlCol05n>ield = 2 xl;a4e>ield = B xlData>ield = L 'nd 'n05

29.6.".$ Position !ro!erty 0,i% ead;w ite ' o'e t( etu n% o %et% t,e 'o%ition o5 t,e 'ivot 5ield a#ong all 'ivot 5ield% in t,e %a#e a ea ? ow* colu#n* 'age* o data@.

29.6.$ Selecting ,anges


0,e &ivot$ield object ,a% two ' o'e tie% elated to %electing 'o tion% o5 t,e 'ivot table elated to t,e 5ield. 29.6.$.1 (ata,ange !ro!erty 0,i% ' o'e t( etu n% a Range object e' e%enting t,e value a ea a%%ociated wit, t,e given &ivot$ield. 0o illu%t ate* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G Data"an4e.Select

e%ult% in $igu e 20;26. Figure 29-25. (ata,ange 4or Store y!e

321

29.6.$.2 )abel,ange !ro!erty 0,e 3abelRange ' o'e t( etu n% a Range object t,at e' e%ent% t,e label cell% 5o t,e &ivot$ield. 0o illu%t ate* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G Label"an4e.Select

will %elect ju%t t,e cell containing t,e button labeled 1to e 0('e in $igu e 20;26.

29.6.' (ragging Pivot Fiel#s


0,e &ivot$ield object ,a% %o#e ' o'e tie% t,at can ' event t,e u%e 5 o# #oving t,e 5ield. 0,e( a e a% 5ollow% ?all de5ault value% a e Tr0e@: "rag!o(olumn property 1et to >alse to ' event t,e 5ield 5 o# being d agged to t,e colu#n a ea. "rag!oJide property 1et to >alse to ' event t,e 5ield 5 o# being ,idden b( being d agged o55 o5 t,e 'ivot table. "rag!oPage property 1et to >alse to ' event t,e 5ield 5 o# being d agged to t,e 'age 5ield a ea. "rag!o,ow property 1et to >alse to ' event t,e 5ield 5 o# being d agged to t,e ow 5ield a ea.

29.6.5 Na&eI *alueI an# SourceNa&e Pro!erties


322

0,e ead;w ite +a#e ' o'e t( etu n% o %et% t,e na#e o5 t,e &ivot$ield. 0,i% i% t,e value t,at a''ea % in t,e label 5o t,at 5ield. 0,e =alue ' o'e t( i% t,e %a#e a% t,e +a#e ' o'e t(. 0,e ead;onl( 1ou ce+a#e ' o'e t( etu n% t,e na#e o5 t,e 5ield in t,e o iginal %ou ce data. 0,i% #a( di55e 5 o# t,e value o5 t,e +a#e ' o'e t( i5 t,e +a#e ' o'e t( ,a% been c,anged.

29.6.6 :rou!ing
E6cel al%o let% u% g ou' ?and ung ou'@ t,e data 5o a %election o5 'ivot ite#% into a %ingle new 'ivot ite#. 0,i% i% done u%ing t,e . ou' and 2ng ou' #et,od% o5 t,e Range object. +ote t,at t,e%e #et,od% a''l( to t,e Range object* not t,e &ivot$ield o &ivot/te# object%. 0,e . ou' #et,od ,a% two %(nta6e%* but we will loo- at onl( t,e #o e 5le6ible o5 t,e two. ?$o all o5 t,e detail% on t,e . ou' #et,od* we e5e t,e inte e%ted eade to t,e E6cel ,el' docu#entation.@ 3et u% loo- at an e6a#'le. Re5e ing a% u%ual to t,e 'ivot table in $igu e 20;10* t,e 5ollowing code %elect% all label% and data 5o >o%ton and +ew Lo - and t,en g ou'% t,i% data into a %ingle g ou'. 0,e g ou' i% t,en ena#ed Ea%te n. 0,e e%ult% a e %,own in $igu e 20;27. Ob%e ve t,at E6cel c eate% bot, a new 'ivot 5ield and a new 'ivot ite#. 0,e 'ivot 5ield i% called 1to e !it(2 and contain% t,e e6i%ting 3o% Angele% 'ivot ite# along wit, a new 'ivot ite#* w,ic, would ,ave been given t,e na#e . ou'1 b( E6cel i5 we ,ad not %'eci5ied t,e na#e Ea%te n.
Hith ActiveSheet.;ivotTables("SalesKTrans") .;ivotSelect ".Store Cit .U1*ST*9=.9ew @or8.V"= xlDataAndLabel Set rn4 = Selection rn4.:ro03 .;ivot>ields("Store Cit 2").;ivot2te5s(1). 9a5e = "'astern" .;ivotSelect "'astern"= xlDataAndLabel 'nd Hith

Figure 29-26. /llustrating the :rou! &etho#

29.6.7 (ata Fiel# +alculation


+o #all(* data 5ield% %,ow a value ba%ed on t,e $unction ' o'e t( o5 t,e 5ield. On t,e ot,e ,and* E6cel al%o 'e #it% u% to c,ange t,e meaning o5 t,e value t,at i% di%'la(ed in a data 5ield.

29.6.7.1 +alculation !ro!erty 0,i% i% done b( %etting t,e !alculation ' o'e t( 5o t,e data 5ield. ?0,e ' o'e t( a''lie% onl( to data 5ield%.@ 0,e 'o%%ible value% o5 t,i% ' o'e t( a e given b( t,e 5ollowing enu#:
'n05 #l;ivot>ieldCalc0lation xl9oAdditionalCalc0lation = %L1LB xlDi66erence>ro5 = 2 xl;ercent*6 = B xl;ercentDi66erence>ro5 = L xl"0nnin4Total = & xl;ercent*6"ow = N xl;ercent*6Col05n = F xl;ercent*6Total = D xl2ndex = E 'nd 'n05

A% (ou can %ee 5 o# t,e%e con%tant%* t,e idea i% to e'lace t,e aw value in t,e data 5ield b( a t('e o5 elative value. ?4e will %ee an e6a#'le in a #o#ent.@ +ote t,at 5o %o#e value% o5 !alculation* additional in5o #ation i% e7ui ed. /n 'a ticula * i5 !alculation i% e7ual to xlDi66erence>ro5* xl;ercentDi66erence>ro5 * o xl;ercent*6* t,en we #u%t %'eci5( t,e 5ollowing two ' o'e tie%: Base&ield property 0,i% ' o'e t(* w,ic, a''lie% onl( to data 5ield%* etu n% o %et% t,e 5ield u'on w,ic, t,e data 5ield calculation i% ba%ed. Base tem property 0,i% ' o'e t(* w,ic, a''lie% onl( to data 5ield%* etu n% o %et% t,e ite# in t,e ba%e data 5ield u%ed 5o t,e calculation. 29.6.7.2 +alculations not requiring a .aseFiel#H.ase/te& 0,e !alculation value% t,at do not e7ui e a >a%e$ieldE>a%e/te# 'ai a e: xl"0nnin4Total Ree'% a unning total o5 all value%* going down t,e ow% o5 t,e table. xl;ercent*6"ow Eac, cell i% e'laced b( t,at cellB% 'e centage o5 t,e %u# o5 t,e value% in t,at cellB% ow ?5o t,e given data 5ield@. xl;ercent*6Col05n Eac, cell i% e'laced b( t,at cellB% 'e centage o5 t,e %u# o5 t,e value% in t,at cellB% colu#n ?5o t,e given data 5ield@. xl;ercent*6Total

Eac, cell i% e'laced b( t,at cellB% 'e centage o5 t,e %u# o5 t,e value% in t,e enti e table ?5o t,e given data 5ield@. 0,e 5o #ula to co#'ute t,e value in a cell i%:
new val0e = (c0rrent val0e R 4rand total) J (row total R col05n total)

$igu e% 20;28 t, oug, 20;31 illu%t ate t,e%e calculation%* %ta ting wit, t,e 'ivot table in $igu e 20;10. +ote t,at t,e 'e cent calculation% e7ui e t,at t,e g and ow and colu#n total% be di%'la(ed. 0,e code 5o $igu e 20;28 i%:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale").Calc0lation = xl"0nnin4Total

Re5e ing to $igu e 20;28* cell )6 contain% %ale% 5o >o%ton* cell )10 contain% total %ale% 5o >o%ton and 3o% Angele%* and cell )14 contain% total %ale% 5o >o%ton* 3o% Angele%* and +ew Lo -. ?/ ,ave ,ad e'o t% t,at t,e !alculation ' o'e t( doe% not alwa(% wo - ' o'e l(. $o %o#e ea%on* it #a( %i#'l( cau%e t,e elevant cell% to 5ill wit, E6celB% in5a#ou% U+EA %(#bol%. /ndeed* / ,ave ,ad t,i% %a#e e6'e ience at ti#e%* but / ,ave not been able to 5igu e out w,(.@ Figure 29-27. +alculation J xl,unning otal

0,e code 5o $igu e 20;29 i%:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale").Calc0lation = xl;ercent*6"ow

Figure 29-28. +alculation J xlPercent34,ow

0,e code 5o $igu e 20;30 i%:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale").Calc0lation = xl;ercent*6Col05n

Figure 29-"9. +alculation J xlPercent34+olu&n

0,e code 5o $igu e 20;31 i%:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale").Calc0lation = xl;ercent*6Total

Figure 29-"1. +alculation J xlPercent34 otal

29.6.7." +alculations requiring a .aseFiel#H.ase/te& 0,e ' ocedu e 5o #a-ing calculation% wit, a >a%e$ieldE>a%e/te# i% not e6'lained ve ( well in t,e docu#entation* %o let u% %ee i5 we can clea it u' b( 5i %t con%ide ing an e6a#'le. !on%ide t,e code:
Hith ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale") .Calc0lation = xlDi66erence>ro5 .1ase>ield = "Store Cit " .1ase2te5 = "1oston" 'nd Hith

Re5e ing to $igu e 20;32* we ,ave e' oduced ou u%ual 'ivot table ?5 o# $igu e 20;10@ wit, %eve al c,ange%. $i %t* we e#oved t,e 0 an% value%* %ince t,e( a e not elevant to ou e6a#'le. 4e ,ave al%o e'laced t,e 1ale value% 5o t,e given >a%e$ield and >a%e/te# b( %(#bol% ?b1%bD@. $inall(* we e'laced t,e ot,e 1ale value% wit, nu#be %ign%* %ince we do not ca e about t,e actual value%. Figure 29-"2. /llustrating a calculation

Tea3m29 77l'D

+ow* t,e t ic- in %eeing ,ow t,e calculation% a e #ade i% to 5i6 a value 5o t,e 5ield% ot,e t,an t,e ba%e 5ield<in ou ca%e t,e 1to e 0('e and &e iod. !on%ide * 5o in%tance* t,e value%:
Store T 3e = "Co53an " ;eriod = 1

0,e 1ale data cell% co e%'onding to t,e%e value% a e g a(ed in $igu e 20;32. One o5 t,e%e cell% ?cell )4@ i% t,e ba%e ite# cell. $o t,i% 1to e 0('eE&e iod co#bination* a calculation i% #ade u%ing t,e value in t,i% cell a% t,e ba%e value. /n ou ca%e* it i% t,e xlDi66erence>ro5 calculation t,at i% being #ade. Aence* t,e ba%e value i% %ubt acted 5 o# t,e value% in all t, ee g a(ed cell%. 0,i% give% t,e table in $igu e 20;33. +ote t,at t,e ba%e value i% even %ubt acted 5 o# it%el5* giving 0. 0,i% i% done 5o eac, 1to e 0('eE&e iod co#bination* a% %,own in $igu e 20;33. Figure 29-"". he 4inishe# calculation

0,e 5o #ula% 5o t,e !alculation ' o'e t( t,at e7ui e >a%e$ieldE>a%e/te# value% a e: xlDi66erence>ro5
O % base val0e

xl;ercent*6
OJbase val0e ?e6' e%%ed a% a 'e cent@

xl;ercentDi66erence>ro5
(O % base val0e)Jbase val0e ?e6' e%%ed a% a 'e cent@

0o illu%t ate* $igu e 20;34 %,ow% t,e actual e55ect o5 t,e ea lie code on $igu e 20;10:
Hith ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Sale") .Calc0lation = xlDi66erence>ro5 .1ase>ield = "Store Cit " .1ase2te5 = "1oston" 'nd Hith >i40re 27%17.

Figure 29-"$. /llustrating the +alculation !ro!erty

29.6.8 +urrentPage Pro!erty


0,i% ' o'e t( etu n% o %et% t,e cu ent 'age. /t i% onl( valid 5o 'age 5ield%. +ote t,at t,e ' o'e t( %,ould be %et eit,e to t,e na#e o5 t,e 'age 5ield o to 8All8 to %,ow all 'age%.

29.6.19 (ata y!e Pro!erty


0,i% ead;onl( ' o'e t( etu n% a con%tant t,at de%c ibe% t,e t('e o5 data in t,e 'ivot 5ield. /t can be one o5 t,e 5ollowing #l;ivot>ieldDataT 3e con%tant%:
'n05 #l;ivot>ieldDataT 3e xlText = %L1&D xl905ber = %L1L& xlDate = 2 'nd 'n05

29.6.11 Ci##en/te&s an# *isible/te&s Pro!erties


0,e Aidden/te#% ' o'e t( etu n% t,e &ivot/te#% collection o5 all ,idden &ivot/te# object%* u%ing t,e %(nta6:

Pi+otField/bject.?idden2te5s
/t can al%o etu n a %ingle o an a a( o5 ,idden &ivot/te# object% u%ing t,e %(nta6:
;ivot>ield*bTect.?idden2te5s(2ndex)

w,e e 1ndex i% a %ingle inde6 o an a a( o5 inde6e%. 1i#ila l(* t,e =i%ible/te#% ' o'e t( etu n% a &ivot/te#% collection o5 all vi%ible &ivot/te# object% o a %ingle o an a a( o5 %uc, object%. 0,e %(nta6 i% t,e %a#e a% 5o t,e Aidden/te#% ' o'e t(.

29.6.12 -e&ory2se# Pro!erty

0,i% ead;onl( ' o'e t( etu n% t,e a#ount o5 #e#o ( cu entl( being u%ed b( t,e &ivot$ield ?it al%o a''lie% to &ivot/te# object%@ a% a 3ong intege .

29.6.1" Server.ase# Pro!erty


0,i% ead;w ite >oolean ' o'e t( a''lie% to 'age 5ield% onl( and i% u%ed wit, ?gene all( ve ( la ge@ e6te nal data %ou ce%. /t i% Tr0e w,en t,e &ivot0ableB% data %ou ce i% e6te nal and onl( t,e ite#% #atc,ing t,e 'age;5ield %election a e et ieved. >( %etting t,i% ' o'e t( to Tr0e* t,e 'ivot cac,e ?di%cu%%ed late in t,e c,a'te @ need% to acco##odate onl( t,e data 5o a %ingle 'age 5ield. 0,i% #a( be i#'o tant* o even nece%%a (* w,en t,e e6te nal data %ou ce i% la ge. 0,e t ade;o55 i% t,at eac, ti#e t,e 'age 5ield i% c,anged* t,e e #a( be a dela( w,ile E6cel e7ue ie% t,e o iginal %ou ce data to et ieve data on t,e new 'age 5ield. +ote t,at t,e e a e %o#e ci cu#%tance% unde w,ic, t,i% ' o'e t( cannot be %et to Tr0e:
C C C C

0,e 5ield i% g ou'ed. 0,e data %ou ce i% not e6te nal. 0,e cac,e i% u%ed b( #o e t,an one &ivot0able. 0,e 5ield ,a% a data t('e t,at cannot be %e ve ;ba%ed ?%uc, a% a #e#o 5ield o an O3E object@.

29.6.1$ Show%ll/te&s Pro!erty


0,i% ead;w ite >oolean ' o'e t( i% Tr0e i5 all ite#% in t,e &ivot0able a e di%'la(ed. 0,e de5ault value i% >alse* in w,ic, ca%e t,e 'ivot ite#% t,at do not cont ibute to t,e data 5ield% a e not di%'la(ed. +ote t,at t,i% ' o'e t( co e%'ond% to t,e 81,ow ite#% wit, no data8 c,ec- bo6 on E6celB% &ivot0able $ield dialog bo6. 0o illu%t ate* %u''o%e we add a %ingle ow to ou %ou ce table: D 1 1*%1 1*ST*9 AStoreT 3e 1777
C17777.77

0,i% ow c eate% a new %to e t('e but add% data onl( 5o >o%ton. 0,e e%ulting 'ivot table will ,ave t,e de5ault a''ea ance %,own in $igu e 20;35. 1ince 1,owAll/te#% i% >alse* t,e e a e no ow% 5o t,e new %to e t('e co e%'onding to 3o% Angele% o +ew Lo -. Figure 29-"'. Show%ll/te&s J False

On t,e ot,e ,and* t,e code:


ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e").ShowAll2te5s = Tr0e

will ' oduce t,e 'ivot table %,own in $igu e 20;36. Figure 29-"5. Show%ll/te&s J rue

29.6.1' Subtotals -etho#


0,i% #et,od etu n% o %et% t,e di%'la( o5 a 'a ticula t('e o5 %ubtotal 5o t,e %'eci5ied 'ivot 5ield. /t i% valid 5o all 5ield% ot,e t,an data 5ield%. 0,e %(nta6 i%:

Pi+otField/bject.S0btotals(1ndex)
w,e e t,e o'tional /nde6 'a a#ete indicate% t,e t('e o5 %ubtotal and i% a nu#be 5 o# 0able 20;4. able 29-$. *alues 4or the Subtotals -etho#>s /n#ex Para&eter (n&ex Subtotal Type Auto#atic 1u# !ount Ave age "a6 "in & oduct !ount +u#% 1td)ev 1td)ev' =a =a '

1 2 3 4 5 6 7 8 9 10 11 12

$o in%tance* t,e 5ollowing code e7ue%t% a di%'la( o5 %ubtotal% 5o bot, 1u# and !ount:

332

ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit ").S0btotals(2) = Tr0e ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit ").S0btotals(B) = Tr0e

4e can al%o %et t,e 1ubtotal% ' o'e t( to an a a( o5 12 >oolean value% to %et #ulti'le %ubtotal%. $o in%tance* t,e 5ollowing code di%'la(% all %ubtotal%:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit "). G S0btotals = Arra (>alse= Tr0e= Tr0e= Tr0e= Tr0e= G Tr0e= Tr0e= Tr0e= Tr0e= Tr0e= Tr0e= Tr0e)

+ote t,at we %et Auto#atic to >alse in t,i% a a(* %ince i5 Auto#atic i% %et to Tr0e* t,en all ot,e value% a e %et to >alse ?t,u% ' oviding a 7uic- wa( to %et all %ubtotal% to >alse@. /5 t,i% a gu#ent i% o#itted* t,e 1ubtotal% #et,od etu n% an a a( containing a >oolean value 5o eac, %ubtotal.

'>%< The Pi otCache Ob1ect


&ivot table% can #ani'ulate t,e %ou ce data in a va iet( o5 wa(%* and t,i% can e7ui e a g eat deal o5 ' oce%%ing 'owe . $o #a6i#u# e55icienc(* t,e data 5o a 'ivot table i% 5i %t %to ed in #e#o ( in w,at i% e5e ed to a% a pi#ot cache. 0,e 'ivot table* it%el5* actuall( ' ovide% va iou% view% o5 t,e 'ivot cac,e. 0,i% allow% #ani'ulation o5 t,e data wit,out t,e need to acce%% t,e o iginal %ou ce 5u t,e w,ic, #ig,t* a5te all* be an e6te nal data %ou ce. 0,e &ivot!ac,e object e' e%ent% a 'ivot tableB% cac,e. /t i% etu ned b( t,e &ivot!ac,e #et,od o5 t,e &ivot0able object. 3et u% ta-e a loo- at %o#e o5 t,e #ain ' o'e tie% and #et,od% o5 t,e &ivot!ac,e object.

29.7.1 ,e4reshing a Pivot +ache


0,e Re5 e%, #et,od e5 e%,e% bot, t,e 'ivot cac,e and t,e 'ivot table. Aoweve * we can ' event a 'ivot table 5 o# being e5 e%,ed* eit,e t, oug, t,e u%e inte 5ace ?t,e Re5 e%, data #enu ite# on t,e &ivot0able #enu@ o t, oug, code* b( %etting t,e EnableRe5 e%, ' o'e t( to >alse. 0,e ead;w ite >oolean Re5 e%,On$ileO'en ' o'e t( i% Tr0e i5 t,e &ivot0able cac,e i% auto#aticall( u'dated eac, ti#e t,e wo -boo- i% o'ened b( t,e u%e . 0,e de5ault value i% >alse. +ote t,at t,i% ' o'e t( i% igno ed i5 t,e EnableRe5 e%, ' o'e t( i% %et to >alse. +ote al%o t,at t,e &ivot0able cac,e i% not auto#aticall( e5 e%,ed w,en t,e wo -boo- i% o'ened t, oug, code* even i5 Re5 e%,On$ileO'en i% Tr0e. 0,e Re5 e%,)ate ' o'e t( etu n% t,e date on w,ic, t,e 'ivot cac,e wa% la%t e5 e%,ed* and t,e Re5 e%,+a#e ' o'e t( etu n% t,e na#e o5 t,e u%e w,o la%t e5 e%,ed t,e cac,e.

29.7.2 -e&ory2se# Pro!erty


0,e ead;onl( "e#o (2%ed ' o'e t( a''lie% to eit,e a Pi#ot(ache object o a &ivot$ield object and etu n% t,e a#ount o5 #e#o ( cu entl( being u%ed b( t,e cac,e* in b(te%.

29.7." 3!ti&i?e+ache Pro!erty


!ac,e o'ti#iGation i% u%ed wit, la ge o co#'le6 %ou ce data. /t will %low t,e initial con%t uction o5 t,e cac,e. 0,i% ead;w ite >oolean ' o'e t( can be u%ed to %et cac,e o'ti#iGation. 2n5o tunatel(* t,e ' eci%e con%e7uence% o5 cac,e o'ti#iGation a e not #ade clea in t,e docu#entation* leaving u% to u%e t ial and e o to decide w,et,e it %,ould be e#'lo(ed. &e ,a'% t,e be%t %t ateg( i% to leave t,i% ' o'e t( %et to it% de5ault ?>alse@ unle%% 'e 5o #ance %ee#% to be a ' oble#.

29.7.$ ,ecor#+ount Pro!erty


0,i% ead;onl( ' o'e t( etu n% t,e nu#be o5 eco d% in t,e &ivot0able cac,e.

29.7.' Source(ata Pro!erty


0,i% ' o'e t( etu n% t,e data %ou ce 5o t,e &ivot0able* a% we di%cu%%ed in t,e %ection on t,e &ivot0able object.

29.7.5 Sql Pro!erty


0,i% ead;w ite %t ing ' o'e t( etu n% o %et% t,e 1O3 7ue ( %t ing u%ed wit, an O)>! data %ou ce.

'>%= The Pi ot&tem Ob1ect


A &ivot/te# i% a uni7ue value o5 a &ivot$ield. 0o illu%t ate* con%ide t,e 5ollowing code:
Di5 3i As ;ivot2te5 >or 'ach 3i 2n G ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit ").;ivot2te5s Deb04.;rint 3i.9a5e 9ext

0,at code will ' int t,e li%t:


1*ST*9 L*S A9:'L'S 9'H @*"P

w,ic, contain% t,e di%tinct 1to e !it( value% 5 o# t,e 1to e !it( 'ivot 5ield. 0,e &ivot/te#% #et,od o5 t,e &ivot$ield object etu n% &ivot/te# object%. 0,e %(nta6:

Pi+otField/bject.;ivot2te5s
etu n% t,e collection o5 all &ivot/te# object% 5o t,at &ivot$ield. 0,e %(nta6:

Pi+otField/bject.;ivot2te5s(1ndex)

can etu n a %ingle &ivot/te# object o an a a( o5 &ivot/te# object% ?b( %etting 1ndex to an a a( o5 inde6e%@. 0able 20;5 %,ow% t,e ' o'e tie% and #et,od% o5 t,e &ivot/te# object. 3et u% ta-e a loo- at %o#e o5 t,e%e #e#be %. +ote t,at %eve al o5 t,e #e#be % o5 t,e &ivot$ield object al%o a''l( to t,e &ivot/te# object. able 29-'. -e&bers o4 the Pivot/te& 3bject /%!alculated &o%ition 3abelRange Reco d!ount +a#e 1,ow)etail &a ent 1ou ce+a#e &a ent/te# =alue &a ent1,ow)etail =i%ible

A''lication !,ild/te#% ! eato )ataRange )elete $o #ula

29.8.1 (ata,ange Pro!erty


0,i% ' o'e t( etu n% a Range object e' e%enting t,e data a ea a%%ociated wit, t,e given &ivot/te#. 0o illu%t ate* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s("Co53an ").Data"an4e.Select

e%ult% in $igu e 20;37. Figure 29-"6. (ata,ange 4or Store y!e J +o&!any

29.8.2 )abel,ange Pro!erty


0,e 3abelRange ' o'e t( etu n% a Range object t,at e' e%ent% t,e label cell% 5o t,e &ivot/te#. $igu e 20;38 illu%t ate% t,e e%ult% o5 t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G

;ivot>ields("Store T 3e"). G ;ivot2te5s("Co53an ").Label"an4e.Select

Figure 29-"7. )abel,ange 4or Store y!e J +o&!any

29.8." /s+alculate# Pro!erty


0,i% ' o'e t( etu n% Tr0e i5 t,e 'ivot ite# i% a calculated ite#. 4e di%cu%% calculated ite#% late in t,e c,a'te .

29.8.$ Na&eI *alueI an# SourceNa&e Pro!erties


0,e ead;w ite +a#e ' o'e t( etu n% o %et% t,e na#e o5 t,e &ivot/te#. 0,i% i% t,e value t,at a''ea % in t,e label 5o t,at ite#. 0,e =alue ' o'e t( i% t,e %a#e a% t,e +a#e ' o'e t(. 0,e ead;onl( 1ou ce+a#e ' o'e t( etu n% t,e na#e o5 t,e ite# in t,e o iginal %ou ce data. 0,i% #a( di55e 5 o# t,e value o5 t,e +a#e ' o'e t( i5 t,e +a#e ' o'e t( ,a% been c,anged.

29.8.' Position Pro!erty


0,e &o%ition ' o'e t( etu n% o %et% t,e 'o%ition o5 t,e 'ivot ite#. $o in%tance* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s(">ranchise").;osition

etu n% t,e nu#be 2* %ince $ anc,i%e i% t,e %econd 'ivot ite# in t,e 'ivot table ?%ee $igu e 20;10@. "o eove * we can eve %e t,e 'o%ition% o5 !o#'an( and $ anc,i%e b( %etting t,e &o%ition o5 t,e $ anc,i%e 'ivot ite# to 1* a% 5ollow%:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s(">ranchise").;osition = 1

29.8.5 ,ecor#+ount Pro!erty

0,i% ead;onl( ' o'e t( etu n% t,e nu#be o5 eco d% in t,e &ivot0able cac,e t,at contain t,e 'ivot ite#. $o in%tance* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s(">ranchise")."ecordCo0nt

will etu n t,e nu#be 80 becau%e t,e e a e 80 ow% in t,e %ou ce table ?and* ,ence* t,e 'ivot cac,e@ t,at involve t,e $ anc,i%e %to e t('e.

29.8.6 Show(etail Pro!erty


4,en t,i% ead;w ite ' o'e t( i% %et to Tr0e* t,e 'ivot ite# i% %,own in detailH i5 it i% >alse* t,e &ivot/te# i% ,idden. 0o illu%t ate* con%ide * a% u%ual* t,e 'ivot table in $igu e 20;10. 0,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit "). G ;ivot2te5s("1oston").ShowDetail = >alse

e%ult% in t,e 'ivot table in $igu e 20;39. A% we can %ee* t,e 0 an%action% and 1ale% 5o >o%ton a e %u##ed ove all ?bot,@ %to e t('e% ?!o#'an( and $ anc,i%e@. Figure 29-"8. /llustrating Show(etail

A E

L F M

2n5o tunatel(* t,e e %ee#% to be a ' oble# w,en t,e 1,ow)etail #et,od i% a''lied to inne 'ivot ite#%. $o in%tance* t,e code:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s("Co53an ").ShowDetail = >alse

doe% %ee# to %et t,e 1,ow)etail ' o'e t( to >alse* a% can be ve i5ied b( t,e code:
Ms41ox ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e"). G ;ivot2te5s("Co53an ").ShowDetail

Tea3m39 77l'D

Aoweve * t,e 'ivot table doe% not e5lect t,i% c,angeC ?At lea%t t,i% ,a''en% on t,e two %(%te#% on w,ic, / ,ave un t,i% code. Lou %,ould c,ec- t,i% ca e5ull( on an( %(%te# on w,ic, (ou intend to un t,i% code. A %i#ila ' oble# occu % wit, t,e 1ubtotal% ' o'e t( a% well.@ A% anot,e e6a#'le* t,e 5ollowing code toggle% t,e di%'la( o5 detail% 5o t,e >o%ton 'ivot ite#:
Hith ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store Cit ").;ivot2te5s("1oston") .ShowDetail = 9ot .ShowDetail 'nd Hith

0,e 1,ow)etail ' o'e t( al%o a''lie% to t,e Range object* even w,en t,e ange lie% in%ide a 'ivot table. 0o illu%t ate* t,e 5ollowing code will al%o ' oduce t,e 'ivot table in $igu e 20;39:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect "1oston"= xlDataAndLabel Selection.ShowDetail = >alse

0,e 5ollowing code toggle% t,e di%'la( o5 t,e >o%ton detail%:


Di5 rn4 As "an4e ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect "1oston"= xlDataAndLabel Set rn4 = Selection.Cells(1= 1) rn4.ShowDetail = 9ot rn4.ShowDetail

+ote t,at rn! e5e % onl( to t,e 5i %t cell in t,e ange e' e%enting t,e >o%ton data and label%H t,at i%* we ,ave:
Set rn4 = Selection.Cells(1= 1)

0,i% i% done becau%e* w,en a''lied to t,e Range object* t,e 1,ow)etail ' o'e t( will etu n a value only w,en t,e ange i% a %ingle cell. A% anot,e illu%t ation* t,e 5ollowing code will ,ide t,e detail% 5o t,e enti e 1to e !it( 'ivot 5ield:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ".Store Cit ."= xlDataAndLabel Selection.ShowDetail = >alse ActiveSheet.;ivotTables("SalesKTrans"). G ;ivotSelect ".Store Cit ."= xlDataAndLabel

?Re'lacing >alse b( Tr0e will un,ide t,e detail%.@ 4e #u%t conclude b( %ugge%ting t,at %ince t,e 1,ow)etail% ' o'e t( i% ve ( 'oo l( docu#ented* (ou %,ould e6'e i#ent ca e5ull( and co#'letel( be5o e el(ing on t,i% ' o'e t( to 'e 5o # in a ce tain wa(.

29.8.7 *isible Pro!erty


0,i% ' o'e t( dete #ine% w,et,e o not t,e 'ivot ite# i% vi%ible in t,e 'ivot table.

'>%$> Pi otCell and Pi ot&tem)ist Ob1ects

+ew to E6cel D& i% t,e &ivot!ell object* w,ic, e' e%ent% a cell in a 'ivot table. 0,i% object and it% c,ild en a e %,own in $igu e 20;40. Figure 29-$9. he Pivot+ell object

0o obtain a &ivot!ell object* we u%e t,e &ivot!ell ' o'e t( o5 t,e Range object* a% in:
Ms41ox ActiveSheet."an4e("C&").;ivotCell.;ivotCellT 3e

+ote t,at i5 t,e ange contain% #o e t,an one cell* t,e &ivot!ell object 5o t,e 5i %t cell i% etu ned. 0,e &ivot!ell object ,a% no #et,od%. A#ong it% 13 ' o'e tie%* ,e e a e t,e #o%t i#'o tant ?and t,e one% t,at %ee# to wo -@:
C C C C C C C C

!olu#n/te#% !u%to#1ubtotal$unction )ata$ield &ivot!ell0('e &ivot$ield &ivot/te# &ivot0able Row/te#%

0,e 5ollowing %ection% ' ovide a b ie5 de%c i'tion o5 t,e%e ' o'e tie%. /t i% i#'o tant to -ee' in #ind t,at E6cel i% not at all 5 iendl( w,en it co#e% to a''l(ing t,e%e ' o'e tie% to a &ivot!ell object. 0,at i%* i5 t,e &ivot!ell object i% not o5 a t('e t,at %u''o t% t,e ' o'e t(* t,en E6cel will etu n an e o . $o in%tance* i5 a cell i% not a data cell in a 'ivot table* t,en acce%%ing t,e &ivot!ell objectB% !olu#n/te#% ' o'e t( gene ate% an e o * at,e t,an etu ning an e#'t( collection* 5o e6a#'le.

29.19.1 +olu&n/te&sI ,ow/te&sI an# (ataFiel# Pro!erties


0,e !olu#n/te#% and Row/te#% ' o'e tie% eac, etu n a &ivot/te#3i%t collection t,at contain% t,e colu#n ?o ow@ 'ivot ite#% a%%ociated to t,e &ivot!ell. 0,e )ata$ield ' o'e t( etu n% a %ingle &ivot$ield ite# a%%ociated to t,e &ivot!ell. +ote t,at t,e%e ' o'e tie% will gene ate an e o ?wit, t,e u%ual un,el'5ul #e%%age@ i5 a''lied to t,e &ivot!ell object o5 a cell t,at i% not an agg egate ?data@ cell in t,e 'ivot table. 0o illu%t ate* con%ide t,e 'ivot table in $igu e 20;6. 0,e &ivot/te#3i%t ' ocedu e %,own ,e e di%'la(% t,e colu#n 'ivot ite#%* ow 'ivot* ite#%* and data 5ield a%%ociated wit, t,e &ivot!ell 5o t,e ange ?cell@ E5.
S0b ;ivot2te5List() . 2ll0strate the Col05n2te5s and "ow2te5s 3ro3erties Di5 rn4 As "an4e Di5 3i As ;ivot2te5

Di5 ws As Hor8sheet Set ws = ActiveSheet . 9ote- the 6ollowin4 code will 6ail i6 . the rn4 is not a data cell. Set rn4 = ws."an4e("'&") Deb04.;rint "Col05n ite5s-" >or 'ach 3i 2n rn4.;ivotCell.Col05n2te5s Deb04.;rint 3i.9a5e 9ext Deb04.;rint ""ow ite5s-" >or 'ach 3i 2n rn4.;ivotCell."ow2te5s Deb04.;rint 3i.9a5e 9ext Deb04.;rint "Data >ield-" Deb04.;rint rn4.;ivotCell.Data>ield.9a5e 'nd S0b

0,e out'ut o5 t,i% ' ocedu e i%:


Col05n ite5s2 "ow ite5s1*ST*9 Co53an Data >ieldS05 o6 Transactions

A% #entioned* i5 we c,ange t,e ange to cell !5* t,e line:


>or 'ach 3i 2n rn4.;ivotCell.Col05n2te5s

will gene ate t,e un,el'5ul e o #e%%age 8A''lication de5ined o object;de5ined e o 8.

29.19.2 +usto&SubtotalFunction Pro!erty


A% (ou no doubt -now* a 'ivot table can di%'la( %ubtotal%* a% %,own in $igu e 20;6* w,ic, %,ow% %u#% b( cit(. +ow* t,e %ubtotal t('e can be %et to auto#atic ?t,e de5ault@ o to one o5 %eve al di55e ent cu%to# value%: %u#* count* ave age* and %o on. 4,en a %ubtotal i% %et to a cu%to# t('e* t,e &ivot!ell object o5 t,e cell% t,at %,ow t,e %ubtotal t('e ?not t,e %ubtotal value%@ ,ave t('e 6l&ivot!ell!u%to#1ubtotal ?%ee t,e di%cu%%ion o5 t,e &ivot!ell0('e ' o'e t(@. $o in%tance* i5 we c,anged t,e %ubtotal b( cit( to ave age* t,en t,e &ivot!ell object 5o cell% A9* >9* and !9 in $igu e 20;6 would be o5 t('e 6l&ivot!ell!u%to#1ubtotal. 0,e !u%to#1ubtotal$unction ' o'e t( o5 t,e &ivot!ell object etu n% t,e t('e o5 agg egate 5unction 5o a cu%to# %ubtotal cell. 0,e etu n value% a e de5ined b( t,e 5ollowing enu#.
'n05 #lConsolidation>0nction xl+ar; = %L1N& xl+ar = %L1NL xlS05 = %L1&F xlStDev; = %L1&N xlStDev = %L1&& xl;rod0ct = %L1LE

xlMin = %L1BE xlMax = %L1BN xlCo0nt905s = %L11B xlCo0nt = %L112 xlAvera4e = %L17N xl<n8nown = 1777 'nd 'n05

+ote t,at i5 t,e &ivot!ell object i% not o5 t('e 6l&ivot!ell!u%to#1ubtotal t,en an( e5e ence to t,e !u%to#1ubtotal$unction ' o'e t( gene ate% an e o * %o it i% i#'o tant to c,ec- t,e &ivotcell0('e ' o'e t( ?di%cu%%ed below@ be5o e u%ing t,e !u%to#1ubtotal$unction ' o'e t(C

29.19." Pivot+ell y!e Pro!erty


0,i% ' o'e t( etu n% t,e t('e o5 cell* in t,e 5o # o5 one o5 t,e con%tant% in t,e 5ollowing enu#e ation:
'n05 #l;ivotCellT 3e xl;ivotCell+al0e = 7 xl;ivotCell;ivot2te5 = 1 xl;ivotCellS0btotal = 2 xl;ivotCell:randTotal = B xl;ivotCellData>ield = L xl;ivotCell;ivot>ield = & xl;ivotCell;a4e>ield2te5 = N xl;ivotCellC0sto5S0btotal = F xl;ivotCellData;ivot>ield = D xl;ivotCell1lan8Cell = E 'nd 'n05

$igu e 20;41 %,ow% a 'ivot table and t,e &ivot!ell0('e value% ?on t,e ig,t@. Lou can u%e t,i% to %ee w,at t('e% o5 cell% a e contained in a 'ivot table. $o in%tance* cell A4 ,a% &ivot!ell0('e 5* indicating t,at it i% a 'ivot 5ield cell. Figure 29-$1. Pivot+ell values

29.19.$ Pivot ableI PivotFiel#I an# Pivot/te& Pro!erties

0,e &ivot0able ' o'e t( etu n% t,e &ivot0able object a%%ociated wit, t,e &ivot!ell. 0,e &ivot$ield ' o'e t( etu n% t,e &ivot$ield object a%%ociated wit, t,e &ivot!ellH and t,e &ivot/te# ' o'e t( etu n% t,e &ivot/te# a%%ociated wit, t,e &ivot!ell. Re5e ing to $igu e 20;6* t,e 5ollowing code illu%t ate% t,e%e ' o'e tie%:
S0b test() . "e6er to >i40re 27%N Di5 rn4 As "an4e Di5 ws As Hor8sheet Set ws = ActiveSheet Set rn4 = ws."an4e("'&") Deb04.;rint rn4.;ivotCell.;ivotTable.9a5e Deb04.;rint rn4.;ivotCell.;ivot>ield Transactions Set rn4 = ws."an4e("1&") Deb04.;rint rn4.;ivotCell.;ivot2te5.9a5e 'nd S0b . ret0rns Co53an . ret0rns SalesKTrans . ret0rns S05 o6

'>%$$ Calculated &tems and Calculated :ields


4e ,ave %een t,at it i% 'o%%ible to add a calculated 5ield to a 'ivot table. A calculated 5ield i% %'ecial t('e o5 &ivot$ield object t,at i% not 'a t o5 t,e o iginal %ou ce data* but* in%tead* i% calculated 5 o# %ou ce 5ield% u%ing a 5o #ula. +ote t,at t,e e i% no %uc, t,ing a% a !alculated$ield object* but t,e e i% a !alculated$ield% collection. A% we ,ave %een* to c eate a new calculated 5ield* we u%e t,e Add #et,od o5 t,e !alculated$ield% collection o5 t,e &ivot0able object. 0,e %(nta6 i%:

CalculatedFields/bject.Add(Name= Formula)
w,e e Name i% t,e na#e o5 t,e 5ield and Formula i% t,e 5o #ula 5o t,e 5ield. On t,e ot,e ,and* a calculated item i% a %'ecial t('e o5 &ivot/te# object a%%ociated wit, a given &ivot$ield object. ?0,e e i% no %uc, t,ing a% a !alculated/te# object* but t,e e i% a !alculated/te#% collection.@ 0,e value% o5 t,i% ite# a e calculated b( u%ing a 5o #ula. 0,e &ivot$ield object ,a% a !alculated/te#% collection o5 all calculated ite#% 5o t,at 'ivot 5ield. 0o c eate a new calculated ite#* we u%e t,e Add #et,od o5 t,e !alculated/te#% object. 0,i% #et,od ,a% t,e %a#e %(nta6 a% t,e Add #et,od o5 t,e !alculated$ield% object:

Calculated1tems/bject.Add(Name= Formula)
w,e e Name i% t,e na#e o5 t,e 5ield and Formula i% t,e 5o #ula 5o t,e 5ield. 0o illu%t ate* t,e 5ollowing code add% a new calculated ite# to t,e 1to e 0('e 'ivot 5ield:
ActiveSheet.;ivotTables("SalesKTrans"). G ;ivot>ields("Store T 3e").Calc0lated2te5s. G

Add "Co53an #2"= "=.Store T 3e.Co53an R2"

0,e e%ult% a e %,own in $igu e 20;42* w,e e t,e calculated ite# i% !o#'an(D2. 0,e value in eac, o5 t,e !o#'an(D2 cell% i% twice t,e value in t,e co e%'onding !o#'an( cell. Figure 29-$2. /llustrating a calculate# ite& ;+o&!anyX2<

$o co#'a i%on* let u% add a calculated 5ield to t,e 'ivot table in $igu e 20;42. 4e will add t,e %a#e calculated 5ield t,at we added w,en we di%cu%%ed t,e !alculated$ield% #et,od ea lie in t,e c,a'te :
Hith ActiveSheet.;ivotTables("SalesKTrans"). G Calc0lated>ields.Add("Avera4e"= G "= SalesJTransactions") .*rientation = xlData>ield .9a5e = "Av4 Chec8" .905ber>or5at = "OO.O" 'nd Hith

0,e e%ult i% %,own in $igu e 20;43. Figure 29-$". /llustrating a calculate# 4iel# an# calculate# ite&

+ote t,at t,e 3i%t$o #ula% #et,od o5 t,e &ivot0able object will ' oduce a li%t ?on a %e'a ate wo -%,eet@ o5 all 5o #ula% in t,e 'ivot table. 0,e outco#e 5o t,e 'ivot table in $igu e 20;43 i% %,own in $igu e 20;44. Figure 29-$$. he out!ut o4 )istFor&ulas

3et u% conclude b( ecalling t,at t,e ead;onl( /%!alculated ' o'e t( can be u%ed to dete #ine w,et,e o not a 'ivot 5ield o 'ivot ite# i% calculated.

344

'>%$' Example+ Printing Pi ot Tables


+ow we can i#'le#ent t,e & int&ivot0able% 5eatu e o5 t,e 1RD2til% a''lication. A co#'le6 E6cel wo -boo- #a( ,ave a la ge nu#be o5 'ivot table% %catte ed on va iou% wo -%,eet%. A %i#'le utilit( 5o ' inting t,e%e 'ivot table% can be u%e5ul. ?/ ,ave o5ten been a%-ed to w ite %uc, a utilit( in #( con%ulting ' actice.@ Ou a''lication di%'la(% a dialog bo6* a% %,own in $igu e 20;45. 0,e li%t bo6 contain% a li%t o5 all 'ivot table%. Eac, ent ( include% t,e 'ivot tableB% na#e* 5ollowed b( t,e na#e o5 t,e wo -%,eet. 0,e u%e can %elect one o #o e 'ivot table% and ,it t,e ' int button to ' int t,e%e table%. Figure 29-$'. Print !ivot tables

0,e 5ollowing a e t,e %te'% to c eate t,e ' int utilit(. All t,e action ta-e% 'lace in t,e PrintAxls wo -boo-* %o o'en t,i% wo -boo-. 4,en t,e c,ange% a e 5ini%,ed* (ou will need to %ave PrintAxls a% PrintAutl a% well. /5 PrintAutl i% loaded* t,e onl( wa( to unload it i% to unload t,e add;in /,4'tilsAxla ?i5 it i% loaded@ and clo%e t,e wo -boo- /,4'tilsAxls ?i5 it i% o'en@.

29.12.1 +reate the 2serFor&


! eate t,e dialog %,own in $igu e 20;45 in t,e PrintAxls wo -boo-. +a#e t,e dialog dl4;rint;ivotTables* c,ange it% !a'tion ' o'e t( to 8& int &ivot 0able%*8 and c,ange t,e & int&ivot0able% ' ocedu e a% %,own in E6a#'le 20;5. Exa&!le 29-'. he PrintPivot ables Proce#ure
;0blic S0b ;rint;ivotTables() dl4;rint;ivotTables.Show 'nd S0b

0,e dl!PrintPi+otTables dialog ,a% two co##and button% and one li%t bo6. 29.12.1.1 )ist box &lace a li%t bo6 on t,e 5o # a% in $igu e 20;45. 2%ing t,e & o'e tie% window* %et t,e 5ollowing ' o'e tie%: Property +a#e 0ab/nde6 "ulti1elect l%t&0% 0 5 #"ulti1electE6tended Value

4,en t,e !ancel ' o'e t( o5 t,e cmdCancel button i% %et to Tr0e* t,e button i% 8clic-ed8 w,en t,e u%e ,it% t,e E%ca'e -e(. 0,u%* t,e E%ca'e -e( will di%#i%% t,e ' int dialog. 0,e "ulti1elect ' o'e t( i% %et to 6r5M0ltiSelect'xtended %o t,at t,e u%e can u%e t,e !ont ol -e( to %elect #ulti'le ?'o%%ibl( noncon%ecutive@ ent ie% and t,e 1,i5t -e( to %elect #ulti'le con%ecutive ent ie%. 0,e 0ab/nde6 ' o'e t( dete #ine% not onl( t,e o de in w,ic, t,e cont ol% a e vi%ited a% t,e u%e ,it% t,e 0ab -e(* but al%o dete #ine% w,ic, cont ol ,a% t,e initial 5ocu%. 1ince we want t,e initial 5ocu% to be on t,e li%t bo6* we %et it% tab inde6 to 0. 29.12.1.2 Print button &lace a co##and button on t,e 5o # a% in $igu e 20;45. 2%ing t,e & o'e tie% window* %et t,e 5ollowing ' o'e tie%: Property +a#e Accele ato !a'tion 0ab/nde6 29.12.1." +ancel button &lace anot,e co##and button on t,e 5o # a% in $igu e 20;45. 2%ing t,e & o'e tie% window* %et t,e 5ollowing ' o'e tie%: Property +a#e Accele ato !a'tion 0ab/nde6 !ancel c#d!ancel ! !ancel 2 0 ue Value c#d& int & & int 1 Value

29.12.2 +reate the +o#e .ehin# the 2serFor&


+ow it i% ti#e to c eate t,e code be,ind t,e%e cont ol%. 29.12.2.1 he (eclarations section 0,e )ecla ation% %ection %,ould contain decla ation% o5 t,e #odule;level va iable% a% %,own in E6a#'le 20;6. Exa&!le 29-5. -o#ule-)evel *ariables in the user 4or&>s (eclarations Section
Di5 c;Ts As 2nte4er Di5 s;T9a5es() As Strin4 Di5 sSheets() As Strin4

29.12.2.2 +ancel button co#e 0,e !ancel button code i% %,own in E6a#'le 20;7.

Exa&!le 29-6. he c&#+ancelG+lic1 Event Proce#ure


;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b

29.12.2." Print button co#e 0,e & int button call% t,e #ain ' int ' ocedu e and t,en unload% t,e 5o #H it% event code i% %,own in E6a#'le 20;8. Exa&!le 29-7. he c&#PrintG+lic1 Event Proce#ure
;rivate S0b c5d;rintGClic8() ;rintSelected;Ts <nload Me 'nd S0b

29.12.2.$ he For&>s /nitiali?e event 0,e u%e 5o #B% /nitialiGe event i% t,e 'lace to 5ill t,e li%t bo6 wit, a li%t o5 'ivot table%. Ou a''lication u%e% two #odule;level a a(%: one to ,old t,e wo -%,eet na#e% and one to ,old t,e 'ivot;table na#e%. 0,e e i% al%o a #odule;level va iable to ,old t,e 'ivot;table count. 4e 5ill t,e%e a a(% in t,e /nitialiGe event* a% %,own in E6a#'le 20;9* and t,en u%e t,e a a(% to 5ill t,e li%t. 0,e%e a a(% a e u%ed again in t,e #ain ' int ' ocedu e* w,ic, i% w,( we ,ave decla ed t,e# at t,e #odule level. +ote t,e u%e o5 t,e "eDi5 %tate#ent to edi#en%ion t,e a a(%. 0,i% i% nece%%a ( %ince we do not -now at t,e out%et ,ow #an( 'ivot table% t,e e a e in t,e wo -boo-. Exa&!le 29-8. he /nitiali?e Event
;rivate S0b <ser>or5G2nitialiMe() . >ill lst;Ts with the list o6 3ivot tables Di5 ws As Hor8sheet Di5 ;T As ;ivotTable "eDi5 s;T9a5es(1 To 17) As Strin4 "eDi5 sSheets(1 To 17) As Strin4 lst;Ts.Clear c;Ts = 7 >or 'ach ws 2n ActiveHor8boo8.Hor8sheets >or 'ach ;T 2n ws.;ivotTables . <3date ;T co0nt c;Ts = c;Ts A 1 . "edi5ension arra s i6 necessar 26 <1o0nd(sSheets) $ c;Ts Then "eDi5 ;reserve sSheets(1 To c;Ts A &) "eDi5 ;reserve s;T9a5es(1 To c;Ts A &) 'nd 26 . Save na5e o6 3ivot table and ws s;T9a5es(c;Ts) = ;T.9a5e sSheets(c;Ts) = ws.9a5e

Tea3m49 77l'D

. Add ite5 to list box lst;Ts.Add2te5 ;T.9a5e K " ( in " K G sSheets(c;Ts) K ")" 9ext 9ext 'nd S0b

29.12.2.' PrintP s !roce#ure 0,e #ain ' inting ' ocedu e i% %,own in E6a#'le 20;10. +ote t,at we ,ave been ca e5ul to deal wit, two %'ecial ca%e%. $i %t* t,e e #a( not be an( 'ivot table% in t,e wo -boo-. 1econd* t,e u%e #a( ,it t,e & int button wit,out %electing an( 'ivot table% in t,e li%t bo6. +ote al%o t,at li%t bo6e% a e 0; ba%ed* #eaning t,at t,e 5i %t ite# i% ite# 0. Aoweve * ou a a(% a e 1;ba%ed ?t,e 5i %t ite# i% ite# 1@* %o we #u%t ta-e t,i% into account w,en we #ove 5 o# a %election to an a a( #e#be H to wit* %election i co e%'ond% to a a( inde6 i.#. Exa&!le 29-19. he PrintSelecte#P s Proce#ure
S0b ;rintSelected;Ts() . ;rint the selected 3ivot tables in lst;Ts Di5 i As 2nte4er Di5 b9oneSelected As 1oolean b9oneSelected = Tr0e 26 c;Ts = 7 Then Ms41ox "9o 3ivot tables in this wor8boo8."= G vb'xcla5ation 'xit S0b 'lse >or i = 7 To lst;Ts.ListCo0nt % 1 26 lst;Ts.Selected(i) Then b9oneSelected = >alse . List box is 7%based= arra s are 1%based Hor8sheets(sSheets(i A 1)). G ;ivotTables(s;T9a5es(i A 1)). G Table"an4e2.;rint*0t 'nd 26 9ext 'nd 26 26 b9oneSelected Then Ms41ox "9o 3ivot tables have been selected."= G vb'xcla5ation 'nd 26 'nd S0b

Chapter '$% The Chart Ob1ect


E6cel c,a t% a e e' e%ented b( !,a t object% in t,e E6cel object #odel. 1ince c,a t% a e 7uite co#'le6* t,e !,a t object i% one o5 t,e #o%t co#'licated in t,e object #odel. 0o %et t,e te #inolog(* $igu e 21;1 %,ow% a t('ical c,a t t,at ,a% been labeled wit, %o#e o5 t,e co##onl( u%ed c,a t; elated te #%. Figure 21-1. So&e chart ter&inology

$igu e 21;2 %,ow% t,e !,a t object and it% i##ediate c,ild en. Figure 21-2. he +hart object

'$%$ Chart Ob1ects and ChartOb1ect Ob1ects

A% (ou ' obabl( -now* E6cel c,a t% can e%ide in a %'ecial t('e o5 %,eet called a chart sheet o t,e( can be e#bedded in an o dina ( wo -%,eet. Acco dingl(* a !,a t object can e' e%ent a c,a t %,eet ?%tandalone c,a t@ o an e#bedded c,a t. /n t,e latte ca%e* t,e !,a t object i% not contained di ectl( in a wo -%,eet. Rat,e * t,e wo -%,eet contain% a !,a tObject object t,at act% a% a container 5o t,e !,a t object. 0,u%* 5o in%tance* i5 we c eate a new c,a t u%ing t,e c,a t wiGa d* t,e 5ou t, %te' in t,e wiGa d di%'la(% t,e dialog %,own in $igu e 21;3. Figure 21-". Ste! $ in the chart wi?ar#

/5 we c,oo%e t,e 8A% new %,eet8 o'tion in %te' 4 o5 t,e c,a t wiGa d* we can acce%% t,e e%ulting c,a t u%ing t,e code:
Di5 c as Chart Set c = ThisHor8boo8.Charts("Chart1")

On t,e ot,e ,and* c,oo%ing t,e 8A% object in8 o'tion in %te' 4 o5 t,e c,a t wiGa d* we acce%% t,e c,a t u%ing t,e code:
Di5 c As Chart Set c = Hor8sheets("Sheet1").Chart*bTects("Chart 1").Chart

+ote t,e %'ace between t,e wo d !,a t and t,e nu#be 1 in t,e na#e o5 t,e !,a tObject object* but not in t,e na#e o5 t,e !,a t object. 4e e#',a%iGe t,at t,e e i% no !,a t1,eet object. 0,e !,a t% ' o'e t( o5 t,e A''lication object etu n% a %o;called 1,eet% collection containing one !,a t object 5o eac, c,a t %,eet. /t doe% not contain !,a t object% 5o t,e e#bedded c,a t%.

'$%' Creating a Chart


4e ,ave %een t,at a &ivot0able i% c eated and added to t,e &ivot0able% collection b( invo-ing t,e &ivot0able4iGa d #et,od. On t,e ot,e ,and* c eating a new c,a t e7ui e% a di55e ent a'' oac,* %ince it de'end% u'on w,et,e t,e c,a t i% %tandalone ?a c,a t %,eet@ o e#bedded in a wo -%,eet ?and t,u% contained in a !,a tObject object@. Al%o* unli-e t,e &ivot0able4iGa d #et,od* t,e !,a t4iGa d #et,od doe% not c eate a c,a tH it #e el( 5o #at% an e6i%ting c,a t. Acco dingl(* t,e e a e t, ee %te'% e7ui ed to c eate a #eaning5ul c,a t:

C C C

)ecide w,et,e to c eate a %tandalone c,a t ?a c,a t %,eet@ o an e#bedded c,a t. ! eate t,e %tandalone c,a t o e#bedded c,a t a% de%c ibed in t,e 5ollowing %ection. $o #at t,e c,a t u%ing eit,e t,e !,a t4iGa d #et,od o u%ing individual ' o'e tie% and #et,od% o5 t,e c,a t object.

21.2.1 +reating +hart Sheets


0,e 4o -boo- object ,a% a !,a t% ' o'e t( t,at etu n% t,e !,a t% collection o5 all chart sheets in t,e wo -boo-. 4e can u%e t,e Add #et,od o5 t,e !,a t% collection to c eate and add a new c,a t%,eet to t,e wo -boo-. 0,e %(nta6 5o t,e Add #et,od i%:

Charts/bject.Add()e*ore= -*ter= Count)


A% u%ual* t,i% #et,od etu n% a !,a t object. 0,e )e*ore 'a a#ete %'eci5ie% t,e %,eet be5o e w,ic, t,e new %,eet i% added* and t,e -*ter 'a a#ete %'eci5ie% t,e %,eet a5te w,ic, t,e new %,eet i% added. Onl( one o5 t,e%e 'a a#ete % can be %'eci5ied at one ti#e o an e o will e%ult. /5 neit,e i% %et* t,e new c,a t i% in%e ted be5o e t,e active %,eet. 0,e o'tional Count 'a a#ete i% an intege t,at %'eci5ie% t,e nu#be o5 %,eet% to be added. 0,e de5ault value i% 1. $o e6a#'le* t,e 5ollowing code c eate% a new c,a t %,eet na#ed 81ale%8:
Di5 ch As Chart Set ch = ThisHor8boo8.charts.Add() ch.9a5e = "Sales"

0,e Add #et,od cannot be u%ed to 5o #at t,e c,a t. A% #entioned ea lie * t,i% #u%t be done u%ing t,e va iou% ' o'e tie% o5 t,e !,a t object o t,e !,a t4iGa d #et,od* di%cu%%ed late in t,e c,a'te .

21.2.2 +reating E&be##e# +harts


0,e 4o -%,eet object al%o ,a% a !,a tObject% ' o'e t( t,at etu n% a !,a tObject% collection* w,ic, i% t,e collection o5 all !,a tObject% in t,e wo -%,eet. A% we ,ave #entioned* a !,a tObject object i% a containe 5o a !,a t object<t,at i%* an e#bedded c,a t. 0,e !,a tObject% collection ,a% an Add #et,od t,at i% u%ed to c eate a new e#bedded c,a t. 0,e %(nta6 i%:

Charts/bject/bject.Add("e*t= To = Width= 6ei!ht)


w,e e t,e e7ui ed "e*t and To 'a a#ete % give t,e coo dinate% o5 t,e u''e ;le5t co ne o5 t,e c,a t ?in 'oint%@ elative to t,e u''e ;le5t co ne o5 cell A1 on t,e wo -%,eet* and Width and 6ei!ht %'eci5( t,e initial %iGe o5 t,e c,a t ?al%o in 'oint%@. Recall t,at t,e /nc,e%0o&oint% #et,od can be u%ed to conve t inc,e% to 'oint%. +ote t,at t,e Add #et,od etu n% a !,a tObject object* at,e t,an a !,a t object. 0,i% i% a bit con5u%ing* %ince t,e #et,od c eate% bot, a !,a tObject object and t,e contained !,a t object. 0,e code in E6a#'le 21;1 c eate% a new !,a tObject object called E6a#'le!,a t along wit, it% contained !,a t object. /t 'o%ition% t,e c,a t %o t,at it% u''e ;le5t co ne i% t, ee colu#n% 5 o# t,e

351

le5t edge o5 t,e %,eet and 1E2 ow down 5 o# t,e to' o5 t,e %,eet. 0,e di#en%ion% o5 t,e c,a t a e 8 colu#n% wide and 20 ow% ,ig,. Exa&!le 21-1. +reating an E&be##e# +hart
S0b CreateAChart() . Create an e5bedded chart Di5 co As Chart*bTect Di5 cw As Lon4= rh As Lon4 . :et data 6or 3ositionin4 chart cw = Col05ns(1).Hidth rh = "ows(1).?ei4ht . ;osition chart 0sin4 col05n width and row hei4ht 0nits Set co = ActiveSheet.Chart*bTects.Add(cw R B= rh R 7.&= cw R D= rh R 27) . 9a5e it co.9a5e = "Chart'xa53le" Deb04.;rint co.9a5e Deb04.;rint co.Chart.9a5e . Set chart t 3e co.Chart.ChartT 3e = xlLine 'nd S0b

0,e out'ut o5 t,e )ebug.& int %tate#ent% a e:


Chart'xa53le Sheet1 Chart 1

0,e c,a t a''ea % a% in $igu e 21;4. ?Le%* t,e c,a t i% e#'t(.@ Figure 21-$. he results o4 creating an e&be##e# chart object

Lou #a( ,ave noticed t,at t,e c,a t in $igu e 21;4 i% a t i5le uninte e%ting. 4e will need to u%e t,e va iou% ' o'e tie% and #et,od% o5 t,e !,a t object ?o t,e !,a t4iGa d #et,od@ to c eate a u%e5ul c,a t.

+ote t,at* alt,oug, t,e docu#entation doe% not di%cu%% t,e #atte * e6'e i#entation %,ow% t,at t,e +a#e ' o'e t( o5 t,e !,a t object a''ea % to be ead;onl(. /ndeed* t,e code:
co.Chart.9a5e = "AChart"

e%ult% in t,e e o #e%%age: 8"et,od +a#e o5 object I!,a t 5ailed.8 +ote al%o t,at we can un t,e CreateAChart ' ocedu e #ulti'le ti#e% wit,out e o and t,i% will ' oduce #ulti'le !,a tObject object% wit, t,e %a#e na#eC 0,u%* t,e na#e ' o'e t( %ee#% to be o5 little u%e 5o bot, !,a tObject object% and e#bedded !,a t object%. /n 5act* a5te unning t,e CreateAChart ' ocedu e twice and getting two c,a t% na#ed E6a#'le!,a t* t,e code:
Deb04.;rint ActiveSheet.Chart*bTects(1).9a5e Deb04.;rint ActiveSheet.Chart*bTects(2).9a5e ActiveSheet.Chart*bTects("Chart'xa53le").Le6t = N77

actuall( ' oduce% t,e out'ut:


Chart'xa53le Chart'xa53le

and #ove% one o5 t,e c,a t% to t,e new 'o%ition %'eci5ied b( t,e 3e5t ' o'e t(C

21.2." %n Exa&!le o4 +hart +reation


A% we ,ave %aid* c eating a u%e5ul c,a t 5 o# %c atc, e7ui e% u%ing t,e ' o'e tie% and #et,od% o5 t,e !,a t object. A% we will %ee* t,i% object i% 7uite co#'le6* wit, a g eat #an( ' o'e tie%* #et,od%* and c,ild en. >e5o e 'lunging into a di%cu%%ion o5 t,e%e ite#%* we want to give an e6a#'le o5 c,a t c eation. 0,i% will 'ut ou 5utu e di%cu%%ion into %o#e 'e %'ective. /n 5act* we will ,ave %eve al occa%ion% to #a-e e5e ence to t,i% code in t,e %e7uel. Figure 21-'. he results o4 +reate+hart

1o* t,e CreateChart ' ocedu e %,own in E6a#'le 21;2 ' oduce% t,e c,a t in $igu e 21;5. /t #a( not be ' ett(* but it doe% illu%t ate #uc, o5 t,e c,a t #a-ing ' oce%%. Exa&!le 21-2. he +reate+hart Proce#ure
S0b CreateChart() . Create an e5bedded chart

Di5 co As Chart*bTect Di5 cw As Lon4= rh As Lon4 . :et data 6or 3ositionin4 chart cw = Col05ns(1).Hidth rh = "ows(1).?ei4ht . ;osition chart 0sin4 col05n width and row hei4ht 0nits Set co = ActiveSheet.Chart*bTects.Add(cw R B= rh R 7.&= cw R D= rh R 27) . 9a5e it co.9a5e = "Chart'xa53le" . Set chart t 3e co.Chart.ChartT 3e = xlLine . Add data series co.Chart.SeriesCollection.Add G So0rce-=ActiveSheet."an4e("A1-1N")= G "owcol-=xlCol05ns= SeriesLabels-=Tr0e= G Cate4or labels-=Tr0e . Add axes . (This is act0all the de6a0lt settin4= . b0t is added here 6or ill0stration) Hith co.Chart .?asAxis(xlCate4or = xl;ri5ar ) = Tr0e .?asAxis(xlCate4or = xlSecondar ) = >alse .?asAxis(xl+al0e= xl;ri5ar ) = Tr0e .?asAxis(xl+al0e= xlSecondar ) = >alse 'nd Hith . Axis title 6or5attin4 Hith co.Chart.Axes(xlCate4or ) .?asTitle = Tr0e .AxisTitle.Ca3tion = "T 3es" .AxisTitle.1order.Hei4ht = xlMedi05 'nd Hith Hith co.Chart.Axes(xl+al0e) .?asTitle = Tr0e Hith .AxisTitle .Ca3tion = ",0antit 6or 1EEE" .>ont.SiMe = N .*rientation = xl?oriMontal .Characters(1L= L).>ont.2talic = Tr0e .1order.Hei4ht = xlMedi05 'nd Hith 'nd Hith . Chan4e the cate4or na5es (T 3es) to lower case . (*n the wor8sheet the are in 033er case) co.Chart.Axes(xlCate4or ).Cate4or 9a5es = G Arra ("a"= "b"= "c"= "d"= "e") . Set the crossin4 3oint on the (3ri5ar ) val0e axis at &7 co.Chart.Axes(xl+al0e).CrossesAt = &7 . ?oriMontal b0t no vertical 4ridlines

co.Chart.Axes(xl+al0e).?asMaTor:ridlines = Tr0e co.Chart.Axes(xlCate4or ).?asMaTor:ridlines = >alse . *0tside Tic85ar8s on cate4or axis co.Chart.Axes(xlCate4or ).MaTorTic8Mar8 = xlTic8Mar8Cross . Move tic8 labels to below chart area co.Chart.Axes(xlCate4or ).Tic8Label;osition = G xlTic8Label;osition9extToAxis . Set chart area 6ill to solid white co.Chart.ChartArea.2nterior.Color = ":1(2&&= 2&&= 2&&) . Set 3lot area 6ill to 4ra co.Chart.;lotArea.2nterior.Color2ndex = 1& . >or5at chart title Hith co.Chart.ChartTitle .Ca3tion = ":reat Chart" .>ont.SiMe = 1L .>ont.1old = Tr0e .1order.Hei4ht = xlThic8 'nd Hith 'nd S0b

21.2.$ K-3r#er an# +hart3bject 3bjects


>e5o e loo-ing at t,e #ain ' o'e tie%* #et,od%* and c,ild en o5 t,e !,a t object* we can get one %i#'le* but i#'o tant* ite# out o5 t,e wa(. +a#el(* it i% 'o%%ible 5o two o #o e e#bedded c,a t% to ove la'* w,ic, ai%e% t,e 7ue%tion o5 ,ow to cont ol w,ic, c,a t object a''ea % on t,e to'. Eve ( !,a tObject object ,a% an o de * called it% 7-order* t,at indicate% t,e objectB% elative 'o%ition wit, e%'ect to an i#agina ( G;a6i% t,at co#e% di ectl( out o5 t,e #onito at ig,t angle%* towa d% t,e u%e * a% 'ictu ed in $igu e 21;6. Figure 21-5. /llustrating ?-or#er

0,e !,a tObject object ,a% a ead;onl( [O de ' o'e t( t,at i% u%ed to etu n t,e G;o de o5 t,e !,a tObject. /t al%o ,a% > ing0o$ ont and 1end0o>ac- #et,od% 5o c,anging t,e G;o de . 0,e%e ' o'e tie% can be u%ed to %,u55le t,e o de o5 !,a tObject object%.

'$%* Chart T/pes


Eac, E6cel c,a t ,a% eit,e a standard c,a t t('e o a custom c,a t t('e. /n addition* t,e e a e two t('e% o5 cu%to# c,a t t('e%: built;in and u%e ;de5ined. All c,a t t('e% a e acce%%ible b( t,e u%e t, oug, t,e !,a t 0('e dialog bo6 %,own in $igu e 21;7 ? ig,t;clic- a c,a t and c,oo%e !,a t 0('e@. Figure 21-6. he +hart y!e #ialog

21.".1 +hart y!e !ro!erty


0,e !,a t0('e ' o'e t( i% a ead;w ite ' o'e t( t,at can be %et to an( one o5 t,e #lChartT 3e con%tant% in 0able 21;1. able 21-1. +hart y!e +onstants Des ription Constant$Value% 3) A ea 6l3)A ea?;4098@ 3) 1tac-ed A ea 6l3)A ea1tac-ed?78@ 3) 100J 1tac-ed A ea 6l3)A ea1tac-ed100?79@ A ea 6lA ea?1@ 1tac-ed A ea 6lA ea1tac-ed?76@ 100J 1tac-ed A ea 6lA ea1tac-ed100?77@ 3) !lu%te ed >a 6l3)>a !lu%te ed?60@ 3) 1tac-ed >a 6l3)>a 1tac-ed?61@ 3) 100J 1tac-ed >a 6l3)>a 1tac-ed100?62@ !lu%te ed >a 6l>a !lu%te ed?57@

Type Area

,ar

,ubble Column

Cone

Cylin&er

Doug*nut Line

Pie

Pyrami&

1tac-ed >a 100J 1tac-ed >a >ubble >ubble wit, 3) e55ect% 3) !olu#n 3) !lu%te ed !olu#n 3) 1tac-ed !olu#n 3) 100J 1tac-ed !olu#n !lu%te ed !olu#n 1tac-ed !olu#n 100J 1tac-ed !olu#n !lu%te ed !one >a 1tac-ed !one >a 100J 1tac-ed !one >a 3) !one !olu#n !lu%te ed !one !olu#n 1tac-ed !one !olu#n 100J 1tac-ed !one !olu#n !lu%te ed !(linde >a 1tac-ed !(linde >a 100J 1tac-ed !(linde >a 3) !(linde !olu#n !lu%te ed !(linde !olu#n 1tac-ed !(linde !olu#n 100J 1tac-ed !(linde !olu#n )oug,nut E6'loded )oug,nut 3) 3ine 3ine 3ine wit, "a -e % 1tac-ed 3ine wit, "a -e % 100J 1tac-ed 3ine wit, "a -e % 1tac-ed 3ine 100J 1tac-ed 3ine 3) &ie E6'loded 3) &ie >a o5 &ie &ie E6'loded &ie &ie o5 &ie !lu%te ed &( a#id >a 1tac-ed &( a#id >a 100J 1tac-ed &( a#id >a 3) &( a#id !olu#n !lu%te ed &( a#id !olu#n 1tac-ed &( a#id !olu#n

6l>a 1tac-ed?58@ 6l>a 1tac-ed100?59@ 6l>ubble?15@ 6l>ubble3)E55ect?87@ 6l3)!olu#n?;4100@ 6l3)!olu#n!lu%te ed?54@ 6l3)!olu#n1tac-ed?55@ 6l3)!olu#n1tac-ed100?56@ 6l!olu#n!lu%te ed?51@ 6l!olu#n1tac-ed?52@ 6l!olu#n1tac-ed100?53@ 6l!one>a !lu%te ed?102@ 6l!one>a 1tac-ed?103@ 6l!one>a 1tac-ed100?104@ 6l!one!ol?105@ 6l!one!ol!lu%te ed?99@ 6l!one!ol1tac-ed?100@ 6l!one!ol1tac-ed100?101@ 6l!(linde >a !lu%te ed?95@ 6l!(linde >a 1tac-ed?96@ 6l!(linde >a 1tac-ed100?97@ 6l!(linde !ol?98@ 6l!(linde !ol!lu%te ed?92@ 6l!(linde !ol1tac-ed?93@ 6l!(linde !ol1tac-ed100?94@ 6l)oug,nut?;4120@ 6l)oug,nutE6'loded?80@ 6l3)3ine?;4101@ 6l3ine?4@ 6l3ine"a -e %?65@ 6l3ine"a -e %1tac-ed?66@ 6l3ine"a -e %1tac-ed100?67@ 6l3ine1tac-ed?63@ 6l3ine1tac-ed100?64@ 6l3)&ie?;4102@ 6l3)&ieE6'loded?70@ 6l>a O5&ie?71@ 6l&ie?5@ 6l&ieE6'loded?69@ 6l&ieO5&ie?68@ 6l&( a#id>a !lu%te ed?109@ 6l&( a#id>a 1tac-ed?110@ 6l&( a#id>a 1tac-ed100?111@ 6l&( a#id!ol?112@ 6l&( a#id!ol!lu%te ed?106@ 6l&( a#id!ol1tac-ed?107@

Tea3m-95Fly

Ra&ar

100J 1tac-ed &( a#id !olu#n Rada $illed Rada Rada wit, )ata "a -e % Aig,;3ow;!lo%e

6l&( a#id!ol1tac-ed100?108@ 6lRada ?;4151@ 6lRada $illed?82@ 6lRada "a -e %?81@ 6l1toc-A3!?88@ 6l1toc-OA3!?89@ 6l1toc-=A3!?90@ 6l1toc-=OA3!?91@ 6l1u 5ace?83@ 6l1u 5ace0o'=iew?85@ 6l1u 5ace0o'=iew4i e5 a#e?86@ 6l1u 5ace4i e5 a#e?84@ 6lDL1catte ?;4169@ 6lDL1catte 3ine%?74@ 6lDL1catte 3ine%+o"a -e %?75@ 6lDL1catte 1#oot,?72@ 6lDL1catte 1#oot,+o"a -e %?73@

Sto . /uotes

O'en;Aig,;3ow;!lo%e =olu#e;Aig,;3ow;!lo%e =olu#e;O'en;Aig,;3ow;!lo%e 3) 1u 5ace Surfa e 1u 5ace ?0o' =iew@ 1u 5ace ?0o' =iew@ wi e5 a#e@ 3) 1u 5ace ?wi e5 a#e@ 0- $S atter% 1catte 1catte wit, 3ine% 1catte wit, 3ine% and +o )ata "a -e % 1catte wit, 1#oot,ed 3ine% 1catte wit, 1#oot,ed 3ine% and +o )ata "a -e %

/n E6a#'le 21;3* at t,e end o5 t,e c,a'te * we ' e%ent a #ac o t,at %c oll% t, oug, t,e c,a t t('e% in 0able 21;1* allowing (ou to dete #ine w,ic, c,a t t('e i% a'' o' iate 5o a 'a ticula 'u 'o%e. +ote t,at it i% 'o%%ible t,at t,e etu n value o5 t,e !,a t0('e ' o'e t( #a( not be one o5 t,e value% in 0able 21;1. $o in%tance* t,e code:
Ms41ox ActiveChart.ChartT 3e

etu n% %L111 w,en a''lied to a c,a t wit, a 'a ticula u%e ;de5ined c,a t t('e. 0,i% value actuall( occu % onl( once in t,e E6cel object #odel. /t i% 'a t o5 t,e Constants enu# and i% a%%igned t,e %(#bolic na#e xlCo5bination. ?1ince t,i% %ee#% not to be docu#ented* / cannot %a( t,at t,i% i% t,e onl( value o5 !,a t0('e t,at i% not in 0able 21;1.@ +ote t,at eac, individual data %e ie% can ,ave a c,a t t('eH t,at i%* t,e 1e ie% object al%o ,a% a !,a t0('e ' o'e t(. /n t,i% wa(* i5 a c,a t ,a% two data %e ie%* eac, %e ie% can ,ave a di55e ent c,a t t('e. $o in%tance* one %e ie% can be 'lotted a% a line g a', and t,e ot,e a% a colu#n g a',. ?4e will di%cu%% 1e ie% object% late in t,e c,a'te .@ 21.".1.1 %!!ly+usto& y!e &etho# !ont a ( to it% na#e* t,e A''l(!u%to#0('e #et,od can a''l( eit,e a %tanda d o a cu%to# c,a t t('e to a c,a t. 0,e %(nta6 i%:

Chart/bject.A33l C0sto5T 3e(ChartTy e= Ty eName)


0,e ChartTy e 'a a#ete i% eit,e a %tanda d c,a t t('e con%tant 5 o# 0able 21;1 o one o5 t,e 5ollowing #lChart:aller con%tant%:
'n05 #lChart:aller xl10ilt2n = 21 xl<serDe6ined = 22

xlAn :aller 'nd 'n05

= 2B

?0,e te # !,a t.alle ( doe% not %ee# to a''ea in t,e E6cel docu#entation. Aoweve * in t,e 4o d object #odel* t,e te # 3i%t.alle ( e5e % to t,e object% t,at e' e%ent t,e t, ee tab% in t,e >ullet% and +u#be ing dialog bo6. E6t a'olating to E6cel* we #ig,t %u #i%e t,at t,e te # !,a t.alle ( e5e % to t,e two tab% in t,e !,a t 0('e dialog bo6 in $igu e 21;8.@ /5 !,a t0('e i% an #lChart:aller t,e na#e o5 t,e cu%to# c,a t t('e. $o in%tance* t,e 5ollowing code:
ActiveChart.A33l C0sto5T 3e G ChartT 3e-=xl10ilt2n= T 3e9a5e-="1l0e ;ie"

con%tant* t,en t,e o'tional Ty eName 'a a#ete %'eci5ie%

a''lie% t,e built;in c,a t t('e called >lue &ie. 0,i% i% e7uivalent to %electing >lue &ie in t,e E6cel dialog bo6 %,own in $igu e 21;8. Figure 21-7. /llustrating %!!ly+usto& y!e

A% anot,e e6a#'le* t,e 5ollowing code %et% t,e c,a t t('e to a u%e ;de5ined t('e na#ed )e5aultDL:
ActiveChart.A33l C0sto5T 3e G ChartT 3e-=xl<serDe6ined= T 3e9a5e-="De6a0lt#@"

'$%2 Children of the Chart Ob1ect


359

$igu e 21;9 %,ow% t,e c,ild en o5 t,e !,a t object. Figure 21-8. +hil#ren o4 the +hart object

An E6cel c,a t ,a% %eve al co#'onent%: a6e%* t,e c,a t a ea* a c,a t title* a data table* a 5loo ?5o a 3;);c,a t@* a 'lot a ea* and one o #o e data %e ie% ?wit, data label% and data value%@. 0,e%e co#'onent% a e e' e%ented b( t,e c,ild en o5 t,e !,a t object* w,ic, we will now e6a#ine.

'$%3 The Axes Collection


$igu e 21;10 %,ow% t,e 'o tion o5 t,e E6cel object #odel t,at elate% to c,a t a6e%. Figure 21-19. %xes-relate# objects

0,e !,a t object ,a% an A6e% collection t,at contain% an A6i% object 5o eac, a6i% in t,e c,a t. 0,e A6e% #et,od etu n% eit,e a %ingle a6i% o t,e A6e% collection 5o a c,a t. 0o etu n t,e A6e% collection 5o a c,a t* u%e t,e %(nta6:

Chart/bject.Axes
0o etu n a %'eci5ic A6i% object* u%e t,e %(nta6:

Chart/bject.Axes(Ty e= -xis:rou )
Ae e t,e o'tional Ty e 'a a#ete %'eci5ie% t,e a6i% to etu n. /t can be one o5 t,e 5ollowing #lAxisT 3e con%tant%:
'n05 #lAxisT 3e xlCate4or = 1 xl+al0e = 2 xlSeriesAxis = B 'nd 'n05

+ote t,at xlSeriesAxis i% valid onl( 5o 3;) c,a t%. 0,e o'tional -xis:rou 'a a#ete %'eci5ie% t,e a6i% g ou'. /t can be one o5 t,e 5ollowing #lAxis:ro03 con%tant%:
'n05 #lAxis:ro03 xl;ri5ar = 1 xlSecondar = 2 'nd 'n05 . The de6a0lt

A %econda ( a6i% i% a %econd ve tical o ,o iGontal a6i% u'on w,ic, a %econd value %e ie% ?ve tical ca%e@ o catego ( %e ie% ?,o iGontal ca%e@ i% 'lotted ?%ee $igu e 21;1@. /5 t,i% a gu#ent i% o#itted* t,e ' i#a ( g ou' i% u%ed. +ote t,at 3;) c,a t% ,ave onl( one a6i% g ou'.

0,e !,a t object ,a% a ead;w ite ' o'e t( na#ed Aa%A6i% t,at dete #ine% w,et,e o not t,e c,a t displays va iou% t('e% o5 a6e%. Aoweve * it i% i#'o tant to note t,at t,i% #et,od will 5ail i5 t,e c,a t doe% not (et ,ave t,e co e%'onding data %e ie%. $o in%tance* i5 (ou a e c eating a c,a t 5 o# %c atc,* it #ig,t be natu al to add t,e code to c eate a catego ( a6i% be5o e adding t,e catego ( data t,at will be 'lotted again%t t,at a6i%. !his not to do ?to 7uote Aa#let@C /n 5act* e5e ing to t,e CreateChart e6a#'le ' ocedu e ea lie in t,e c,a'te * i5 we eve %e t,e data %e ie% code and t,e a6e%; elated code* c,anging t,i%:
. Add data series 0sin4 data on the sheet itsel6 co.Chart.SeriesCollection.Add G So0rce-=ActiveSheet."an4e("A1-1N")= G "owcol-=xlCol05ns= SeriesLabels-=Tr0e= G Cate4or labels-=Tr0e . Add axes Hith co.Chart .?asAxis(xlCate4or = xl;ri5ar ) = Tr0e .?asAxis(xlCate4or = xlSecondar ) = >alse .?asAxis(xl+al0e= xl;ri5ar ) = Tr0e .?asAxis(xl+al0e= xlSecondar ) = >alse 'nd Hith

to t,i%:
. Add axes Hith co.Chart .?asAxis(xlCate4or = xl;ri5ar ) = Tr0e .?asAxis(xlCate4or = xlSecondar ) = >alse .?asAxis(xl+al0e= xl;ri5ar ) = Tr0e .?asAxis(xl+al0e= xlSecondar ) = >alse 'nd Hith . Add data series 0sin4 data on the sheet itsel6 co.Chart.SeriesCollection.Add G So0rce-=ActiveSheet."an4e("A1-1N")= G "owcol-=xlCol05ns= SeriesLabels-=Tr0e= G Cate4or labels-=Tr0e

E6cel will i%%ue t,e co#'letel( u%ele%% e o #e%%age: 8"et,od BAa%A6i%B o5 object BI!,a tB ,a% 5ailed.8 ?2n5o tunatel(* a% i% all too o5ten t,e ca%e* t,e docu#entation doe% not di%cu%% t,i% i%%ue at all.@ 0,e %(nta6 5o t,e Aa%A6i% ' o'e t( i%:

Chart/bject.?asAxis(1ndex#= 1ndex$)
w,e e t,e 'a a#ete %* de%'ite t,ei gene ic na#e%* co e%'ond di ectl( to t,e Ty e and -xis:rou 'a a#ete % o5 t,e A6e% #et,od. $o in%tance* t,e 5ollowing code di%'la(% a ' i#a ( catego ( a6i% and bot, ' i#a ( and %econda ( value a6e% 5o t,e active c,a t:
Hith ActiveChart .?asAxis(xlCate4or = xl;ri5ar ) = Tr0e .?asAxis(xlCate4or = xlSecondar ) = >alse .?asAxis(xl+al0e= xl;ri5ar ) = Tr0e .?asAxis(xl+al0e= xlSecondar ) = Tr0e 'nd Hith

362

'$%8 The Axis Ob1ect


0able 21;2 %,ow% t,e ' o'e tie% and #et,od% o5 t,e A6i% object. A% (ou can %ee* A6i% object% a e 5ai l( involved in t,ei own ig,t. $o tunatel(* #o%t o5 t,e #e#be % in 0able 21;2 a e %el5 e6'lanato (* %o we con%ide t,e# onl( b ie5l(. +ote t,at #o%t o5 t,e%e #e#be % co e%'ond to t,e #( iad c,ec- bo6e% and edit bo6e% in t,e 5ive tab% o5 t,e E6cel $o #at A6i% dialog bo6. able 21-2. -e&bers o4 the %xis 3bject A''lication Aa%)i%'la(2nit3abelSv9Z "ino 0ic-"a A6i%>etween!atego ie% Aa%"ajo . idline% "ino 2nit A6i%. ou' Aa%"ino . idline% "ino 2nit/%Auto A6i%0itle Aa%0itle "ino 2nit1cale >a%e2nit Aeig,t &a ent >a%e2nit/%Auto 3e5t Reve %e&lotO de >o de "ajo . idline% 1cale0('e !atego (+a#e% "ajo 0ic-"a 1elect !atego (0('e "ajo 2nit 0ic-3abel&o%ition ! eato "ajo 2nit/%Auto 0ic-3abel% ! o%%e% "ajo 2nit1cale 0ic-3abel1'acing ! o%%e%At "a6i#u#1cale 0ic-"a -1'acing )elete "a6i#u#1cale/%Auto 0o' )i%'la(2nitSv9Z "ini#u#1cale 0('e )i%'la(2nit!u%to#Sv9Z "ini#u#1cale/%Auto 4idt, )i%'la(2nit3abelSv9Z "ino . idline%

21.5.1 %xis:rou! Pro!erty


0,i% ead;onl( ' o'e t( etu n% t,e g ou' 5o t,e %'eci5ied a6i%. /t can be eit,e xl;ri5ar o xlSecondar ?T2@. ?T1@

21.5.2 %xis itles an# heir For&atting


0,e A6i%0itle ' o'e t( etu n% an A6i%0itle object t,at e' e%ent% t,e title o5 t,e %'eci5ied a6i%. 21.5.2.1 Cas itle !ro!erty ;,H0 .oolean< >e5o e we can 5o #at an a6i% title* we #u%t tell E6cel t,at t,e a6i% ,a% a title u%ing t,e Aa%0itle ' o'e t(:
Axis*bTect.?asTitle = Tr0e

0,e A6i%0itle object ,a% %eve al ' o'e tie%* t,e #o%t ' o#inent o5 w,ic, a e t,e 5ollowing: Auto/cale&ont 1cale% t,e label te6t 5ont %iGe i% auto#aticall( w,en t,e c,a t %iGe c,ange% w,en t,i% ' o'e t( i% Tr0e. 0,e de5ault value i% Tr0e.

Border Retu n% a >o de object t,at can be u%ed to %et t,e colo * line %t(le* and widt, o5 t,e bo de o5 t,e a6i% title. (aption 1et% t,e te6t 5o t,e title. (haracters Retu n% a !,a acte % object* w,ic, i% a ange o5 c,a acte % in t,e ca'tion. 0,i% allow% u% to 5o #at a portion o5 t,e te6t in t,e ca'tion. &ill Retu n% a !,a t$ill$o #at object u%ed to %et 5ill;5o #atting ' o'e tie% 5o t,e a6i% title. &ont Retu n% a $ont object t,at can be u%ed to %et t,e 5ont c,a acte i%tic% o5 t,e label%. Jori7ontalAlignment and VerticalAlignment 1et% t,e align#ent o5 a6i% title te6t. nterior Retu n% an /nte io object t,at can be u%ed to 5o #at t,e inte io o5 t,e a6i% title a ea. ?umber&ormat 1et% t,e nu#be 5o #at code 5o t,e label%. 0,i% ' o'e t( etu n% 90ll i5 all label% do not ,ave t,e %a#e nu#be 5o #at. +ote t,at %ince t,e 5o #at code% a e t,e %a#e a% t,o%e u%ed b( E6cel to 5o #at wo -%,eet cell%* we can u%e t,e #ac o eco de to get a'' o' iate 5o #at code%. $rientation Retu n% o %et% t,e o ientation 5o t,e a6i% title. /t can be an( one o5 t,e 5ollowing con%tant%:
'n05 #l*rientation xl<3ward = %L1F1 xlDownward = %L1F7 xl+ertical = %L1NN xl?oriMontal = %L12D 'nd 'n05

3et u% ta-e a clo%e loo- at t,e !,a acte % object. A !,a acte % object e' e%ent% a contiguous 'o tion o5 te6t wit,in a te6t %t ing. 0,e !,a acte % ' o'e t( etu n% a !,a acte % object. ?+ote t,at t,e !,a acte % ' o'e t( al%o a''lie% to t,e !,a t0itle object and t,e Range object.@ 0,e %(nta6 o5 t,e !,a acte % ' o'e t( i%:

-xisTitle/bject.Characters(start= len!th)
w,e e start i% t,e %ta t c,a acte nu#be and len!th i% t,e nu#be o5 c,a acte % to etu n in t,e !,a acte % object. 4,en %ta t i% #i%%ing* it i% a%%u#ed to be e7ual to 1* and w,en lengt, i% #i%%ing* all c,a acte % a5te t,e %ta ting c,a acte a e included. 0o illu%t ate* t,e 5ollowing code c eate% a title 5o t,e ' i#a ( value a6i% and italiciGe% t,e wo d 8billion%.8 ?+ote t,e %etting o5 t,e Aa%0itle ' o'e t(* to avoid an e o #e%%age.@
Hith ActiveChart.Axes(xl+al0e= xl;ri5ar ) .?asTitle = Tr0e .AxisTitle.Text = "These are billions" .AxisTitle.Characters(11= D).>ont.2talic = Tr0e 'nd Hith

O5 cou %e* i5 we wanted to italiciGe t,e enti e title* we could %i#'l( u%e t,e $ont ' o'e t( o5 t,e A6i%0itle object* a% in:
ActiveChart.Axes(xl+al0e= xl;ri5ar ). G AxisTitle.>ont.2talic = Tr0e

21.5.2.2 he .or#er !ro!erty an# the .or#er object 0,i% ' o'e t( etu n% a >o de object t,at e' e%ent% t,e bo de o5 t,e object. 0,e >o de object can be u%ed to %et t,e colo * line %t(le* and weig,t o5 t,e bo de o5 an object* %uc, a% an a6i% title. 0,e >o de object ,a% no #et,od%. /t% #ain ' o'e tie% a e !olo * !olo /nde6* 3ine1t(le* and 4eig,t. 0,e !olo ' o'e t( can be %et to an( R.> value. $o in%tance* t,e 5ollowing code %et% t,e #ajo a6i% colo to blue:
ActiveChart.Axes(xlCate4or = xl;ri5ar ). G MaTor:ridlines.1order.Color = ":1(7= 7= 2&&)

0,e !olo /nde6 ' o'e t( can be u%ed to %et t,e colo via a colo 'alette. $o #o e in5o #ation* including t,e !olo /nde6 value%* %ee t,e ,el' docu#entation. 0,e 3ine1t(le ' o'e t( can ta-e on an( o5 t,e 5ollowing value%:
'n05 #lLineSt le xlLineSt le9one = %L1L2 xlDo0ble = %L11E xlDot = %L11D xlDash = %L11& xlContin0o0s = 1 xlDashDot = L xlDashDotDot = & xlSlantDashDot = 1B 'nd 'n05

0,e 4eig,t ' o'e t( can be %et to one o5 t,e 5ollowing #L1orderHei4ht con%tant%:
'n05 #l1orderHei4ht xlMedi05 = %L1BD xl?airline = 1

xlThin = 2 xlThic8 = L 'nd 'n05

0o 5u t,e illu%t ate a6i%;title 5o #atting* ,e e i% t,e elevant code 5 o# t,e (reate(hart ' ocedu e:
. Axis 6or5attin4 Hith co.Chart.Axes(xlCate4or ) .?asTitle = Tr0e .AxisTitle.Ca3tion = "T 3es" .AxisTitle.1order.Hei4ht = xlMedi05 'nd Hith Hith co.Chart.Axes(xl+al0e) .?asTitle = Tr0e Hith .AxisTitle .Ca3tion = ",0antit 6or 1EEE" .>ont.SiMe = N .*rientation = xl?oriMontal .Characters(1L= L).>ont.2talic = Tr0e .1order.Hei4ht = xlMedi05 'nd Hith 'nd Hith

21.5." +ategoryNa&es Pro!erty


0,i% ' o'e t( etu n% o %et% t,e catego ( na#e% 5o t,e a6i%. /t can be %et to eit,e an a a( o a Range object t,at contain% t,e catego ( na#e%. $o in%tance* t,e code:
ActiveChart.Axes(xlCate4or = xlSecondar ). G Cate4or 9a5es = Arra ("*ne"= "Two"= "Three"= ">o0r"= ">ive"= "Six")

c,ange% t,e label% on t,e u''e ,o iGontal a6i% to 8One8* 80wo8* 80, ee8* etc.

21.5.$ +ategory y!e Pro!erty an# .ase2nit Pro!erty


0,e !atego (0('e ' o'e t( etu n% o %et% t,e t('e 5o a catego ( a6i%. ?/t a''lie% onl( to catego (;t('e a6e%.@ /t can be one o5 t,e 5ollowing #lCate4or T 3e con%tant%:
'n05 #lCate4or T 3e xlA0to5aticScale = %L17& xlCate4or Scale = 2 xlTi5eScale = B 'nd 'n05

0,e de5ault i% xlA0to5aticScale. 4,en t,e !atego (0('e ' o'e t( 5o t,e a6i% i% %et to xlTi5eScale* t,e >a%e2nit ' o'e t( etu n% o %et% t,e ba%e unit 5o t,e %'eci5ied catego ( a6i%. /t can be one o5 t,e 5ollowing #lTi5e<nit con%tant%:
'n05 #lTi5e<nit xlDa s = 7 xlMonths = 1

xl@ears = 2 'nd 'n05

+ote t,at t,e value o5 t,i% ' o'e t( ta-e% e55ect onl( w,en t,e !atego (0('e ' o'e t( 5o t,e a6i% i% %et to xlTi5eScale* even t,oug, t,e value can be c,anged at an( ti#e.

21.5.' +rosses an# +rosses%t Pro!erties


0,e ! o%%e% ' o'e t( etu n% o %et% t,e 'oint on t,e %'eci5ied a6i% at w,ic, t,e 8ot,e 8 a6i% c o%%e% <t,at i% ?we %u #i%e@* t,e ot,e a6i% t,at i% in t,e same axis group. 0,u%* i5 t,e %'eci5ied a6i% i% a ' i#a ( a6i%* %o i% t,e ot,e a6i%. /5 t,e %'eci5ied a6i% i% a %econda ( a6i%* %o i% t,e ot,e a6i%. $o in%tance* t,e code:
ActiveChart.Axes(xl+al0e= xl;ri5ar ).Crosses = xlAxisCrossesMaxi505

in%t uct% t,e ' i#a ( catego ( a6i% ?t,e ot,e a6i%@ to c o%% t,e ' i#a ( value a6i% ?t,e %'eci5ied a6i%@ at t,e #a6i#u# 'oint o5 t,e ' i#a ( value a6i% ?t,e %'eci5ied a6i%@. 0,e ! o%%e% ' o'e t( can a%%u#e one o5 t,e 5ollowing #lAxisCrosses con%tant%:
'n05 #lAxisCrosses xlAxisCrossesC0sto5 = %L11L xlAxisCrossesA0to5atic = %L17& xlAxisCrossesMaxi505 = 2 xlAxisCrossesMini505 = L 'nd 'n05

4,en %et to xlAxisCrossesA0to5atic* E6cel dete #ine% t,e c o%%ing 'oint. 4,en %et to xlMini505 * t,e ot,e a6i% c o%%e% at t,e #ini#u# value w,en t,e %'eci5ied a6i% i% a value a6i% o t,e 5a ;le5t catego ( w,en t,e %'eci5ied a6i% i% a catego ( a6i%. 4,en %et to xlMaxi505 * t,e ot,e a6i% c o%%e% at t,e #a6i#u# value w,en t,e %'eci5ied a6i% i% a value a6i% o t,e 5a ; ig,t catego ( w,en t,e %'eci5ied a6i% i% a catego ( a6i%. ?2n5o tunatel(* t,i% ' o'e t( doe% not alwa(% %ee# to act a% adve ti%ed. /n 'a ticula * / ,ave noticed t,at %etting t,i% ' o'e t( to xlMini505 doe% not alwa(% adju%t t,e c o%%ing 'oint ' o'e l(.@ $inall(* t,e xlAxisCrossesC0sto5 %etting a''lie% onl( to value a6e%* in w,ic, ca%e w,en %et to xlAxisCrossesC0sto5 * t,e ! o%%e%At ' o'e t( dete #ine% t,e c o%%ing 'oint 5o t,e ot,e ?catego (@ a6i%. +ote t,at %etting t,e ! o%%e%At ' o'e t( auto#aticall( %et% t,e ! o%%e% ' o'e t( to xlAxisCrossesC0sto5 . $o in%tance* e5e ing to t,e (reate(hart ' ocedu e* t,e code:
co.Chart.Axes(xl+al0e).CrossesAt = &7

cau%e% t,e catego ( a6i% to c o%% t,e value a6i% at 50. $inall(* note t,at t,e%e ' o'e tie% do not a''l( to 3;) c,a t%.

21.5.5 (is!lay 2nits


+ew to E6cel 9 a e t,e di%'la( unit; elated ' o'e tie% Aa%)i%'la(2nit3abel* )i%'la(2nit3abel* )i%'la(2nit* and )i%'la(2nit!u%to#* and t,e )i%'la(2nit3abel object. 0,e%e ' o'e tie% can be u%ed to di%'la( a unitB% legend 5o a c,a t a6i%. 0,i% i% u%e5ul w,en dealing wit, label% t,at contain ve ( la ge nu#be %. $o in%tance* i5 a %e ie% o5 label% ,a% t,e 5o # 1000000* 2000000* 3000000*

Tea3m69 77l'D

and %o on* we can c eate a legend wit, t,e wo d 8#illion%8 and t,en c,ange t,e label% to 1* 2* 3* . . . . A% an e6a#'le* t,e code:
S0b Dis3la <nitLabel() Hith ActiveChart.Axes(xl+al0e) .Dis3la <nit = xlMillions .?asDis3la <nitLabel = Tr0e Hith .Dis3la <nitLabel .Ca3tion = "5illions" .>ont.SiMe = 1L 'nd Hith 'nd Hith 'nd S0b

' oduce% t,e c,a t in $igu e 21;11. +ote t,at t,e value a6i% i% labeled wit, #illion% a% t,e legend indicate%. Figure 21-11. (is!lay unit labels

21.5.6 :ri#line-,elate# Pro!erties an# the :ri#line 3bject


0,e A6i% object ,a% %eve al ' o'e tie% t,at elate to g idline%H t,e( a e de%c ibed in t,e 5ollowing li%t: JasMa%or-ridlines 1et t,i% ead;w ite ' o'e t( to Tr0e to %,ow #ajo g idline% 5o t,e a6i%* o >alse to ,ide g idline%. A''lie% onl( to ' i#a ( a6e% ?not %econda ( a6e%@. JasMinor-ridlines 1et t,i% ead;w ite ' o'e t( to Tr0e to %,ow #ino g idline% 5o t,e a6i%* o >alse to ,ide g idline%. A''lie% onl( to ' i#a ( a6e% ?not %econda ( a6e%@. Ma%or-ridlines

0,i% ead;onl( ' o'e t( etu n% a . idline% object t,at e' e%ent% t,e #ajo g idline% 5o t,e %'eci5ied a6i%. A''lie% onl( to ' i#a ( a6e% ?not %econda ( a6e%@. Minor-ridlines 0,i% ead;onl( ' o'e t( etu n% a . idline% object t,at e' e%ent% t,e #ino g idline% 5o t,e %'eci5ied a6i%. A''lie% onl( to ' i#a ( a6e% ?not %econda ( a6e%@. +ote t,at t,e . idline% object i% not a collection objectH t,at i%* t,e e i% no . idline object. /n%tead* t,e ' o'e tie% o5 t,e . idline% object a''l( to all o5 t,e g idline% 5o t,e a6i%. $o in%tance* t,e 5ollowing code add% #ajo g idline% to t,e catego ( a6i% and 5o #at% t,e%e line%:
ActiveChart.Axes(xlCate4or = xl;ri5ar ). G ?asMaTor:ridlines = Tr0e Hith ActiveChart.Axes(xlCate4or = xl;ri5ar ). G MaTor:ridlines.1order .Color = ":1(7= 7= 2&&) .LineSt le = xlDot .Hei4ht = xlThic8 'nd Hith

21.5.7 Position- an# (i&ension-,elate# Pro!erties


0,e A6i% object ,a% t,e 5ollowing ' o'e tie% elated to it% 'o%ition and di#en%ion%. 0,e%e ' o'e tie% a e ead;onl(. Jeight Retu n% t,e ,eig,t o5 t,e a6i% in 'oint%. Width Retu n% t,e widt, o5 t,e a6i% in 'oint%. Left Retu n% t,e di%tance 5 o# t,e le5t edge o5 t,e a6i% to t,e le5t edge o5 t,e c,a t a ea. !op Retu n% t,e di%tance 5 o# t,e to' edge o5 t,e a6i% to t,e to' o5 t,e c,a t a ea. ?+ote t,at t,e widt, o5 a ve tical a6i% i% 0 and t,e ,eig,t o5 a ,o iGontal a6i% i% 0.@

21.5.8 ic1 -ar1-,elate# Pro!erties


0,e A6i% object ,a% %eve al ' o'e tie% elated to tic- #a -%. 0,e "ajo 0ic-"a - ' o'e t( etu n% o %et% t,e t('e o5 #ajo tic- #a - 5o t,e %'eci5ied a6i%* and t,e "ino 0ic-"a - ' o'e t( doe% t,e %a#e 5o #ino tic- #a -%. Eac, o5 t,e%e ' o'e tie% can a%%u#e an( value 5 o# t,e 5ollowing enu#:
'n05 #lTic8Mar8 xlTic8Mar89one = %L1L2 xlTic8Mar82nside = 2 xlTic8Mar8*0tside = B

xlTic8Mar8Cross = L 'nd 'n05

0,e 0ic-"a -1'acing ' o'e t( etu n% o %et% t,e nu#be o5 catego ie% o %e ie% between tic#a -%. 0,i% a''lie% onl( to catego ( and %e ie% a6e% ?5o 3;) c,a t%@. 0o %et t,e tic- #a - %'acing on a value a6i%* we can u%e t,e "ajo 2nit and "ino 2nit ' o'e tie% ?de%c ibed late in t,i% c,a'te @. 0,e e a e al%o %eve al ' o'e tie% elated to tic-;#a - label%. 0,e 0ic-3abel% ' o'e t( etu n% a 0ic-3abel% object t,at e' e%ent% t,e tic-;#a - label% 5o t,e %'eci5ied a6i%. 0,e 0ic-3abel&o%ition ' o'e t( etu n% o %et% t,e 'o%ition o5 tic-;#a - label% on t,e %'eci5ied a6i%. /t can be one o5 t,e 5ollowing #lTic8Label;osition con%tant%:
'n05 #lTic8Label;osition xlTic8Label;osition9one = %L1L2 xlTic8Label;ositionLow = %L1BL xlTic8Label;osition?i4h = %L12F xlTic8Label;osition9extToAxis = L 'nd 'n05

0,e 0ic-3abel1'acing ' o'e t( etu n% o %et% t,e nu#be o5 catego ie% o %e ie% between tic- #a - label%. 0,i% ' o'e t( a''lie% onl( to catego ( and %e ie% a6e% ?5o 3;) c,a t%@. +ote t,at E6cel dete #ine% label %'acing on all value a6e%. 21.5.8.1 he ic1)abels object 0,e 0ic-3abel% object e' e%ent% t,e %et o5 tic-;#a - label% 5o an a6i%. +ote t,at t,i% i% not a collection objectH t,at i%* t,e e i% no 0ic-3abel object. 0,u%* t,e ' o'e tie% o5 t,e 0ic-3abel% object a55ect all o5 t,e label% 5o an a6i% %i#ultaneou%l(. 0,e 0ic-3abel% object ,a% %eve al ' o'e tie%* t,e #o%t ' o#inent o5 w,ic, a e t,e 5ollowing: &ont Retu n% a $ont object t,at can be u%ed to %et t,e 5ont c,a acte i%tic% o5 t,e label%. Auto/cale&ont 1cale% t,e label te6t 5ont %iGe auto#aticall( w,en t,e c,a t %iGe c,ange% w,en t,i% ' o'e t( i% Tr0e. 0,e de5ault value i% Tr0e. ?umber&ormat 1et% t,e nu#be ;5o #at code 5o t,e label%. 0,i% ' o'e t( etu n% 90ll i5 all label% do not ,ave t,e %a#e nu#be 5o #at. +ote t,at %ince t,e 5o #at code% a e t,e %a#e a% t,o%e u%ed b( E6cel to 5o #at wo -%,eet cell%* we can u%e t,e #ac o eco de to get a'' o' iate 5o #at code%. $rientation Retu n% o %et% t,e o ientation 5o t,e label% and can be an( one o5 t,e 5ollowing con%tant%:
'n05 #lTic8Label*rientation xlTic8Label*rientation<3ward = %L1F1

xlTic8Label*rientationDownward = %L1F7 xlTic8Label*rientation+ertical = %L1NN xlTic8Label*rientation?oriMontal = %L12D xlTic8Label*rientationA0to5atic = %L17& 'nd 'n05

21.5.19 2nits-,elate# Pro!erties


0,e A6i% object ,a% %eve al ' o'e tie% elated to %etting unit% and t,e %cale 5acto on t,e a6i%. Ma%or'nit and Minor'nit Retu n% o %et% ?a% a )ouble@ t,e #ajo unit% o #ino unit% 5o t,e %'eci5ied a6i%. 1etting t,i% ' o'e t( %et% t,e co e%'onding "ajo 2nit/%Auto o "ino 2nit%/%Auto ' o'e t( to >alse. Ma%or'nit sAuto and Minor'nit sAuto !alculate% t,e #ajo unit% o #ino unit% 5o t,e a6i% i5 Tr0e. 0,e%e ' o'e tie% a e eadEw ite >oolean. Ma%or'nit/cale and Minor/cale'nit Retu n% o %et% t,e #ajo unit %cale value o #ino unit %cale value 5o t,e catego ( a6i% w,en t,e !atego (0('e ' o'e t( i% %et to xlTi5eScale. /t can be one o5 t,e 5ollowing #lTi5e<nit con%tant%:
'n05 #lTi5e<nit xlDa s = 7 xlMonths = 1 xl@ears = 2 'nd 'n05

Maximum/cale and Minimum/cale Retu n% o %et% t,e #a6i#u# o #ini#u# value on t,e a6i% a% a )ouble. Maximum/cale sAuto and Minimum/cale sAuto !alculate% t,e #a6i#u# value o #ini#u# value 5o t,e a6i% i5 Tr0e . 0,i% ' o'e t( i% eadEw ite >oolean. /cale!ype Retu n% o %et% t,e value a6i% %cale t('eH t,i% ' o'e t( a''lie% onl( to value a6e%. /t can be one o5 t,e 5ollowing #lScaleT 3e con%tant%:
'n05 #lScaleT 3e xlScaleLo4arith5ic = %L1BB xlScaleLinear = %L1B2 'nd 'n05 .Co55on lo4arith5

0o illu%t ate* t,e 5ollowing code:


Hith ActiveChart.Axes(xl+al0e= xl;ri5ar )

.MaTor<nit = 1 .Maxi505Scale = & 'nd Hith

will cau%e t,e a6i% to a''ea a% on t,e le5t in $igu e 21;12. On t,e ot,e ,and* t,e code:
Hith ActiveChart.Axes(xl+al0e= xl;ri5ar ) .MaTor<nit = & .Maxi505Scale = B 'nd Hith

e%ult% in t,e a6i% %,own on t,e ig,t in $igu e 21;12. Figure 21-12. %xis units an# scale

21.5.11 ,eversePlot3r#er Pro!erty


0,i% eadEw ite >oolean ' o'e t( can be %et to Tr0e to ,ave E6cel eve %e t,e di ection o5 an a6i% ?alt,oug, t,e na#e i% not ve ( de%c i'tive o5 t,e 5unction@. $o in%tance* i5 t,e active c,a t i% t,e c,a t in $igu e 21;13* t,en t,e code:
ActiveChart.Axes(xl+al0e= xl;ri5ar )."everse;lot*rder = Tr0e

c,ange% t,i% c,a t to t,e one in $igu e 21;14* w,e e t,e ' i#a ( a6i% data i% 'lotted 5 o# t,e to' down ?%o to %'ea-@. Figure 21-1". /llustrating ,eversePlot3r#er ;be4ore<

Figure 21-1$. /llustrating ,eversePlot3r#er ;a4ter<

21.5.12 y!e Pro!erty


0,e 0('e ' o'e t( etu n% o %et% t,e a6i% t('e. /t can be one o5 t,e 5ollowing #lAxisT 3e con%tant%:
'n05 #lAxisT 3e xlCate4or = 1 xl+al0e = 2 xlSeriesAxis = B 'nd 'n05

'$%9 The ChartArea Ob1ect


0,e !,a tA ea object e' e%ent% t,e c,a t a ea o5 a c,a t. A% %,own in $igu e 21;1* t,e c,a t a ea on a 2;) c,a t contain% e%%entiall( t,e enti e c,a t: t,e a6e%* c,a t title* a6i% title%* and legend. On t,e ot,e ,and* t,e c,a t a ea on a 3;) c,a t contain% t,e c,a t title and t,e legend but doe% not include t,e 'lot a ea* w,e e t,e data i% 'lotted.

373

0,e !,a tA ea object ,a% %eve al c,ild en<>o de * !,a t$ill$o #at* $ont* and /nte io <t,at can be u%ed to %et t,e c,a acte i%tic% o5 t,e enti e c,a t. ?0,e%e object% ,ave been di%cu%%ed ea lie * in connection wit, a6i% title%.@ 2n5o tunatel(* t,e docu#entation doe% not clea l( de5ine t,e di55e ence% between t,e !,a t$ill$o #at object ?w,ic, e' e%ent% 5ill 5o #atting 5o c,a t ele#ent%@ and it% u%e and between t,e /nte io object and it% u%e. 0,u%* %o#e e6'e i#enting i% in o de . 4,ile bot, o5 t,e%e object% ,ave a &atte n ' o'e t( in co##on* t,e( %ee# gene all( to ,ave di55e ent ' o'e tie% and #et,od%. 0,e !,a t$ill$o #at object a''ea % to elate #o e to g adient and te6tu e 5ill%* w,e ea% t,e /nte io object %ee#% to elate #o e to %olid 5ill% and 'atte n 5ill%. 0o illu%t ate* t,e 5ollowing line %et% t,e inte io o5 a c,a t a ea to a %olid colo ?!olo /nde6 3 i% ed@:
ActiveChart.ChartArea.2nterior.Color2ndex = B

0,e 5ollowing code c eate% a g adient 'atte n in t,e c,a t a ea* c,anging 5 o# ed at t,e to' to violet at t,e botto#:
Hith ActiveChart.ChartArea.>ill .+isible = Tr0e .>oreColor.Sche5eColor = B .1ac8Color.Sche5eColor = F .TwoColor:radient St le-=5so:radient?oriMontal= +ariant-=1 'nd Hith

'$%< The Chart5roup Ob1ect


A% (ou no doubt -now* an E6cel c,a t can contain #o e t,an one data %e ie%. 4e ,ave e#a -ed t,at eac, %e ie% ?t,at i%* t,e 1e ie% object@ ,a% a !,a t0('e ' o'e t( t,at can be u%ed to %et t,e c,a t t('e o5 t,e %e ie%. 0,u%* a %ingle c,a t #a( ,ave one o #o e %e ie% wit, a colu#n;t('e 5o #at and one o #o e %e ie% wit, a line;t('e 5o #at. A !,a t. ou' object e' e%ent% one o #o e %e ie% t,at a e 'lotted on a c,a t wit, t,e %a#e c,a t t('e. +ote t,at a !,a t. ou' object i% not a collection. 0o acce%% t,e individual 1e ie% object% e' e%ented b( a !,a t. ou'* we #u%t u%e t,e 1e ie%!ollection ' o'e t( ?di%cu%%ed late in t,i% c,a'te @. 0,e !,a t. ou' object% 5o a %ingle c,a t a e %to ed in t,e !,a t. ou'% collection 5o t,e !,a t object. 0,i% collection i% acce%%ed u%ing t,e !,a t. ou'% ' o'e t(. 0,e E6cel object #odel ' ovide% a wa( to get 8%ubcollection%8 o5 t,e !,a t. ou'% collection t,at co e%'ond to t,e #ajo c,a t t('e% ?line* colu#n* ba * etc.@. 0o illu%t ate* t,e !olu#n. ou'% #et,od a''lie% to a 2;) c,a t and etu n% t,e collection o5 all !,a t. ou' object% t,at co e%'ond to t,e va iou% colu#n;t('e 5o #at%. 0,e %(nta6 i%:

Chart/bject.Col05n:ro03s
4e can al%o acce%% a %ingle !,a t. ou' in t,i% collection u%ing t,e %(nta6:

Chart/bject.Col05n:ro03s(1ndex)

+ote* ,oweve * t,at t,e e i% no !olu#n!,a t. ou'% collection 'e %e. 0,e !olu#n. ou'% #et,od actuall( etu n% a !,a t. ou'% collection* but not t,e 5ull collection t,at would be etu ned b(:

Chart/bject.Chart:ro03s
0o illu%t ate* t,e c,a t in $igu e 21;15 ,a% two %e ie%. 4,ile eac, %e ie% ,a% a line t('e* t,e %ubt('e% a e di55e ent. One %e ie% ,a% been 5o #atted wit, a line t('e wit, no data 'oint #a -e %* w,e ea% t,e ot,e ,a% data #a -e %. Acco dingl(* t,e code:
ActiveChart.Line:ro03s.Co0nt

etu n% t,e value 2* %ince t,e e a e two di%tinct !,a t. ou' object% t,at 5it in t,e 3ine. ou'% collection. Figure 21-1'. /llustrating chart grou!s

0,e !,a t object ,a% t,e 5ollowing #et,od% t,at etu n co e%'onding collection% o5 !,a t. ou' object%:
C C C C C C C C

A ea. ou'% >a . ou'% !olu#n. ou'% )oug,nut. ou'% 3ine. ou'% &ie. ou'% Rada . ou'% DL. ou'%

0,e e a e al%o %o#e #e#be % o5 t,e !,a t object t,at etu n c,a t g ou'% 5o 3;) c,a t%. 0,e( a e: A ea3). ou'* >a 3). ou'* !olu#n3). ou'* 3ine3). ou'* &ie3). ou'* and 1u 5ace. ou'. 0,e%e #e#be % a e %ingula becau%e t,e( etu n a %ingle !,a t. ou' object. 0,e 'o tion o5 t,e E6cel object #odel t,at elate% to !,a t. ou' object% i% %,own in $igu e 21;16. Figure 21-15. he +hart:rou! object

21.7.1 2!.ars an# (own.ars


2'>a % and )own>a % a e %,own in $igu e 21;17. 0,e%e ba % give a 7uic- indication o5 t,e di55e ence between data value% in two di55e ent data %e ie% o5 line t('e. 0,e 2'>a % a e in w,ite and )own>a % a e in blac-. 0,e code to gene ate t,e%e ba % i%:
Hith ActiveChart.Line:ro03s(1) .?as<3Down1ars = Tr0e .<31ars.2nterior.Color = ":1(2&&= 2&&= 2&&) .Down1ars.2nterior.Color = ":1(7= 7= 7) 'nd Hith

+ote t,at 2'>a % and )own>a % a''l( onl( to 2;) line;t('e c,a t%. Figure 21-16. 2!.ars are in white an# (own.ars are in blac1

21.7.2 (ro!)ines
) o'3ine% a e ve tical line% t,at e6tend 5 o# t,e data #a -e % on a line c,a t to t,e catego ( a6i%. 0,e Aa%) o'3ine% ' o'e t( o5 t,e !,a t. ou' object can be %et to Tr0e to di%'la( ) o'3ine%.

376

21.7." Ci)o)ines
Ai3o3ine% a e %,own in $igu e 21;18. 0,e Aa%Ai3o3ine% ' o'e t( o5 t,e !,a t. ou' object can be %et to Tr0e to di%'la( Ai3o3ine%. Ai3o3ine% a''l( onl( to two;di#en%ional c,a t%. Figure 21-17. Ci)o)ines

21.7.$ Series+ollection an# Series 3bjects


0,e 1e ie%!ollection ' o'e t( o5 a !,a t. ou' object etu n% t,e 1e ie%!ollection collection o5 all 1e ie% object% t,at lie in t,at c,a t g ou'. 4e will di%cu%% 1e ie% object% late in t,e c,a'te .

21.7.' Series)ines
1e ie%3ine% a e %,own in $igu e 21;19. 0,e( a''l( onl( to %tac-ed colu#n o %tac-ed ba c,a t g ou'%. 0,e Aa%1e ie%3ine% ' o'e t( can be %et to Tr0e to di%'la( %e ie% line% 5o a c,a t g ou'. Figure 21-18. Series)ines

Tea3m79 77l'D

'$%= The ChartTitle Ob1ect


A !,a t0itle object e' e%ent% a c,a t title. 0,e !,a t0itle object i% %,own in $igu e 21;20. Figure 21-29. he +hart itle object

A% wit, %eve al o5 t,e ot,e c,a t; elated object%* t,e !,a t0itle object ,a% >o de * !,a acte %* !,a t$ill$o #at* $ont* and /nte io c,ild en t,at a e u%ed to 5o #at t,e co e%'onding 'o tion o5 t,e c,a t title. 4e ,ave di%cu%%ed t,e%e object% be5o e* %o we will not co##ent 5u t,e on t,e# ,e e.

'$%$> The (ataTable Ob1ect


$igu e 21;21 %,ow% a data table. )ata table% a e e' e%ented b( )ata0able object%. Figure 21-21. % #ata table

0,e !,a t object ,a% a ' o'e t( called Aa%)ata0able. 1etting t,i% ' o'e t( to Tr0e di%'la(% a data table* a% in $igu e 21;21. /ndeed* t,e data table in $igu e 21;21 wa% ' oduced and given a bo de wit, t,e 5ollowing code:
ActiveChart.?asDataTable = Tr0e ActiveChart.DataTable.?as1order*0tline = Tr0e

0,e )ata0able object ,a% a va iet( o5 %el5;e6'lanato ( ' o'e tie%* %uc, a% Auto1cale$ont* >o de * $ont* Aa%>o de Ao iGontal* Aa%>o de Outline* Aa%>o de =e tical* and 1,ow3egendRe(. ?0,e

1,ow3egendRe( ' o'e t( i% e%'on%ible 5o t,e %#all %7ua e% on t,e 5a le5t 'o tion o5 t,e data table in $igu e 21;21.@

'$%$$ The :loor Ob1ect


0,e $loo object a''lie% onl( to 3;) c,a t% and e' e%ent% t,e 5loo o5 t,e c,a t. $o in%tance* t,e 5ollowing code:
ActiveChart.>loor.2nterior.;attern = xl;atternChec8er

%et% t,e 5loo o5 t,e active 3;) c,a t to a c,ec-e ed 'atte n* a% %,own in $igu e 21;22. 0,e $loo object ,a% >o de * !,a t$ill$o #at* and /nte io c,ild en. Figure 21-22. he 4loor o4 a "-( chart ;chec1ere#<

'$%$' The )egend Ob1ect


3egend object% e' e%ent legend%. 0,e 3egend object and it% c,ild en a e %,own in $igu e 21;23. Figure 21-2". he )egen# object

0,e !,a t object ,a% a 3egend ' o'e t( t,at etu n% t,e 3egend object 5o t,at c,a t. A% e6'ected* t,e 3egend object ,a% >o de * !,a t$ill$o #at* $ont* and /nte io c,ild en t,at %e ve t,e %a#e 'u 'o%e ,e e a% t,e( do 5o t,e !,a t0itle* A6i%0itle* !,a tA ea* and ot,e object%.

21.12.1 he )egen#Entry 3bject


$igu e 21;1 %,ow% a c,a t legend wit, two legend ent ie%. 3egend ent ie% a e e' e%ented b( 3egendEnt ( object%. 0,e 3egendEnt ( object% 5o a legend a e -e't in t,e 3egendEnt ie% collection object 5o t,e 3egend object. 0,i% collection i% acce%%ed u%ing t,e 3egendEnt ie% ' o'e t( o5 t,e !,a t object. Eac, legend ent ( ,a% two 'a t%: t,e te6t o5 t,e legend ent ( i% t,e na#e o5 t,e %e ie% a%%ociated wit, t,at ent ( and t,e entry )ey ?al%o called an ent ( #a -e @ i% a %#all co'( o5 t,e a%%ociated %e ie% and it% 5o #atting. +ote t,at t,e text o5 a legend ent ( cannot be c,anged. Aoweve * t,e 3egendEnt ( object doe% ,ave a $ont ' o'e t( t,at can be u%ed to c,ange t,e 5ont o5 t,e legend ent (* a% in:
ActiveChart.Le4end.>ont.2talic = Tr0e

/n addition* 3egendEnt ( object% can be deleted. Aoweve * a5te a legend ent ( ,a% been deleted* t,e onl( wa( to e%to e it i% to e#ove and ec eate t,e enti e legend b( %etting t,e Aa%3egend ' o'e t( 5o t,e c,a t 5i %t to >alse and t,en to Tr0e. Al%o* no 'atte n 5o #atting i% allowed 5o legend ent ie%* no can a legend ent (B% 'o%ition o %iGe be c,anged. +ote 5inall( t,at t,e e i% no di ect wa( to etu n t,e %e ie% co e%'onding to a given legend ent (.

21.12.2 he )egen#@ey 3bject


A legend -e( i% e' e%ented b( a 3egendRe( object. 0,i% object ,a% >o de * !,a t$ill$o #at* and /nte io c,ild en. /t i% ve ( i#'o tant to note t,at 5o #atting t,e 3egendRe( object will al%o auto#aticall( 5o #at t,e actual %e ie% t,at t,e legend ent ( e' e%ent%. /n ot,e wo d%* t,e %e ie% and it% legend -e( always #atc,. 0,u%* 5o in%tance* t,e 5ollowing code 5o #at% t,e 5i %t data %e ie% and it% legend -e( wit, a ed inte io and a t,ic- bo de :

Hith ActiveChart.Le4end.Le4end'ntries(1).Le4endPe .2nterior.Color2ndex = B .1order.Hei4ht = xlThic8 'nd Hith

'$%$* The PageSetup Ob1ect


0,e &age1etu' object e' e%ent% all o5 t,e 'age 5o #atting 5o a c,a t ?o wo -%,eet@. 0,e #e#be % o5 t,e &age1etu' object a e %,own in 0able 21;3. able 21-". -e&bers o4 the PageSetu! object A''lication $it0o&age%4ide & intE o %Sv10Z >lac-And4,ite $oote "a gin & int. idline% >otto#"a gin Aeade "a gin & intAeading% !ente $oote 3e5t$oote & int+ote% !ente $oote &ictu eSv10Z 3e5t$oote &ictu eSv10Z & intOualit( !ente Aeade 3e5tAeade & int0itle!olu#n% !ente Aeade &ictu eSv10Z 3e5tAeade &ictu eSv10Z & int0itleRow% !ente Ao iGontall( 3e5t"a gin Rig,t$oote !ente =e ticall( O de Rig,t$oote &ictu eSv10Z !,a t1iGe O ientation Rig,tAeade ! eato &a'e 1iGe Rig,tAeade &ictu eSv10Z ) a5t &a ent Rig,t"a gin $i %t&age+u#be & intA ea 0o'"a gin $it0o&age%0all & int!o##ent% [oo# $o in%tance* t,e 5ollowing code %et% t,e #a gin% and t,en doe% a ' int ' eview 5o t,e active c,a t:
Hith ActiveChart.;a4eSet03 .Le6tMar4in = A33lication.2nchesTo;oints(7.&) ."i4htMar4in = A33lication.2nchesTo;oints(7.F&) .To3Mar4in = A33lication.2nchesTo;oints(1.&) .1otto5Mar4in = A33lication.2nchesTo;oints(1) .?eaderMar4in = A33lication.2nchesTo;oints(7.&) .>ooterMar4in = A33lication.2nchesTo;oints(7.&) 'nd Hith ActiveChart.;rint;review

'$%$2 The PlotArea Ob1ect


0,e 'lot a ea o5 a c,a t ?%ee $igu e 21;1@ i% t,e a ea w,e e t,e c,a t data i% 'lotted. $o a 2;) c,a t* it con%i%t% o5 t,e data #a -e %* g idline%* data label%* t end line%* and o'tional c,a t ite#%* but not t,e a6e%. $o a 3;) c,a t* it al%o include% t,e wall%* 5loo * a6e%* a6i% title%* and tic-;#a label% in t,e c,a t. 0,e 'lot a ea i% %u ounded b( t,e c,a t a ea ?w,ic, doe% contain t,e a6e% on a 2;) c,a t@. 0,e &lotA ea object ,a% >o de * !,a t$ill$o #at* and /nte io c,ild en u%ed 5o t,e 5o #atting o5 t,e%e ite#%. 0,e &lotA ea object al%o ,a% 0o'* 3e5t* Aeig,t* and 4idt, ' o'e tie% t,at can be u%ed

to %et t,e %iGe and 'o%ition o5 t,e 'lot a ea wit,in t,e c,a t a ea. +ote t,at t,e e a e %o#e e%t iction% on ,ow t,e%e value% can be %et. $o in%tance* it a''ea % t,at E6cel will not let u% %et t,e 0o' ' o'e t( in %uc, a wa( t,at t,e botto# o5 t,e 'lot a ea would 5all below t,e botto# o5 t,e c,a t a ea ?w,ic, #a-e% %en%e@.

'$%$3 The Series Ob1ect


0,e 1e ie% object e' e%ent% a data %e ie% in an E6cel c,a t. 0,e 1e ie% object and it% c,ild en a e %,own in $igu e 21;24. Figure 21-2$. he Series object an# its chil#ren

0,e 1e ie% object ,a% >o de * !,a t$ill$o #at* and /nte io c,ild object* w,ic, we ,ave di%cu%%ed be5o e. 3et u% loo- at %o#e o5 it% ot,e c,ild en. 0,e 1e ie% object% 5o a c,a t a e contained in a collection object na#ed 1e ie%!ollection. 0,i% collection i% etu ned b( t,e 1e ie%!ollection #et,od o5 t,e !,a t object. ?4e will %ee e6a#'le% late in t,i% %ection.@ 0,e #e#be % o5 t,e 1e ie% object a e %,own in 0able 21;4. able 21-$. -e&bers o4 the Series 3bject E o >a % "a -e $o eg ound!olo /nde6

A''lication

A''l(!u%to#0('e A''l()ata3abel% A''l(&ict0oEnd A''l(&ict0o$ ont A''l(&ict0o1ide% A6i%. ou' >a 1,a'e >o de >ubble1iGe% !,a t0('e !lea $o #at% !o'( ! eato )ata3abel% )elete E o >a

E6'lo%ion $ill $o #ula $o #ula3ocal $o #ulaR1!1 $o #ulaR1!13ocal Aa%3)E55ect Aa%)ata3abel% Aa%E o >a % Aa%3eade 3ine% /nte io /nve t/5+egative 3eade 3ine% "a -e >ac-g ound!olo "a -e >ac-g ound!olo /nde6 "a -e $o eg ound!olo

"a -e 1iGe "a -e 1t(le +a#e &a ent &a%te &ictu e0('e &ictu e2nit &lotO de &oint% 1elect 1,adow 1#oot, 0 endline% 0('e =alue% D=alue%

21.1'.1 %##ing a New Series


0o add a new %e ie% to a c,a t* we u%e t,e Add #et,od o5 t,e 1e ie%!ollection object. 0,e %(nta6 i%:
SeriesCollection*bTect.Add(So0rce= "owcol= G SeriesLabels= Cate4or Labels= "e3lace)

0,e Source 'a a#ete %'eci5ie% t,e new data a% a Range object. 0,e o'tional %owcol 'a a#ete %'eci5ie% w,et,e t,e data %e ie% a e in ow% o colu#n% in t,e %'eci5ied ange. /t can be one o5 t,e 5ollowing #l"owCol con%tant%:
'n05 #l"owCol xl"ows = 1 xlCol05ns = 2 'nd 'n05

. The de6a0lt

0,e o'tional Series"abels 'a a#ete a''lie% onl( w,en Source i% a ange ?not an a a(@. /t i% Tr0e i5 t,e 5i %t ow o colu#n contain% t,e na#e o5 t,e data %e ie% and >alse i5 t,e 5i %t ow o colu#n contain% t,e 5i %t data 'oint o5 t,e %e ie%. /5 t,i% a gu#ent i% o#itted* E6cel atte#'t% to dete #ine t,e location o5 t,e %e ie% na#e 5 o# t,e content% o5 t,e 5i %t ow o colu#n. ?A% / ,ave %tated %eve al ti#e% be5o e* #( advice i% to %u''l( an( value% t,at (ou e7ui e* at,e t,an letting E6cel gue%%.@ 1i#ila l(* t,e o'tional Cate!ory"abels 'a a#ete a''lie% onl( w,en Source i% a ange ?not an a a(@. /t i% Tr0e i5 t,e 5i %t ow o colu#n contain% t,e na#e o5 t,e catego ( label% and >alse i5 t,e 5i %t ow o colu#n contain% t,e 5i %t data 'oint o5 t,e %e ie%. /5 t,i% a gu#ent i% o#itted* E6cel atte#'t% to dete #ine t,e location o5 t,e catego ( label 5 o# t,e content% o5 t,e 5i %t ow o colu#n. 0,e o'tional %e lace 'a a#ete ,a% t,e 5ollowing #eaning: i5 Cate!ory"abels i% Tr0e and %e lace i% Tr0e* t,e %'eci5ied catego ie% e'lace t,e catego ie% t,at cu entl( e6i%t 5o t,e %e ie%. /5 %e lace i% >alse* t,e e6i%ting catego ie% will not be e'laced. 0,e de5ault value i% >alse.

0o illu%t ate* con%ide t,e wo -%,eet in $igu e 21;25. Figure 21-2'. /llustrating the %## &etho#F the #ata

0,e 5ollowing code will c eate t,e c,a t in $igu e 21;26:


Di5 co As Chart*bTect Set co = ActiveSheet.Chart*bTects. G Add(177= 177= B77= 277) co.Chart.ChartT 3e = xlCol05nCl0stered co.Chart.SeriesCollection.Add G So0rce-=ActiveSheet."an4e("A1-CF")= G "owcol-=xlCol05ns= SeriesLabels-=Tr0e= G Cate4or labels-=Tr0e

+ote t,at t,e %e ie% label% a e in t,e 5i %t ow and t,e catego ( label% a e in t,e 5i %t colu#n. Figure 21-25. /llustrating the %## &etho#F the chart

21.1'.2 he (ata)abel 3bject


A )ata3abel object e' e%ent% t,e data label o5 a c,a t data 'oint ?o t endline@. ?4e di%cu%% t,e &oint object late in t,e c,a'te .@ Eac, 1e ie% object ,a% a )ata3abel% collection t,at contain% one )ata3abel object 5o eac, 'oint in t,e %e ie%. 0,e )ata;3abel% collection i% etu ned b( t,e )ata3abel% #et,od* a% in:
26 ActiveChart.SeriesCollection(1). G ?asDataLabels Then Ms41ox ActiveChart.SeriesCollection(1).G DataLabels.Co0nt

'nd 26

+ote t,at i5 t,e e a e no data label% 5o a given %e ie%* t,en t,e )ata3abel% #et,od will gene ate an e o H we %,ould c,ec- t,i% 5i %t be5o e calling t,e #et,od* u%ing t,e Aa%)ata3abel% ' o'e t( o5 t,e 1e ie% object. 0,e #isibility o5 a data label ?not it% e6i%tence@ i% gove ned b( t,e Aa%)ata3abel ' o'e t( o5 t,e co e%'onding &oint object ?di%cu%%ed late @. 0,u%* t,e code:
ActiveChart.SeriesCollection(1).;oints(1).?asDataLabel = >alse

%u'' e%%e% t,e di%'la( o5 a data label 5o t,e 5i %t data 'oint in t,e %e ie%. 4e can u%e t,e A''l()ata3abel% #et,od to di%'la( o ,ide data label% and to c,ange t,e t('e o5 label%. 0,e %(nta6 5o t,i% #et,od i%:

ex ression.A33l DataLabels(Ty e= "e!end5ey)


w,e e ex ression can etu n eit,e a !,a t* &oint* o 1e ie% object. 4,en t,e #et,od i% a''lied to a !,a t object* it a55ect% t,e data label% 5o all %e ie% in t,e c,a t at t,e %a#e ti#e. 0,e Ty e 'a a#ete i% t,e data;label t('e and can be one o5 t,e 5ollowing #lDataLabelsT 3e con%tant%:
'n05 #lDataLabelsT 3e xlDataLabelsShow9one = %L1L2 xlDataLabelsShow+al0e = 2 xlDataLabelsShow;ercent = B xlDataLabelsShowLabel = L xlDataLabelsShowLabelAnd;ercent = & xlDataLabelsShow10bbleSiMes = N 'nd 'n05

0,e o'tional "e!end5ey 'a a#ete can be %et to Tr0e to %,ow t,e legend -e( ne6t to eac, data 'oint. 0,e de5ault value i% >alse. $igu e 21;27 %,ow% data 'oint legend% in action. Figure 21-26. /llustrating the #ata !oint legen#

0,e ' o'e tie% and #et,od% o5 t,e )ata3abel object a e %,own in 0able 21;5.

A''lication Auto1cale$ont Auto0e6t >o de !a'tion !,a acte % ! eato )elete $ill

able 21-'. -e&bers o4 the (ata)abel 3bject $ont ReadingO de Ao iGontalAlign#ent 1elect /nte io 1,adow 3e5t 1,ow3egendRe( +a#e 0e6t +u#be $o #at3in-ed 0o' O ientation 0('e &a ent =e ticalAlign#ent &o%ition

+ote t,at on a t endline ?di%cu%%ed late in t,i% c,a'te @* t,e )ata3abel ' o'e t( etu n% t,e te6t %,own wit, t,e t endline. 0,i% te6t can be t,e e7uation* t,e R;%7ua ed value* o bot, ?i5 bot, a e %,owing@.

21.1'." he Point 3bject


A &oint object e' e%ent% a %ingle data 'oint in a %e ie%. 0,e &oint object 5o t,e 'oint% in a given %e ie% a e contained in t,e &oint% collection 5o t,e 1e ie% object. 0,i% collection i% etu ned b( t,e &oint% ' o'e t( o5 t,e 1e ie% object. 0,e &oint object ,a% t,e 5ollowing c,ild en: >o de * !,a t$ill$o #at* )ata3abel* and /nte io . 0,e #e#be % o5 t,e &oint object a e %,own in 0able 21;6. "o%t o5 t,e%e #e#be % a e %el5;e6'lanato (. 3et u% loo- b ie5l( at %o#e o5 t,e ot,e %. able 21-5. -e&bers o4 the Point 3bject )elete "a -e 1iGe E6'lo%ion "a -e 1t(le $ill &a ent Aa%)ata3abel &a%te /nte io &ictu e0('e /nve t/5+egative &ictu e2nit "a -e >ac-g ound!olo 1econda (&lot "a -e >ac-g ound!olo /nde6 1elect "a -e $o eg ound!olo 1,adow "a -e $o eg ound!olo /nde6

A''lication A''l()ata3abel% A''l(&ict0oEnd A''l(&ict0o$ ont A''l(&ict0o1ide% >o de !lea $o #at% !o'( ! eato )ata3abel

21.1'.".1 Ex!losion !ro!erty 0,i% ' o'e t( etu n% o %et% t,e e6'lo%ion value 5o a 'ie;c,a t o doug,nut;c,a t %lice. $igu e 21 28 %,ow% an e6'lo%ion value o5 20* t,e e%ult o5 t,e 5ollowing code:
ActiveChart.SeriesCollection(1).;oints(2).'x3losion = 27

+ote t,at t,e E6'lo%ion ' o'e t( can be a''lied to a data %e ie%* in w,ic, ca%e it 8e6'lode%8 all o5 t,e %eg#ent%. An e6'lo%ion value o5 0 co e%'ond% to no e6'lo%ion. Figure 21-27. Ex!losion J 29

21.1'.".2 -ar1erSi?e an# -ar1erStyle 0,e "a -e 1iGe ' o'e t( etu n% o %et% t,e %iGe o5 a data 'oint in 'oint% ?a% a 3ong@. 0,e ' o'e t( al%o a''lie% to t,e 1e ie% object* in w,ic, ca%e it %et% all #a -e % in t,e %e ie% at once. 0,e "a -e 1t(le ' o'e t( dete #ine% t,e %t(le o5 t,e data 'oint and can be one o5 t,e 5ollowing value%:
'n05 #lMar8erSt le xlMar8erSt le# = %L1ND xlMar8erSt le;ict0re = %L1LF xlMar8erSt le9one = %L1L2 xlMar8erSt leDot = %L11D xlMar8erSt leDash = %L11& xlMar8erSt leA0to5atic = %L17& xlMar8erSt leS/0are = 1 xlMar8erSt leDia5ond = 2 xlMar8erSt leTrian4le = B xlMar8erSt leStar = & xlMar8erSt leCircle = D xlMar8erSt le;l0s = E 'nd 'n05

0o illu%t ate* t,e 5ollowing code ' oduce% t,e at,e odd;loo-ing c,a t in $igu e 21;29.
Hith ActiveChart.SeriesCollection(1) .Mar8erSiMe = 17 .Mar8erSt le = xlMar8erSt leDia5ond Hith .;oints(2) .Mar8erSiMe = 27 .Mar8erSt le = xlMar8erSt leCircle 'nd Hith 'nd Hith

Tea3m89 77l'D

Figure 21-28. /llustrating -ar1erSi?e an# -ar1erStyle

'$%$8 Properties and 7ethods of the Chart Ob1ect


0,e 91 ' o'e tie% and #et,od% o5 t,e !,a t object a e %,own in 0able 21;7. able 21-6. -e&bers o4 the +hart 3bject IA''l()ata3abel%Sv10Z )oug,nut. ou'% &ie. ou'% I!ode+a#e ) awingObject% &ivot3a(outSv9Z I)u##(23 ) awing% &lotA ea IEvaluate ) o')own% &lot>( I& intOutSv9Z Elevation &lot=i%ibleOnl( I& otectSv10Z Evaluate & eviou% I1aveA%Sv10Z E6'o t & intOut Activate $loo & int& eview A''lication .a')e't, & otect A''l(!u%to#0('e .et!,a tEle#ent & otect!ontent% A''l()ata3abel% . ou'>o6e% & otect)ata A c% . ou'Object% & otect) awingObject% A ea3). ou' Aa%A6i% & otect$o #atting A ea. ou'% Aa%)ata0able & otect.oal1eeAuto$o #at Aa%3egend & otection"ode Auto1caling Aa%&ivot$ield%Sv9Z & otect1election A6e% Aa%0itle Rada . ou'% >a 3). ou' Aeig,t&e cent Rectangle% >a . ou'% A('e lin-% Re5 e%,

>a 1,a'e >utton% !,a tA ea !,a t. ou'% !,a tObject% !,a t0itle !,a t0('e !,a t4iGa d !,ec->o6e% !,ec-1'elling !ode+a#e !olu#n3). ou' !olu#n. ou'% !o'( !o'(!,a t>uild !o'(&ictu e !o ne % ! eate&ubli%,e ! eato )ata0able )elete )e't,&e cent )e%elect )i%'la(>lan-%A%

/nde6 3abel% 3egend 3ine3). ou' 3ine. ou'% 3ine% 3i%t>o6e% 3ocation "ailEnvelo'eSv10Z "ove +a#e +e6t O3EObject% On)ouble!licOn1,eetActivate On1,eet)eactivate O'tion>utton% Oval% &age1etu' &a ent &a%te &e %'ective &ictu e% &ie3). ou'

Rig,tAngleA6e% Rotation 1aveA% 1c i't%Sv9Z 1c oll>a % 1elect 1e ie%!ollection 1et>ac-g ound&ictu e 1et1ou ce)ata 1,a'e% 1,ow4indow 1iGe4it,4indow 1'inne % 1ub0('e 1u 5ace. ou' 0abSv10Z 0e6t>o6e% 0('e 2n' otect =i%ible 4all% 4all%And. idline%2) DL. ou'%

0able 21;8 %,ow% t,e #e#be % o5 t,e !,a t object t,at etu n c,ild en o5 t,e !,a t object* along wit, t,e object% t,at t,e( etu n. +ote t,at %eve al #e#be % can etu n a %ingle object o a collection o5 object%. able 21-7. -e&bers that ,eturn +hil#ren Name Return Type A''lication A''lication A ea3). ou' !,a t. ou' A ea. ou'% !,a t. ou'?%@ A6e% A6i%EA6e% >a 3). ou' !,a t. ou' >a . ou'% !,a t. ou'?%@ !,a tA ea !,a tA ea !,a t. ou'% !,a t. ou'?%@ !,a tObject% !,a tObject?%@ !,a t0itle !,a t0itle !olu#n3). ou' !,a t. ou' !olu#n. ou'% !,a t. ou'?%@ !o ne % !o ne % )ata0able )ata0able )oug,nut. ou'% !,a t. ou'?%@

$loo A('e lin-% 3egend 3ine3). ou' 3ine. ou'% 3ocation O3EObject% &age1etu' &ie3). ou' &ie. ou'% &lotA ea Rada . ou'% 1e ie%!ollection 1,a'e% 1u 5ace. ou' 4all% DL. ou'%

$loo A('e lin-% 3egend !,a t. ou' !,a t. ou'?%@ !,a t O3EObject?%@ &age1etu' !,a t. ou' !,a t. ou'?%@ &lotA ea !,a t. ou'?%@ 1e ie%E1e ie%!ollection 1,a'e% !,a t. ou' 4all% !,a t. ou'?%@

3et u% di%cu%% a 5ew o5 t,e #e#be % o5 t,e !,a t object. ?4e ,ave encounte ed #an( o5 t,e%e #e#be % in connection wit, ot,e c,a t; elated object%.@

21.15.1 +hart0i?ar# -etho#


0,e !,a t4iGa d #et,od #odi5ie% t,e ' o'e tie% o5 a c,a t. +ote t,at* unli-e t,e &ivot0able wiGa d* t,e !,a t4iGa d #et,od doe% not c eate a c,a t. 0,e !,a t4iGa d #et,od i% u%e5ul 5o a''l(ing %eve al 5o #atting ' o'e tie% to a c,a t at one ti#e. 0,e #et,od c,ange% onl( t,e ' o'e tie% t,at a e %'eci5ied b( t,e 'a a#ete % t,at a e included in t,e call to t,e #et,od. 0,e %(nta6 5o t,e !,a t4iGa d #et,od i%:

Chart/bject.ChartHiMard(Source= :allery= Format= G Plot)y= Cate!ory"abels= Series"abels= 6as"e!end= G Title= Cate!oryTitle= ValueTitle= ,xtraTitle)
+ote t,at all 'a a#ete % a e o'tional. 0,e Source 'a a#ete i% t,e ange t,at contain% t,e %ou ce data 5o t,e c,a t. /5 Source i% o#itted* t,en E6cel will u%e t,e %elected e#bedded c,a t o t,e active c,a t %,eet. /5 no e#bedded c,a t i% %elected and no c,a t %,eet i% active* t,en an e o will e%ult. 0,e :allery 'a a#ete %'eci5ie% a gene al c,a t t('e and can be one o5 t,e 5ollowing #lChartT 3e con%tant%: xlArea* xl1ar* xlCol05n * xlLine* xl;ie* xl"adar* xl#@Scatter* xlCo5bination * xlBDArea* xlBD1ar* xlBDCol05n* xlBDLine* xlBD;ie* xlBDS0r6ace* xlDo04hn0t* o xlDe6a0ltA0to>or5at. 0,e Format 'a a#ete %'eci5ie% t,e %'eci5ic c,a t t('e* given t,e value o5 :allery. 0,e value o5 $o #at can be a nu#be 5 o# 1 t, oug, 10* de'ending on t,e galle ( t('e. +ote t,at t,i% value co e%'ond% to t,e c,a t t('e% in t,e !,a t $o #at dialog. /5 t,i% a gu#ent i% o#itted* E6cel will %elect a value ba%ed on t,e galle ( t('e and data %ou ce.

0,e Plot)y 'a a#ete %'eci5ie% w,et,e t,e data 5o eac, %e ie% i% in ow% o colu#n%. /t can be one o5 t,e value% xl"ows o xlCol05ns. 0,e Cate!ory"abels 'a a#ete i% an intege t,at %'eci5ie% t,e nu#be o5 ow% o colu#n% wit,in t,e %ou ce ange t,at contain catego ( label%. /t can be an( value 5 o# 0 t, oug, one le%% t,an t,e #a6i#u# nu#be o5 catego ie% o %e ie%. 1i#ila l(* Series"abels i% an intege t,at %'eci5ie% t,e nu#be o5 ow% o colu#n% wit,in t,e %ou ce ange t,at contain %e ie% label%. /t can be an( value 5 o# 0 t, oug, one le%% t,an t,e #a6i#u# nu#be o5 catego ie% o %e ie%. 0,e 6as"e!end 'a a#ete %,ould be %et to Tr0e to include a c,a t legend. 0,e Title 'a a#ete %,ould be %et to t,e c,a t title te6t. 1i#ila l(* Cate!oryTitle i% t,e catego ( a6i% title te6t* ValueTitle i% t,e value a6i% title te6t* and ,xtraTitle i% t,e %e ie% a6i% title 5o 3;) c,a t% o t,e %econd value a6i% title 5o 2;) c,a t% wit, a %econd value a6i%. 0o illu%t ate* i#agine t,at t,e c,a t in $igu e 21;30 i% t,e active c,a t 0,e 5ollowing code e5o #at% t,e c,a t in $igu e 21;30* a% %,own in $igu e 21;31:
ActiveChart.ChartHiMard :aller -=xlLine= G >or5at-=1= ?asLe4end-=Tr0e= Title-="Avera4es"= G Cate4or Title-=":rades"= +al0eTitle-="Avera4e".

Figure 21-"9. /llustrating the +hart0i?ar# &etho#

Figure 21-"1. ,esults o4 the +hart0i?ar# &etho#

21.15.2 Ex!ort -etho#

0,e Export #et,od e6'o t% a c,a t in a g a',ic 5o #at. 0,e %(nta6 i%:

Chart/bject.'x3ort(FileName= FilterName= 1nteracti+e)


Ae e FileName i% t,e na#e o5 t,e g a',ic 5ile to c eate. FilterName i% t,e na#e o5 t,e g a',ic 5ilte a% it a''ea % in t,e egi%t (. "ic o%o5t doe% not %a( where in t,e egi%t (* but it %ee#% li-el( to be t,e -e(:
?P'@GL*CALGMAC?29'ISo6twareIMicroso6tIShared ToolsI:ra3hics >iltersI'x3ort

0,e 1nteracti+e 'a a#ete can be %et to Tr0e to di%'la( t,e dialog bo6 t,at contain% 5ilte %'eci5ic o'tion%. /5 t,i% a gu#ent i% >alse ?t,e de5ault@* E6cel u%e% t,e de5ault value% 5o t,e 5ilte . 0o illu%t ate* t,e 5ollowing code c eate% t, ee g a',ic 5ile% 5 o# t,e active c,a t:
ActiveChart.'x3ort "d-IexcelItest.3n4"= ";9:" ActiveChart.'x3ort "d-IexcelItest.T34"= "S;':" ActiveChart.'x3ort "d-IexcelItest.4i6"= ":2>"

+ote t,at in t,e%e ca%e%* %etting 1nteracti+e to Tr0e %ee#% to ,ave no e55ect. +ote al%o t,at an( 5ile o5 t,e na#e FileName will be ove w itten wit,out wa ning.

21.15." Print3ut -etho#


0,e & intOut #et,od ' int% t,e c,a t. 0,i% #et,od a''lie% to a va iet( o5 ot,e object%* %uc, a% !,a t%* 4o -%,eet?%@* 4o -boo-?%@* and t,e Range object. 0,e %(nta6 i%:

Chart/bject.;rint*0t(*rom= To= Co ies= Pre+iew= G -cti+ePrinter= PrintToFile= Collate)


+ote t,at all o5 t,e 'a a#ete % to t,i% #et,od a e o'tional. 0,e From 'a a#ete %'eci5ie% t,e 'age nu#be o5 t,e 5i %t 'age to ' int* and t,e To 'a a#ete %'eci5ie% t,e la%t 'age to ' int. /5 o#itted* t,e enti e object ? ange* wo -%,eet* etc.@ i% ' inted. 0,e Co ies 'a a#ete %'eci5ie% t,e nu#be o5 co'ie% to ' int. 0,e de5ault i% 1. 1et Pre+iew to Tr0e to invo-e ' int ' eview at,e t,an ' inting i##ediatel(. 0,e de5ault i% >alse.
-cti+ePrinter %et% t,e na#e o5 t,e active ' inte . On t,e ot,e ,and* %etting PrintToFile to Tr0e cau%e% E6cel to ' int to a 5ile. E6cel will ' o#'t t,e u%e 5o t,e na#e o5 t,e out'ut 5ile.

?2n5o tunatel(* t,e e i% no wa( to %'eci5( t,e na#e o5 t,e out'ut 5ile in code.@ 0,e Collate 'a a#ete %,ould be %et to Tr0e to collate #ulti'le co'ie%.

'$%$9 Example+ Scrolling Through Chart T/pes


0,e e a e a total o5 73 di%tinct c,a t t('e%. 0,i% i% too #an( to ea%il( loo- at e6a#'le% o5 eac, t('e b( ,and. Aoweve * a bit o5 coding can ' oduce a %i#'le a''lication t,at %c oll% t, oug, t,e

va iou% c,a t t('e%* %o t,at we can dete #ine w,ic, c,a t t('e i% #o%t a'' o' iate 5o a 'a ticula 'u 'o%e. 1ta t b( c eating a c,a t ?wit, %o#e data %uc, a% t,at in $igu e 21;6@ in a c,a t%,eet. 0,en add t,e code in E6a#'le 21;3 to t,e c,a t %,eetB% code #odule. Exa&!le 21-". +o#e in the +hart Sheet>s +o#e -o#ule
*3tion 'x3licit Di5 b;a0se As 1oolean S0b ScrollChartT 3es() Di5 iT 3e As 2nte4er= s9a5e As Strin4 Di5 6r As 2nte4er 6r = >ree>ile *3en ThisHor8boo8.;ath K G "Ichartt 3es.txt" >or 2n30t As O6r Do Hhile 9ot '*>(6r) 2n30t O6r= iT 3e= s9a5e *n 'rror "es05e 9ext ActiveChart.ChartT 3e = iT 3e ActiveChart.?asTitle = Tr0e ActiveChart.ChartTitle.Text = G iT 3e K " %% " K s9a5e Dela 2

26 b;a0se Then Do Do'vents Loo3 <ntil b;a0se = >alse 'nd 26 Loo3 Close 6r 'nd S0b .%%%%% S0b Dela (rTi5e As Sin4le) .Dela rTi5e seconds (5in=.71= 5ax=B77) Di5 *ldTi5e As +ariant .Sa6t net 26 rTi5e $ 7.71 *r rTi5e ( B77 Then rTi5e = 1 *ldTi5e = Ti5er Do Do'vents Loo3 <ntil Ti5er % *ldTi5e (= rTi5e 'nd S0b .%%%%% ;rivate S0b ChartGMo0seDown(1 +al 10tton As Lon4= 1 +al Shi6t As Lon4= 1 +al # As Lon4= 1 +al @ As Lon4) 26 10tton = xl;ri5ar 10tton Then G

b;a0se = 9ot b;a0se 'nd S0b

0,i% code contain% t, ee ' ocedu e%. 0,e #ain ' ocedu e i% ScrollChartT 3es* w,ic, %et% t,e c,a t t('e and adju%t% t,e c,a tB% title acco dingl(. 0,e ' ocedu e u%e% a te6t 5ile* (hart!ypesAtxt* t,at contain% a li%t o5 all c,a t t('e% and t,ei na#e%. 0,e content% o5 t,at te6t 5ile a e %,own in E6a#'le 21;4. 0,i% 5ile will need to be in t,e %a#e di ecto ( a% t,e wo -boo-. Exa&!le 21-$. he +hart y!es.txt File
%L1NE %L1&1 %L127 %L172 %L171 %L177 %L7ED 1 L & 1& &1 &2 &B &L && &N &F &D &E N7 N1 N2 NB NL N& NN NF ND NE F7 F1 F2 FB FL F& FN FF FD FE D7 D1 D2 DB DL D& DN DF DD DE #@Scatter "adar Do04hn0t BD;ie BDLine BDCol05n BDArea Area Line ;ie 10bble Col05nCl0stered Col05nStac8ed Col05nStac8ed177 BDCol05nCl0stered BDCol05nStac8ed BDCol05nStac8ed177 1arCl0stered 1arStac8ed 1arStac8ed177 BD1arCl0stered BD1arStac8ed BD1arStac8ed177 LineStac8ed LineStac8ed177 LineMar8ers LineMar8ersStac8ed LineMar8ersStac8ed177 ;ie*6;ie ;ie'x3loded BD;ie'x3loded 1ar*6;ie #@ScatterS5ooth #@ScatterS5ooth9oMar8ers #@ScatterLines #@ScatterLines9oMar8ers AreaStac8ed AreaStac8ed177 BDAreaStac8ed BDAreaStac8ed177 Do04hn0t'x3loded "adarMar8ers "adar>illed S0r6ace S0r6aceHire6ra5e S0r6aceTo3+iew S0r6aceTo3+iewHire6ra5e 10bbleBD'66ect Stoc8?LC Stoc8*?LC

E7 E1 E2 EB EL E& EN EF ED EE 177 171 172 17B 17L 17& 17N 17F 17D 17E 117 111 112

Stoc8+?LC Stoc8+*?LC C linderColCl0stered C linderColStac8ed C linderColStac8ed177 C linder1arCl0stered C linder1arStac8ed C linder1arStac8ed177 C linderCol ConeColCl0stered ConeColStac8ed ConeColStac8ed177 Cone1arCl0stered Cone1arStac8ed Cone1arStac8ed177 ConeCol ; ra5idColCl0stered ; ra5idColStac8ed ; ra5idColStac8ed177 ; ra5id1arCl0stered ; ra5id1arStac8ed ; ra5id1arStac8ed177 ; ra5idCol

+ote t,e u%e o5 t,e *n 'rror line in ScrollChartT 3es* w,ic, e%u#e% e6ecution in ca%e we t ( to %et t,e c,a t t('e to a value t,at i% not acce'table 5o t,e 'a ticula c,a t. 0,e Dela ' ocedu e %i#'l( wait% 5o t,e ' e%c ibed nu#be o5 %econd%. $inall(* t,e "ou%e)own event c,ange% t,e %tate o5 t,e #odule level >oolean va iable bPause. 4,en t,e le5t #ou%e button i% clic-ed* %c olling i% 'au%ed until t,e #ou%e button i% clic-ed again. 0o %to' t,e ' ocedu e co#'letel(* ju%t ,it !t l;> ea-.

'$%$< Example+ Printing Embedded Charts


4e can now i#'le#ent t,e ;rintCharts 5eatu e o5 ou 1RD2til% a''lication. 0,i% i% de%igned to ' ovide a li%t o5 t,e e#bedded c,a t% in t,e active wo -boo-* %o t,e u%e can %elect 5 o# t,i% li%t and ' int t,e %elected c,a t%. ?0o ' int a c,a t %,eet* u%e t,e ;rintSheets utilit(.@ /#'le#enting t,e ;rintCharts utilit( i% %i#ila to i#'le#enting t,e ;rintSheets and ;rint;ivotTables utilitie%* w,ic, we did ea lie in t,e boo-. At t,e ' e%ent ti#e* t,i% ' int utilit(* located in t,e PrintAutl add;in* %i#'l( di%'la(% a #e%%age bo6. 0o i#'le#ent t,i% 5eatu e* we want t,e utilit( to 5i %t di%'la( a dialog bo6* a% %,own in $igu e 21;32. Figure 21-"2. Print +harts #ialog

0,e li%t bo6 contain% a li%t o5 all e#bedded c,a t% in t,e active wo -boo-. 0,e u%e can %elect one o #o e c,a t% and ,it t,e & int button. 0,e 5ollowing a e t,e %te'% to c eate t,e ' int utilit(. All t,e action ta-e% 'lace in t,e PrintAxls wo -boo-* %o o'en t,i% wo -boo-. 4,en t,e c,ange% a e 5ini%,ed* (ou will need to %ave PrintAxls a% PrintAutl a% well. /5 PrintAutl i% loaded* t,e onl( wa( to unload it i% to unload t,e add;in /,4'lilsAxla ?i5 it i% loaded@ and clo%e t,e wo -boo- /,4'tilsAxls ?i5 it i% o'en@.

21.17.1 +reate the 2serFor&


! eate t,e dialog %,own in $igu e 21;32 in t,e PrintAxls wo -boo-. +a#e t,e dialog dl!PrintCharts* c,ange it% ca'tion to 8& int !,a t%*8 and c,ange t,e ;rintCharts ' ocedu e to:
;0blic S0b ;rintCharts() dl4;rintCharts.Show 'nd S0b

0,e dl4;rintCharts dialog ,a% two co##and button% and one li%t bo6. 21.17.1.1 )ist box &lace a li%t bo6 on t,e 5o # a% in $igu e 21;32. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 21;9. able 21-8. Non#e4ault Pro!erties o4 the )ist.ox +ontrol Property Value +a#e l%t!,a t% 0ab/nde6 0 "ulti1elect 5 #"ulti1electE6tended 0,e "ulti1elect ' o'e t( i% %et to 6r5M0ltiSelect'xtended %o t,at t,e u%e can u%e t,e !ont ol -e( to %elect #ulti'le ?'o%%ibl( noncon%ecutive@ ent ie% and t,e 1,i5t -e( to %elect #ulti'le con%ecutive ent ie%. 0,e 0ab/nde6 ' o'e t( dete #ine% not onl( t,e o de in w,ic, t,e cont ol% a e vi%ited a% t,e u%e ,it% t,e 0ab -e(* but al%o dete #ine% w,ic, cont ol ,a% t,e initial 5ocu%. 1ince we want t,e initial 5ocu% to be on t,e li%t bo6* we %et it% tab inde6 to 0. 21.17.1.2 Print button &lace a co##and button on t,e 5o # a% in $igu e 21;32. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 21;10. able 21-19. Non#e4ault Pro!erties o4 the Print .utton Property Value c#d& int & & int 1

+a#e Accele ato !a'tion 0ab/nde6

21.17.1." +ancel button

&lace anot,e co##and button on t,e 5o # a% in $igu e 21;32. 2%ing t,e & o'e tie% window* %et t,e ' o'e tie% %,own in 0able 21;11. able 21-11. Non#e4ault Pro!erties o4 the +ancel .utton Property Value c#d!ancel ! !ancel 2 0 ue

+a#e Accele ato !a'tion 0ab/nde6 !ancel

4,en t,e !ancel ' o'e t( o5 t,e cmdCancel button i% %et to Tr0e* t,e button i% 8clic-ed8 w,en t,e u%e ,it% t,e E%ca'e -e(. 0,u%* t,e E%ca'e -e( will di%#i%% t,e ' int dialog.

21.17.2 +reate the +o#e .ehin# the 2serFor&


+ow it i% ti#e to c eate t,e code be,ind t,e%e cont ol%. 21.17.2.1 he (eclarations section 0,e )ecla ation% %ection o5 t,e 2%e $o # %,ould contain decla ation% o5 t,e #odule;level va iable%* a% %,own in E6a#'le 21;5. Exa&!le 21-'. -o#ule-)evel (eclarations in the 2serFor&>s (eclarations Section
*3tion 'x3licit Di5 cCharts As 2nte4er Di5 sChart*bT9a5es() As Strin4 Di5 sSheets() As Strin4

21.17.2.2 +ancel button co#e 0,e !ancel button code i% %,own in E6a#'le 21;6. Exa&!le 21-5. he +ancel .utton>s +lic1 Event Can#ler
;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b

21.17.2." Print button co#e 0,e & int button call% t,e #ain ' int ' ocedu e and t,en unload% t,e 5o #* a% %,own in E6a#'le 21;7. Exa&!le 21-6. he c&#PrintG+lic1 Proce#ure
;rivate S0b c5d;rintGClic8() ;rintSelectedCharts <nload Me 'nd S0b

21.17.2.$ he For&>s /nitiali?e event

Tea3m99 77l'D

0,e 2%e $o #B% /nitialiGe event* w,ic, i% %,own in E6a#'le 21;8* i% t,e 'lace to 5ill t,e li%t bo6 wit, a li%t o5 e#bedded c,a t%. Ou a''lication u%e% a #odule;level a a( to ,old t,e c,a t na#e%* a #odule;level a a( to ,old t,e !,a tObject object na#e%* and a #odule;level intege va iable to ,old t,e c,a t count. 4e 5ill t,e%e va iable% in t,e /nitialiGe event and t,en u%e t,e a a(% to 5ill t,e li%t. 0,e va iable% a e u%ed again in t,e #ain ' int ' ocedu e* w,ic, i% w,( we ,ave decla ed t,e# at t,e #odule level. +ote t,e u%e o5 t,e "eDi5 %tate#ent to edi#en%ion t,e a a(%. 0,i% i% nece%%a ( %ince we do not -now at t,e out%et ,ow #an( e#bedded c,a t% t,e e a e in t,e wo -boo-. Exa&!le 21-7. he 2serFor&>s /nitiali?e Event Proce#ure
;rivate S0b <ser>or5G2nitialiMe() . >ill lstCharts with the list o6 e5bedded charts Di5 ws As Hor8sheet Di5 ch*bT As Chart*bTect "eDi5 sChart*bT9a5es(1 To 17) As Strin4 "eDi5 sSheets(1 To 17) As Strin4 lstCharts.Clear cCharts = 7 >or 'ach ws 2n ActiveHor8boo8.Hor8sheets >or 'ach ch*bT 2n ws.Chart*bTects . <3date chart co0nt cCharts = cCharts A 1 . "edi5ension arra s i6 necessar 26 <1o0nd(sSheets) $ cCharts Then "eDi5 ;reserve sSheets(1 To cCharts A &) "eDi5 ;reserve sChart*bT9a5es(1 To cCharts A &) 'nd 26 . Save na5e o6 chart and ws sChart*bT9a5es(cCharts) = ch*bT.9a5e sSheets(cCharts) = ws.9a5e . Add ite5 to list box 26 ch*bT.Chart.?asTitle Then lstCharts.Add2te5 ch*bT.Chart.ChartTitle.Text K " (" K G sChart*bT9a5es(cCharts) K " in " K sSheets(cCharts) K ")" 'lse lstCharts.Add2te5 "$9o Title( (" K sChart*bT9a5es(cCharts) K " in " K G sSheets(cCharts) K ")" 'nd 26 9ext 9ext 'nd S0b

21.17.2.' he Print+harts !roce#ure 0,e #ain ' inting ' ocedu e i% %,own in E6a#'le 21;9. +ote t,at we ,ave been ca e5ul to deal wit, two %'ecial ca%e%. $i %t* t,e e #a( not be an( e#bedded c,a t% in t,e wo -boo-. 1econd* t,e u%e #a( ,it t,e & int button wit,out %electing an( c,a t% in t,e li%t bo6.

/t i% i#'o tant to note al%o t,at li%t bo6e% a e 0;ba%ed* #eaning t,at t,e 5i %t ite# i% ite# 0. Aoweve * ou a a(% a e 1;ba%ed ?t,e 5i %t ite# i% ite# 1@* %o we #u%t ta-e t,i% into account w,en we #ove 5 o# a %election to an a a( #e#be H to wit* %election i co e%'ond% to a a( inde6 i.#. Exa&!le 21-8. he PrintSelecte#+harts Proce#ure
S0b ;rintSelectedCharts() . ;rint the selected charts in lstCharts Di5 i As 2nte4er Di5 b9oneSelected As 1oolean b9oneSelected = Tr0e 26 cCharts = 7 Then Ms41ox "9o e5bedded charts in this wor8boo8."= vb'xcla5ation 'xit S0b 'lse >or i = 7 To lstCharts.ListCo0nt % 1 26 lstCharts.Selected(i) Then b9oneSelected = >alse . List box is 7%based= arra s are 1%based Hor8sheets(sSheets(i A 1)). G Chart*bTects(sChart*bT9a5es(i A 1)).Chart.;rint*0t 'nd 26 9ext 'nd 26 26 b9oneSelected Then Ms41ox "9o charts have been selected 6ro5 the list box."= vb'xcla5ation 'nd 26 'nd S0b

'$%$= Example+ Setting (ata Series )abels


A% (ou #a( -now* data label% can be edited individuall( b( clic-ing twice ?'au%ing in between clic-%@ on a data label. 0,i% 'lace% t,e label in edit #ode* a% %,own in $igu e 21;33. Figure 21-"". % #ata label in e#it &o#e

Once in edit #ode* we can c,ange t,e te6t o5 a data label ?w,ic, b ea-% an( lin-%@ o %et a new lin- to a wo -%,eet cell. $o in%tance* t,e code:
ActiveChart.SeriesCollection(1).DataLabels(2).Text = "=M ChartSheet!"12C2"

%et% t,e data label 5o t,e %econd data 'oint to t,e value o5 cell >12. +ote t,at t,e 5o #ula #u%t be in R1!1 notation. 2n5o tunatel(* ,oweve * E6cel doe% not ' ovide a wa( to a%%ociate all o5 t,e data label% 5o a data %e ie% wit, a wo -%,eet ange in a %i#'le wa( ?be(ond doing t,i% one data label at a ti#e@. 1o let u% w ite a utilit( 5o t,i% 'u 'o%e and add it to 1RD2til%. 4,en t,e utilit( i% invo-ed* it ' e%ent% a dialog ?%ee $igu e 21;34@ wit, a li%t o5 all t,e data %e ie% 5o t,e %elected c,a t. 0,e u%e can %elect a data %e ie% and t,en de5ine a ange to w,ic, t,e data label% will be lin-ed o 5 o# w,ic, t,e value% will be co'ied. /5 t,e cell value% a e co'ied* no lini% e%tabli%,ed and %o c,ange% #ade to t,e ange a e not e5lected in t,e c,a t. 0,e e i% al%o an o'tion wit, ega d to w,et,e 5o #atting i% lin-ed o co'ied. Figure 21-"$. Set (ata )abels #ialog

4e begin b( aug#enting t,e )ata1,eet %,eet b( adding a ow 5o t,e new utilit(* a% in $igu e 21 35 ?t,e new utilit( i% li%ted in ow 2@. Figure 21-"'. (ataSheet

+e6t* c eate t,e dialog %,own in $igu e 21;34. 4e ,ave u%ed t,e Re5Edit cont ol* w,ic, %i#ulate% t,e E6cel 5eatu e o5 c,oo%ing a ange u%ing t,e #ou%e. 2n5o tunatel(* t,i% cont ol i% not docu#ented in t,e ,el' 5ile%. ?Lou can get a li%t o5 it% ' o'e tie%* #et,od%* and event% in t,e "ic o%o5t Object > ow%e * but no ,el'.@ /n an( ca%e* we a e inte e%ted in onl( one o two ' o'e tie%* a% %,own in t,e u'co#ing code. 0,e LabelDataSeries ' ocedu e* w,ic, i% called w,en t,e #enu ite# i% %elected* %,ould be 'laced in a new %tanda d code #odule called basData"abels. 0,e )ecla ation% %ection o5 t,e code #odule ,a% %o#e #odule;level decla ation%* w,ic, a e %,own in E6a#'le 21;10. Exa&!le 21-19. he (eclarations Section o4 the bas(ata)abels +o#e -o#ule
*3tion 'x3licit ;rivate T 3e 0tDataLabels ?asDataLabel As 1oolean Label As Strin4 >ont9a5e As Strin4 >ontSiMe As Sin4le Color As Lon4 1old As 1oolean 2talic As 1oolean 'nd T 3e ;0blic ;0blic ;0blic ;0blic ;0blic Labels>or<ndo() As 0tDataLabels DataSeries As Series c;oints As 2nte4er bCo3 >or5attin4 As 1oolean oChart As Chart

Di5 cSeries as Lon4

+ote* in 'a ticula * t,e u%e ;de5ined t('e decla ation. 0,i% %ave% t,e o iginal data label% 5o a 'o%%ible 2ndo o'e ation. /t can ,old not onl( t,e data labelB% te6t* but al%o t,e 5o #atting o'tion% t,at can be %et u%ing t,i% utilit(. 0,e LabelDataSeries ' ocedu e* w,ic, i% %,own in E6a#'le 21;11* 5i %t ve i5ie% t,at a c,a t %,eet o e#bedded c,a t i% %elected. 0,en it %et% t,e global va iable oChart to e5e to t,i% c,a t. 0,i% va iable will be u%ed b( t,e dialog* %o it need% to be 'ublic. +e6t* it %et% t,e global va iable cSeries to t,e nu#be o5 data %e ie% in t,e c,a t. /5 t,e c,a t ,a% no data %e ie%* a #e%%age i% di%'la(ed. /5 eve (t,ing i% %ati%5acto (* t,e 1et )ata 3abel% dialog i% di%'la(ed. Exa&!le 21-11. he )abel(ataSeries Proce#ure
;0blic S0b LabelDataSeries() . +eri6 that a chart sheet or . an e5bedded chart is active. . 26 so= set it to oChart. Set oChart = 9othin4 26 ActiveHor8boo8.Sheets.Co0nt = 7 Then

Ms41ox "Active wor8boo8 has no charts. 'xitin4."= vb2n6or5ation 'xit S0b 'nd 26 *n 'rror :oTo 9oChartActive Set oChart = ActiveChart 26 9ot oChart 2s 9othin4 Then . Chec8 6or data series cSeries = oChart.SeriesCollection.Co0nt 26 cSeries = 7 Then Ms41ox "Active chart has no data series."= vb2n6or5ation 'xit S0b 'nd 26 . 26 so= dis3la dialo4 dl4DataLabels.Show 'lse Ms41ox "This 0tilit re/0ires that a chart be selected."= vb2n6or5ation 'nd 26 'xit S0b 9oChartActiveMs41ox "This 0tilit vb2n6or5ation 'xit S0b 'nd S0b re/0ires that a chart be selected."=

A5te c eating basData"abels* (ou %,ould c eate t,e dialog in $igu e 21;34* na#e it dl!Data"abels* and a%%ign t,e %t ing 81et )ata 3abel%8 to it% !a'tion ' o'e t(. 4e ,ave c eated %eve al cu%to# dialog% ea lie in t,e boo-* %o we will not go into an( detail% ,e e. ?Lou can de%ign (ou dialog di55e entl( i5 (ou wi%,. 0,e e i% not,ing %ac ed about #( de%ign.@ 0,e cont ol na#e% a e:
CmdCancel

$o t,e !ancel button


CmdSet"abels

$o t,e 1et 3abel% button


cmd7ndo

$o t,e 2ndo button


"blSeries

$o t,e 8!,oo%e a 1e ie%:8 label


"stSeries

$o t,e li%t bo6

402

"bl%an!e

$o t,e 8!,oo%e a Range 5o t,e 3abel%8 label


redit"abels

$o t,e Re5e ence Edito cont ol


*ra/ tions

$o t,e 5 a#e
/ t"ink

$o t,e 3in- o'tion button


/ tCo y

$o t,e !o'( o'tion button


chk/ tion

$o t,e !o'( $o #atting c,ec- bo6 Lou %,ould not ,ave an( t ouble dete #ining w,ic, na#e goe% wit, w,ic, cont ol ?w,ic, i% a bene5it o5 u%ing a na#ing convention* a5te all@. "o%t o5 t,e code be,ind t,e dialog i% ' ett( %t aig,t5o wa d. 0,e /nitialiGe event* %,own in E6a#'le 21;12* 5i %t 5ill% t,e li%t bo6 ?lstSeries@ wit, a li%t o5 all o5 t,e data %e ie% in t,e c,a t oChart. 0,en it initialiGe% %o#e o5 t,e ot,e cont ol%. Exa&!le 21-12. he /nitiali?e Event Proce#ure
;rivate S0b <ser>or5G2nitialiMe() . oChart is set to re6er to the active chart . cSeries has co0nt o6 series Di5 ds As Series . >ill the lstSeries >or 'ach ds 2n oChart.SeriesCollection lstSeries.Add2te5 ds.9a5e 9ext o3tCo3 .+al0e = Tr0e ch8*3tion.Ca3tion = "Co3 >or5attin4" ch8*3tion.Accelerator = ">" c5d<ndo.'nabled = >alse 'nd S0b

4e want t,e ca'tion ?and accele ato -e(@ o5 t,e c,ec- bo6 at t,e botto# o5 t,e dialog to c,ange ba%ed on t,e c,oice o5 o'tion button ?3in- o !o'(@. Aence* we need %o#e code in t,e a'' o' iate !lic- event%* a% %,own in E6a#'le 21;13. Exa&!le 21-1". he 3!tion .uttons> +lic1 Events

403

;rivate S0b o3tCo3 GClic8() . Set correct chec8 box label and enable ch8*3tion.Ca3tion = "Co3 >or5attin4" ch8*3tion.Accelerator = ">" ch8*3tion.'nabled = Tr0e 'nd S0b ;rivate S0b o3tLin8GClic8() . Set correct chec8 box label and enable ch8*3tion.Ca3tion = "Lin8 905ber >or5at" ch8*3tion.Accelerator = "9" ch8*3tion.'nabled = Tr0e 'nd S0b

A% u%ual* t,e co##and button%B !lic- event% a e %,o t. 0,e( a e %,own in E6a#'le 21;14. Exa&!le 21-1$. he c&#+ancel an# c&#Set)abels +lic1 Events
;rivate S0b c5dCancelGClic8() <nload Me 'nd S0b ;rivate S0b c5dSetLabelsGClic8() DoDataLabels 'nd S0b

0,e #ain 'o tion o5 t,e code* t,e DoDataLabels ' ocedu e %,own in E6a#'le 21;15 ?and ,ou%ed in t,e 2%e $o # #odule@* c,ec-% to %ee i5 a data %e ie% and label ange ,ave been %elected and co#'a e% t,ei %iGe%* w,ic, #u%t #atc, o el%e an e o #e%%age i% di%'la(ed. Exa&!le 21-1'. he (o(ata)abels Proce#ure
S0b DoDataLabels() Di5 i As 2nte4er Di5 rn4Labels As "an4e Di5 6nt As >ont . 2s a data series selectedQ :et its siMe. 26 lstSeries.List2ndex = %1 Then Ms41ox "@o0 50st select a data series."= vb2n6or5ation 'xit S0b 'lse Set DataSeries = oChart.SeriesCollection(lstSeries.List2ndex A . There will be an error i6 the chart does not s033ort data *n 'rror "es05e 9ext c;oints = DataSeries.;oints.Co0nt 26 'rr.905ber $( 7 Then Ms41ox "Charts o6 the selected t 3e do not s033ort data labels."= G vbCritical <nload Me 'xit S0b 'nd 26 'nd 26

1)

3oints

404

. :et the labels ran4e Set rn4Labels = "an4e(reditLabels.+al0e) 26 rn4Labels 2s 9othin4 Then Ms41ox "@o0 50st select a ran4e o6 cells e/0al in n05ber to " K G "the n05ber o6 data 3oints in the series."= vb2n6or5ation 'xit S0b 'nd 26 . Chec8 co0nts 26 c;oints $( rn4Labels.Co0nt Then Ms41ox "The n05ber o6 label cells (" K rn4Labels.Co0nt K G ") does not e/0al the n05ber o6 data 3oints (" K c;oints K G ") in the selected series."= vb2n6or5ation 'xit S0b 'nd 26 . Chec8 6or existin4 labels and save the5 26 DataSeries.?asDataLabels Then . Di5ension the arra "eDi5 Labels>or<ndo(1 To c;oints) . >ill arra >or i = 1 To c;oints Labels>or<ndo(i).?asDataLabel = DataSeries.;oints(i).?asDataLabel 26 Labels>or<ndo(i).?asDataLabel Then . Save the label text Labels>or<ndo(i).Label = DataSeries.;oints(i).DataLabel.Text . Save the 6or5attin4 Hith DataSeries.;oints(i).DataLabel.>ont Labels>or<ndo(i).>ont9a5e = .9a5e Labels>or<ndo(i).>ontSiMe = .SiMe Labels>or<ndo(i).Color = .Color Labels>or<ndo(i).1old = .1old Labels>or<ndo(i).2talic = .2talic 'nd Hith 'nd 26 9ext c5d<ndo.'nabled = Tr0e 'nd 26 . 9ow do data labels based on o3tions 26 o3tLin8 Then >or i = 1 To c;oints DataSeries.;oints(i).?asDataLabel = Tr0e DataSeries.;oints(i).DataLabel.Text = "=" K rn4Labels.;arent.9a5e G K "!" K rn4Labels.Cells(i).Address("e6erenceSt le-=xl"1C1) 26 ch8*3tion Then . Set n05ber 6or5at lin8 DataSeries.;oints(i).DataLabel.905ber>or5atLin8ed = Tr0e 'nd 26 9ext 'lse >or i = 1 To c;oints

DataSeries.;oints(i).?asDataLabel = Tr0e DataSeries.;oints(i).DataLabel.Text = rn4Labels.Cells(i).+al0e 26 ch8*3tion Then bCo3 >or5attin4 = Tr0e Hith DataSeries.;oints(i).DataLabel.>ont .9a5e = rn4Labels.Cells(i).>ont.9a5e .SiMe = rn4Labels.Cells(i).>ont.SiMe .1old = rn4Labels.Cells(i).>ont.1old .2talic = rn4Labels.Cells(i).>ont.2talic .Color = rn4Labels.Cells(i).>ont.Color 'nd Hith DataSeries.;oints(i).DataLabel.905ber>or5at = G rn4Labels.Cells(i).905ber>or5at 'lse bCo3 >or5attin4 = >alse 'nd 26 9ext 'nd 26 'nd S0b

0,e 2ndo co##and buttonB% !lic- event* w,ic, i% %,own in E6a#'le 21;16* e%to e% t,e o iginal data label% t,at a e %aved in t,e DoDataLabels ' ocedu e. Exa&!le 21-15. he c&#2n#oG+lic1 Event Proce#ure
;rivate S0b c5d<ndoGClic8() . "estore labels 6or DataSeries Di5 i As 2nte4er >or i = 1 To c;oints 26 Labels>or<ndo(i).?asDataLabel Then DataSeries.;oints(i).?asDataLabel = Tr0e DataSeries.;oints(i).DataLabel.Text = Labels>or<ndo(i).Label 26 bCo3 >or5attin4 Then . "estore 6or5attin4 Hith DataSeries.;oints(i).DataLabel.>ont .9a5e = Labels>or<ndo(i).>ont9a5e .SiMe = Labels>or<ndo(i).>ontSiMe .Color = Labels>or<ndo(i).Color .1old = Labels>or<ndo(i).1old .2talic = Labels>or<ndo(i).2talic 'nd Hith 'nd 26 'lse DataSeries.;oints(i).?asDataLabel = >alse 'nd 26 9ext c5d<ndo.'nabled = >alse 'nd S0b

Chapter ''% Smart Tags


/n t,i% c,a'te * / di%cu%% t,e ' og a##atic cont ol o5 %#a t tag%. +ote t,at to c eate cu%to# %#a t tag%* we need to u%e an a''lication t,at can c eate auto#ation %e ve % ?!O" )33%@* %uc, a% =i%ual >a%ic o =i%ual !FF. !u%to# %#a t tag% cannot be c eated in E6cel D&* alt,oug, e6i%ting %#a t tag% can be cont olled ' og a##aticall(.

''%$ -hat Are Smart Tags.


1ince %#a t tag% a e new to O55ice D&* let u% begin wit, a b ie5 de%c i'tion o5 t,ei 'u 'o%e. 0o illu%t ate t,e conce't o5 a %#a t tag* i#agine t,at (ou t('e a date* %uc, a% Nanua ( 1* 2002* in 4o d D& ?wit, %#a t tag% tu ned on@. 0,e %#a t tag %(%te# ecogniGe% t,e te6t 8Nanua ( 1* 20028 a% a date and unde line% it wit, a dotted line. /5 (ou t,en 'lace t,e #ou%e ove t,e date* a %#all button t,at loo-% li-e an 8i8 wit, a ci cle a ound it ?a %#a t tag action button@ a''ea %. !lic-ing on t,i% button d o'% down a li%t o5 action%* a% %,own in $igu e 22;1. Figure 22-1. he s&art tag actions &enu

$ o# t,i% #enu* a% t,e na#e %ugge%t%* we can c,oo%e an action to 'e 5o #* %uc, a% %c,eduling a #eeting on t,i% date. /ndeed* i5 we clic- on t,i% action* "ic o%o5t Outloo-B% a''oint#ent dialog will o'en* w,e ein we can %et u' t,e #eeting. $igu e 22;2 illu%t ate% t,e u%e o5 %#a t tag% in E6cel D&. Ae e we ,ave ente ed a %toc- tic-e %(#bol into a wo -%,eet cell* w,e eu'on leaving t,e cell E6cel D& will ecogniGe t,e %(#bol a% a %#a t tag t('e. ?E6cel D& co#e% wit, %o#e built;in %#a t tag ecogniGe %* a% can be %een 5 o# t,e %#a t tag tab o5 t,e Auto!o ect O'tion% dialog bo6.@ Figure 22-2. % s&art tag in Excel XP

L F

Tea4m09 77l'D

0,u%* %#a t tag% a e tool% t,at ecogniGe ce tain t('e% o5 data wit,in an O55ice docu#ent and ' e%ent t,e u%e wit, a li%t o5 ' ede5ined action%. ?At t,e u%e ;inte 5ace level* t,e %#a t tag o'tion% dialog can be eac,ed t, oug, t,e Auto!o ect O'tion% #enu ite# unde t,e 0ool% #enu.@

''%' SmartTag6ecognizer Ob1ect


0,e object t,at i% e%'on%ible 5o ecogniGing a 'a ticula t('e o5 data a% a %#a t tag i% called a smart tag recogni7er. 0,e%e ecogniGe % ta-e t,e 5o # o5 code in a )(na#ic 3in- 3ib a ( ?)33@ t,at i% loaded into E6celB% add e%% %'ace w,en E6cel i% 5i %t loaded. 0o illu%t ate* on #( %(%te#* t,e 5ollowing code:
Di5 str As S5artTa4"eco4niMer >or 'ach str 2n A33lication.S5artTa4"eco4niMers Deb04.;rint str.3ro42D K "J" K str.'nabled 9ext

' oduce% t,e out'ut:


>9a5e.S5artTa4JTr0e M*>L.S5artTa4JTr0e

indicating t,at two %#a t tag ecogniGe )33% a e loaded in E6celB% add e%% %'ace. ?A %ingle )33 #a( be u%ed to ecogniGe #o e t,an one t('e o5 %#a t tag.@ 4e al%o %ee 5 o# t,i% code t,at t,e 1#a t0agRecogniGe % collection ,old% a 1#a t0agRecogniGe object 5o eac, o5 t,e cu entl( available %#a t tag ecogniGe %. 0,e 1#a t0agRecogniGe object ,a% a ead;w ite Enabled ' o'e t( u%ed to enable o di%able t,e ecogniGe * a% well a% ead;onl( ' o'e tie% called ' og/) and $ull+a#e* w,ic, etu n t,e na#e o5 t,e ecogniGe ?t,e $ull+a#e ' o'e t( include% t,e 'at,@.

''%* SmartTag Ob1ect


0,e Range object ,a% a 1#a t0ag% ' o'e t( t,at etu n% t,e collection o5 all 1#a t0ag object%. O5 cou %e* a 1#a t0ag object e' e%ent% a %#a t tag. 3et u% di%cu%% t,e #o e i#'o tant #e#be % o5 t,e 1#a t0ag object* li%ted ne6t. ?0,e )elete #et,od i% %el5;e6'lanato (.@
C C C C C

)elete #et,od +a#e ' o'e t( )ownload2R3 ' o'e t( D"3 ' o'e t( 1#a t0agAction% ' o'e t(

0,e +a#e ' o'e t( o5 t,e 1#a t0ag object etu n% a 5ull( 7uali5ied na#e 5o t,e %#a t tag. A 5ull( 7uali5ied na#e con%i%t% o5 a D"3 na#e%'ace 2R/ 5ollowed b( a nu#be %ign ?U@ and t,en t,e tag na#e. $o in%tance* e5e ing to t,e %#a t tag in $igu e 22;2* t,e code:
Deb04.;rint st.9a5e

' int%:

408

0rn-sche5as%5icroso6t%co5-o66ice-s5artta4sOstoc8tic8er

)o not con5u%e t,e na#e o5 t,e %#a t tag wit, t,e te6t* %uc, a% />"* o5 t,e %#a t tag. 0,e e doe% not a''ea to be a %#a t tag #e#be t,at etu n% t,e %#a t tagB% te6t. 0,e )ownload2R3 ' o'e t( i% an o'tional 2R3 t,at i% %'eci5ied b( t,e %#a t tagB% c eato . /t ' ovide% an add e%% w,e e additional %#a t tag action% #a( be available 5o download. Acco ding to t,e "ic o%o5t docu#entation* t,e ead;onl( D"3 ' o'e t( o5 t,e %#a t tag object 8Retu n% a 1t ing e' e%enting a %a#'le o5 t,e D"3 t,at would be 'a%%ed to t,e action ,andle .8 0,e te # 8%a#'le8 could ce tainl( u%e cla i5ication. /n an( ca%e* 5o t,e %#a t tag in $igu e 22;2* t,e code:
Deb04.;rint st.#ML

' oduce% t,e %t ing:


$x5l x5lns-6a1="0rn-sche5as%5icroso6t% co5-o66ice-s5artta4s"($6a1-stoc8tic8er(21M$J6a1-stoc8tic8er($Jx5l(

w,ic, contain% t,e %#a t tagB% 5ull( 7uali5ied na#e a% well a% t,e tagB% te6t ?/>"@.

''%2 SmartTagAction Ob1ect


0,e 1#a t0agAction% ' o'e t( etu n% t,e 1#a t0agAction% collection o5 1#a t0agAction object%. A 1#a t0agAction object e' e%ent% an action t,at can be ta-en 5o a %#a t tag. A% an e6a#'le* e5e ing to $igu e 22;2* t,e code:
Di5 st As S5artTa4 Set st = A33lication."an4e("A1").S5artTa4s(1) >or i = 1 To st.S5artTa4Actions.Co0nt Deb04.;rint st.S5artTa4Actions(i).9a5e 9ext

' oduce% t,e out'ut:


2nsert re6reshable stoc8 3rice... Latest,0oteData Co53an "e3ortData "ecent9ews

w,ic, co e%'ond% to t,e 5ou action% in $igu e 22;2. /ncidentall(* t,e code:
Di5 st As S5artTa4 Di5 sta As S5artTa4Action >or 'ach sta 2n st.S5artTa4Actions Deb04.;rint sta.9a5e 9ext

doe% not ' int an(t,ing* no doe% it ' oduce an e o #e%%ageC 0,e 1#a t0agAction object ,a% an E6ecute #et,od t,at e6ecute% an action. Ae e i% an e6a#'le t,at e6ecute% t,e 5i %t action in $igu e 22;2:

S0b 'xec0teAS5artTa4() Di5 st As S5artTa4 Di5 sAction As Strin4 Di5 ws As Hor8sheet Set ws = A33lication.ActiveSheet sAction = "2nsert re6reshable stoc8 3rice..." . 2nvo8e a s5art ta4 6or the Microso6t tic8er s 5bol. Set st = ws."an4e("A1").S5artTa4s( G "0rn-sche5as%5icroso6t%co5-o66ice-s5artta4sOstoc8tic8er") st.S5artTa4Actions(sAction).'xec0te 'nd S0b

''%3 SmartTagOptions Ob1ect


0,e 1#a t0agO'tion% object* etu ned b( t,e 1#a t0agO'tion% ' o'e t( o5 t,e 4o -boo- object* ,a% two u%e5ul ' o'e tie%. 0,e )i%'la(1#a t0ag% ' o'e t( ta-e% a value 5 o# t,e 5ollowing enu#:
'n05 #lS5artTa4Dis3la Mode xl2ndicatorAnd10tton = 7 xlDis3la 9one = 1 xl10tton*nl = 2 'nd 'n05

0,u%* we can c,oo%e to di%'la( not,ing* a button onl(* o a button and indicato 5o eac, %#a t tag. 0,e E#bed1#a t0ag% ' o'e t( i% a >oolean ' o'e t( t,at dete #ine% w,et,e o not %#a t tag% a e %aved along wit, t,e wo -boo- ?alt,oug, t,e te # 8e#bed8 %ee#% to be a at,e 'oo c,oice o5 te #inolog(@. $o #o e in5o #ation on %#a t tag% and ,ow to c eate cu%to# %#a t tag% ?w,ic, a% we #entioned ea lie cannot be done 5 o# wit,in E6cel D& it%el5@* (ou can c,ec- out t,e %#a t tag 1)R on "ic o%o5tB% web %ite at: ,tt':EE#%dn.#ic o%o5t.co#Edownload%Ede5ault.a%'92R3TEcodeE%a#'le.a%'9u lTE"1)+ $/3E1E027E001E652E#%dnco#'o%itedoc.6#l.

Part &,+ Appendixes


A''endi6 A A''endi6 > A''endi6 ! A''endi6 ) A''endi6 E A''endi6 $ A''endi6 .

Appendix A% The Shape Ob1ect


+ow we want to ta-e a b ie5 loo- at t,e i%%ue o5 d awing 'ictu e% u%ing =>A code. 1ince t,i% %ubject i% not 5unda#ental to E6cel =>A ' og a##ing* we will be ve ( b ie5* but ,o'e5ull( t,i% int oduction will give (ou t,e nece%%a ( bac-g ound 5o 5u t,e %tud( u%ing t,e =>A ,el' 5ile%.

A%$ -hat &s the Shape Ob1ect.


Eac, E6cel %,eet ?c,a t%,eet o wo -%,eet@ and eac, E6cel c,a t ,a% a drawing layer u'on w,ic, we can 'lace d awing object%. A d awing object i% e' e%ented b( a 1,a'e object. A% u%ual* t,e 1,a'e object% 5o a %,eet a e %to ed in a 1,a'e% collection. 0,e !,a t object and t,e 4o -%,eet object bot, ,ave a 1,a'e% ' o'e t( t,at etu n% t,e collection o5 all 1,a'e object% d awn on t,e c,a t o wo -%,eet. 0,e e i% al%o a 1,a'eRange object t,at ,old% a collection o5 %elected 1,a'e object%* #uc, a% a Range object can contain a collection o5 %elected cell%. 0,e 1,a'eRange object allow% u% to %et t,e ' o'e tie% o5 a %ubcollection o5 all 1,a'e object%. 0,e 1,a'e; elated object% a e %,own in $igu e A;1. Figure %-1. he Sha!e-relate# objects

A%' A;Order

Eve ( 1,a'e object ,a% an o de * called it% 7-order * t,at indicate% t,e objectB% elative 'o%ition wit, e%'ect to an i#agina ( G;a6i% t,at co#e% di ectl( out o5 t,e #onito at ig,t angle%* towa d% t,e u%e * a% 'ictu ed in $igu e A;2. Figure %-2. /llustrating ?-or#er

0,e ead;onl( [O de &o%ition ' o'e t( o5 a 1,a'e object e'o t% t,e cu ent G;o de o5 t,e object w,ic,* incidentall(* i% t,e %a#e a% t,e objectB% inde6 wit,in t,e 1,a'e% collection. 1,a'e object% wit, a la ge G;o de a''ea on to' o5 object% wit, a %#alle G;o de . Aence* t,e 1,a'e object wit, G;o de e7ual to 1 i% 1,a'e%?1@ and lie% at t,e botto# o5 t,e 'ileC 0,e [O de #et,od %et% t,e G;o de o5 a 1,a'e object relati#e to ot,e object%. +ote t,at t,e #et,od doe% not %et t,e ab%olute G;o de . 0,e %(nta6 i%:
Sha3e*bTect.[*rder([*rderC5d)

w,e e ;/rderCmd i% one o5 t,e con%tant% in t,e 5ollowing enu# ?5 o# t,e "ic o%o5t O55ice object #odel@:
'n05 Mso[*rderC5d 5so1rin4To>ront 5soSendTo1ac8 = 5so1rin4>orward 5soSend1ac8ward 'nd 'n05 = 7 1 = 2 = B

0,u%* t,e G;o de can onl( be %et in t,e 5ollowing wa(%:


C C C C

"ove t,e object to t,e 5 ont o5 t,e G;o de . "ove t,e object to t,e bac- o5 t,e G;o de . "ove t,e object one 5o wa d in t,e G;o de H t,at i%* inc ea%e it% inde6 b( 1. "ove t,e object one bac-wa d in t,e G;o de H t,at i%* dec ea%e it% inde6 b( 1.

/ncidentall(* a% we ,ave %een* t,e !,a tObject object ,a% a ead;onl( [O de ' o'e t( t,at etu n% t,e G;o de o5 t,e !,a tObject. /t al%o ,a% > ing0o$ ont and 1end0o>ac- #et,od% 5o c,anging t,e G;o de .

A%* Creating Shapes

An Auto1,a'e i% a 1,a'e object t,at e' e%ent% a built;in d awing. 0o add a new Auto1,a'e object* we u%e t,e Add1,a'e #et,od* w,o%e %(nta6 i%:
Sha3es*bTect.AddSha3e(T 3e= Le6t= To3= Hidth= ?ei4ht)

0,e 'a a#ete Ty e i% t,e t('e o5 Auto1,a'e to c eate. /t can be an( one o5 t,e MsoA0toSha3eT 3e con%tant% in 0able A;1. 0,e e7ui ed 'a a#ete % "e*t and To %'eci5( t,e 'o%ition ?in 'oint% a% a 1ingle@ o5 t,e u''e le5t co ne o5 t,e bounding bo6 5o t,e Auto1,a'e object* #ea%u ed elative to t,e u''e ;le5t co ne o5 t,e containe object ?c,a t* c,a t %,eet* o wo -%,eet@. 0,e Width and 6ei!ht 'a a#ete % %'eci5( t,e widt, and ,eig,t ?in 'oint% a% a 1ingle@ o5 t,e bounding bo6 5o t,e Auto1,a'e. +ote t,at t,e t('e o5 a 1,a'e object can be c,anged b( %etting t,e Auto1,a'e0('e ' o'e t(. able %-1. -so%utoSha!e y!e +onstants ;an# *alues<
#%o1,a'e16'oint1ta ?94@ #%o1,a'e24'oint1ta ?95@ #%o1,a'e32'oint1ta ?96@ #%o1,a'e4'oint1ta ?91@ #%o1,a'e5'oint1ta ?92@ #%o1,a'e$lowc,a t!a d ?75@ #%o1,a'e$lowc,a t!ollate ?79@ #%o1,a'e$lowc,a t!onnecto ?73@ #%o1,a'e$lowc,a t)ata ?64@ #%o1,a'e$lowc,a t)eci%ion ?63@ #%o1,a'e3ine!allout2>o de andAccent>a ?122@ #%o1,a'e3ine!allout2+o>o de ?118@ #%o1,a'e3ine!allout3 ?111@ #%o1,a'e3ine!allout3Accent>a ?115@ #%o1,a'e3ine!allout3>o de andAccent>a ?123@ #%o1,a'e3ine!allout3+o>o de ?119@ #%o1,a'e3ine!allout4 ?112@ #%o1,a'e3ine!allout4Accent>a ?116@ #%o1,a'e3ine!allout4>o de andAccent>a ?124@ #%o1,a'e3ine!allout4+o>o de ?120@ #%o1,a'e"i6ed ?;2@ #%o1,a'e"oon ?24@ #%o1,a'e+o1(#bol ?19@ #%o1,a'e+otc,edRig,tA ow ?50@ #%o1,a'e+ot& i#itive ?138@

#%o1,a'e8'oint1ta ?93@ #%o1,a'e$lowc,a t)ela( ?84@ #%o1,a'eAction>utton>ac-o & eviou% #%o1,a'e$lowc,a t)i ectAcce%%1to age ?129@ ?87@ #%o1,a'eAction>utton>eginning ?131@ #%o1,a'eAction>utton!u%to# ?125@ #%o1,a'eAction>utton)ocu#ent ?134@ #%o1,a'eAction>uttonEnd ?132@ #%o1,a'eAction>utton$o wa do +e6t ?130@ #%o1,a'eAction>uttonAel' ?127@ #%o1,a'eAction>uttonAo#e ?126@ #%o1,a'eAction>utton/n5o #ation ?128@ #%o1,a'eAction>utton"ovie ?136@ #%o1,a'eAction>uttonRetu n ?133@ #%o1,a'eAction>utton1ound ?135@ #%o1,a'eA c ?25@ #%o1,a'e>alloon ?137@ #%o1,a'e>entA ow ?41@ #%o1,a'e>ent2'A ow ?44@ #%o1,a'e>evel ?15@ #%o1,a'e>loc-A c ?20@ #%o1,a'e!an ?13@ #%o1,a'e!,ev on ?52@ #%o1,a'e!i cula A ow ?60@ #%o1,a'e!loud!allout ?108@ #%o1,a'e$lowc,a t)i%'la( ?88@ #%o1,a'e$lowc,a t)ocu#ent ?67@ #%o1,a'e$lowc,a tE6t act ?81@ #%o1,a'e$lowc,a t/nte nal1to age ?66@ #%o1,a'e$lowc,a t"agnetic)i%- ?86@ #%o1,a'e$lowc,a t"anual/n'ut ?71@ #%o1,a'e$lowc,a t"anualO'e ation ?72@ #%o1,a'e$lowc,a t"e ge ?82@

#%o1,a'e$lowc,a t"ultidocu#ent ?68@ #%o1,a'eOctagon ?6@ #%o1,a'e$lowc,a tO55'age!onnecto ?74@ #%o1,a'eOval ?9@ #%o1,a'e$lowc,a tO ?78@ #%o1,a'eOval!allout ?107@ #%o1,a'e$lowc,a t& ede5ined& oce%% ?65@ #%o1,a'e&a allelog a# ?2@ #%o1,a'e$lowc,a t& e'a ation ?70@ #%o1,a'e&entagon ?51@ #%o1,a'e$lowc,a t& oce%% ?61@ #%o1,a'e&la7ue ?28@ #%o1,a'e$lowc,a t&unc,ed0a'e ?76@ #%o1,a'eOuadA ow ?39@ #%o1,a'e$lowc,a t1e7uentialAcce%%1to age #%o1,a'eOuadA ow!allout ?59@ ?85@ #%o1,a'e$lowc,a t1o t ?80@ #%o1,a'eRectangle ?1@ #%o1,a'e$lowc,a t1to ed)ata ?83@ #%o1,a'eRectangula !allout ?105@ #%o1,a'e$lowc,a t1u##ingNunction ?77@ #%o1,a'eRegula &entagon ?12@ #%o1,a'e$lowc,a t0e #inato ?69@ #%o1,a'eRig,tA ow ?33@ #%o1,a'e$olded!o ne ?16@ #%o1,a'eRig,tA ow!allout ?53@

#%o1,a'e! o%% ?11@ #%o1,a'e!ube ?14@ #%o1,a'e!u ved)ownA ow ?48@ #%o1,a'e!u ved)ownRibbon ?100@ #%o1,a'e!u ved3e5tA ow ?46@ #%o1,a'e!u vedRig,tA ow ?45@ #%o1,a'e!u ved2'A ow ?47@ #%o1,a'e!u ved2'Ribbon ?99@ #%o1,a'e)ia#ond ?4@ #%o1,a'e)onut ?18@ #%o1,a'e)ouble> ace ?27@ #%o1,a'e)ouble> ac-et ?26@ #%o1,a'e)ouble4ave ?104@ #%o1,a'e)ownA ow ?36@ #%o1,a'e)ownA ow!allout ?56@ #%o1,a'e)ownRibbon ?98@ #%o1,a'eE6'lo%ion1 ?89@ #%o1,a'eE6'lo%ion2 ?90@ #%o1,a'e$lowc,a tAlte nate& oce%% ?62@

#%o1,a'eAea t ?21@ #%o1,a'eAe6agon ?10@ #%o1,a'eAo iGontal1c oll ?102@ #%o1,a'e/%o%cele%0 iangle ?7@ #%o1,a'e3e5tA ow ?34@ #%o1,a'e3e5tA ow!allout ?54@ #%o1,a'e3e5t> ace ?31@ #%o1,a'e3e5t> ac-et ?29@ #%o1,a'e3e5tRig,tA ow ?37@ #%o1,a'e3e5tRig,tA ow!allout ?57@ #%o1,a'e3e5tRig,t2'A ow ?40@ #%o1,a'e3e5t2'A ow ?43@ #%o1,a'e3ig,tning>olt ?22@ #%o1,a'e3ine!allout1 ?109@ #%o1,a'e3ine!allout1Accent>a ?113@ #%o1,a'e3ine!allout1>o de andAccent>a ?121@ #%o1,a'e3ine!allout1+o>o de ?117@ #%o1,a'e3ine!allout2 ?110@ #%o1,a'e3ine!allout2Accent>a ?114@

#%o1,a'eRig,t> ace ?32@ #%o1,a'eRig,t> ac-et ?30@ #%o1,a'eRig,t0 iangle ?8@ #%o1,a'eRoundedRectangle ?5@ #%o1,a'eRoundedRectangula !allout ?106@ #%o1,a'e1#ile($ace ?17@ #%o1,a'e1t i'edRig,tA ow ?49@ #%o1,a'e1un ?23@ #%o1,a'e0 a'eGoid ?3@ #%o1,a'e2'A ow ?35@ #%o1,a'e2'A ow!allout ?55@ #%o1,a'e2')ownA ow ?38@ #%o1,a'e2')ownA ow!allout ?58@ #%o1,a'e2'Ribbon ?97@ #%o1,a'e20u nA ow ?42@ #%o1,a'e=e tical1c oll ?101@ #%o1,a'e4ave ?103@

0,e %,o t ' og a# in E6a#'le A;1 will di%'la( eac, Auto1,a'e* along wit, it% Auto1,a'e0('e* 5o 0.5 %econd%. ?/t %,ould be un on a blan- wo -%,eet. Lou can inte u't t,i% ' og a# at an( ti#e b( %t i-ing !t l;> ea-.@ 0,e )ela( %ub outine t,at it call% i% %,own in E6a#'le A;2. Exa&!le %-1. (is!laying Each %utoSha!e
S0b Dis3la A0toSha3es() Di5 sh As Sha3e Di5 i As 2nte4er Set sh = ActiveSheet.Sha3es.AddSha3e(1= 177= 177= F2= F2) >or i = 1 To 1BD sh.A0toSha3eT 3e = i sh.+isible = Tr0e ActiveSheet.Cells(1= 1).+al0e = sh.A0toSha3eT 3e Dela 7.& 9ext i 'nd S0b

Exa&!le %-2. he (elay Proce#ure


;0blic S0b Dela (rTi5e As Sin4le) .Dela rTi5e seconds (5in=.71= 5ax=B77) Di5 *ldTi5e As +ariant .Sa6t net 26 rTi5e $ 7.71 *r rTi5e ( B77 Then rTi5e = 1 *ldTi5e = Ti5er Do Do'vents Loo3 <ntil Ti5er % *ldTi5e (= rTi5e 'nd S0b

415

%.".1 he extFra&e 3bject


Eac, 1,a'e object ,a% a te6t 5 a#e a%%ociated wit, it t,at ,old% an( te6t a%%ociated wit, t,e object. 0,e 0e6t$ a#e ' o'e t( etu n% t,i% 0e6t$ a#e object. 0,e 0e6t$ a#e object ,a% a !,a acte % ' o'e t( t,at etu n% a !,a acte % collection. 0,i% collection can %et t,e te6t in t,e te6t 5 a#e. $o in%tance* t,e code in E6a#'le A;3 add% a ectangle to t,e active %,eet and al%o add% te6t to t,e ectangle and %et% t,e align#ent 5o t,e te6t 5 a#e. Exa&!le %-". he %##,ectangle Proce#ure
S0b Add"ectan4le() Hith ActiveSheet.Sha3es.AddSha3e(5soSha3e"ectan4le= 17= 17= 277= 177).Text>ra5e .Characters.Text = "This is a rectan4le" .?oriMontalAli4n5ent = xl?Ali4nCenter .+erticalAli4n5ent = xl+Ali4nCenter 'nd Hith 'nd S0b

%.".2 he FillFor&at 3bject


0,e $ill$o #at object %et% va iou% 5o #atting 5o a 1,a'e object. /t i% acce%%ed u%ing t,e $ill ' o'e t( o5 t,e 1,a'e object. A#ong t,e ' o'e tie% o5 t,e $ill$o #at object a e t,e >ac-!olo * $o e!olo * &atte n* and =i%ible ' o'e tie%. 0o %et one o5 t,e colo ' o'e tie%* we u%e t,e R.> colo #odel* a% in t,e 5ollowing e6a#'le:
sh.>ill.>oreColor.":1 = ":1(7= 7= 2&&)

%."." Exa&!les
0o illu%t ate t,e u%e o5 Auto/hapes* E6a#'le A;4 in%e t% a da#'ened %ine cu ve o5 %#all %ta % in t,e d awing la(e . Exa&!le %-$. (rawSine2I to :enerate a (a&!ene# Sine +urve o4 S&all Stars
S0b DrawSine2() . Da53ened sine wave o6 s5all stars Const 3i = B.1L1N Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 i As 2nte4er x As Sin4le= As Sin4le rn4 As "an4e . >or startin4 3oint n As Sin4le . C cle len4th in inches 8 As 2nte4er . 8 stars Scale@ As Sin4le . +ertical scalin4 sSiMe As Sin4le . Star siMe sDa531 As Sin4le . Da53enin4 6actor sDa532 As Sin4le . Da53enin4 6actor cC cles As 2nte4er . 905ber o6 c cles sh As Sha3e StartLe6t As 2nte4er StartTo3 As 2nte4er

. Startin4 3osition StartLe6t = ActiveCell.Le6t

StartTo3 = ActiveCell.To3 cC cles = B sDa531 = 1 sDa532 = 7.2 n = 2 8 = 27 Scale@ = 7.& sSiMe = A33lication.2nchesTo;oints(7.1) . Loo3 6or 6irst c0rve with 3hase shi6t >or i = 1 To cC cles R 8 x = n R i J 8 = Scale@ R Sin((2 R 3i R i) J 8 A n) R G (sDa531 J (x A sDa532)) = A33lication.2nchesTo;oints( ) x = A33lication.2nchesTo;oints(x) Set sh = ActiveSheet.Sha3es.AddSha3e G (5soSha3e&3ointStar= StartLe6t A x= StartTo3 A = sSiMe= sSiMe) sh.>ill.>oreColor.":1 = ":1(1E2= 1E2= 1E2) . 2&\ 4ra sh.>ill.+isible = 5soTr0e 9ext i 'nd S0b

0,e out'ut 5 o# t,i% code i% %,own in $igu e A;3. Figure %-". % #a&!ene# sine wave o4 stars

0,e code in E6a#'le A;5 ' oduce% a ando# %e ie% o5 %ta %* eac, containing a %ingle lette t,at toget,e %'ell% a na#e. +ote t,at eac, ti#e t,e ' og a# i% un* t,e 'atte n i% di55e ent. Exa&!le %-'. +o#e to Pro#uce a ,an#o& Series o4 Stars
S0b Draw9a5e() . "ando5 3lace5ent o6 lar4e stars with na5e Const 3i = B.1L1N Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 i As 2nte4er x As Sin4le= As Sin4le M As Sin4le rn4 As "an4e . >or startin4 3oint n As Sin4le . C cle len4th in inches 8 As 2nte4er . 8 stars sSiMe As Sin4le . Star siMe sh As Sha3e s9a5e As Strin4 . 9a5e to dis3la StartLe6t As 2nte4er StartTo3 As 2nte4er

Tea4m19 77l'D

. Startin4 3osition StartLe6t = ActiveCell.Le6t StartTo3 = ActiveCell.To3 s9a5e = "Steven "o5an" n = & 8 = Len(s9a5e) sSiMe = A33lication.2nchesTo;oints(7.&) "ando5iMe Ti5er M = 7O . Loo3 6or 6irst c0rve with 3hase shi6t >or i = 1 To 8 26 Mid(s9a5e= i= 1) $( " " Then x = n R i J 8 x = A33lication.2nchesTo;oints(x) . :et rando5 7 or 1. :o 03 or down accordin4l . 26 2nt(2 R "nd) = 7 Then M = M A 7.2 'lse M = M % 7.2 'nd 26 = A33lication.2nchesTo;oints(M) Set sh = ActiveSheet.Sha3es.AddSha3e G (5soSha3e&3ointStar= StartLe6t A x= StartTo3 A sSiMe) . Add shadin4 sh.>ill.>oreColor.":1 = ":1(2B7= 2B7= 2B7) sh.>ill.+isible = 5soTr0e . Add text sh.Text>ra5e.Characters.Text = Mid(s9a5e= i= 1) sh.Text>ra5e.Characters.>ont.SiMe = 17 sh.Text>ra5e.Characters.>ont.9a5e = "Arial" sh.Text>ra5e.Characters.>ont.1old = Tr0e 'nd 26 9ext i 'nd S0b

= sSiMe=

0,e out'ut 5 o# t,i% code i% %,own in $igu e A;4. Figure %-$. ,an#o& stars s!elling a na&e

E6a#'le A;6 ' int% a ,('oc(cloid. ?/t #a( ta-e a 5ew #inute% to co#'lete.@ Exa&!le %-5. he (rawCy!ocycloi# Proce#ure

418

S0b Draw? 3oc cloid() . Draw h 3oc cloid o6 s5all stars Const 3i = B.1L1N Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 Di5 t As Sin4le i As 2nte4er x As Sin4le= As Sin4le rn4 As "an4e . >or startin4 3oint n As Sin4le 8 As 2nte4er sSiMe As Sin4le . Star siMe r As 2nte4er r7 As 2nte4er "1 As 2nte4er sh As Sha3e sc As Sin4le StartLe6t As 2nte4er StartTo3 As 2nte4er

. Startin4 3osition StartLe6t = ActiveCell.Le6t StartTo3 = ActiveCell.To3 r = 1 r7 = B R r "1 = D R r n = L77 8 = L sc = 7.1 sSiMe = A33lication.2nchesTo;oints(7.7B) . Start c0rve at insertion 3oint Set rn4 = ActiveCell >or i = 1 To n t = 8 R 3i R i J n x = ("1 % r) R Cos(t) A r7 R Cos(t R ("1 % r) J r) = ("1 % r) R Sin(t) % r7 R Sin(t R ("1 % r) J r) x = sc R x = sc R x = A33lication.2nchesTo;oints(x) = A33lication.2nchesTo;oints( ) Set sh = ActiveSheet.Sha3es.AddSha3e G (5soSha3e&3ointStar= StartLe6t A x= StartTo3 A = sSiMe= sSiMe) 9ext i 'nd S0b

0,e e%ult% a e %,own in $igu e A;5. ?0,e %#all ve tical ba in $igu e A;5 indicate% t,e le5t edge o5 t,e active cell. Figure %-'. % hy!ocycloi#

A%2 (iagram0 (iagram4ode0 and (iagram4odeChildren Ob1ects


)iag a#% a e %'ecial t('e% o5 %,a'e% t,at o ganiGe data vi%uall(. $igu e A;6 %,ow% a diag a#. Figure %-5. % #iagra&

0,e e a e %i6 t('e% o5 diag a#%* a% %,own in t,e )iag a# dialog bo6 in $igu e A;7. Figure %-6. he (iagra& #ialog box

$igu e A;8 %,ow% t,e 'o tion o5 t,e E6cel D& object #odel devoted to t,e )iag a# object. Figure %-7. he (iagra& object an# its chil#ren

0able A;2 %,ow% t,e #e#be % o5 t,e )iag a# object. able %-2. -e&bers o4 the (iagra& object !onve t &a ent ! eato Reve %e +ode% 0('e

A''lication Auto$o #at Auto3a(out

0,e #e#be % o5 t,e )iag a#+ode object a e a bit #o e inte e%ting* a% %,own in 0able A;3. able %-". -e&bers o4 the (iagra&No#e object )iag a# Re'lace+ode 3a(out Root "ove+ode 1,a'e +e6t+ode 1wa'+ode &a ent 0e6t1,a'e & ev+ode 0 an%5e !,ild en

Add+ode A''lication !,ild en !lone+ode ! eato )elete

O5 cou %e* t,e )iag a#+ode!,ild en object e' e%ent% t,e collection o5 )iag a# node% t,at a e c,ild en o5 a %'eci5ic node. 0,e )iag a#+ode!,ild en objectB% #e#be % a e %,own in 0able A;4. able %-$. -e&bers o4 the (iagra&No#e+hil#ren object ! eato &a ent $i %t!,ild 1electAll /te# 3a%t!,ild

I+ewEnu# Add+ode A''lication !ount

4e can u%e t,e !,ild en ' o'e t( o5 t,e )iag a#+ode object to etu n t,i% collection object. $o in%tance* to get t,e la%t c,ild o5 a node* we can w ite:
ActiveSheet.Sha3es(1).Dia4ra5.9odes(1).Children.LastChild

2n5o tunatel(* t,e ' og a##atic a%'ect% o5 t,e )iag a#; elated object% do not %ee# to be 5unctioning co ectl( ?a% o5 1e vice &ac- 1 o5 E6cel D&@* a% we will %ee %,o tl(. 0o c eate a diag a# ' og a##aticall(* we u%e t,e Add)iag a# #et,od o5 t,e 1,a'e% collection o5 t,e wo -%,eet. 0,e 5ollowing code c eate% t,e diag a# in $igu e A;6. +ote* ,oweve * t,at t,e e

%ee#% to be a ' oble# in adding o editing te6t 5o t,e node% in a diag a#. $ an-l(* / could not get t,e 5eatu e to wo - at all. Even "ic o%o5tB% own e6a#'le gene ate% an e o ?w,o%e #e%%age i% totall( u%ele%%@ on #( &!. ?/ even t ied eco ding a #ac o t,at c,ange% t,e te6t and t,en e'la(ing t,e #ac o* but t,at too gene ate% an e o C@ Ae e i% t,e code:
S0b 'xa53leGDia4ra5() Di5 ws As Hor8sheet Set ws = A33lication.ActiveSheet Di5 Di5 Di5 Di5 Di5 Di5 sh As Sha3e dia As Dia4ra5 node1 As Dia4ra59ode node2*61 As Dia4ra59ode nodeB*61 As Dia4ra59ode nodeL*62 As Dia4ra59ode

Set sh = ws.Sha3es.AddDia4ra5(5soDia4ra5*r4Chart= 7= 7= B77= B77) . Add 6irst node. 9ote the odd s ntax= since this node is not a child. Set node1 = sh.Dia4ra59ode.Children.Add9ode . Add s0bse/0ent nodes Set node2*61 = node1.Children.Add9ode Set nodeB*61 = node1.Children.Add9ode Set nodeL*62 = node2*61.Children.Add9ode . This wor8s %% can read the text Ms41ox node1.TextSha3e.Text>ra5e.Characters.Text . This does not wor8 %% cannot set the text!! node1.TextSha3e.Text>ra5e.Characters.Text = "?a5let" 'nd S0b

Appendix B% 5etting the &nstalled Printers


A% di%cu%%ed in !,a'te 10* t,e Active& inte ' o'e t( can %et t,e active ' inte . 0,i% ai%e% t,e i%%ue o5 ,ow to dete #ine t,e in%talled ' inte % on a given co#'ute . 2n5o tunatel(* =>A doe% not %ee# to ' ovide a wa( to do t,i%. ?=i%ual >a%ic ,a% a & inte % collection* but =i%ual >a%ic 5o A''lication% doe% not.@ /n t,i% a''endi6* we de%c ibe a ' og a# 5o getting t,i% ' inte in5o #ation. A% #entioned in !,a'te 10* t,i% ' og a# u%e% t,e 4indow% A&/. 0o u%e t,i% ' og a#* ju%t t('e it into (ou own code* a% de%c ibed ,e e. 0,e 5i %t %te' i% to decla e %o#e %'ecial con%tant% in t,e )ecla ation% %ection o5 a %tanda d #odule:
;0blic Const P'@G'9<M'"AT'GS<1GP'@S = K?D ;0blic Const ?P'@GL*CALGMAC?29' = K?D7777772 ;0blic Const S<CC'SS = 7K

+e6t* we need to decla e a user-defined type. 4e ,ave not di%cu%%ed t,e%e data %t uctu e% in t,i% boo-* but a u%e ;de5ined t('e i% e%%entiall( ju%t a cu%to# data t('e. Ente t,e 5ollowing code into t,e )ecla ation% %ection:
T 3e >2L'T2M' dwLowDateTi5e As Lon4 dw?i4hDateTi5e As Lon4 'nd T 3e

0,en we need to decla e t, ee A&/ 5unction%. A% (ou can %ee* t,e%e a e elativel( co#'licated 5unction% a% =>A 5unction% go* but not a% A&/ 5unction% go. Ente t,e 5ollowing in t,e )ecla ation% %ection:
Declare >0nction "e4*3enPe 'x Lib "adva3iB2.dll" Alias G ""e4*3enPe 'xA" (1 +al hPe As Lon4= 1 +al l3S0bPe As G Strin4= 1 +al 0l*3tions As Lon4= 1 +al sa5Desired As G Lon4= 3h8"es0lt As Lon4) As Lon4 Declare >0nction "e4'n05Pe 'x Lib "adva3iB2.dll" Alias G ""e4'n05Pe 'xA" (1 +al hPe As Lon4= 1 +al dw2ndex As G Lon4= 1 +al l39a5e As Strin4= l3cb9a5e As Lon4= 1 +al G l3"eserved As Lon4= 1 +al l3Class As Strin4= l3cbClass G As Lon4= l36tLastHriteTi5e As >2L'T2M') As Lon4 Declare >0nction "e4ClosePe Lib "adva3iB2.dll" G (1 +al hPe As Lon4) As Lon4

4e a e now ead( 5o t,e #ain ' ocedu e* %,own in E6a#'le >;1* w,ic, will e6t act t,e na#e% o5 t,e in%talled ' inte % 5 o# t,e 4indow% egi%t (. Exa&!le .-1. he :et/nstalle#Printers Proce#ure
;0blic S0b :et2nstalled;rinters(1 "e6 s;rinters() As G Strin4= 1 "e6 c;rinters As Lon4) . Sets c;rinters to the n05ber o6 installed 3rinters. . SiMes and 6ills s;rinters arra with the na5es . o6 these 3rinters. Di5 6t As >2L'T2M'

Di5 Di5 Di5 Di5

Pe ?andle As Lon4 Pe 9a5e As Strin4 Pe Len As Lon4 "es3onse As Lon4

*n 'rror :oTo '""G29STALL'DG;"29T'"S "eDi5 s;rinters(1 To &) c;rinters = 7 . *3en re4istr 8e whose s0b8e s are installed 3rinters "es3onse = "e4*3enPe 'x(?P'@GL*CALGMAC?29'= G "S@ST'MIC0rrentControlSetIControlI;rintI;rinters"= G 7= P'@G'9<M'"AT'GS<1GP'@S= Pe ?andle) . 26 'rror dis3la 5essa4e and exit 26 "es3onse $( S<CC'SS Then Ms41ox "Co0ld not o3en the re4istr 'xit S0b 'nd 26

8e ."

. Loo3 to 4et s0b8e s Do Pe Len = 1777 . ;lent o6 roo5 6or 3rinter na5e Pe 9a5e = Strin4(Pe Len= 7) . >ill with 7s "es3onse = "e4'n05Pe 'x(Pe ?andle= c;rinters= G Pe 9a5e= Pe Len= 7K= vb90llStrin4= 7K= 6t) . 26 0ns0ccess60l= then exit 26 "es3onse $( S<CC'SS Then 'xit Do . 9ext 6ree index c;rinters = c;rinters A 1 . Ma8e roo5 i6 necessar 26 <1o0nd(s;rinters) $ c;rinters Then "eDi5 ;reserve s;rinters(1 To c;rinters A &) 'nd 26 . Add to arra s;rinters(c;rinters) = Le6t(Pe 9a5e= Pe Len) Loo3 "e4ClosePe 'xit S0b Pe ?andle

'""G29STALL'DG;"29T'"SMs41ox 'rr.Descri3tion 'xit S0b 'nd S0b

0,e :et2nstalled;rinters ' ocedu e ,a% two 'a a#ete %: a 1t ing a a( na#ed sPrinters and a 3ong na#ed cPrinters. 0,e ' ocedu e will %et t,e value o5 cPrinters to t,e nu#be o5 in%talled ' inte % and e%iGe and 5ill t,e sPrinters a a( wit, t,e na#e% o5 t,e ' inte %. E6a#'le >;2 %,ow% ,ow to u%e t,e :et2nstalled;rinters %ub outine. /t %i#'l( gat,e % t,e ' inte na#e% in a %ingle 1t ing va iable and di%'la(% t,at va iable.

424

Exa&!le .-2. +alling the :et/nstalle#Printers Proce#ure


S0b Dis3la 2nstalled;rinters() Di5 s;rinters() As Strin4 Di5 c;rinters As Lon4 Di5 i As 2nte4er Di5 5s4 As Strin4 . :et the installed 3rinters :et2nstalled;rinters s;rinters()= c;rinters . Create the 5essa4e and dis3la 5s4 = "" it

>or i = 1 To c;rinters 5s4 = 5s4 K s;rinters(i) K vbCrL6 9ext i Ms41ox 5s4= = c;rinters K " ;rinters" 'nd S0b

0,e out'ut o5 t,i% #ac o on #( %(%te# i% %,own in $igu e >;1. Figure .-1. /nstalle# !rinters

One wo d o5 caution: be5o e e6ecuting t,e :et2nstalled;rinters %ub outine 5o t,e first ti#e ?t, oug, t,e Dis3la 2nstalled;rinters #ac o o an( b( ot,e #ean%@* be %u e to %ave all o5 (ou wo - in all o5 (ou o'en a''lication%. /5 (ou ,ave #ade an e o in t('ing in t,i% ' og a#* t,e e%ult #a( be a %(%te#;wide co#'ute c a%,* in w,ic, ca%e (ou will loo%e all un%aved wo -

Appendix C% Command Bar Controls


/n t,i% a''endi6* we ' e%ent a li%t o5 built;in co##and;ba cont ol%* along wit, t,ei /) nu#be % 5o u%e wit, t,e Add #et,od o5 t,e !o##and>a !ont ol% object.

C%$ Built;in Command;Bar Controls


(ommand-Bar (ontrols and !heir (orresponding " ?umbers S!u%to#Z K1'elling K1ave K& int K+ew K!o'( !uKt K&a%te O'en !anBt Re'eat K"ic o%o5t 4o d !lea !ontent% !u%to# K&igg( >an!u%to# K)ouble 2nde line !u%to# !u%to# K!lo%e Auto$o #at K$o #at &ainte & int & eKview !u%to# K>old K/talic K2nde line K!u%to# K)a - 1,ading KAlign 3e5t KAlign Rig,t K!ente KNu%ti5( 4,atB% K0,i%9 !u%to# K2ndo 1 2 3 4 18 19 21 22 23 37 42 47 51 52 59 60 67 68 106 107 108 109 112 113 114 115 117 118 120 121 122 123 124 126 128

KRedo K3ine 0eK6t >o6 K$ind $ile K0o' >o de K>otto# >o de K3e5t >o de KRig,t >o de A''l( /n%ide >o de % KOut%ide >o de % !lea >o de K. ou' K2ng ou' > ing to $ onKt 1end to >acK> ing K$o wa d 1end K>ac-wa d $Kull 1c een K1elect Object% KReco d +ew "ac o K"ac o% 1te' "ac o Re%u#e "ac o $li' KAo iGontal $li' K=e tical Rotate KRig,t Rotate K3e5t K$ ee5o # K>o de % K) awing KEdit &oint% 1o t KA%cending 1o t )e%Kcending KEdit >o6 K!,ec- >o6 K!o#bo >o6 !ont ol & o'e tie% 3oc- !ell KAuto1u# KA ow &age 1etKu' K1t(le Routing 1li' "ic o%o5t K"ail "ic o%o5t KAcce%% "ic o%o5t K1c,eduleF

129 130 139 140 145 146 147 148 149 150 151 164 165 166 167 170 171 178 182 184 186 187 189 196 197 198 199 200 203 204 206 210 211 219 220 221 222 225 226 243 247 254 259 262 264 265

Tea,m295Fly

"ic o%o5t =i%ual K$o6& o "ic o%o5t K&owe &oint "ic o%o5t &K oject !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# K!a#e a !u%to# K>utton !u%to# !u%to# K1t i-et, oug, )elete )elete KRow% )elete K!olu#n% !Kell% KRow% K!olu#n% KA ange K1'lit K+ew 4indow KAcce't o Reject !,ange% RKe'lace Reg Kou' K3;) 1eKnd K1et & int A ea K/n%e t "1 E6cel 4.0 "ac o K!lea $o #atting K&a%te $o #atting K&a%te =alue% KRig,t K)own KE7ual 1ign K&lu% 1ign K"inu% 1ign K"ulti'lication 1ign K)ivi%ion 1ign KE6'onentiation 1ign K3e5t &a ent,e%i%

266 267 269 270 271 272 273 274 275 276 278 279 280 281 282 283 286 290 292 293 294 295 296 297 298 302 303 305 313 338 339 363 364 365 368 369 370 371 372 373 374 375 376 377 378 379

KRig,t &a ent,e%i% K!olon K!o##a K&e cent 1ign K)olla 1ign &a%te $unction K!on%t ain +u#e ic KD3 3e5t >o de KD3 Rig,t >o de KD3 0o' >o de K3ig,t 1,ading K1,adow K!u enc( 1t(le K&e cent 1t(le K!o##a 1t(le K/nc ea%e )eci#al K)ec ea%e )eci#al K$ont !olo K"e ge and !ente K/nc ea%e $ont 1iGe K)ec ea%e $ont 1iGe K=e tical 0e6t KRotate 0e6t 2' KRotate 0e6t )own )i%t ibute KAo iGontall( K1c ibble !u%to# KA ea !,a t K>a !,a t K!olu#n !,a t K1tac-ed !olu#n !,a t K3ine !,a t K&ie !,a t K3;) A ea !,a t K3;) >a !,a t K3;) !lu%te ed !olu#n !,a t K3;) !olu#n !,a t K3;) 3ine !,a t K3;) &ie !,a t K?DL@ 1catte !,a t K3;) 1u 5ace !,a t KRada !,a t K=olu#eEAig,;3ow;!lo%e !,a t K)e5ault !,a t K!,a t 4iGa d K=alue A6i% . idline%

380 381 382 383 384 385 387 389 390 391 393 394 395 396 397 398 399 401 402 403 404 405 406 407 408 409 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 434 435 436 437

K!atego ( A6i% . idline% K3egend K1,ow Outline 1(#bol% K1elect =i%ible !ell% K1elect !u ent Region K$ eeGe &ane% K[oo# /n K[oo# Out KO'tion >utton K1c oll >a K3i%t >o6 K)oug,nut !,a t KRe#ove )e'endent A ow% 0 ace K)e'endent% KRe#ove & ecedent A ow% Re#ove KAll A ow% KAttac, +ote K2'date $ile K0oggle Read Onl( K4iGa d AutoK$ilte KRe5 e%, )ata K$ield 1,ow K&age% K1,ow )etail 0 ace KE o KAide )etail )i%t ibute K=e ticall( K. ou' >o6 K1'inne 0aKb O de KRun )ialog K!o#bination 3i%t;Edit K!o#bination ) o';)own Edit K3abel !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# !u%to# 0oggle . id K0 ace & ecedent% K!ode

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 467 468 469 470 471 475 476 477 478 479 480 481 482 483 484 485 486 488

K)ialog &age K> eaKO'tion% &a aK#ete % AKeig,t K4idt, KObject !ont ol 0Koolbo6 0o K. id !Kancel K"o e !ont ol% Align K3e5t Align KRig,t Align K0o' Align K>otto# Align K!ente Align K"iddle K!li' A t $ ee RoKtate K3ine 1t(le K)a%, 1t(le KA ow 1t(le KO ganiGation !,a t K+o #al K&age > ea- & eview K! o' 1ave KA% & o'e tKie% EK6it &a%te K1'ecial K.o 0o 3inK-% KAeade and $oote KAuto$o #at KAuto!o ect K!u%to#iGe K"e ge !ell% 2n#e ge !ell% 0Ke6t to !olu#n% A%%ign K"ac o K4indow +a#e .oe% Ae e KRecent $ile +a#e .oe% Ae e K! eate "ic o%o5t Outloo- 0a%"iKni#iGe KRe%to e K!lo%e

489 509 522 537 541 542 546 548 549 569 642 664 665 666 667 668 669 682 688 692 693 694 702 723 724 732 748 750 752 755 757 759 762 786 793 797 798 800 806 825 830 831 837 838 839 840

K"ove K1iGe "aK6i#iGe 1ave K4o -%'ace )eKlete 1,eet K"ove o !o'( 1,eet K$o #ula >a K1tatu% >a K4o -%,eet !Kell% K.oal 1ee1cKena io% A%%igKn "ac o $Ko # 1uKbtotal% K0able !oKn%olidate KAide K2n,ide K2' K3e5t KAc o%% 4o -%,eet% K1e ie% KNu%ti5( K$o #at% K!ontent% !oK##ent% K)e5ine K&a%te K! eate KA''l( KAuto$it KAide K2n,ide KAuto$it 1election KAide K2n,ide K1tanda d 4idt, KRena#e KAide K2n,ide K1,ow Auditing 0oolba K& otect 1,eet & otect K4o -booRelative Re5e ence AutoK$ilte

841 842 843 846 847 848 849 850 852 855 856 857 859 860 861 862 863 865 866 867 868 869 870 871 872 873 874 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 896 899

K1,ow All KAdvanced $ilte KAuto Outline K!lea Outline 1Ketting% K1e ie% 1iGed wit, K4indow Add 0K endline K1elected Object !,a t K0('e 3;) K=iew K[oo# KAbout "ic o%o5t E6cel K1o t K3abel K3otu% 1;2;3 Ael' KAn%we 4iGa d KE6c,ange $olde Add;K/n% K/n%e t K1elect All 1,eet% !u%to# K=iew% K>ac-g ound K1ou ce )ata K3ocation !,a t KO'tion% K1,eet 3i%t K!alculate +ow KObject !Kell% K!ontent% and /nde6 "ic o%o5t E6cel KAel' K)ata 0able 1e ie% in K!olu#n% 1e ie% in KRow% KEnable 1election K3i%t $o #ula% K)ata 3abel Kand )ata K3abel K$ill E55ect% KAngle 0e6t 2'wa d KAngle 0e6t )ownwa d KO'en K1ta t &age K>ac-

900 901 904 905 906 907 908 914 917 918 919 925 927 928 932 936 937 938 943 945 946 950 952 954 955 956 957 960 961 962 983 984 987 988 989 991 992 993 994 995 1006 1013 1014 1015 1016 1017

K$o wa d K1to' !u ent Nu#' KRe5 e%, !u ent &age O'en K$avo ite% KAdd to $avo ite% K1,ow Onl( 4eb 0oolba K4o dA t Re ouKte !onnecto % K2' K)own K3e5t KRig,t K!u ve 1tK aig,t !onnecto KElbow !onnecto !Ku ved !onnecto K!allout% K$lowc,a t >loc- KA ow% K1ta % and >anne % K"o e $ill !olo % K"o e 3ine !olo % K&atte ned 3ine% K"o e 3ine% K"o e A ow% K1,adow 1etting% K3;) 1etting% K4o dA t 1,a'e K4o dA t Align#ent K4o dA t !,a acte 1'acing K4o dA t =e tical 0e6t K4o dA t 1a#e 3ette Aeig,t% K"o e !ont a%t K3e%% !ont a%t K"o e > ig,tne%% K3e%% > ig,tne%% K+udge 1,adow 2' K+udge 1,adow )own K+udge 1,adow 3e5t K+udge 1,adow Rig,t K"o e 1,adow !olo % K"o e 3;) !olo % K3e5t Align K!ente K3ine !allout 4 ?>o de and Accent >a @ KRectangle

1018 1019 1020 1021 1022 1023 1031 1033 1035 1036 1037 1038 1041 1042 1043 1044 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1108 1109 1110 1111

K&a allelog a# K0 a'eGoid K)ia#ond KRounded Rectangle KOctagon K/%o%cele% 0 iangle KRig,t 0 iangle KOval KAe6agon K! o%% K!ube K&la7ue KRegula &entagon K4ave K!an K=e tical 1c oll KAo iGontal 1c oll K$olded !o ne K>evel K1#ile( $ace K)onut K8+o8 1(#bol K>loc- A c K3e5t > ac-et KRig,t > ac-et K3e5t > ace KRig,t > ace KA c K3ig,tning >olt KAea t KRig,t A ow K3e5t A ow K2' A ow K)own A ow K3e5t;Rig,t A ow K2';)own A ow KOuad A ow K3e5t;Rig,t;2' A ow K3e5t;2' A ow K>ent;2' A ow K>ent A ow K2;0u n A ow K1t i'ed Rig,t A ow K+otc,ed Rig,t A ow K&entagon K!,ev on

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

K!i cula A ow K!u ved Rig,t A ow K!u ved 3e5t A ow K!u ved 2' A ow K!u ved )own A ow KRig,t A ow !allout K3e5t A ow !allout K2' A ow !allout K)own A ow !allout K3e5t;Rig,t A ow !allout K2';)own A ow !allout KOuad A ow !allout KRectangula !allout KRounded Rectangula !allout KOval !allout K!loud !allout K3ine !allout 2 K3ine !allout 3 K3ine !allout 4 K)own Ribbon K2' Ribbon K!u ved )own Ribbon K!u ved 2' Ribbon K5;&oint 1ta K8;&oint 1ta K16;&oint 1ta K24;&oint 1ta K32;&oint 1ta KE6'lo%ion 1 KE6'lo%ion 2 K$lowc,a t: & oce%% K$lowc,a t: Alte nate & oce%% K$lowc,a t: )eci%ion K$lowc,a t: )ata K$lowc,a t: & ede5ined & oce%% K$lowc,a t: /nte nal 1to age K$lowc,a t: )ocu#ent K$lowc,a t: "ultidocu#ent K$lowc,a t: 0e #inato K$lowc,a t: & e'a ation K$lowc,a t: "anual /n'ut K$lowc,a t: "anual O'e ation K$lowc,a t: !onnecto K$lowc,a t: O55;'age !onnecto K$lowc,a t: !a d K$lowc,a t: &unc,ed 0a'e

1158 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

K$lowc,a t: 1u##ing Nunction K$lowc,a t: O K$lowc,a t: !ollate K$lowc,a t: 1o t K$lowc,a t: E6t act K$lowc,a t: "e ge K$lowc,a t: 1to ed )ata K$lowc,a t: 1e7uential Acce%% 1to age K$lowc,a t: "agnetic )i%K$lowc,a t: )i ect Acce%% 1to age K$lowc,a t: )i%'la( K$lowc,a t: )ela( K3ine !allout 1 K3ine !allout 1 ?Accent >a @ K3ine !allout 2 ?Accent >a @ K3ine !allout 3 ?Accent >a @ K3ine !allout 4 ?Accent >a @ K3ine !allout 1 ?+o >o de @ K3ine !allout 2 ?+o >o de @ K3ine !allout 3 ?+o >o de @ K3ine !allout 4 ?+o >o de @ K3ine !allout 1 ?>o de and Accent >a @ K3ine !allout 2 ?>o de and Accent >a @ K3ine !allout 3 ?>o de and Accent >a @ KRig,t Align 3eKtte Nu%ti5( K4o d Nu%ti5( K1t etc, Nu%ti5( =e ( 0Kig,t K0ig,t K+o #al K3oo%e K=e ( 3oo%e KRe n !,a acte &ai % KRe%et &ictu e KAuto#atic K. a(%cale K>lac- KK 4,ite K4ate #a K3;) OnEO55 K0ilt )own K0ilt 2' K0ilt 3e5t K0ilt Rig,t K)e't, K)i ection

1206 1207 1208 1209 1210 1211 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1365 1366 1367 1368 1374 1375 1376 1377 1378 1379 1380

Tea,m395Fly

K3ig,ting K1u 5ace K0 't. K36 't. K72 't. K144 't. K288 't. K/n5init( K&e %'ective &Ka allel K> ig,t K+o #al K)i# K"atte K&la%tic "eKtal K4i e $ a#e Edit 0eK6t 0o K1,a'e K/#age !ont ol K>a%ic 1,a'e% K!allout% K$lowc,a t >loc- KA ow% K1ta % and >anne % 1,ow 4o dA t 0oolbaK KE6it Rotate "ode 1,ow &ictu e 0oolbaK KAdd &oint )eKlete &oint K1t aig,t 1eg#ent K!u ved 1eg#ent !lo%e !Ku ve AuKto &oint K1#oot, &oint 1tK aig,t &oint K!o ne &oint E6it KEdit &oint K+o !olo K!olo 1c,e#e K1tanda d !olo % KAuto#atic !olo K!olo 1c,e#e K1tanda d !olo % K!olo 1c,e#e K1tanda d !olo %

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1398 1399 1401 1402 1403 1405 1406 1407 1408 1409 1410 1412 1413 1415 1416 1417 1418 1420 1421 1422 1423 1424 1425 1453 1455 1456 1459 1460 1461 1465 1466

K!olo 1c,e#e K1tanda d !olo % K=iew !ode O'en in +ew K4indow K!o'( A('e linA('e lKinEdit KA('e linK1elect A('e linKAdd to & int A ea K!lea & int A ea Re%et All &age K> ea-% KE6clude $ o# & int A ea /n%e t &age K> ea!oK##ent K& eviou% !o##ent K+e6t !o##ent )elete !oK##ent 1,Kow !o##ent Aide All !o##ent% !alculated K$ield !alculated K/te# Enti e K0able PP0>0/&I162'date"QQ KO'tion% K)e%ign "ode K4o dA t .alle ( K!,a t 0('e K&atte n K>o de K!,a t Object%: K+avigate !i cula Re5e ence: K>ubble !,a t K3;) !(linde !,a t K3;) &( a#id !,a t K3;) !one !,a t K)ouble A ow K$ill !olo K3ine !olo K1,adow !olo K3;) !olo K=i%ual >a%ic Edito K>otto# )ouble >o de K$ont: K$ont 1iGe: K1t(le: K[oo#:

1470 1471 1561 1574 1575 1576 1577 1578 1583 1584 1585 1586 1588 1589 1590 1591 1592 1593 1594 1597 1598 1600 1601 1604 1605 1606 1616 1617 1618 1622 1623 1635 1636 1637 1638 1639 1691 1692 1693 1694 1695 1699 1728 1731 1732 1733

K1cena io: KAdd e%%: "Ka' K"e ge Ac o%% $ o# K1canne K/n%ide Ao iGontal >o de K/n%ide =e tical >o de K)iagonal )own >o de K)iagonal 2' >o de K!,ec- >o6 K$ind K0e6t >o6 K!o##and >utton KO'tion >utton K3i%t >o6 K!o#bo >o6 K0oggle >utton K1'in >utton K1c oll >a K3abel K1e#it an%'a ent 1,adow 1ea c, t,e K4eb KAuto#atic !olo K!olo 1c,e#e K!olo K!olo 1c,e#e K!olo Run K4eb Oue ( Run K)ataba%e Oue ( KEdit Oue ( )Kata Range & o'e tie% Re5 e%, KAll K!ancel Re5 e%, KRe5 e%, 1tatu% !K,a t KAdd )ata KAll KSve bZ &icK- $ o# 3i%t !onKve t K2ng ou' 1,eet% K&atte n K+one KAve age K!ount !Kount +u#%

1737 1740 1741 1742 1764 1840 1841 1842 1843 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1922 1926 1927 1928 1929 1930 1948 1949 1950 1951 1952 1953 1954 1957 1963 1964 1965 1966 1967 1968 1988 2012 2013 2014 2015

K"a6 "Kin K1u# K!o'( !ell% $ill K1e ie% $ill K$o #at% $ill K=alue% $ill K)a(% $ill K4ee-da(% $ill K"ont,% $ill KLea % K3inea 0 end K. owt, 0 end 1Ke ie% /n%e t !oK##ent =aKlidation !i cle /nvalid )ata RKe%et & int A ea 1K,a e 4o -booKAig,lig,t !,ange% "e ge K4o -boo-% K!o'( Ae e K"ove Ae e !o'( Ae e a% K$o #at% Onl( !o'( Ae e a% K=alue% Onl( K1,i5t )own and !o'( 1,i5Kt Rig,t and !o'( 1,i5t K)own and "ove 1,i5t KRig,t and "ove ! eate K+ew Oue ( K!lea =alidation !i cle% K/#age K1t aig,t !onnecto KElbow !onnecto !Ku ved !onnecto Edit 0eK6t K3in- Ae e ! eate KA('e lin- Ae e K1,adow OnEO55 1et Auto1,a'e K)e5ault% 1to' KReco ding K"ail Reci'ient K+ew & int ?A& 3a%e Net 4000 1e ie% &1@ !,Ka t 4indow K$ o# $ile

2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2031 2034 2035 2038 2040 2042 2044 2045 2046 2047 2048 2049 2050 2051 2052 2054 2055 2089 2091 2092 2093 2094 2158 2159 2175 2179 2186 2188 2520 2521 2571 2619

KAuto1,a'e% K3ine% K>a%ic 1,a'e% !oKnnecto % K1un K"oon K)ouble > ac-et K)ouble > ace K4;&oint 1ta K)ouble 4ave K1t aig,t A ow !onnecto K1t aig,t )ouble;A ow !onnecto KElbow A ow !onnecto KElbow )ouble;A ow !onnecto K!u ved A ow !onnecto K!u ved )ouble;A ow !onnecto K$ill E55ect% )eKlete 1eg#ent O'en !Ku ve &a%te a% KA('e linK1et 0 an%'a ent !olo K1olve O de K&ivot0able Re'o t K4eb 0oolba K!u%to# "enu /te# K!u%to# >utton K$ ee 1tu55 K& oduct +ew% $ e7uentl( A%-ed KOue%tion% Online K1u''o t K4eb Ael' 5 K4eb Ael' 6 K4eb Ael' 7 K4eb Ael' 8 "ic o%o5t KO55ice Ao#e &age 1end $eedbacKK>e%t o5 t,e 4eb 1ea c, t,e K4eb 4eb K0uto ial K4eb Ael' 14 K4eb Ael' 15 K4eb Ael' 16 !onKditional $o #atting & otect and K1,a e 4o -boo"ic o%o5t KAo#e &age !lea !oKntent%

2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2668 2692 2693 2787 2827 2914 2915 2934 2949 2950 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3058 3059 3091 3125

KAuto#atic !olo K. ou' K2ng ou' K/nc ea%e /ndent K)ec ea%e /ndent

3141 3159 3160 3161 3162

Appendix (% :ace &(s


0,e $ace/) ' o'e t( o5 a !o##and>a >utton object de5ine% t,e icon t,atB% di%'la(ed on t,e buttonB% 5ace. ?$o an e6a#'le o5 u%ing t,e $ace/) ' o'e t( to de5ine t,e i#age on a buttonB% 5ace* %ee t,e %ection 8E6a#'le: ! eating a 0oolba *8 in !,a'te 12.@ $igu e );1 t, oug, $igu e );5 %,ow t,e icon% t,at a e available 5 o# =i%ual >a%ic* along wit, t,ei co e%'onding 5ace/)%. Eac, 5igu e %,ow% 400 icon% w,o%e beginning and ending 5ace/)% a e %,own in t,e 5igu e ca'tion o5 t,e i#age. /n addition* to #a-e identi5(ing a 'a ticula 5ace/) ea%ie * a nu#be ed g id ,a% been %u'e i#'o%ed on t,e i#age. 0,e colu#n nu#be % indicate t,e oneB% digitH t,e ow nu#be % indicate all ot,e %igni5icant digit%. $o e6a#'le* t,e 5ace/) o5 t,e $ icon in $igu e );1 i% 85* becau%e itB% in ow 86 ?t,e ow containing 5ace/)% 80;89@ and in colu#n 65 ?t,e colu#n containing 5ace/)% w,o%e oneB% digit i% 5@. +ote t,at %o#e nu#be % a enBt u%ed a% 5ace/)%H in t,e%e ca%e%* no icon i% di%'la(ed in t,at 5ace/)B% g id in $igu e );1 t, oug, $igu e );5. Figure (-1. Face/(s 4or icons 9 -688

Figure (-2. Face/(s 4or icons 799 -1'88

Figure (-". Face/(s 4or icons 1599 -2"88

Figure (-$. Face/(s 4or icons 2$99 -"188

Tea4m49 77l'D

Figure (-'. Face/(s 4or icons "299 -""88

Appendix E% Programming Excelfrom Another Application


/n t,i% a''endi6* we will b ie5l( de%c ibe ,ow t,e E6cel object #odel can be ' og a##ed 5 o# wit,in ce tain ot,e a''lication%* including "ic o%o5t Acce%%* 4o d* and &owe &oint. > ie5l( 'ut* a well;-nown tec,ni7ue t,at "ic o%o5t e5e % to a% Automation ?5o #e l( called O3E Auto#ation@ allow% one a''lication to gain acce%% to t,e object% o5 anot,e . An a''lication t,at 8e6'o%e%8 it% object% i% called an Automation ser#er. An a''lication t,at can acce%% t,e object% o5 an Auto#ation %e ve i% called an Automation controller o Automation client. 1ince "ic o%o5t 4o d* Acce%%* E6cel* and &owe &oint a e Auto#ation %e ve % and Auto#ation cont olle %* a% a =>A ' og a##e * (ou can ' og a# an( o5 t,e%e a''lication% 5 o# wit,in an( ot,e .

E%$ Setting a 6eference to the Excel Ob1ect 7odel


0,e 5i %t %te' in co##unicating wit, t,e E6cel object #odel i% to %et a e5e ence to it% object lib a (. Eac, o5 t,e client a''lication% ?4o d* Acce%%* E6cel* and &owe &oint@ ,a% a Re5e ence% #enu ite# unde t,e 0ool% #enu. 1electing t,i% ite# di%'la(% t,e Re5e ence% dialog bo6 %,own in $igu e E;1. $ o# ,e e* we can %elect t,e object #odel% t,at we want to acce%% 5 o# wit,in t,e Auto#ation cont olle . Figure E-1. he ,e4erences #ialog box

E%' 5etting a 6eference to the Excel Application Ob1ect


Once t,e ' o'e e5e ence% a e %et* we can decla e an object va iable o5 t('e A''lication:

450

Di5 #lA33 As 'xcel.A33lication

w,ic, t,e Auto#ation client will unde %tand* becau%e it can now c,ec- t,e %e ve B% object lib a (. +ote t,at we need to 7uali5( t,e object na#e* %ince ot,e object #odel% ,ave an A''lication object a% well. +e6t* we want to %ta t t,e E6cel Auto#ation %e ve * c eate an E6cel A''lication object* and get a e5e ence to t,at object. 0,i% i% done in t,e 5ollowing line:
Set #LA33 = 9ew 'xcel.A33lication

At t,i% 'oint* we ,ave co#'lete acce%% to E6celB% object #odel. /t i% i#'o tant to note* ,oweve * t,at t,e ' eviou% line %ta t% t,e E6cel Auto#ation %e ve * but doe% not %ta t E6celB% g a',ical u%e inte 5ace* %o E6cel will be unning invi%ibl(. 0o #a-e E6cel vi%ible* we ju%t %et it% =i%ible ' o'e t( to Tr0e:
#LA33.+isible = Tr0e

4e can now ' og a# a% t,oug, we we e wit,in t,e E6cel =>A /)E. $o in%tance* t,e 5ollowing code c eate% a new wo -boo-* add% a wo -%,eet to it* 'ut% a value in cell A1* and t,en %ave% t,e wo -boo-:
S0b Ma8eHor8boo8() Di5 #lA33 As 'xcel.A33lication Di5 wb As 'xcel.Hor8boo8 Di5 ws As 'xcel.Hor8sheet Set #lA33 = 9ew 'xcel.A33lication #lA33.+isible = Tr0e Set wb = #lA33.Hor8boo8s.Add Set ws = wb.Hor8sheets.Add ws.9a5e = "Sales" ws."an4e("A1").+al0e = 12B wb.SaveAs "d-Ite53ISales1oo8" 'nd S0b

+ote t,at t,e E6cel %e ve will not te #inate b( it%el5* even i5 t,e 9"va iable i% de%t o(ed. /5 we ,ave #ade E6cel vi%ible* t,en we can clo%e it ' og a##aticall(* a% well a% 5 o# t,e u%e inte 5ace in t,e u%ual wa( ?c,oo%ing E6it 5 o# t,e $ile #enu* 5o in%tance@. >ut i5 t,e E6cel %e ve i% invi%ible* it #u%t be clo%ed u%ing t,e Ouit #et,od:
#lA33.,0it

?/5 we 5ail to te #inate t,e E6cel %e ve * it will e#ain unning invi%ibl(* ta-ing u' %(%te# e%ou ce%* until t,e &! i% e%ta ted.@

E.2.1 %n %lternative %!!roach


0,e a'' oac, de%c ibed 5o ' og a##ing E6cel 5 o# wit,in anot,e a''lication i% t,e ' e5e ed a'' oac,* %ince it i% t,e #o%t e55icient. Aoweve * t,e e i% an alte native a'' oac, t,at (ou #a( encounte * %o let u% di%cu%% it b ie5l(. A% be5o e* we a%%u#e t,at a e5e ence ,a% been %et to t,e E6cel object lib a (. E.2.1.1 he +reate3bject 4unction

0,e Create*bTect 5unction can %ta t an Auto#ation %e ve * c eate an object* and a%%ign it to an object va iable. 0,u%* we can w ite:
Di5 #LA33 as 'xcel.A33lication Set #LA33 = Create*bTect("'xcel.A33lication")

0,i% a'' oac, will e6ecute #o e %lowl( t,an t,e ' eviou% a'' oac, u%ing t,e 9ew -e(wo d* but it i% 'e 5ectl( valid. A% be5o e* we #u%t e#e#be to clo%e E6cel u%ing t,e Ouit #et,od ?o t, oug, no #al #ean% i5 E6cel i% vi%ible@. E.2.1.2 he :et3bject 4unction /5 E6cel i% al ead( unning* t,e Create*bTect 5unction will %ta t a %econd co'( o5 t,e E6cel %e ve . 0o u%e t,e cu entl( unning ve %ion* we can u%e t,e :et*bTect 5unction to %et a e5e ence to t,e A''lication object o5 a unning co'( o5 E6cel. 0,i% i% done a% 5ollow%:
Set #LA33 = :et*bTect(= "'xcel.A33lication")

?0,e 5i %t 'a a#ete o5 :et*bTect i% not u%ed ,e e.@ One o5 t,e ' oble#% wit, u%ing :et*bTect i% t,at it will ' oduce an e o i5 E6cel i% not unning. 0,u%* we need %o#e code t,at will %ta t E6cel i5 it i% not unning o u%e t,e e6i%ting co'( o5 E6cel i5 it i% unning. 0,e t ic- to t,i% i% to -now t,at i5 :et*bTect 5ail% to 5ind a unning co'( o5 E6cel* t,en it i%%ue% e o nu#be 429 ?8ActiveD co#'onent canBt c eate object8@. 0,u%* t,e 5ollowing code doe% t,e t ic-:
Di5 #LA33 As 'xcel.A33lication *n 'rror "es05e 9ext . Tr to 4et re6erence to r0nnin4 'xcel Set #LA33 = :et*bTect(= "'xcel.A33lication") 26 'rr.905ber = L2E Then . 26 error L2E= then create new obTect Set #LA33 = Create*bTect("'xcel.A33lication") 'lse26 'rr.905ber $( 7 Then . 26 another t 3e o6 error= re3ort it Ms41ox "'rror- " K 'rr.Descri3tion 'xit S0b 'nd 26

E.2.1." No object library re4erence 4e ,ave been a%%u#ing t,at t,e client a''lication ,a% a e5e ence to t,e %e ve B% object lib a (. Aoweve * it i% %till 'o%%ible 5o a client a''lication ?an Auto#ation client@ to ' og a# t,e object% o5 an Auto#ation %e ve ?%uc, a% E6cel@ wit,out %uc, a e5e ence. 2nde t,e%e ci cu#%tance%* we cannot e5e to object% b( na#e in code* %ince t,e client will not unde %tand t,e%e na#e%. /n%tead* we #u%t u%e t,e gene ic Object data t('e* a% in t,e 5ollowing code:
Di5 #LA33 As *bTect Di5 wb As *bTect

Set #LA33 = Create*bTect("'xcel.A33lication") #LA33.+isible = Tr0e Set wb = #LA33.Hor8boo8s.Add wb.SaveAs "d-Ite53ISales1oo8"

0,i% code will un even #o e %lowl( t,an t,e ' eviou% code* w,ic,* in tu n* i% %lowe t,an t,e 5i %t ve %ion. 0,u%* we ,ave t, ee ve %ion% o5 Auto#ation:
C C C

2%ing t,e 9ew -e(wo d %(nta6 ? e7ui e% an object lib a ( e5e ence@ 2%ing Create*bTect and %'eci5ic object va iable decla ation% ? e7ui e% an object lib a ( e5e ence@ 2%ing Create*bTect wit, gene ic As *bTect decla ation% ?doe% not u%e an object lib a ( e5e ence@

0,e%e ve %ion% o5 auto#ation a e %o#eti#e% e5e ed to b( t,e na#e% #ery early binding* early binding* and late binding9 e%'ectivel( ?alt,oug, (ou #a( ,ea t,e%e te #% u%ed %o#ew,at di55e entl(@. 0,e%e te #% e5e to t,e ti#e at w,ic, =>A can a%%ociate ?o bind @ t,e object* ' o'e t(* and #et,od na#e% in ou code to t,e actual add e%%e% o5 t,e%e ite#%. /n ve ( ea l( binding* all binding% a e done at co#'ile ti#e b( =>A<t,at i%* before t,e ' og a# un%. /n ea l( binding* %o#e o5 t,e binding% a e done at co#'ile ti#e and ot,e % a e done at un ti#e. /n late binding* all binding% a e done at un ti#e. 0,e i%%ue i% now evident. 0,e #o e binding t,at need% to be done at un ti#e* t,e #o e %lowl( t,e ' og a# will un. 0,u%* ve ( ea l( binding i% t,e #o%t e55icient* 5ollowed b( ea l( binding* and t,en late binding.

Appendix :% !igh;)e el and )o";)e el )anguages


/n t,i% a''endi6* weBll e6a#ine t,e 'o%ition o5 =i%ual >a%ic a% a ' og a##ing language b( ta-ing a %o#ew,at clo%e loo- at ,ig,;level and low;level language%* wit, %o#e e6a#'le% 5o co#'a i%on. A low;level language i% c,a acte iGed b( it% abilit( to #ani'ulate t,e co#'ute B% o'e ating %(%te# and ,a dwa e #o e o le%% di ectl(. $o in%tance* a ' og a##e w,o i% u%ing a low;level language #a( be able to ea%il( tu n on t,e #oto o5 a 5lo''( d ive* c,ec- t,e %tatu% bit% o5 t,e ' inte inte 5ace* o loo- at individual %ecto % on a di%-* w,e ea% t,e%e ta%-% #a( be di55icult* i5 not i#'o%%ible* wit, a ,ig,;level language. Anot,e bene5it o5 low;level language% i% t,at t,e( tend to 'e 5o # ta%-% #o e 7uic-l( t,an ,ig,;level language%. On t,e ot,e ,and* t,e 'owe to #ani'ulate t,e co#'ute at a low level co#e% at a ' ice. 3 ow level language% a e gene all( #o e c ('tic<t,e( tend to be 5a t,e e#oved 5 o# o dina ( %'o-en language% and a e t,e e5o e ,a de to lea n* e#e#be * and u%e. Aig,;level language% ?and a''lication;level language%* w,ic, #an( 'eo'le would e5e to %i#'l( a% ,ig,;level language%@ tend to be #o e u%e ;5 iendl(* but t,e ' ice we 'a( 5o t,at 5 iendline%% i% le%% cont ol ove t,e co#'ute and %lowe unning ' og a#%. 0o illu%t ate* con%ide t,e ta%- o5 ' inting %o#e te6t. A low;level language #a( onl( be able to %end individual c,a acte % to a ' inte . 0,e ' oce%% o5 ' inting wit, a low;level language #ig,t go %o#et,ing li-e t,e 5ollowing: 1. 2. 3. 4. 5. 6. !,ec- t,e %tatu% o5 t,e ' inte . /5 t,e ' inte i% 5 ee* initialiGe t,e ' inte . 1end a c,a acte to t,e ' inte . !,ec- to %ee i5 t,i% c,a acte a ived %a5el(. /5 not* %end t,e c,a acte again. /5 %o* %ta t ove wit, t,e ne6t c,a acte .

0,e 8lowe%t8 level language t,at ' og a##e % u%e i% called assembly language . /ndeed* a%%e#bl( language ,a% e%%entiall( co#'lete cont ol ove t,e co#'ute B% ,a dwa e. 0o illu%t ate a%%e#bl( language code* t,e 5ollowing ' og a# ' int% t,e #e%%age 8Aa''( ' inting.8 )onBt wo ( i5 t,e%e in%t uction% %ee# #eaningle%%<(ou can ju%t %-i# ove t,e# to get t,e 5eel. /n 5act* t,e ve ( 'oint we want to #a-e i% t,at low;level language% a e #uc, #o e c ('tic t,an ,ig,;level language%. ?3ine% t,at begin wit, a %e#icolon a e co##ent%. 4e ,ave le5t out e o c,ec-ing to %ave a little %'ace.@
) %%%%%%%%%%%%%%%%%%%% ) Data 6or the 3ro4ra5 ) %%%%%%%%%%%%%%%%%%%% ) 5essa4e to 3rint Messa4e D1 .?a33

3rintin4.= 7Dh= 7Ah

) len4th o6 5essa4e Ms4GLen ',< C%Messa4e ) %%%%%%%%%%%%%%%%%%%% ) 2nitialiMe 3rinter 7 ) %%%%%%%%%%%%%%%%%%%% 5ov ah=1 5ov dx=7 int 1Fh

454

) %%%%%%%%%%%%%%%%%%%%% ) ;rintin4 instr0ctions ) %%%%%%%%%%%%%%%%%%%%% ) 4et n05ber o6 characters to 3rint 5ov cx=Ms4GLen ) 4et location o6 5essa4e 5ov bx=o66set Messa4e ) 4et 3rinter n05ber (6irst 3rinter is 3rinter 7) 5ov dx=7 ;rintGLoo3) send character to 3rinter 7 5ov ah=7 5ov al=UbxV int 1Fh ) do next character inc bx loo3 ;rintGLoo3

$o co#'a i%on* let u% %ee ,ow t,i% %a#e ta%- would be acco#'li%,ed in t,e >A1/! ' og a##ing language:
L;"29T "?a33 3rintin4"

0,e di55e ence i% ' ett( obviou%. A% we ,ave di%cu%%ed* ,ig,;level language% a e u%uall( de%igned 5o a %'eci5ic 'u 'o%e. .ene all(* t,i% 'u 'o%e i% to w ite %o5twa e a''lication% o5 a %'eci5ic t('e. $o in%tance* =i%ual !FF and =i%ual >a%ic a e u%ed ' i#a il( to w ite %tandalone 4indow% a''lication%. /ndeed* "ic o%o5t E6cel it%el5 i% w itten in =i%ual !FF. A% anot,e e6a#'le* $OR0RA+ ?w,ic, i% a cont action o5 &ormula !ranslation@ i% de%igned to w ite %cienti5ic and co#'utational a''lication% 5o va iou% 'lat5o #% ?including 4indow%@. !O>O3 i% u%ed to w ite bu%ine%%; elated a''lication% ?gene all( 5o #ain5 a#e co#'ute %@. At t,e ,ig,e%t level in t,e ' og a##ing language ,ie a c,(* we 5ind ' og a#% %uc, a% E6cel =>A* w,o%e primary 'u 'o%e i% not to #ani'ulate t,e o'e ating %(%te# o ,a dwa e* no to w ite %tandalone 4indow% a''lication%* but at,e to #ani'ulate a ,ig,;level %o5twa e a''lication ?in t,i% ca%e "ic o%o5t E6cel@. Nu%t 5o 5un* let u% ta-e a b ie5 loo- at a ,and5ul o5 t,e #o e co##on ' og a##ing language%.

:%$ BAS&C
0,e wo d >A1/! i% an ac on(# 5o Beginners All-Purpose /ymbolic nstruction (ode* t,e -e( wo d ,e e being >eginne %. >A1/! wa% develo'ed at )a t#out, !ollege in 1963* b( two #at,e#atician%: No,n Re#en( and 0,o#a% Ru tG. 0,e intention wa% to de%ign a ' og a##ing language 5o libe al a t% %tudent%* w,o #ade u' t,e va%t #ajo it( o5 t,e %tudent 'o'ulation at )a t#out,. 0,e goal wa% to c eate a language t,at would be 5 iendl( to t,e u%e and ,ave a 5a%t tu n;a ound ti#e %o it could be u%ed e55ectivel( 5o ,o#ewo - a%%ign#ent%. ?/n t,o%e da(%* a %tudent would %ub#it a ' og a# to a co#'ute o'e ato * w,o would 'lace t,e ' og a# in a 7ueue*

455

awaiting ' oce%%ing b( t,e co#'ute . 0,e %tudent would %i#'l( ,ave to wait 5o t,e e%ult%<t,e e we e no &!% in t,e 1960%C@ 0,e 5i %t ve %ion o5 >A1/! wa% ve ( %i#'leH indeed* it wa% ' i#itive. $o e6a#'le* it ,ad onl( one data t('e: 5loating;'oint. ?)ata t('e% a e di%cu%%ed in !,a'te 5.@ 1ince t,en >A1/! ,a% #ade t e#endou% %t ide% a% a 'e %onal co#'ute ' og a##ing language* due #o%tl( to t,e e#b ace o5 t,e language b( "ic o%o5t. Even to t,i% da(* ,oweve * t,e >A1/! language* and it% o55%,oot =i%ual >a%ic* do not get #uc, e%'ect a#ong co#'ute %cienti%t% o acade#ician%. 0,e >A1/! language get% a bad a' on two 5 ont%. $i %t* it i% gene all( con%ide ed a wea- language in t,e %en%e t,at it doe% not ' ovide ve ( #uc, cont ol ove t,e co#'ute B% ,a dwa e ?o o'e ating %(%te#@* at lea%t a% co#'a ed to ot,e language% %uc, a% !. 1econd* >A1/! ,a% a e'utation 5o not 85o cing8 ?o in %o#e ca%e% even allowing@ ' og a##e % to u%e good ' og a##ing %t(le. $o co#'a i%on wit, %o#e ot,e language%* ,e e i% a >A1/! ' og a# t,at a%-% t,e u%e 5o %o#e nu#be % and t,en etu n% t,ei ave age. 3ine% beginning wit, an a'o%t o',e a e co##ent line% t,at a e igno ed b( t,e co#'ute .
. 1AS2C 3ro4ra5 to co530te the avera4e . o6 a set o6 at 5ost 177 n05bers . As8 6or the n05ber o6 n05bers 29;<T "'nter the n05ber o6 n05bers- "= 905 . 26 905 is between 1 and 177 then 3roceed . 2> 905 ( 7 A9D 905 $= 177 T?'9 S05 = 7 . Loo3 to collect the n05bers to avera4e >*" 2 = 1 T* 905 . As8 6or next n05ber 29;<T "'nter next n05ber- "= 9ext905 . Add the n05ber to the r0nnin4 s05 S05 = S05 A 9ext905 9'#T 2 . Co530te the avera4e Ave = S05 J 905 . Dis3la the avera4e ;"29T "The avera4e is- ") Ave '9D 2> '9D

:%' ,isual Basic


"ic o%o5t too- t,e >A1/! ' og a##ing language to new ,eig,t% w,en it develo'ed =i%ual >a%ic. /n t,e ea l( 1990%* "ic o%o5t 5aced a dile##a. 4indow% wa% ?and i%@ a con%ide abl( #o e co#'le6 o'e ating %(%te# t,an )O1* %o #uc, %o t,at onl( ' o5e%%ional ' og a##e % could e55ectivel( u%e "ic o%o5tB% #ain ' og a##ing tool 5o c eating 4indow%;ba%ed a''lication%<t,e "ic o%o5t 4indow% 1)R* w,ic, i% ba%ed on t,e ! language. ?0,e%e da(%* t,i% tool ,a% given wa(

to a #o e #ode n tool* "ic o%o5t $oundation !la%%e%* w,ic, i% %till not 5o t,e ca%ual ' og a##e .@ >ut "ic o%o5t wanted #o e 'eo'le to be able to c eate 4indow% a''lication%* %ince it wa% good 5o bu%ine%%. 1o in 1991* t,e co#'an( int oduced Visual Basic ?=> 5o %,o t@* w,ic, e%%entiall( co#bined t,e >A1/! language wit, a vi%ual ' og a##ing envi on#ent %o u%e % could ea%il( c eate g a',ical co#'onent%* %uc, a% window%* co##and button%* te6t bo6e%* o'tion button%* and #enu% t,at a e e7ui ed b( 4indow% a''lication%. 0,e unde l(ing language 5o => i% called Visual Basic for Applications* o =>A 5o %,o t* alt,oug, t,i% te # wa% not coined until late in t,e develo'#ent o5 =>. 0,e 5i %t ve %ion o5 =i%ual >a%ic wa% little #o e t,an an inte e%ting to(. /t did not eall( ,ave t,e 'owe to c eate %e iou% 4indow% a''lication%. Aoweve * it ' ovided a nece%%a ( %ta ting 'oint 5o 5u t,e develo'#ent o5 t,e ' oduct. Eac, %ucce%%ive ve %ion o5 =i%ual >a%ic ,a% ta-en #ajo %te'% 5o wa d in %o',i%tication* %o t,at now => i% b( 5a t,e #o%t widel( u%ed ' og a##ing language 5o &!%. ?"ic o%o5t e%ti#ate% t,at ove t, ee #illion 'eo'le u%e %o#e 5o # o5 =i%ual >a%ic* about ,al5 o5 w,o# ' og a# u%ing %o#e 5o # o5 O55ice =>A* t,e e%t u%ing t,e %tandalone => ' oduct.@ 4,ile =i%ual >a%ic ,a% beco#e a ve ( e%'ectable tool 5o c eating %tandalone 4indow% a''lication%* t,e eal 'o'ula it( o5 =i%ual >a%ic 5o A''lication% ?=>A@ lie% in t,e 5act t,at it i% t,e unde l(ing ' og a##ing language 5o t,e "ic o%o5t O55ice a''lication %uite* w,ic, ' obabl( ,a% clo%e to 100 #illion u%e %* eac, o5 w,o# i% a 'otential =>A ' og a##e . /ndeed* ' e%u#abl( t,e ea%on t,at (ou a e eading t,i% boo- i% t,at (ou want to join t,e g ou' o5 =>A ' og a##e %. =>A i% a ,ig,;level ' og a##ing language t,at unde lie% %eve al i#'o tant 4indow% a''lication%* including "ic o%o5t 4o d* E6cel* Acce%%* and &owe &oint* a% well a% "ic o%o5t =i%ual >a%ic. /n addition* co#'anie% ot,e t,an "ic o%o5t can licen%e =>A and u%e it a% t,e unde l(ing ' og a##ing language 5o t,ei a''lication%. Eac, %o;called host application ' ovide% e6ten%ion% to =>A to acco##odate it% own need% and idio%(nc a%ie%. $o in%tance* %ince "ic o%o5t 4o d deal% wit, docu#ent%* 4o d =>A need% to unde %tand %uc, t,ing% a% ,eade % and 5oote %* g a##a c,ec-ing* 'age nu#be ing* and %o on. On t,e ot,e ,and* %ince E6cel deal% wit, wo -%,eet%* E6cel =>A need% to unde %tand %uc, t,ing% a% cell%* 5o #ula%* c,a t%* and %o on. $inall(* %ince =i%ual >a%ic i% de%igned 5o w iting %tandalone 4indow% a''lication%* it #u%t e6cel at #ani'ulating 4indow% cont ol% ?te6t bo6e%* co##and button%* li%t bo6e%@.

:%* C and CBB


0,e ! ' og a##ing language* a de%cendant o5 %eve al olde language% ?including >@* wa% develo'ed b( )enni% Ritc,ie at >ell 3abo ato ie% in 1972. ! i% a %i#'le language in it% %(nta6* wit, elativel( 5ew in%t uction%. Aoweve * it ,a% been e6tended con%ide abl( b( "ic o%o5t ?a#ong ot,e %@ 5o u%e in t,e &! envi on#ent. 0,e %t engt, o5 ! and it% de%cendant%* %uc, a% !FF* a e t,at it co#bine% t,e advantage% o5 a ,ig, level ' og a##ing language* %uc, a% elative eadabilit(* wit, t,e abilit( to eac, down to t,e o'e ating %(%te# and ,a dwa e level% o5 t,e co#'ute . 2n5o tunatel(* t,e 'owe o5 ! can %o#eti#e% be dange ou% in t,e ,and% o5* %,all we %a(* ' og a##e % o5 onl( #ode%t ca'abilit(. Al%o* t,e %(nta6 o5 ! allow% 5o w,at %o#e ' og a##e % con%ide 8cleve 8 o 8elegant8 ' og a##ing %t(le* but w,ic, #a( be #o e accu atel( te #ed 8,ig,l( un eadable.8

Tea4m59 77l'D

$o co#'a i%on 'u 'o%e%* ,e e i% t,e ! language ve %ion o5 t,e >A1/! ' og a# t,at co#'ute% t,e ave age o5 %o#e nu#be %. / t,in- (ou will ag ee t,at it i% not 7uite a% eadable a% t,e ea lie >A1/! ve %ion. 3ine% beginning wit, EE a e co##ent line% t,at a e igno ed b( t,e co#'ute .
JJ C 3ro4ra5 to co530te the avera4e JJ o6 a set o6 at 5ost 177 n05bers Oincl0de $stdio.h( void 5ain(void) X JJ Declare so5e variables int 905= i) 6loat S05= 9ext905= Ave) JJ As8 6or the n05ber o6 n05bers 3rint6( "'nter n05ber o6 n05bers- " )) scan6( "\0"= K905 )) JJ 26 905 is between 1 and 177 then 3roceed i6( (905 ( 7) KK (905 $= 177) ) X S05 = 7.7) JJ Loo3 to collect the n05bers to avera4e 6or( i = 1) i $= 905) iAA ) X JJ As8 6or next n05ber 3rint6( "'nter next n05ber- " )) scan6( "\6"= K9ext905 )) JJ Add the n05ber to the r0nnin4 s05 S05 A= 9ext905) Z JJ Co530te the avera4e Ave = S05 J 905) JJDis3la the avera4e 3rint6 ("The avera4e is- \6In"= Ave )) Z Z

An object;o iented e6ten%ion to !* -nown a% !FF* wa% develo'ed in t,e ea l( 1980% b( >ja ne 1t ou%t u' ?al%o at >ell 3ab%@.

:%2 ,isual CBB


)e%'ite t,e %igni5icant %t ide% t,at =i%ual >a%ic ,a% ta-en* it i% not* in gene al* t,e ' e5e ed language 5o c eating co#'le6 %tandalone 4indow% a''lication%. 0,at ole belong% to "ic o%o5tB% =i%ual !FF. Actuall(* t,i% i% a good t,ing. "ic o%o5t #u%t gua d again%t t (ing to #a-e an( %ingle language t,e %olution 5o too #an( dive %e ' og a##ing need%. 1uc, an e55o t can onl( be counte ' oductive. >( inc ea%ing t,e 'owe o5 =i%ual >a%ic ?and =>A@ in o de to ,andle #o e dive %e and %o',i%ticated a''lication ' og a##ing* t,e language beco#e% #o e co#'le6 and di55icult to lea n and u%e. 0,i% will e%ult in t,e language being u%ed b( 5ewe 'eo'le.

=i%ual !FF i% a #a iage between t,e !FF ' og a##ing language and t,e 4indow% g a',ical envi on#ent. =i%ual !FF i% not nea l( a% u%e ;5 iendl( a% =i%ual >a%ic. 0,i% i% due in 'a t to t,e natu e o5 t,e unde l(ing language ?! i% le%% 5 iendl( t,an >A1/!@* in 'a t to t,e 5act t,at !FF i% a 5ull( object;o iented language and t,e e5o e natu all( #o e co#'licated* and in 'a t to t,e 5act t,at =i%ual !FF i% de%igned to cont ol t,e 4indow% envi on#ent at a #o e 5unda#ental level t,an =i%ual >a%ic. $o in%tance* =i%ual >a%ic doe% not ' ovide wa(% to c eate a te6t bo6 w,o%e te6t can u%e #o e t,an one colo * %et t,e tab% in a li%t bo6* o c,ange t,e colo o5 t,e ca'tion in a co##and button* and %o on. 1i#'l( 'ut* w,en ' og a##ing in => ?o =>A@* we #u%t %ac i5ice 'owe in %o#e di ection% in 5avo o5 'owe in ot,e di ection% and a %i#'le ' og a##ing envi on#ent.

:%3 Pascal
&a%cal wa% develo'ed b( +i-lau% 4i t, ?' onounced 8=i t8@ in t,e late 1960% and ea l( 1970%. 0,e goal wa% to ' oduce a language t,at could be ea%il( i#'le#ented on a va iet( o5 co#'ute % and t,at would ' ovide %tudent% wit, a #odel teac,ing language. 0,at i% to %a(* &a%cal i% 5ull o5 5eatu e% t,at encou age well;w itten and well;%t uctu ed ' og a#%. /ndeed* #an( unive %itie% teac, &a%cal to t,ei co#'ute %cience %tudent% a% a 5i %t language. &a%cal ,a% al%o #ig ated to t,e 'e %onal co#'ute a ena* 5i %t wit, >o landB% 0u bo &a%cal and #o e ecentl( wit, >o landB% vi%ual ' og a##ing envi on#ent called )el',i. $o cont a%t* ,e e i% ,ow ou ' og a# to co#'ute t,e ave age would loo- in &a%cal. 0e6t contained wit,in cu l( b ace% ?`*a@ a e co##ent% t,at a e igno ed b( t,e co#'ute .
X ;ascal 3ro4ra5 to co530te the avera4e o6 a set o6 at 5ost 177 n05bers Z 3ro4ra5 avera4e (in30t= o0t30t)) X Declare so5e variables Z var 905= i - inte4er) Ave= S05= 9ext905 - real) be4in X As8 6or the n05ber o6 n05bers Z writeln(.'nter the n05ber o6 n05bers.)) readln(905)) X 26 905 is between 1 and 177 then 3roceed Z i6 ((905 ( 7 ) and (905 $= 177)) then be4in S05 -= 7) X Loo3 to collect the n05bers to avera4e Z 6or i -= 1 to 905 do be4in X As8 6or next n05ber Z writeln(.'nter next n05ber.)) readln(9ext905)) X Add the n05ber to the r0nnin4 s05 Z S05 -= S05 A 9ext905) end X Co530te the avera4e Z Ave -= S05 J 905) X Dis3la the avera4e Z writeln(.The avera4e is- .= Ave))

end end

:%8 :O6T6A4
$OR0RA+ i% a cont action o5 &ormula !ranslation9 a na#e t,at co#e% 5 o# a tec,nical e'o t entitled 80,e />" "at,e#atical $OR#ula 0RA+%lating 1(%te#*8 w itten b( No,n >ac-u% and ,i% tea# at />" in t,e #id;1950%. $OR0RA+ i% ' i#a il( de%igned 5o %cienti5ic calculation% and ,a% t,e di%tinction o5 being t,e 5i %t widel( u%ed ,ig,;level ' og a##ing language. >ac-u% #ade %o#e at,e inte e%ting clai#% about $OR0RA+H 5o in%tance* it wa% not de%igned 5o it% beaut( ?a ea%onable %tate#ent@ but it would eli#inate coding e o % and t,e con%e7uent debugging ' oce%%C Ae e i% t,e $OR0RA+ ve %ion o5 ou little ave aging ' og a#. ?3ine% t,at begin wit, a ! a e co##ent%.@
C >*"T"A9 ;"*:"AM T* C*M;<T' T?' A+'"A:' C *> A S'T *> AT M*ST 177 9<M1'"S "eal S<M= A+'= 9'#T9<M S<M = 7.7 C As8 6or the n05ber o6 n05bers H"2T'(R=R) .'nter the n05ber o6 n05bers- . "'AD(R=R) 9<M C 26 905 is between 1 and 177 then 3roceed 2> 9<M .:T. 7 .A9D. 9<M .L'. 177 then C Loo3 to collect the n05bers to avera4e D* 17 2 = 1= 9<M C As8 6or next n05ber H"2T'(R=R) .'nter next n05ber- . "'AD(R=R) 9'#T9<M C Add the n05ber to the r0nnin4 s05 S<M = S<M A 9'#T9<M 17 C*9T29<' C Co530te the avera4e A+' = S<MJ9<M C Dis3la the avera4e H"2T'(R=R) .The avera4e is- . H"2T'(R=R) A+' '9D2> ST*; '9D

:%9 COBO)
!O>O3 i% an ac on(# 5o (ommon Business $riented Language and it wa% develo'ed in t,e late 1950% b( . ace Ao''e 5o t,e 'u 'o%e o5 w iting bu%ine%%; elated ' og a#%* w,ic, %,e 5elt %,ould be w itten in Engli%,. Aoweve * it %ee#% at,e t,at t,e language wa% develo'ed wit, t,e e6' e%% 'u 'o%e o5 avoiding all #at,e#atical;li-e notation. 0,e inevitable con%e7uence i% t,at conci%ene%% and eadabilit( i% al%o avoided. At an( ate* / could onl( b ing #(%el5 to code a !O>O3 %a#'le ' og a# t,at add% two nu#be %.
R C*1*L ;"*:"AM T* ADD TH* 9<M1'"S

2D'9T2>2CAT2*9 D2+2S2*9. ;"*:"AM%2D. ADD72. '9+2"*9M'9T D2+2S2*9. DATA D2+2S2*9. H*"P29:%ST*"A:' S'CT2*9. 71 >2"ST%9<M1'" 71 S'C*9D%9<M1'" 71 S<M ;"*C'D<"' D2+2S2*9. ;"*:"AM%1':29. D2S;LA@ "'9T'" >2"ST 9<M1'" ". ACC';T >2"ST%9<M1'". D2S;LA@ "'9T'" S'C*9D 9<M1'" ". ACC';T S'C*9D%9<M1'". C*M;<T' S<M = >2"ST%9<M1'" A S'C*9D%9<M1'" D2S;LA@ "T?' S<M 2S- " S<M. ;"*:"AM%D*9'. ST*; "<9. ;2C 2S EE. ;2C 2S EE. ;2C 2S EEE.

/n >A1/!* t,e ' eceding ' og a# would be:


29;<T "'nter 6irst n05ber- "= n1 29;<T "'nter second n05ber- "= n2 ;"29T "The s05 is- "= n1 A n2

0,i% clea l( 'oint% out t,e e6t e#e ve bo%it( o5 !O>O3.

:%< )&SP
>A1/!* !* &a%cal* and $OR0RA+ a e in #an( wa(% 7uite %i#ila . Al%o* ' og a#% w itten in t,e%e language% can be #ade 7uite eadable* e%'eciall( i5 t,e ' og a##e intend% to #a-e it %o. 0,e e a e ot,e language% t,at %ee# not to be eadable unde an( ci cu#%tance%. $o in%tance* 3/1& wa% develo'ed in t,e late 1950% b( No,n "c!a t,( and "a vin "in%-( at "/0* 5o t,e 'u 'o%e o5 doing li%t ' oce%%ing ?,ence t,e na#e@ in connection wit, a ti5icial intelligence a''lication%. /n 3/1&* eve (t,ing i% a li%t. Ae e i% a %a#'le:
) L2S; sa53le 3ro4ra5 to de6ine a 3redicate ) that ta8es two lists and ret0rns the val0e ) T (6or tr0e) i6 the lists are e/0al and > otherwise (D'>29' ( .(e/0al (LAMD1A (list1 list2) (C*9D ((AT*M list1) (', list1 list2)) ((AT*M list1 92L) ((e/0al (CA" list1) (CA" list2))

(e/0al (CD" list1) (CD" list2))) ) )) )) (T 92L)

0,i% %a#'le 'oint% out one 5act. 4,ateve el%e we #ig,t t,in- o5 "ic o%o5t* we can at lea%t t,ant,e# 5o c,oo%ing >A1/! ?=>A@ a% t,e unde l(ing language 5o t,e "ic o%o5t O55ice %uiteC

Appendix 5% 4e" Ob1ects in Excel @P


E6cel D& contain% 37 new object%. 0,i% a''endi6 ' e%ent% a li%t* along wit, 'at,% and a b ie5 de%c i'tion o5 eac, object. / al%o include a li%t o5 t,e objectB% #e#be % ?o#itting t,e al#o%t ubi7uitou% A''lication* ! eato * and &a ent ' o'e tie%@.

AllowEditRange Object
;ath- A33lication %% "an4e %% Hor8sheet %% -llow,dit%an!es < -llow,dit%an!e %% ;rotection

)b'e t AllowEditRange Object


C C C

Met*o&s !,ange&a%%wo d?&a%%wo d A% 1t ing@ )elete?@ 2n' otect?P&a%%wo dQ A% =a iant@


C C C

Properties Range?@ A% Range 0itle?@ A% 1t ing 2%e %?@ A% 2%e Acce%%3i%t !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% AllowEditRange

AllowEditRange% Object

Add?0itle A% 1t ing* Range A% Range* P&a%%wo dQ A% =a iant@ A% AllowEditRange

C C

0,e AllowEditRange object allow% a %'eci5ied ange o5 cell% on a wo -%,eet to be 'a%%wo d ' otected 5 o# editing. Once a ange ,a% been ' otected in t,i% wa(* and the entire wor)sheet has been protected* an( atte#'t at editing cell% in t,at ange will e7ui e t,e 'a%%wo d. 0,e AllowEditRange% collection object ,old% t,e cu ent AllowEditRange object%.

AutoRecover Object
;ath- A33lication % A0to"ecover

)b'e t AutoRecove
C C C

Properties Enabled?@ A% >oolean &at,?@ A% 1t ing 0i#e?@ A% 3ong

0,e AutoRecove 5eatu e allow% 5o t,e 'o%%ible ecove ( o5 data i5 E6cel D& %to'% e%'onding ?o %,ould / %a( when E6cel D& %to'% e%'onding9@. 0,e u%e can %et t,e location in w,ic, E6cel will %ave AutoRecove 5ile% a% well a% t,e ti#e between %aving.

0,e AutoRecove object i#'le#ent% auto#atic ecove (. /t% &at, ' o'e t( %et% t,e location w,e e E6cel will 'lace t,e AutoRecove 5ile and it% 0i#e ' o'e t( %et% t,e ti#e ?in #inute%@ inte val 5o bac-ing u' t,e wo -boo- to t,e AutoRecove 5ile* a% in:
A33lication.A0to"ecover.;ath = "d-Ite53" A33lication.A0to"ecover.Ti5e = 17. 5in0tes

CalculatedMember Object
;ath- A33lication %% "an4e %% ;ivotTable Calculated(embers < Calculated(ember %%

)b'e t !alculated"e#be
C

Met*o&s
C C C C C C

)elete?@

Properties $o #ula?@ A% 1t ing /%=alid?@ A% >oolean +a#e?@ A% 1t ing 1olveO de ?@ A% 3ong 1ou ce+a#e?@ A% 1t ing 0('e?@ A% Dl!alculated"e#be 0('e

!alculated"e#be %

C C C C

Add?+a#e A% 1t ing $o #ula A% 1t ing P1olveO de Q A% =a iant P0('eQ A% =a iant@ A% !alculated"e#be

C C

!ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% !alculated"e#be

0,e !alculated"e#be object ' og a##aticall( #ani'ulate% t,e calculated 5ield% and calculated ite#% 5o &ivot0able% t,at a e connected to e6te nal O3A& ?Online Anal(tical & oce%%ing@ data %ou ce%. 4e will not di%cu%% t,e%e data %ou ce% 5u t,e in t,i% boo-.

CellFormat Object
;ath- A33lication % Cell>or5at

)b'e t !ell$o #at


C

Met*o&s !lea ?@
C C C C C C C C C C C

Properties Add/ndent?@ A% =a iant >o de %?@ A% >o de % $ont?@ A% $ont $o #ulaAidden?@ A% =a iant Ao iGontalAlign#ent?@ A% =a iant /ndent3evel?@ A% =a iant /nte io ?@ A% /nte io 3oc-ed?@ A% =a iant "e ge!ell%?@ A% =a iant +u#be $o #at?@ A% =a iant +u#be $o #at3ocal?@ A% =a iant

C C C C

O ientation?@ A% =a iant 1, in-0o$it?@ A% =a iant =e ticalAlign#ent?@ A% =a iant 4 a'0e6t?@ A% =a iant

0,e !ell$o #at object wo -% in conjunction wit, t,e $ind$o #at and Re'lace$o #at object% to ' og a##aticall( 5ind and e'lace cell 5o #atting. $o #o e on t,i%* 'lea%e %ee !,a'te 16.

CustomProperty Object
;ath- A33lication %% "an4e %% Hor8sheet CustomPro erties < CustomPro erty %%

)b'e t !u%to#& o'e t(


C

Met*o&s )elete?@
C C C C

Properties +a#e?@ A% 1t ing =alue?@ A% =a iant !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% !u%to#& o'e t(

!u%to#& o'e tie%

Add?+a#e A% 1t ing* =alue A% =a iant@ A% !u%to#& o'e t(

0,e "ic o%o5t docu#entation* wit, it% u%ual ob%cu it(* %tate% t,e 5ollowing about t,e !u%to#& o'e t( object: 8Re' e%ent% identi5ie in5o #ation. /denti5ie in5o #ation can be u%ed a% #etadata 5o D"3.8 Aoweve * t,e docu#entation doe% not bot,e to elabo ate on how t,i% in5o #ation can be u%ed a% #etadata. /n an( ca%e* it i% not ou intention in t,i% boo- to cove t,e D"3; elated 5eatu e% o5 E6cel. 0,e !u%to#& o'e tie% collection ,old% !u%to#& o'e t( object%.

Diagram, Diagram ode and Diagram odeC!ildren Objects


;ath- A33lication %% Chart %% Dia4ra5 %% Dia!ramNodes << Dia!ram<Node<Dia!ramNodeChildren Sha3es %% Sha3e %

)b'e t )iag a#
C

Met*o&s
C C

!onve t?0('e A% "%o)iag a#0('e@

C C C

Properties Auto$o #at?@ A% "%o0 i1tate Auto3a(out?@ A% "%o0 i1tate +ode%?@ A% )iag a#+ode% Reve %e?@ A% "%o0 i1tate 0('e?@ A%

"%o)iag a#0('e
C

)iag a#+ode

C C

C C C C C

Add+ode?'o% A% "%oRelative+ode&o%ition* node0('e A% "%o)iag a#+ode0('e@ A% )iag a#+ode !lone+ode?co'(!,ild en A% >oolean* '0a get+ode A% )iag a#+ode* 'o% A% "%oRelative+ode&o%ition@ A% )iag a#+ode )elete?@ "ove+ode?'0a get+ode A% )iag a#+ode* 'o% A% "%oRelative+ode&o%ition@ +e6t+ode?@ A% )iag a#+ode & ev+ode?@ A% )iag a#+ode Re'lace+ode?'0a get+ode A% )iag a#+ode@ 1wa'+ode?'0a get+ode A% )iag a#+ode* %wa'!,ild en A% >oolean@ 0 an%5e !,ild en?'Receiving+ode A% )iag a#+ode@ /te#?/nde6 A% =a iant@ A% )iag a#+ode 1electAll?@ Add+ode?/nde6 A% =a iant* node0('e A% "%o)iag a#+ode0('e@ A% )iag a#+ode /te#?/nde6 A% =a iant@ A% )iag a#+ode 1electAll?@

C C C C C C

!,ild en?@ A% )iag a#+ode!,il d en )iag a#?@ A% /"%o)iag a# 3a(out?@ A% "%oO g!,a t3a( out0('e Root?@ A% )iag a#+ode 1,a'e?@ A% 1,a'e 0e6t1,a'e?@ A% 1,a'e

)iag a#+ode%

C C

!ount?@ A% 1igned #ac,ine int !ount?@ A% 1igned #ac,ine int $i %t!,ild?@ A% )iag a#+ode 3a%t!,ild?@ A% )iag a#+ode

C C C

)iag a#+ode!,ild en

C C

)iag a#% a e %'ecial t('e% o5 %,a'e% t,at o ganiGe data vi%uall(. 0,e e a e %i6 t('e% o5 diag a#%* including 5low c,a t% ?o ganiGational c,a t%@ and va iou% t('e% o5 elation%,i' c,a t%. 0,e )iag a#; elated object% ' og a##aticall( c eate and edit diag a#%. $o #o e on t,i%* 'lea%e %ee A''endi6 A* !he /hape $b%ect.

Error Object
;ath- A33lication %% "an4e %% ,rrors < ,rror

)b'e t E o
C C

Properties /gno e?@ A% >oolean =alue?@ A% >oolean

E o object% dete #ine w,et,e o not a 'a ticula t('e o5 e o ,a% occu ed in a %'eci5ied ange. /n 'a ticula * t,e E o % ' o'e t( o5 t,e Range object etu n% an E o % collection. 0,i% collection alwa(% contain% e6actl( %even E o object%* co e%'onding to t,e %even t('e% o5 available e o c,ec-ing o'tion%* a% %,own in t,e E o !,ec-ing tab o5 t,e O'tion% dialog bo6 ?%ee t,e E o !,ec-ingO'tion% ent (@. 0,e E o object ,a% a >oolean* ead;onl( =alue ' o'e t( t,at i% %et to Tr0e b( E6cel i5 t,e e i% an e o o5 t,e t('e co e%'onding to t,e E o object wit,in t,e given ange. /t i% i#'o tant to note t,at* in o de 5o t,e E o objectB% =alue ' o'e t( to 5unction co ectl(* t,e e o ;c,ec-ing o'tion #u%t be enabled* eit,e ' og a##aticall( o t, oug, t,e E o !,ec-ing tab. 0o illu%t ate* t,e 5ollowing code 5i %t tu n% on global e#'t( cell e o c,ec-ing. 0,en in cell A1* it 'lace% a 5o #ula t,at e5e ence% one o #o e e#'t( cell%. $inall(* t,e code c,ec-% to %ee i5 t,e e i% an e#'t( cell e5e ence e o wit,in t,e ange denoted b( ng.
S0b 'xa53leG'rror*bTect() Di5 rn4 As "an4e Set rn4 = A33lication."an4e("A1") . Ma8e s0re e53t cell error chec8in4 is on 4loball A33lication.'rrorChec8in4*3tions.'53t Cell"e6erences = Tr0e . 2nsert 6or50la re6erencin4 e53t . in a cell within the ran4e rn4 "an4e("A1").>or50la = "=A12AA1B" cells

. Chec8 to see i6 there is an e53t cell t 3e error in the s3eci6ied ran4e 26 rn4.'rrors.2te5(xl'53t Cell"e6erences).+al0e = Tr0e Then Ms41ox "'53t cell error in ran4e " K rn4.Address 'lse Ms41ox "9o e53t cell error in ran4e " K rn4.Address 'nd 26 'nd S0b

+ote t,at i5 we c,ange t,e %'eci5ied ange to one t,at contain% #o e t,an one cell* a% in:
Set rn4 = A33lication."an4e("A1-12")

t,en t,e line:


26 rn4.'rrors.2te5(xl'53t Cell"e6erences).+al0e = Tr0e Then

gene ate% a gene ic ?and t,e e5o e totall( u%ele%%@ e o #e%%ageC 0,e e i% ce tainl( no ea%on w,( t,e E o % ' o'e t( o5 t,e Range object could not wo - wit, #ulti;cell ange% and t,e docu#entation #ention% no %uc, e%t iction. O5 cou %e* t,e e i% no wa( to tell w,et,e t,e e i% %i#'l( an o#i%%ion in t,e docu#entation o an e o in t,e E6cel code. 0,e onl( clue i% t,at t,e E o object doe% not ,ave an( ' o'e tie% t,at etu n% a li%t o5 t,e o55ending cell% wit,in t,e ange. 0,i% #ig,t lead u% to believe t,at t,e E o % ' o'e t( wa% intended to a''l( onl( to one;cell ange%.

Tea4m69 77l'D

ErrorC!ec"ingOptions Object
Path= lication < 'rrorChec8in4*3tions

)b'e t E o !,ec-ingO'tion%
C C C C C C C C C

Properties >ac-g ound!,ec-ing?@ A% >oolean E#'t(!ellRe5e ence%?@ A% >oolean Evaluate0oE o ?@ A% >oolean /ncon%i%tent$o #ula?@ A% >oolean /ndicato !olo /nde6?@ A% Dl!olo /nde6 +u#be A%0e6t?@ A% >oolean O#itted!ell%?@ A% >oolean 0e6t)ate?@ A% >oolean 2nloc-ed$o #ula!ell%?@ A% >oolean

0,i% object co e%'ond% to t,e E o !,ec-ing tab unde t,e E6cel O'tion% dialog bo6. 0,e E o !,ec-ingO'tion% objectB% ' o'e tie% co e%'ond to t,e c,ec- bo6e% in t,e a5o e#entioned dialog bo6 tab* a% %,own in $igu e .;1. Figure :-1. he Error +hec1ing tab $o in%tance* w,en t,e >oolean 0e6t)ate ' o'e t( o5 t,e E o !,ec-ingO'tion% object i% %et to Tr0e ?co e%'onding to c,ec-ing t,e c,ec- bo6 labeled 80e6t date wit, 2 digit (ea %8@* E6cel will di%'la( an Auto!o ect o'tion% button ne6t to te6t cell% t,at e6' e%% t,e (ea a% a two;digit value ? at,e t,an a 5ou ;digit value@* 5o e6a#'le Nan 1* 01. 0,e E o % collection object ,old% E o object%. 0,e E o % object ,a% t,e 5ollowing #e#be %:

Properties
C

/te#?/nde6 A% =a iant@ A% E o

#rap!ic Object
;ath- A33lication %% Chart %% ;a4eSet03 % :ra hic

)b'e t . a',ic
C C C C C C C C C

Properties > ig,tne%%?@ A% 1ingle !olo 0('e?@ A% "%o&ictu e!olo 0('e !ont a%t?@ A% 1ingle ! o'>otto#?@ A% 1ingle ! o'3e5t?@ A% 1ingle ! o'Rig,t?@ A% 1ingle ! o'0o'?@ A% 1ingle $ilena#e?@ A% 1t ing Aeig,t?@ A% 1ingle

C C

3oc-A%'ectRatio?@ A% "%o0 i1tate 4idt,?@ A% 1ingle

0,e . a',ic object allow% t,e ' og a##e to %et va iou% ' o'e tie% o5 i#age% t,at can a''ea in E6cel ,eade % and 5oote %. $o e6a#'le* (ou can %et t,e 5ile na#e* i#age %iGe and va iou% colo a%'ect% o5 t,e i#age* a% well a% it% location in t,e ,eade o 5oote .

$R%D&erver and $R%D'pdateEvent Objects


;ath- A33lication 2"TD<3date'vent %% 2"tdServer) ;ath- A33lication %

)b'e t
C

/Rtd1e ve
C C C C C C C

Met*o& !onnect)ata?0o'ic/) A% 3ong* 1t ing% A% A a( o5 =a iant* .et+ew=alue% A% >oolean@ A% =a iant )i%connect)ata?0o'ic/) A% 3ong@ Aea tbeat?@ A% 3ong Re5 e%,)ata?0o'ic!ount A% 3ong@ A% A a( o5 =a iant 1e ve 1ta t?!allbac-Object A% /R0)2'dateEvent@ A% 3ong 1e ve 0e #inate?@ )i%connect?@ 2'date+oti5(?@
C

Properties

/R0)2'dateEvent

Aea tbeat/nte val?@ A% 3ong

0,e /R0)1e ve object and t,e /R0)2'dateEvent elate to eal;ti#e data %e ve %* w,ic, we do not cove in t,i% boo-.

PivotCell and Pivot$tem(ist Objects


;ath- A33lication %% "an4e % Pi+otCell < Pi+ot1tem"ist

)b'e t &ivot!ell

Met*o&s
C C C C C C C C

Properties !olu#n/te#%?@ A% &ivot/te#3i%t !u%to#1ubtotal$unction?@ A% Dl!on%olidation$unction )ata$ield?@ A% &ivot$ield &ivot!ell0('e?@ A% Dl&ivot!ell0('e &ivot$ield?@ A% &ivot$ield &ivot/te#?@ A% &ivot/te# &ivot0able?@ A% &ivot0able Range?@ A% Range

C C

Row/te#%?@ A% &ivot/te#3i%t !ount?@ A% 3ong

&ivot/te#3i%t

/te#?/nde6 A% =a iant@ A% &ivot/te#

+ew to E6cel D& i% t,e &ivot!ell object* w,ic, e' e%ent% a cell in a 'ivot table. $o #o e on t,i% object ?and t,e &ivot/te#3i%t object@* 'lea%e %ee !,a'te 20.

Protection Object
;ath- A33lication %% "an4e %% Hor8sheet % Protection

)b'e t
C C C C C C C C C C C C

& otection

Properties Allow)eleting!olu#n%?@ A% >oolean Allow)eletingRow%?@ A% >oolean AllowEditRange%?@ A% AllowEditRange% Allow$ilte ing?@ A% >oolean Allow$o #atting!ell%?@ A% >oolean Allow$o #atting!olu#n%?@ A% >oolean Allow$o #attingRow%?@ A% >oolean Allow/n%e ting!olu#n%?@ A% >oolean Allow/n%e tingA('e lin-%?@ A% >oolean Allow/n%e tingRow%?@ A% >oolean Allow1o ting?@ A% >oolean Allow2%ing&ivot0able%?@ A% >oolean

4,en (ou ' otect a wo -%,eet* E6cel 'e #it% (ou to %'eci5( t,at ce tain o'e ation% a e %till 'e #itted on unloc)ed cell%. At t,e u%e level* t,i% i% done t, oug, t,e & otection dialog bo6. At t,e ' og a##ing level* it i% done t, oug, t,e ' o'e tie% o5 t,e & otection object.

R%D Object
;ath- A33lication % "TD

)b'e t R0)
C C

Met*o&s Re5 e%,)ata?@ Re%ta t1e ve %?@

Properties
C

0, ottle/nte val?@ A% 3ong

0,e R0) object e' e%ent% a eal;ti#e data object* a %ubject t,at we do not cove in t,i% boo-.

&mart%ag Related Objects


;ath- A33lication %%"an4e %% S5artTa4s % S5artTa4 S5artTa4Actions %%S5artTa4Action) ;ath- A33lication %% Hor8boo8 %% S5artTa4*3tions) Path= - lication << S5artTa4"eco4niMers % S5artTa4"eco4niMer %%

)b'e t 1#a t0ag


C

Met*o&s
C C C

)elete?@

C C C

Properties )ownload2R3?@ A% 1t ing +a#e?@ A% 1t ing & o'e tie%?@ A% !u%to#& o'e tie% Range?@ A% Range 1#a t0agAction%?@ A% 1#a t0agAction% D"3?@ A% 1t ing !ount?@ A% 3ong +a#e?@ A% 1t ing !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% 1#a t0agAction )i%'la(1#a t0ag%?@ A% Dl1#a t0ag)i%'la("ode E#bed1#a t0ag%?@ A% >oolean Enabled?@ A% >oolean $ull+a#e?@ A% 1t ing ' og/)?@ A% 1t ing !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% 1#a t0agRecogniGe RecogniGe?@ A% >oolean

1#a t0ag% 1#a t0agAction


C

Add?1#a t0ag0('e A% 1t ing@ A% 1#a t0ag E6ecute?@

C C C C

1#a t0agAction%

1#a t0agO'tion%

1#a t0agRecogniGe

C C C C C

1#a t0agRecogniGe %
C

0,i% collection o5 object% enable% ' og a##atic cont ol ove e6i%ting 1#a t 0ag%. +ote t,at in o de to c eate cu%to# 1#a t 0ag%* we need need to u%e an a''lication t,at can c eate !O" )33%* %uc, a% =i%ual >a%ic o =i%ual !FF. !u%to# 1#a t 0ag% cannot be c eated in E6cel D&. $o #o e on 1#a t 0ag%* 'lea%e %ee !,a'te 22.

&peec! Object

;ath- A33lication % S3eech

)b'e t
C

Met*o&s 1'ea-?0e6t A% 1t ing* P1'ea-A%(ncQ A% =a iant* P1'ea-D"3Q A% =a iant* P&u geQ A% =a iant@
C C

1'eec,

Properties )i ection?@ A% Dl1'ea-)i ection 1'ea-!ellOnEnte ?@ A% >oolean

0,e 1'eec, object i% a ve ( %i#'le object t,at i% u%ed to i#'le#ent %'eec, in E6cel D&. 0o obtain a 1'eec, object* u%e t,e 1'eec, ' o'e t( o5 t,e A''lication object. 0,e 1'eec, object ,a% onl( two ' o'e tie%* )i ection and 1'ea-!ellOnEnte * and one #et,od* 1'ea-. 0,e )i ection ' o'e t( %et% t,e %'eec, di ection to one o5 t,e con%tant% in t,e 5ollowing enu#:
'n05 #lS3ea8Direction xlS3ea81 "ows = 7 xlS3ea81 Col05ns = 1 'nd 'n05

0,i% %et% t,e %'eec, di ection w,en u%ing t,e 1'ea- ' o'e t( o5 t,e Range object. $o in%tance* i5 t,e active%,eet ,a% content:
A1 = 1= 11 = 2 A2 = B= 12 = L

0,en t,e code:


"an4e("A1-12").S3ea8 xlS3ea81 "ows

will %'ea- 81* 2* 3* 48 w,e ea% t,e code:


"an4e("A1-12").S3ea8 xlS3ea81 Col05ns

will %'ea- 81* 3* 2* 48 0,e 1'ea-!ellOnEnte ' o'e t( i% a >oolean ' o'e t( t,at %'eci5ie% w,et,e o not E6cel will %'ea- t,e content% o5 t,e active cell w,en t,e Ente -e( i% %t uc- o w,en t,e 5ocu% #ove% to anot,e cell after editing of that cell. ?0,e cell content% a e not %'o-en ju%t becau%e 5ocu% i% lo%t< it onl( ,a''en% w,en t,e cell ,a% been edited and 5ocu% i% lo%t.@ 0,e 1'ea- #et,od %'ea-% te6t* a% in:
A33lication.S3eech.S3ea8 "To be or not to be"

o:
A33lication.S3eech.S3ea8 ActiveSheet."an4e("A1").+al0e

&pellingOptions Object
;ath- A33lication % S3ellin4*3tions

)b'e t
C C C C C C C C C C C C

1'ellingO'tion%

Properties A abic"ode%?@ A% DlA abic"ode% )ict3ang?@ A% 3ong .e #an&o%tRe5o #?@ A% >oolean Aeb ew"ode%?@ A% DlAeb ew"ode% /gno e!a'%?@ A% >oolean /gno e$ile+a#e%?@ A% >oolean /gno e"i6ed)igit%?@ A% >oolean Ro ean!o#bineAu6?@ A% >oolean Ro ean& oce%%!o#'ound?@ A% >oolean Ro ean2%eAuto!,ange3i%t?@ A% >oolean 1ugge%t"ainOnl(?@ A% >oolean 2%e )ict?@ A% 1t ing

0,e 1'ellingO'tion% object* etu ned b( t,e 1'ellingO'tion% ' o'e t( o5 t,e A''lication object* can %et o'tion% 5o E6celB% %'ell c,ec-ing 5eatu e. 0,e object ,a% no #et,od%* and it% ' o'e tie% gene all( co e%'ond to t,e o'tion% available t, oug, t,e 1'elling tab o5 E6celB% O'tion% dialog.

%ab Object
;ath- A33lication %% Chart % Tab

)b'e t 0ab
C C

Properties !olo ?@ A% =a iant !olo /nde6?@ A% Dl!olo /nde6

0,e abilit( to c,ange t,e colo o5 wo -%,eet and c,a t tab% i% new 5o E6cel D&. 0,i% i% done t, oug, t,e u%e inte 5ace b( ig,t;clic-ing on a tab and c,oo%ing 0ab !olo 5 o# t,e 'o'u' #enu. 0o c,ange t,e colo ' og a##aticall(* we can w ite code %uc, a%:
Hor8sheets("Sheet2").Tab.Color = vb:reen

0,e 0ab object i% etu ned b( t,e 0ab ' o'e t( o5 t,e 4o -%,eet and t,e c,a t object%* and it ,a% !olo and !olo /nde6 ' o'e tie% t,at can %et t,e colo .

'sedObjects Object
;ath- A33lication % <sed*bTects

)b'e t

Properties

2%edObject%

C C

!ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% Object

0,e docu#entation 5o t,e 2%edObject% collection object i% inc edibl( bad* even b( "ic o%o5t %tanda d%* w,ic, i% %a(ing a lot. /t give% ab%olutel( no clue a% to w,at u%ed object% #ig,t be* but it doe% tell u% ,ow to count t,e nu#be o5 u%ed object%C /5 (ou o'en a b and new wo -boo- ?wit, t,e de5ault t, ee e#'t( wo -%,eet%@* t,en t,e code:
A33lication.<sed*bTects.Co0nt

etu n% t,e nu#be 7* %o w,ateve i% u%ed object #ig,t be* t,e e a e 7 o5 t,e# in a new wo -boo-. A5te %o#e e6'e i#entation and #uc, 5 u%t ation* / conclude t,at i5 "ic o%o5t doe% not con%ide it wo t, %'ending ti#e to w ite even a %entence e6'laining u%ed object%* t,en it i% ' obabl( not wo t, (ou ti#e o #ine to t ( to 5igu e it out.

'serAccess(ist and'serAccess Objects


;ath- A33lication %% "an4e %% Hor8sheet %% %% Allow'dit"an4es %% Allow'dit"an4e %% 7ser-ccess"ist < 7ser-ccess ;rotection

)b'e t 2%e Acce%%


C C

Met*o&s )elete?@ Add?+a#e A% 1t ing* AllowEdit A% >oolean@ A% 2%e Acce%% )eleteAll?@


C C C C

Properties AllowEdit?@ A% >oolean +a#e?@ A% 1t ing !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% 2%e Acce%%

2%e Acce%%3i%t

2%e Acce%% object% allow ce tain u%e % to acce%% a ' otected ange wit,out e7ui ing t,e 'a%%wo d.

)atc! Object
;ath- A33lication %% Watches % Watch

)b'e t 4atc, 4atc,e%


C C C

Met*o&s )elete?@ Add?1ou ce A% =a iant@ A% 4atc, )elete?@


C C C

Properties 1ou ce?@ A% =a iant !ount?@ A% 3ong /te#?/nde6 A% =a iant@ A% 4atc,

0,e E6cel D& 4atc, window toolba i% a ve ( %i#'le* (et u%e5ul tool. /t enable% t,e u%e to view t,e content% o5 a cell even w,en t,e 'o tion o5 t,e wo -%,eet t,at contain% t,e cell i% not vi%ible. $igu e .;2 %,ow% t,e 4atc, 4indow 0oolba . Figure :-2. he 0atch 0in#ow 0,e 4atc, object i#'le#ent% t,e 4atc, 4indow ' og a##aticall(. 0,e 4atc,e% ' o'e t( o5 t,e A''lication object etu n% a 4atc,e% collection t,at contain% all o5 t,e o'en 4atc, object%. 0,e 4atc, object it%el5 ,a% onl( one eall( u%e5ul ' o'e t(<t,e 1ou ce ' o'e t(* w,ic, %'eci5ie% t,e cell to watc,. 0,e 4atc, object ,a% onl( one #et,od<)elete* w,ic, delete% t,e watc,. 0o illu%t ate* t,e watc, in $igu e .;2 can be c eated in eit,e o5 t,e 5ollowing wa(%:
Di5 wa As Hatch Set wa = A33lication.Hatches.Add("A1") Set wa = A33lication.Hatches.Add("an4e("A1"))

Colophon
Ou loo- i% t,e e%ult o5 eade co##ent%* ou own e6'e i#entation* and 5eedbac- 5 o# di%t ibution c,annel%. )i%tinctive cove % co#'le#ent ou di%tinctive a'' oac, to tec,nical to'ic%* b eat,ing 'e %onalit( and li5e into 'otentiall( d ( %ubject%. 0,e ani#al on t,e cove o5 Writing Excel Macros with VBA* 1econd Edition* i% a blue ja( ?(yanocitta cristata@* a voci5e ou%* agg e%%ive bi d co##on in t,e ea%te n ,al5 o5 t,e 2nited 1tate% and %out,e n !anada. 0,e blue;c e%ted ja( i% al%o an agile 5l(e and occa%ional ne%t; obbe . 0,e te # 8blue ja(8 i% al%o a''lied to t,e 1telle B% ja( ?(yanocitta stelleri@* a la ge * da -e ja( co##on in #uc, o5 t,e we%te n 2.1. and !anada* a% well a% %eve al ot,e %'ecie%. >lue ja(% eat ' i#a il( nut%* %eed%* and in%ect%* %o#eti#e% 'lanting aco n% in t,e g ound* t,u% ,el'ing t ee g owt,. Rnown 5o t,ei loud* ,a %,* and ea%il( identi5iable call%* blue ja(% ? elated to c ow% and aven%@ o5ten %'oil t,e ,unting 5o a(% o5 ot,e ani#al% b( wa ning 'otential ' e(. >lue ja(% a e b ig,t blue* w,ite* and blac-* wit, bot, %e6e% %i#ila in a''ea ance. 0,e( a e about 10 to 12 inc,e% in lengt,* and build la ge t ee ne%t% about 25 5eet o55 t,e g ound* into w,ic, a e laid 3 to 6 %'otted olive;colo ed egg%. 0,e #ale i% ve ( attentive du ing t,e ne%ting 'e iod%. Na(% a e %ociable and 5 e7uentl( t avel in g ou'%* anging 5 o# a #ating 'ai to a la ge 5loc-. !at,e ine "o i% wa% t,e ' oduction edito and 0atiana A'andi )iaG wa% t,e ' oo5 eade 5o Writing Excel Macros with VBA* 1econd Edition. )a en Rell( and !lai e !loutie ' ovided 7ualit( cont ol. Noe 4iGda w ote t,e inde6. /nte io co#'o%ition wa% done b( 1a a, 1,e #an and !at,e ine "o i%. Aanna )(e de%igned t,e cove o5 t,i% boo-* ba%ed on a %e ie% de%ign b( Edie $ eed#an. 0,e cove i#age i% a 19t,;centu ( eng aving 5 o# t,e )ove &icto ial A c,ive. E##a !olb( ' oduced t,e cove la(out wit, Oua -D& e%% 4.1 u%ing AdobeB% /0! .a a#ond 5ont. )avid $utato de%igned t,e inte io la(out. 0,i% boo- wa% conve ted to $ a#e"a-e 5.5.6 wit, a 5o #at conve %ion tool c eated b( E i- Ra(* Na%on "c/nto%,* +eil 4all%* and "i-e 1ie a t,at u%e% &e l and D"3 tec,nologie%. 0,e te6t 5ont i% 3inot('e >i -aH t,e ,eading 5ont i% Adobe "( iad !onden%edH and t,e code 5ont i% 3uca%$ontB% 0,e1an% "ono !onden%ed. 0,e illu%t ation% t,at a''ea in t,e boo- we e ' oduced b( Robe t Ro#ano and Ne%%a#(n Read u%ing "ac o#edia $ eeAand 9 and Adobe &,oto%,o' 6. 0,e ti' and wa ning icon% we e d awn b( !, i%to',e >ing. 0,i% colo',on wa% w itten b( +anc( Rota (. 0,e online edition o5 t,i% boo- wa% c eated b( t,e 1a5a i ' oduction g ou' ?No,n !,odac-i* >ec-i "ai%c,* and "adeleine +ewell@ u%ing a %et o5 $ a#e;to;D"3 conve %ion and cleanu' tool% w itten and #aintained b( E i- Ra(* >enn 1alte * No,n !,odac-i* and Ne55 3iggett.

You might also like