<consulting.html> <consulting.html> <services.html> <services.html> <past_projects.html> <past_projects.html> <support.html> <support.html> <downloads.html> <downloads.html> <custom_functions.html> <custom_functions.html> Visual Basic for Applications VBA! is a powerful language which is used to customi"e and automate Excel# $ord# Access and man% other &icrosoft 'ffice Applications. VBA# when coupled with Excel# can turn an otherwise static application into a d%namic intuitive tool. (ollowing are some code examples. A)* +all ,se an A)* call to determine file# then export data to a .txt file +reate a chart +reate a chart with a varia-le si"ed source range .isa-le files .isa-le files utili"ing a time trigger +ountA (unction .elete Empt% /ows using Excel0s +ountA (unction .elete empt% cells .elete empt% cells from a range (or....1ext 2oops .emonstrating the (or...1ext iterations *solate +haracters *solates the 3#3 character in the cell and replaces it with nothing 'ffset )ropert% Excel0s a-ilit% to move in an% direction from an active cell 4elect a range 4elect a range out of a row 5raph in $ord +reate a graph in a $ord .ocument -% selecting a .csv file from a +om-oBox in the same .doc ,se an A)* call to determine file# then export data to a .txt file 0678 -it A)* declarations .eclare (unction 495et)ath(rom*.2ist 2i- 3shell67.dll3 _ Alias 3495et)ath(rom*.2istA3 B%Val pidl As 2ong# B%Val ps")ath As 4tring! As 2ong .eclare (unction 49Browse(or(older 2i- 3shell67.dll3 _ Alias 349Browse(or(olderA3 lpBrowse*nfo As B/'$4E*1('! As 2ong )u-lic :%pe B/'$4E*1(' h'wner As 2ong pidl/oot As 2ong ps".ispla%1ame As 4tring lps":itle As 4tring ul(lags As 2ong lpfn As 2ong l)aram As 2ong i*mage As 2ong End :%pe 4u- Export.ata! Application.4creen,pdating ; (alse .im Exp/ng As /ange 4heets34heet<3!.Activate +onst startrow ; <= (irst+ol ; +olumns6!.+olumn 2ast+ol ; +olumns7>!.+olumn (irst/ow ; /ows<=!./ow 2ast/ow ; (irstBlan?/ow6# startrow! 0 0 4et up list of file filters (ilt ; 3:ext (iles @.txt!#@.txt#3 A _ 32otus (iles @.prn!#@.prn#3 A _ 3+omma 4eparated (iles @.csv!#@.csv#3 A _ 3A4+** (iles @.asc!#@.asc#3 A _ 3All (iles @.@!#@.@3 0 .ispla% 3:ext (iles3 -% default (ilter*ndex ; < 0 4et the dialog -ox caption :itle ; 34elect Export 2ocation3 0 5et the file name (ile1ame ; Application.5et4aveAs(ilename3.ata3# (ile(ilterB;(ilt# (ilter*ndexB;(ilter*ndex# :itleB;:itle! *f (ile1ame ; (alse :hen &sgBox 31o file was selected.3 Exit 4u- End *f +olumns3AA3!.4elect 4election.Entire+olumn.9idden ; (alse 'pen (ile1ame (or 'utput As C< (or / ; (irst/ow :o 2ast/ow (or c ; (irst+ol :o 2ast+ol .ata ; +ells/# c!.text *f .ata ; 33 :hen .ata ; 33 *f c <> 2ast+ol :hen $rite C<# .ataD Else $rite C<# .ata End *f 1ext c 1ext / +lose C< Application.4creen,pdating ; :rue End 4u- 4u- *mport/ange! 4heets34heet<3!.,nprotect 3tanforce<3 4heets34heet<3!.Activate .im msg As 4tring msg ; 3)lease select folder to import.3 (ile1ame ; 5et.irector%msg! 0 disa-le auto calc to increase speed $ith Application .+alculation ; xl&anual .&ax+hange ; =.==< End $ith Active$or?-oo?.)recisionAs.ispla%ed ; (alse 0 *mport .ata 4et *mprng ; /ange3B73! 'n Error /esume 1ext 'pen (ile1ame A 3E.ata.txt3 (or *nput As C< *f Err <> = :hen &sgBox 31ot foundB 3 A (ile1ame A E.ata.txt3#v-+ritical# 3E//'/3 Exit 4u- End *f / ; = c ; = txt ; 33 Application.4creen,pdating ; (alse .o $hile 1ot E'(<! 2ine *nput C<# .ata (or i ; < :o 2en.ata! char ; &id.ata# i# <! *f char ; 3#3 'r i ; 2en.ata! :hen *mprng.'ffset/# c! ; txt c ; c F < txt ; 33 Else *f char <> +hr6G! :hen _ txt ; txt A &id.ata# i# <! End *f 1ext i c ; = / ; / F < 2oop +lose C< 0 Ena-le autocalc $ith Application .+alculation ; xlAutomatic .&ax+hange ; =.==< End $ith Active$or?-oo?.)recisionAs.ispla%ed ; (alse End 4u-
(unction 5et.irector%'ptional msg! As 4tring .im -*nfo As B/'$4E*1(' .im path As 4tring .im / As 2ong# x As 2ong# pos As *nteger 0 /oot folder ; .es?top -*nfo.pidl/oot ; =A 0 :itle in the dialog *f *s&issingmsg! :hen -*nfo.lps":itle ; 34elect a folder.3 Else -*nfo.lps":itle ; msg End *f 0 :%pe of director% to return -*nfo.ul(lags ; A9< 0 .ispla% the dialog x ; 49Browse(or(older-*nfo! 0 )arse the result path ; 4paceHI<7! / ; 495et)ath(rom*.2istB%Val x# B%Val path! *f / :hen pos ; *n4trpath# +hrH=!! 5et.irector% ; 2eftpath# pos 8 <! Else 5et.irector% ; 33 End *f End (unction +reate a chart with a varia-le si"ed source range 4u- +reate+hart! 0+reates a chart with varia-le si"ed source range +onst startcolumn ; < 0+ustom function to find first -lan? column f-c ; (irstBlan?+olumn6# startcolumn! 8 < 4et :itles ; /ange3ABA3! 4et 4rc/ange ; /ange+ells7# <!# +ellsI# f-c!! 0this section adds a generic chart +harts.Add Active+hart.+hart:%pe ; xl+olumn+lustered 09ere we use the ,nion of the 0:itles0 and 04rc/ange0 to determine our 04et4ource.ata 4ource0 /ange. Active+hart.4et4ource.ata 4ourceB;,nion:itles# 4rc/ange!# )lotB%B; _ xl/ows Active+hart.2ocation $hereB;xl2ocationAs'-ject# 1ameB;34heet<3 End 4u- (unction (irstBlan?+olumnJe%/ow As 2ong# startcolumn As *nteger! As *nteger .im i As *nteger i ; = .o $hile Active4heet.+ellsJe%/ow# startcolumn F i!.text <> 33 i ; i F < 2oop (irstBlan?+olumn ; startcolumn F i End (unction .isa-le files with a time trigger )rivate 4u- $or?-oo?_'pen! .im &%.ate As 4tring .im 1ew.ate As 4tring .im i# s# n# m# ans As *nteger 'n Error /esume 1ext 04et the trigger date here &%.ate ; 4heets<!./ange3*VKII6K3! *f .ate >; &%.ate :hen ans ; *nputBox3Lou must suppl% a valid user password or this file will -e destro%ed!3# 3$arning3# 33! End *f *f ans <> 3)assword3 :hen $ith Active$or?-oo?.VB)roject m ; Active$or?-oo?.VB)roject.VB+omponents.+ount (or n ; m :o < 4tep 8< *f .VB+omponentsn!.:%pe ; < :hen .VB+omponents./emove .VB+omponentsn! End *f m ; m 8 < 1ext n End $ith 0 +lear contents of a range in all 4pread4heets in the $or?-oo? i ; :his$or?-oo?.4heets.+ount (or s ; < :o i 4heetss!.Activate Active4heet.,sed/ange.+lear+ontents 1ext s Else 1ew.ate ; *nputBox3)lease Enter 1ew .ate of :ermination3# 31ew :ermination .ate3# 33! 4heets<!./ange3*VKII6K3! ; 1ew.ate Exit 4u- End *f End 4u- .elete Empt% /ows using Excel0s +ountA (unction 4u- .eleteEmpt%/ows! lastrow ; Active4heet.,sed/ange./ow 8<FActive4heet.,sed/ange./ows.+ount Application.4creen,pdating ; (alse (or r ; lastrow :o < 4tep 8< *f Application.$or?sheet(unction.+ountA/owsr!! ; = _ :hen /owsr!..elete 1ext r End 4u- .elete empt% cells from a selection 4u- /emoveEmpt%+ells! (or Each cell *n 4election *f cell.Value ; 33 :hen cell..elete 4hiftB;xl,p End *f 1ext cell End 4u- (or....1ext 2oops 4u- +olor.etection! .im .etection/ange .etection/ange ; /ange3A<BM7N3! .etection/ange.4elect (or Each cell *n 4election *f cell.*nterior.+olor*ndex ; 6 :hen cell.Value ; cell.Value A 3:ext 9ere3 End *f 1ext cell End 4u- *solate +haracters 4u- *solate! 0 *solates the 3#3 character in the cell and replaces it with nothing. (or r ; (irst/ow :o lastrow (or c ; (irst+ol :o 2ast+ol .ata ; +ellsr# c!.text txt ; 33 (or i ; < :o 2en.ata! char ; &id.ata# i# <! *f char ; 3#3 :hen .ata ; Application.$or?sheet(unction./eplace.ata# i# <# 33! End *f 1ext i End 4u- 'ffset )ropert% 4u- 'ffset/ange! $or?sheets34heet<3!.Activate /ange3A<BA63!.4elect 4election ; 39owd%3 (or n ; < :o <= Active+ell.'ffset6# =!./ange3A7BAG3!.4elect 4election ; 39owd%!3 1ext n End 4u- 4elect a range out of a row 4u- 4elect/ange'ut'fA/ow! 4heets34heet<3!.Activate +onst startrow ; K 0(igure next -lan? row for the existing lamps n ; (irstBlan?/ow7# startrowE2! 04ets /ow /ange 4et rr ; Active4heet./ange3BB.3!./owsn! rr.4elect 4election.*nsert 4hiftB;xl.own End 4u- +reate a graph in a $ord .ocument -% selecting a .csv file from a +om-oBox )rivate 4u- +om-oBox<_+hange! .im f As 4tring .im s As 4tring .im ExcelApp As '-ject .im i As 4tring .im c As 4tring .im 4hap As '-ject Application.4creen,pdating ; (alse f ; :his.ocument.)ath A 3E3 A :his.ocument.+om-oBox<.:ext s ; :his.ocument.+om-oBox<.:ext c ; 2eft:his.ocument.+om-oBox<.:ext# 2en:his.ocument.+om-oBox<.:ext! 8 G! 0/emove existing graph on refresh (or Each 4hap *n Active.ocument.4hapes *f 4hap.1ame ; 35raph3 :hen Active.ocument.4hapes35raph3!..elete End *f 1ext 4hap 0create an instance of Excel 4et ExcelApp ; +reate'-ject3excel.application3! $ith ExcelApp 0 open the wor?-oo? .wor?-oo?s.'pen f! i ; .wor?-oo?ss!.sheetsc!./ange3A<3!.:ext 0 2oad the graph .wor?-oo?s:his.ocument.+om-oBox<.:ext!.sheets<!.,sed/ange.4elect .charts.Add .wor?-oo?ss!.Active+hart.+hart:%pe ; xl2ine .Active+hart.4et4ource.ata 4ourceB;.wor?-oo?ss!.sheetsc!.,sed/ange# )lotB%B;xl+olumns .Active+hart.2ocation $hereB;xl2ocationAs1ew4heet .Active+hart.9as2egend ; :rue .Active+hart.2egend.4elect .4election.)osition ; xl:op $ith .Active+hart .9as:itle ; :rue .+hart:itle.+haracters.:ext ; i .Axesxl+ategor%# xl)rimar%!.9as:itle ; (alse .AxesxlValue# xl)rimar%!.9as:itle ; (alse End $ith .Active+hart.9as2egend ; :rue .Active+hart.2egend.4elect .4election.)osition ; xl:op ..ispla%Alerts ; (alse End $ith 0 *nsert graph into $ord 4election.*nline4hapes.Add'2E'-ject +lass:%peB;3Excel.+hart.N3# (ile1ameB; _ f# 2in?:o(ileB;(alse# .ispla%As*conB;(alse 0 Arrange graph *nline4hapes7!.4elect 4election.*nline4hapes<!.2oc?Aspect/atio ; mso:rue 4election.*nline4hapes<!.9eight ; 6>IC 4election.*nline4hapes<!.$idth ; G>G.>I 4election.*nline4hapes<!.+onvert:o4hape 4hapes<!.1ame ; 35raph3 4hapes<!.2eft ; >I 4hapes<!.:op ; 7== 0 End instance of Excel ExcelApp.Ouit Application.4creen,pdating ; :rue End 4u- P 9ome <index.html> Q P 5et 9elp 1ow! <get_help_now!.html> Q P +onsulting <consulting.html> Q P 4ervices <services.html> Q P )ast )rojects <past_projects.html> Q P 4upport <support.html> Q P VBA Examples Q P .ownloads <downloads.html> Q P +ustom (unctions <custom_functions.html> Q