You are on page 1of 16

(last update 23/3/2009) email (without spaces) : julian.excel @gmail.com or julian7_s @yahoo.

com Index listing A B C D E F G H I J K

! " # $ % & ' ( ) * + , - . end

Excel Macros (VBA) tips for eginners. ! "o not use macros that ha#e too many lines of co"es as this may confuse eginers. ! remem ere" $hen ! starte" learning macro programming an" going to sites y %hip &earson ' (ohn )al*en ach an" starte" seeing stars instea" of VB+++. ,implifie" macros are use" for easy un"erstan"ing for people li*e -./ an" ME. By the $ay y0 ! am traine" to e an Accountant an" not a programmer. ! hope the this site gets you starte" in Macro &rogramming ' 1oo" 2uc*. Auto 3un [24/12/2001] (back to top) Making your macros run automatically when opening your workbook !ou can either use the "uto #pen metho$ or the %orkbook #pen metho$ &hese macros will $isplay the message '(ello' when you open the workbook
)ub "uto*#pen() Msgbo+ '(ello' ,n$ )ub

&his co$e woul$ be locate$ in the mo$ule (owe-er i. you use the secon$ metho$/ the co$e must be in the workbook ($ouble click '&his %orkbook' in the e+plorer win$ow) 0lick on the $rop $own list (that says 1eneral) an$ select %orkbook 0lick on the $rop $own list (that says $eclarations) an$ select #pen
2ri-ate )ub %orkbook*#pen() Msgbo+ '(ello' ,n$ )ub

Acti#e %ell [3/1/2002] (back to top) "n acti-e cell is the current cell that is selecte$ &his term is use$ in many macros &his can be use$ as a marker " goo$ e+ample is when you nee$ to mo-e .rom your current cell 3efer to Mo#ing your cursor macro A""ing !tems to a com o ox [13/4/2002] (back to top) &o a$$ a combobo+ re.er to /ser 4orm &o populate a combobo+ or a listbo+ is the same !ou coul$ a$$ .rom the co$e or e-en .rom a range o. cells in your sprea$sheet &o a$$ .rom the co$e/ 5ust a$$ this line to your co$e

0ombo6o+1 "$$7tem '2ro$uct "' 0ombo6o+1 "$$7tem '2ro$uct 6'

%ounting 3o$s ' %olumns ' ,heets [28/10/2001] (back to top) %hen you ha-e selecte$ a range/ it is sometimes use.ul to know how many rows or columns you ha-e selecte$ as this in.ormation can be use$ in your macros (.or eg when you ha-e reache$ the en$/ you will know it is time to stop the macros &his macro will $o the trick
)ub 0ount() my0ount 9 )election :ows 0ount Msg6o+ my0ount ,n$ )ub ;0hange :ows to 0olumns to count columns

&he ne+t macro counts the number o. sheets instea$ 3efer to &rotecting all sheets macro which uses this metho$
)ub 0ount2() my0ount 9 "pplication )heets 0ount Msg6o+ my0ount ,n$ )ub

%arriage 3eturn [10/11/2002] (back to top) )ometimes you may want to put a line o. te+t on the ne+t row an$ not let it continue on the .irst row )ee this e+ample in a message bo+
)ub &wo<ines() Msg6o+ '<ine 1' = -b0r<. = '<ine 2' ,n$ )ub

%lose All 4iles [24/4/200>] (back to top) )ometimes you may want to close all .iles without sa-ing ?oing it manually is a hassle with the @uestion '?o you wanna sa-eA'
)ub 0lose"ll() "pplication ?isplay"lerts 9 Balse my&otal 9 %orkbooks 0ount Bor i 9 1 &o my&otal "cti-e%orkbook 0lose Ce+t i ,n$ )ub

%opying A 3ange [3/1/2002] (back to top) 0opy $ata .rom a speci.ic range can be $one with this macro (ere $ata is copie$ .rom the current sheet to the acti-ecell (:e.er to "cti-e 0ell)

)ub 0opy:ange() :ange('"1:"4') 0opy ?estination:9"cti-e0ell ,n$ )ub

&o copy .rom a range in another sheet (eg )heet4) to the acti-e cell you nee$ to change the co$e toD
)heets('sheet4') :ange('"1:"4') 0opy ?estination:9"cti-e0ell

%ounter [18/2/2002] (back to top) &o use a counter in your macro/ 5ust assign any cell to retain the -alue 7n this e+ample the cell "1 is chosen ,ach time the macro is run/ it a$$s the -alue 1 to the cell "1
)ub 0ount() mycount 9 :ange('a1') E 1 :ange('a1') 9 mycount ,n$ )ub

%urrent 5ate [24/12/2001] (back to top) 7t;s a goo$ i$ea to insert the current $ate when you sa-e the .ile so that you can tell i. it;s the latest -ersion #. course this is shown un$er .ile properties but how many people know where to .in$ itA !ou coul$ also put the current $ate in the .ooter o. your print out 7t is i$eal i. the $ate $oes not change unless the .ile is sa-e$ !ou can use this co$e (#n the $rop $own list that says $eclaration/ select be.ore sa-e an$ you will see the 1st line o. co$e shown below F more $etails refer to Auto 3un macro)
2ri-ate )ub %orkbook*6e.ore)a-e(6yGal )a-e"sH7 "s 6oolean/ 0ancel "s 6oolean) :ange('"1') 9 Cow ;)elect any cell you want ,n$ )ub

%urrent %ell %ontent [24/12/2001] (back to top) )ometimes we nee$ to know what the cell contains ie $ates/ te+t or .ormulas be.ore taking a course o. action 7n this e+ample a message bo+ is $isplaye$ :eplace this with a macro shoul$ you re@uire another course o. action
)ub 0ontent0hk() 7. "pplication 7s&e+t("cti-e0ell) 9 &rue &hen Msg6o+ '&e+t' ;replace this line with your macro ,lse 7. "cti-e0ell 9 '' &hen Msg6o+ '6lank cell' ;replace this line with your macro ,lse ,n$ 7. 7. "cti-e0ell (asBormula &hen Msg6o+ '.ormula' ;replace this line with your macro ,lse ,n$ 7. 7. 7s?ate("cti-e0ell Galue) 9 &rue &hen Msg6o+ '$ate' ;replace this line with your macro ,lse ,n$ 7.

,n$ 7. ,n$ )ub

%urrent %ell &osition [10/4/2002] (back to top) )ometimes we nee$ to know the current cell position &his woul$ $o the trick
)ub My2osition() my:ow 9 "cti-e0ell :ow my0ol 9 "cti-e0ell 0olumn Msgbo+ my:ow = '/' = my0ol ,n$ )ub

5eleting Empty 3o$s [28/10/2001] (back to top) &o $elete empty rows in a selecte$ range we can use the .ollowing macro &he macro here uses the Bor Ce+t <oop Birst the macro counts the rows in a selecte$ range to $etermine the when the macro shoul$ stop &he Bor Ce+t statement acts as a counter
)ub ?el,mpty:ow() :ng 9 )election :ows 0ount "cti-e0ell #..set(0/ 0) )elect "pplication )creenHp$ating 9 Balse Bor i 9 1 &o :ng 7. "cti-e0ell Galue 9 '' &hen ;!ou can replace '' with 0 to $elete rows with ;the -alue Iero )election ,ntire:ow ?elete ,lse "cti-e0ell #..set(1/ 0) )elect ,n$ 7. Ce+t i "pplication )creenHp$ating 9 &rue ,n$ )ub

&he statement '"pplication )creenHp$ating 9 Balse' pre-ents the screen .rom up$ating to ensure the macro runs .aster an$ the screen will not .licker ?on;t .orget to set it back to '&rue' 5eleting 3ange 6ames [13/04/2002] (back to top) &o $elete all the range names in your workbook/ this macro will $o the trick
)ub ?eleteCames() ?im CameJ "s Came Bor ,ach CameJ 7n Cames "cti-e%orkbook Cames(CameJ Came) ?elete Ce+t CameJ ,n$ )ub

5uplicates (7ighlight "uplicates in Bol" 3e") [28/10/01] (back to top) &here are times you nee$ to highlight $uplicate $ata in your worksheet &his macro $oes the trick
)ub ?ups:e$() "pplication )creenHp$ating 9 Balse :ng 9 )election :ows 0ount Bor i 9 :ng &o 1 )tep F1 my0heck 9 "cti-e0ell "cti-e0ell #..set(1/ 0) )elect Bor 5 9 1 &o i 7. "cti-e0ell 9 my0heck &hen )election Bont 6ol$ 9 &rue )election Bont 0olor7n$e+ 9 4 ,n$ 7. "cti-e0ell #..set(1/ 0) )elect Ce+t 5 "cti-e0ell #..set(Fi/ 0) )elect Ce+t i "pplication )creenHp$ating 9 &rue ,n$ )ub

Emailing )or* oo* [2/12/2001] (back to top) &o email your current workbook the .ollowing co$e
)ub ,mail() "cti-e%orkbook )en$Mail recipients:9'5ulsnKyahoo com' ,n$ )ub

E5!8 macros [40/L/2004] (back to top) 3efer to 8ext Manipulation. Errors in macros [1/2/2002] (back to top) ,-er ha$ a macro running per.ectly one $ay an$ the ne+t $ay errors keep on popping up e-en though you ne-er ma$e changes to that macroA &his is no .ault o. yours ?ue to the e+cel G6" $esign/ macro .iles get ba$ly .ragmente$ $ue to hea-y e$iting o. macros/ insertion o. mo$ules = user.orms %hat you nee$ to $o is copy your macros else where/ $elete the macros/ sa-e the .ile without macros #pen the .ile again an$ import the macros an$ sa-e it once more with the macros !ou macros will run properly until it gets .ragmente$ again at a later stage Error 8rapping [4/1/2002] (back to top) &rapping errors are important as users can $o mar-elous things to mess up you macros (ere you can use either o. these 2 statements
F #n ,rror :esume Ce+t #: F #n ,rror 1oto ,rror&rap1 more lines o. co$e ,rror&rap1: more co$e (what to $o i. there is an error)

&he .irst statement will allow the macro to continue the ne+t line o. co$e upon hitting an error but the secon$ statement will run an alternati-e co$e shoul$ there be an error

Excel 4unctions [M/2/2002] (back to top) Hsing ,+cel .unctions in G6" is almost the same as using them in a sprea$sheet Bor e+ample to roun$ an amount to 2 $ecimal places in a sprea$sheet woul$ beD
9roun$(1 2443/2)

7n G6" you woul$ nee$ to use the term "pplication .ollowe$ by the .unction ieD
"cti-e0ell 9 "pplication roun$("cti-e0ell/ 2)

Bor more e+amples see 3oun"ing 6um ers Expiry 5ates for )or* oo* 9 Macro [4/1/2002] ,ee ,ecurity in Excel
(back to top)

4or0 6ext 2oop [3/1/2002] (back to top) ,ee 5eleting Empty 3o$s or &rotect All ,heets " point to note is/ try not to use the Bor/ Ce+t loop because this metho$ is -ery slow unless o. course you $on;t know how to write your macro another way 4lic*ering ,creen [28/10/2001] (back to top) )ometimes when you run a macro/ the screen .lickers a lot $ue to the screen up$ating itsel. &his slows the macro $one especially when the macro has a lot o. work to $o !ou nee$ to inclu$e the statement as shown below "lso see 5eleting Empty 3o$s
"pplication )creenHp$ating 9 Balse

!ou nee$ to set the screen up$ating back to true at the en$ o. the macro 4unctions [41/1/2002] (back to top) 0reating .unction is use.ul as complicate$ .ormulas can be ma$e easier in co$e than in a sprea$ sheet Bormulas can be protecte$ so that users cannot see or mo$i.y them &he e+ample 7 use will calculate ta+ using the ,elect %ase ,tatement (ere;s the scenario
Birst N2300 is ta+ .ree Ce+t N2300 is ta+able at 3O "nything abo-e N3000 is ta+able at 10O 7n cell "1 type 7ncome an$ in cell 61 type in your income in numbers say N20000 7n cell "2 type &a+ payable an$ in cell 62 type 9ta+(61) 2ut the .ollowing co$e in a mo$ule &he ta+ payable here woul$ be N1L23 2ublic Bunction ta+(income "s )ingle) )elect 0ase income 0ase 7s P9 2300 ta+ 9 0 0ase 7s P9 3000 ta+ 9 (income F 2300) Q 0 03 0ase ,lse

ta+ 9 (income F 3000) Q 0 1 E 123 ,n$ )elect ,n$ Bunction

1oto (a range) [28/10/2001] (back to top) &o speci.y a macro to go to a speci.ic range you can use the 1oto metho$ (ere 7 ha-e alrea$y name$ a range in my worksheet calle$ ')ales' !ou may also use an alternati-e metho$ ie the :ange select metho$ Caming a range in e+cel is recommen$e$ rather than speci.ying an absolute cell re.erence
)ub 1o(ere() "pplication 1oto :e.erence:9')ales' ,n$ )ub #: :ange(')ales') )elect

1oing to the :st ,heet [28/10/2001] (back to top) !ou can select the .irst sheet o. the workbook without knowing the name o. the sheet by re.erring to it by the in$e+
)ub Birst)heet() )heets(1) )elect ,n$ )ub

1o8o ,heet [1M/04/2003] (back to top) )ometimes we ha-e many sheets or sheets with long names = we cannot -iew them all !ou can select which sheet to go to by using this macro
)ub 1o2sheet() my)hts 9 "cti-e%orkbook )heets 0ount Bor i 9 1 &o my)hts my<ist 9 my<ist = i = ' F ' = "cti-e%orkbook )heets(i) Came = ' ' = -b0r Ce+t i ?im my)ht "s )ingle my)ht 9 7nput6o+(')elect sheet to go to ' = -b0r = -b0r = my<ist) )heets(my)ht) )elect ,n$ )ub

7i"ing ,heets [28/10/2001] (back to top) &o hi$e your worksheet .rom users you can use the .ollowing co$e
)ub (i$e)heet() )heet1 Gisible 9 +l)heetGery(i$$en ,n$ )ub

7. you hi$e your sheets this way/ users will not be able to unhi$e them using the menus #nly using G6 co$es will be able to $isplay the sheets again 7i"ing Excel [4/>/2002] (back to top) !ou can hi$e the ,+cel application with this macro &his $isables the user .rom using the e+cel menus ?on;t .orget to set it back to -isible

)ub (i$e,+cel() "pplication Gisible 9 Balse ,n$ )ub

!nput Box [28/10/2001] (back to top) %hen you nee$ to get input .rom users/ you can use input bo+es &his macro will ask .or the user;s name an$ will $isplay a message '(ello' plus the user;s name
)ub 1et7nput() ?im My7nput ;&his line o. co$e is optional My7nput 9 7nput6o+(',nter your name') Msg6o+ ('(ello ') = My7nput ,n$ )ub

!nserting 3o$s [4/1/2002] (back to top) &o insert rows re@uire$ by a user is easy (ere the input bo+ is use$ so that a user can $e.ine the number o. rows re@uire$
)ub 7nsert:ow() ?im :ng :ng 9 7nput6o+(',nter number o. rows re@uire$ ') :ange("cti-e0ell #..set(0/ 0)/ "cti-e0ell #..set(:ng F 1/ 0)) )elect )election ,ntire:ow 7nsert ,n$ )ub

(ere the macro uses the range techni@ue where a range is .irst selecte$ an$ then subse@uently rows are inserte$ !40 8hen ,tatement [28/10/2001] ,ee &rotect All ,heets
(back to top)

(oining 8ext 8ogether [10/4/2004] (back to top) &here are times where we import te+t .ile into ,+cel an we get te+t that are separate$ 7 recei-e$ an email asking how put these te+t together )elect across your cells .irst an$ run this macro
)ub Roin&e+t() my0ol 9 )election 0olumns 0ount Bor i 9 1 &o my0ol "cti-e0ell 9 "cti-e0ell #..set(0/ 0) = "cti-e0ell #..set(0/ i) "cti-e0ell #..set(0/ i) 9 '' Ce+t i ,n$ )ub

;illing 4iles [1/12/2001] (back to top) Silling or $eleting .iles is easy (owe-er the .iles must not be in use$

)ub Sill.ile() ?im MyBile "s )tring ;&his line o. co$e is optional #n ,rror :esume Ce+t ;#n hitting errors/ co$e resumes ne+t co$e MyBile 9 'c:T.ol$erT.ilename +ls' kill MyBile ,n$ )ub

%il$car$s can be use :eplace the .ile name with Q (use with cautionU) ;illing 8he %urrent 4ile [M/2/2002] (back to top) Silling the current .ile you nee$ to change it;s status to rea$ only
)ub Sille$() "pplication ?isplay"lerts9Balse &his%orkbook 0hangeBile"ccess +l:ea$#nly Sill &his%orkbook BullCame &his%orkbook 0lose Balse ,n$ )ub

2o$er %ase [28/10/2001] (back to top) &o change te+t in a selecte$ range to lower case use this co$e
)ub <ower0ase() ?im cell "s :ange Bor ,ach cell 7n )election 0ells 7. cell (asBormula 9 Balse &hen cell 9 <0ase(cell) ,n$ 7. Ce+t ,n$ )ub

2ast A#aila le 3o$ [24/4/200>] (back to top) Many users nee$ to know the ne+t a-ailable row to input $ata &his co$e locates the ne+t a-ailable row in column "
)ub <ast:ow() :ange('aL334L') ,n$(+lHp) #..set(1/ 0) )elect ,n$ )ub

Message Box [18/2/2002] (back to top) %hen you nee$ to communicate with users/ you can use message bo+es &his macro will $isplay a message '&his macro is create$ by Rulian' &he Message 6o+ appearance can be customise$ to show whether it is 7n.ormation/ 0ritical Messages (ere the icon in the message bo+ woul$ be $i..erent &he buttons can also be customise to show e+tra !es/ Co/ #k buttons (3efer to # -es6o macro) &his macro will show you 4 $i..erent styles

)ub MyMessage() Msg6o+ '&his macro is create$ by Rulian' Msg6o+ '&he icon is $i..erent'/ -b7n.ormation Msg6o+ '&he top title is $i..erent'/ -b,+clamation/ 'Rulian;s &ips' ,n$ )ub

Mo"eless 4orms [10/11/2002] (back to top) )ometimes you want to allow users to be able to switch between your .orm an$ your sprea$sheet by clicking on either one "ll you nee$ to $o is set the .orm property o. )how Mo$al to Balse or you can try this (owe-er this is only .or ,+cel 2000 = abo-e
)ub myBorm() HserBorm show -bMo$eless ,n$ )ub

Mo#ing your cursor [28/10/2001] (back to top) )ometimes you nee$ to mo-e your cursor aroun$ your worksheet to reFposition it be.ore running the ne+t step o. a macro &he mo-ement here uses the row/ column position metho$ Also see (Visi le 3o$s)
)ub ?own() "cti-e0ell #..set(1/ 0) )elect ,n$ )ub )ub up() "cti-e0ell #..set(F1/ 0) )elect ,n$ )ub )ub :ight() "cti-e0ell #..set(0/ 1) )elect ,n$ )ub )ub <e.t() "cti-e0ell #..set(0/ F1) )elect ,n$ )ub

&rotecting 9 /nprotecting a sheet [28/10/2001] (back to top) &he macros below will protect/unprotect the current worksheet with a passwor$
)ub 2rotect)heet() ?im 2asswor$ ;&his line o. co$e is optional 2asswor$ 9 '1244' "cti-e)heet 2rotect 2asswor$/ &rue/ &rue/ &rue ,n$ )ub )ub Hn2rotect)heet() 2asswor$ 9 '1244' "cti-e)heet Hnprotect 2asswor$ ,n$ )ub

&rotecting all sheets [28/10/2001] (back to top) &o protect all the sheets this macro uses all the metho$s containe$ in this page (see counting sheets) &he !f0 8hen statement is also use$ here &his tests .or a con$ition an$ i. the con$ition is &:H,/ then the macro continuous the ne+t line o. co$e 7n this case it will ,C? the macro 7. the con$ition is C#& &:H,/ then it will go to the .ollowing line which in this case is to select

the ne+t sheet !ou will also notice the 4or0 6ext statement is also use$ &his acts as a counter to tell the macro how many loops to run 7n this case i. there are 4 sheets/ the macro will run 4 times protecting all the 4 sheets
)ub protect"ll() ?im my0ount ;&his line o. co$e is optional ?im i ;&his line o. co$e is optional my0ount 9 "pplication )heets 0ount )heets(1) )elect ;&his line o. co$e selects the 1st sheet Bor i 9 1 &o my0ount "cti-e)heet 2rotect 7. i 9 my0ount &hen ,n$ ,n$ 7. "cti-e)heet Ce+t )elect Ce+t i ,n$ )ub

&rotecting your VB co"e [10/4/2002] (back to top) &o protect your G6 co$e .rom being seen by others/ all you nee$ to $o is go to the pro5ect e+plorer/ point at your pro5ect an$ right click on it )elect G6" pro5ect properties/ click on the protection tab an$ check the <ock pro5ect .or -iewing an$ key your passwor$ &hat;s it 3an"om num ers [28/10/2001] (back to top) Bor macros to generate ran$om numbers/ the co$e is takes this .ormat F 7nt ((upperboun$ F lowerboun$ E1) Q :n$ E lowerboun$) %here the Hpperboun$ is the largest number ran$om number to be generate$ an$ <owerboun$ is the lowest
)ub :an$omCo() :an$omiIe MyCumber 9 7nt((4> F 1 E 1) Q :n$ E 1) Msg6o+ ('&he ran$om number is ') = (MyCumber) ,n$ )ub

7n this case the ran$om numbers that will be generate is between 1 an$ 4> 3ange 6ames [4/>/2002] (back to top) "ssigning range names to a range o. cells
)ub :ngCame() )election Came 9 'my:ange' ,n$ )ub

3esi<ing a 3ange [4/>/2002] (back to top) :esiIing a range is simple !ou can apply this to inserting rows = columns or to e+pan$ a selecte$ range &his macro resiIes the range to 8 rows by 8 columns
)ub :esiIe:ng() )election :esiIe(8/8) )elect ,n$ )ub

3oun"ing 6um ers [M/2/2002] (back to top) (ere 7 will show how to per.orm $i..erent types o. roun$ing Sey in 12443 in any acti-e cell an$ run the .ollowing co$e
)ub :oun$() "cti-e0ell 9 "pplication roun$("cti-e0ell/ F4) ,n$ )ub

&his co$e roun$ to the nearest 1000 thus gi-ing the -alue 12000
"cti-e0ell 9 "pplication 0eiling("cti-e0ell/ 1000) :eplace with this line o. co$e an$ it will roun$ up to the ne+t 1000 ie 14000 "cti-e0ell 9 "pplication Bloor("cti-e0ell/ 1000) :eplace with this line o. co$e an$ it will roun$ $own to the ne+t 1000 ie 12000

3unning A ,u 3outine [3/1/2002] (back to top) &o run another macro .rom within a macro you nee$ to use the 0all statement
)ub Macro1() Msgbo+('&his is Macro1') 0all Macro2 ;&his calls .or Macro2 to run ,n$ )ub

,a#ing a file [24/4/200>] (back to top) &here are times you may want a macro to sa-e a .ile automatically a.ter running a macro &he secon$ macro will sa-e the .ile with a name calle$ 'MyBile' !ou may speci.y the path i. you nee$ to &he last macro sa-es all opene$ workbooks
)ub )a-e() "cti-e%orkbook )a-e ,n$ )ub )ub )a-eCame() "cti-e%orkbook )a-e"s Bilename:9'0:TMyBile +ls' ,n$ )ub )ub )a-e"ll() myBile 9 "cti-e%orkbook Came

"cti-e%orkbook )a-e "cti-e%in$ow "cti-ateCe+t ?o %hile myBile PV "cti-e%orkbook Came "cti-e%orkbook )a-e "cti-e%in$ow "cti-ateCe+t <oop ,n$ )ub

,ecurity in Excel [4/2/2002] (back to top) <e-el 1 F &o protect your e+cel .iles/ there are a .ew steps re@uire$ to make it more $i..icult .or other users to by pass security &o pre-ent changes ma$e to the worksheet/ you nee$ to protect your worksheet )ee protecting sheets &o pre-ent sheets .rom being rename$/ mo-e$ or $elete$/ protect the workbook (owe-er protection o. worksheets an$ workbook can easily be hacke$ using macros as shown by an ,+cel $e-eloper 7 belie-e the ne+t le-el o. protection is protecting your macros &o protect your macros/ point at your pro5ect in the e+plorer win$ow/ right click on it an$ select G6" pro5ect properties/ click on the 2rotection tab/ check on <ock 2ro5ect .or Giewing an$ ne+t key in your passwor$ an$ you;re $one Cow the pro5ect cannot be -iewe$ or amen$e$ <e-el 2 F &he ne+t step is to /012e the user to ena3le your macro when opening your .ile &he best way is to use a macro to hi$e the important sheets (see (i$ing sheets) when sa-ing your .ile Hpon opening the .ile/ a macro will be use$ to unhi$e these sheets 7. the user $isables the macros when opening the worksheet/ they will not be able to -iew your worksheet unless they allow the macro to run <e-el 4 F &he .inal step is to put an e+piry $ate .or your worksheet or your macro (owe-er this has a $raw back as the user may change the system $ate o. the computer to by pass the step "lternati-ely you can use a counter (3efer %ounter Macro) to allow a .i+e$ number o. access to your worksheet or macro (ere you nee$ to sa-e the counter -alue each time the .ile or macro is use$ Hpon reaching the $e.ine$ limit/ $isable the macro or $isable the access o. your worksheet &he steps mentione$ abo-e are not 100O .ool proo. 6ut it will keep normal users out but not hackers an$ crackers (ere 7 will not supply the co$e as this can be lengthy an$ may be $i..icult to un$erstan$ but 7 belie-e these steps may be use.ul to some o. you out there ,elect %ase ,tatement [41/1/2002] (back to top) &his is a use.ul statement to use when you ha-e many con$itions &oo many 7Bs in your co$e will only make you more con.use ,ee 4unctions macro ,entence %ase [L/L/2003] (back to top) &o change te+t in a selecte$ range to sentence case use this co$e &his co$e was supplie$ by ,imon 7uggins. (e $i$ a .ine 5ob o. making the co$e work .or both earlier = current -ersions o. ,+cel &hanks .or the contribution PV )ub )entence0ase()
Bor ,ach cell 7n )election 0ells s 9 cell Galue

)tart 9 &rue Bor i 9 1 &o <en(s) ch 9 Mi$(s/ i/ 1) )elect 0ase ch 0ase ' ' )tart 9 &rue 0ase 'A' )tart 9 &rue 0ase 'a' &o 'I' 7. )tart &hen ch 9 H0ase(ch): )tart 9 Balse 0ase '"' &o 'W' 7. )tart &hen )tart 9 Balse ,lse ch 9 <0ase(ch) ,n$ )elect Mi$(s/ i/ 1) 9 ch Ce+t cell Galue 9 s Ce+t ,n$ )ub

,elect 5ata 3ange [4/4/200L] (back to top) &his is a use.ul when you nee$ to select the whole range o. $ata to copy to another sheet (especially a large range)
)ub )el"ll?ata() "pplication )creenHp$ating 9 Balse ?im my<ast:ow "s <ong ?im my<ast0olumn "s <ong :ange('"1') )elect #n ,rror :esume Ce+t my<ast:ow 9 0ells Bin$('Q'/ ["1]/ / / +l6y:ows/ +l2re-ious) :ow my<ast0olumn 9 0ells Bin$('Q'/ ["1]/ / / +l6y0olumns/ +l2re-ious) 0olumn my<ast0ell 9 0ells(my<ast:ow/ my<ast0olumn) "$$ress my:ange 9 'a1:' = my<ast0ell "pplication )creenHp$ating 9 &rue :ange(my:ange) )elect ,n$ )ub

8ext Manipulation [40/L/2004] (back to top) 7 recei-e$ many @ueries regar$ing te+t manipulation (ere are some use.ul te+t .unctions which you coul$ use to ,?7& your te+t
)ub my,$it() Msg6o+ <e.t('abc$'/ 2) Msg6o+ :ight('abc$'/ 2) Msg6o+ <en('abc$') ,n$ )ub ;?isplays 2 characters .rom <e.t ;?isplays 2 characters .rom :ight ;?isplays number o. characters

8ext Box %alculations (!n user forms) [24/12/200L] (back to top) &o per.orm calculations using te+t bo+es in user .orms/ you will nee$ to -ali$ate the $ata .irst/ otherwise you will be in .or surprises "ssuming you want to a$$ two -alues in 2 separate te+tbo+ an$ assign the answer to another
&e+tbo+1 9 Gal(te+tbo+2)EGal(te+tbo+4)

8imer [1/2/2002] (back to top) &o create a macro to measure time be.ore e+ecuting the ne+t line o. co$e use this simple co$e
)ub timer() "pplication %ait Cow E &imeGalue('00:00:10') Msg6o+ ('10 sec has elaspe$') ,n$ )ub

8itle %ase [28/10/2001] (back to top) &o change te+t in a selecte$ range to title case use this co$e
)ub &itle0ase() ?im cell "s :ange Bor ,ach cell 7n )election 0ells 7. cell (asBormula 9 Balse &hen cell 9 "pplication 2roper(cell) ,n$ 7. Ce+t ,n$ )ub

8op of the screen [10/11/2002] (back to top) &o make the acti-ecell be at the top o. the screen = to the le.t on the screen try this
)ub &op<e.t() "cti-e0ell )elect %ith "cti-e%in$ow )croll0olumn 9 "cti-e0ell 0olumn )croll:ow 9 "cti-e0ell :ow ,n$ %ith ,n$ )ub

/pper %ase [28/10/2001] (back to top) &o change te+t in a selecte$ range to upper case use this co$e
)ub Hpper0ase() ?im cell "s :ange Bor ,ach cell 7n )election 0ells 7. cell (asBormula 9 Balse &hen cell 9 H0ase(cell) ,n$ 7. Ce+t ,n$ )ub

/ser 4orms [M/4/2003] (back to top) "$$ing user .orms in your macro is simple %ith user .orms you can create 1H7s (1raphical Hser 7nter.ace) .or user who $o not ha-e much e+cel knowle$ge an$ make you e+cel programs more pro.essional looking 1o to your Gisual 6asic ,$itor win$ow = click on 7nsert/ select user .orm an$ a user .or will appear along with the toolbo+ Cow you can a$$ labels/ buttons/ te+t bo+es an$ many more items &he property win$ow will allow you to customise your user .orm &o $isplay your user .orm use these co$es PVHserBorm1 show
Hnloa$ Me ;to loa$ .orm ;to close the .orm with a macro

2oa"ing /ser forms $ith Multi&age [M/4/2003] (back to top) &o select a page in a Multipage ob5ect is .airly simple Rust remember the page 1 has a -alue o. 0/ page 2 a -alue o. 1 an$ so .orth &o loa$ a .orm with a speci.ic page in min$/ try using these co$es
)ub HserBorm1 Multi2age1 Galue 9 HserBorm1 )how ;this $isplays the ,n$ 1 user ;this .orm a.ter sets page page has been page2() 2 set )ub

# -es6o [18/2/2002] (back to top) &here are times you may want users to click !es or Co Rust insert this line o. co$e (ere the )elect 0ase statement is use$
!esCo 9 Msg6o+('&his macro will ?o you want to continueA'/ -b!esCo E -b0ritical/ '0aution') )elect 0ase !esCo 0ase -b!es ;7nsert your co$e here i. !es is clicke$ 0ase -bCo ;7nsert your co$e here i. Co is clicke$ ,n$ )elect

Visi le 3o$s (selecting) [24/12/200L] (back to top) &he normal way o. selecting the ne+t row cannot be use$ where there are hi$$en rows or .iltere$ $ata &o select the ne+t -isible row in a .iltere$ list we test each row until we .in$ the ne+t -isible row
)ub Ce+tGisible:ow() "cti-e0ell #..set(1/ 0) )elect ?o %hile "cti-e0ell ,ntire:ow (i$$en 9 &rue "cti-e0ell #..set(1/ 0) )elect <oop ,n$ )ub

You might also like