Worksheets in VBA Coding and in Worksheet Formulas
Transcript of Worksheets in VBA Coding and in Worksheet Formulas
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
1/31
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
2/31
%nothermethod o"loopingthrough thesheets. Sub )s&*o All) Sheets( i% sht As Worksheet For $ch sht n Sheets )s&*o sht.n$%e #e t sht n/ Subor perhaps a little more interesting$ color all "ormula cells Sub AllSheetsColorFor%ul$s( i% sht As Worksheet For $ch sht n Sheets 0n rror Resu%e #e t 'in c$se no or%ul$s sht.Cells.S2eci$lCells( lFor%ul$s . 3 nterior.Color n/e = 4 #e t sht n/ SubExample "rom 9ave (eterson to 66rechec& lin&66 uild an arra# o" sheetnames $1;;0
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
3/31
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
4/31
rr.#u%ber = 7 Then 'sheetn$%e $lre$/ e ists Sheets(ol/Sht .Activ$te it Sub n/ 0n rror Resu%e #e t 'Cre$te #ew Sheet Sheets.A// A ter>=Sheets(Sheets.Count '-- 2l$ce $t en/ ActiveSheet.#$%e = newSht Set ws#ew = ActiveSheet ws#ew.Cells(1, 1 = !'! " newSht 'n$%e o new sheet into cell ' Sheets(Sheets.Count .Activ$te 'tr to show l$st t$b Sheets(ol/Sht .Activ$te
n/ Sub3emove %LL commas "rom text constants in all wor&!oo&s. Aarning watchout "or 'SV "ile t#pe data. 02tion 2licit Sub WsRe2l$ce6ooseCo%%$s( i% ws As Worksheet For $ch ws n ActiveWorkbook.Worksheets
ws.Cells.S2eci$lCells( lCellT 2eConst$nts, . 3 Re2l$ce Wh$t>=!,!, Re2l$ce%ent>=! !, 6ookAt>= l8$rt, 3 Se$rch0r/er>= l* Rows, )$tchC$se>=F$lse #e t ws n/ Sub3ename a sheet to one ending with single 2uote "ollowed !# dou!le 2uote Sheets(! ;; ! .#$%e = ! ;; DD'!!!3un an application: 7no sheet here8 % Su% = A22lic$tion.Run(!)5C9ST0).:6)E) 3Func3Su%!, 1, ? )s&*o !)$cro result> ! " % Su%Examples o" assigning a sheetname < leading Deros can !e tric ActiveSheet.#$%e = !71;
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
5/31
Some Sheet related coding (#sheetcoding)
Sub )$cro (' ver %$cro shoul/ h$ve this o course... A22lic$tion.Screen92/$tin& = F$lse
A22lic$tion.C$lcul$tion = lC$lcul$tion)$nu$l' ooo 5our co/e here oooA22lic$tion.C$lcul$tion = lC$lcul$tionAuto%$ticA22lic$tion.Screen92/$tin& = True
'$ctive worksheet n$%e, $//ress $n/ in/e )s&*o ActiveSheet.#$%e " ! ! " ActiveCell.A//ress(7, 7 "
! sheet(! " A22lic$tion.C$ller.8$rent. n/e " ! !'Cre$te $ #ew Workbook cont$inin& onl one sheet Workbooks.A// lWorksheet set newwb = workbooks.$//(1 set newwb = Workbooks.A//( lW*ATWorksheet
'Cre$te #ew Sheet
Sheets.A//Sheets.A// A ter>=Sheets(Sheets.Count '-- 2l$ce $t en/
Sheets.A// *e ore>=ActiveSheet '-- /e $ult Sheets.A// A ter>=ActiveSheet'Ren$%e current Sheet ActiveSheet.#$%e = !Ren$%e/1 /it, )ove or Co2 Sheet, be sure to check the co2 'co2 $ ter l$st t$b o current workbook Worksheets(!) *e&innin&! .Co2 $ ter>=Worksheets(Worksheets.Count
'co2 sheet ro% $nother workbook be ore irst t$b o *ook Workbooks(!*ook1! .Sheets(!Sheet1! .Co2*e ore>=Workbooks(!*ook ! .Sheets(1
http://groups.google.com/groups?threadm=%[email protected]://groups.google.com/groups?threadm=%[email protected]://groups.google.com/groups?threadm=%[email protected]://groups.google.com/groups?threadm=%[email protected] -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
6/31
ActiveSheet.#$%e = new#$%e'Co2 the $ctive worksheet to Iust be ore the currentl $ctive sheetJco2 sheetK ActiveSheet.Co2 *e ore>=ActiveSheet'Co2 the $ctive worksheet to $ ter the l$st worksheet
ActiveSheet.Co2 A ter>=Sheets(Sheets.Count '-- special coding is required if some cells have more than 255characters 'Co2 the irst sheet Sheets(1 .Co2 A ter>=Sheets(1 'new sheet %i&ht look like
irst3sheet ('Co2 the l$st sheet Sheets(ActiveWorkbook.Sheets.Count .Select ActiveSheet.Co2 $ ter>=ActiveSheet '-- special coding is required if some cells have more than 255characters
'Current 2osition o sheet in the sheet t$bs Sheet n/e = A22lic$tion.C$ller.8$rent. n/e '- in $ unction Sheet n/e = ....construct nee/e/ or $ctive sheet...
')ove $ s2eci ic worksheet to the ront Sheets(! $t$! .)ove *e ore>=Sheets(1')ove $ s2eci ic worksheet to the en/ Sheets(! $t$! .)ove A ter>=Sheets(sheets.count'Select $ll cells on $ worksheet cells.Select'Select $ll cells on $ worksheet within the use/ r$n&e ActiveSheet.9se/R$n&e.Select'8rocess $ll cells on $ workshet within use/ r$n&e with te t const$nts For $ch cell n Cells.S2eci$lCells( lConst$nts, lTe t $lues cell. $lue = C bl(cell. $lue #e t cell'Select $n $re$ within $ worksheet set rn& = A22lic$tion. n2utbo (!Select r$n&e!,T 2e>=L'Select $n $re$ within $ %$cro, with current selection $s /e $ult Set Rn& = n2ut*o (!Select r$n&e!, !T T6 1!, Selection.A//ress(7,7'Select $n $re$ within $ worksheet, /e $ult current selection, re/owith cursor i% Rn& As R$n&e Set Rn& = A22lic$tion. n2ut*o (!Select r$n&e!, !T T6 1!, 3 Selection.A//ress(7, 7 , T 2e>=L )s&*o Rn&.A//ress(7, 7 Rn&.select ' t is sel/o% necess$r to ch$n&e the selection within $ %$cro 'the selection woul/ be ch$n&e/ bec$use ou to see it, but the '%$cro coul/ work with R$n&e(Rn& Iust $s e$sil $s selection.r$n&e
'Recre$te !testin&-co2 ! Worksheet ro% !testin&-skel! worksheet Sub testin&3skel3co2 ( 0n rror Resu%e #e t i% svAlerts As *oole$n svAlerts = A22lic$tion. is2l$ Alerts A22lic$tion. is2l$ Alerts = F$lse Sheets(!testin&-co2 ! . elete A22lic$tion. is2l$ Alerts = svAlerts Sheets(!testin&-Skel! .Co2 *e ore>=Sheets(!testin&-skel!
http://dmcritchie.mvps.org/excel/copyvba.htm#copywshttp://dmcritchie.mvps.org/excel/copyvba.htm#copywshttp://dmcritchie.mvps.org/excel/copyvba.htm#copywshttp://dmcritchie.mvps.org/excel/copyvba.htm#copyws -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
7/31
6C$se(6e t(ActiveSheet.#$%e, 6en(!testin&-skel! 3 = !testin&-skel! Then ActiveSheet.#$%e = !testin&-co2 ! n/ Sub
Some C" related coding and Row $ Column related
as well (#cellcoding)'Assi&n $ r$n&e o cells R$n&e(!A1> 1! = Arr$ (!Sheet #$%e!, !A1!, !*1!, !C1!, !te tbo 1! Rows(!1>1! .Font.*ol/ = True Cells(i, 1 = wkSheet.#$%e -- where wkSheet is set or e$chsheet Cells(i, = wkSheet.Cells(1, 1 .Te t 'Cle$r const$nts 2$st row 1 $n/ 2$st colu%n 1M le$vin& int$ct or%ul$s,$n/ or%$ttin&. 0n rror Resu%e #e t R$n&e(!* >! " Cells.S2eci$lCells( l6$stCell .A//ress . 3 S2eci$lCells( lCellT 2eConst$nts, lTe t $lues .Cle$rContents R$n&e(!* >! " Cells.S2eci$lCells( l6$stCell .A//ress . 3 S2eci$lCells( lCellT 2eConst$nts, l#u%bers .Cle$rContents
'Cle$r const$nts in $ selection ($lso see nsert Rows %$int$inor%ul$s
Selection.S2eci$lCells( lConst$nts .Cle$rContents' elete entire row C 66S(R,1 . ntireRow./elete Rows(R ./elete' elete entire colu%n C 66S(1,C . ntireColu%n./elete Colu%ns(C ./elete
'Fi l$stcell $ ter /oin& /eletions (not $ sure thin&, see reset$ll r = ActiveSheet.9se/R$n&e.Rows.Count '$tte%2t i l$stcell $ t/eletes'Fin/ irstcell in $ colu%n (see 2erson$l. lsE @otoTo20 CurrentColu%n
Cells(1, ActiveCell.Colu%n .Select'Fin/ l$stcell in $ colu%n (see 2erson$l. lsE@oto*otto%o CurrentColu%n i% l$strow $s lon& l$strow = $ctivesheet.Cells(Rows.Count, !A! . n/( l92 .Row'@et the l$st row colu%n o the use/ r$n&e l$strow = Cells.S2eci$lCells( l6$stCell .Row'To2 le t cell o $ r$n&e )s&*o 9se/R$n&e.A//ress(7,7 '(row,colu%n 1=$bsolute'*otto% ri&ht cell o $ r$n&e %s&bo use/r$n&e(1 (1 .o set(use/r$n&e.rows.count-1,use/r$n&e.colu%ns.count-1 .$//ress(7,7
http://dmcritchie.mvps.org/excel/insrtrow.htmhttp://dmcritchie.mvps.org/excel/insrtrow.htmhttp://dmcritchie.mvps.org/excel/lastcell.htm#resetallhttp://dmcritchie.mvps.org/excel/toolbars.htm#macroshttp://dmcritchie.mvps.org/excel/toolbars.htm#macroshttp://dmcritchie.mvps.org/excel/insrtrow.htmhttp://dmcritchie.mvps.org/excel/insrtrow.htmhttp://dmcritchie.mvps.org/excel/lastcell.htm#resetallhttp://dmcritchie.mvps.org/excel/toolbars.htm#macroshttp://dmcritchie.mvps.org/excel/toolbars.htm#macros -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
8/31
um%er of &ages to %e &rinted (#&agenum%ers)
hought *d include this speci"icall# "rom a posting !# Eric 9esart since therewas some 2uestion as to coding. 7all 2uotes in these "ormulas are dou!le
2uotes8#u%8$&es1 = ecute cel
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
9/31
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
10/31
C*cling through sheets looking for s&ecific value in cellA+ (#search)
Sub )$tchWStoA1( 'HH Fin/ cell A1 th$t cont$ins Custo%er #$%e 'HH $vi/ )cRitchie 777-7N-1? 2ro&r$%%in& or 6.Won& i% cn As Strin&, cn As Strin&, ws As Worksheet cn = n2ut*o (! nter custo%er n$%e, $s oun/ in cell A1!retr > cn = !! Then it Sub cn = Tri%(6C$se(cn i% cn; As Strin& cn; = cn For $ch ws n ActiveWorkbook.Sheets cn = Tri%(6C$se(Sheets(ws.#$%e .R$n&e(!A1! . $lue cn = Re2l$ce(cn , Chr(147 , !! cn = cn Then 'O 68, Workin& with Active Cell Worksheets(ws.#$%e .Activ$te '%ust /o this irst
Worksheets(ws.#$%e .R$n&e(!A1! .Activ$te it Sub n/ #e t ws cn = n2ut*o (cn " ! -- #ot Foun/! " Chr(17 " 3 !R #T R Custo%er n$%e, or hit JC$ncelK! @oTo retr
n/ Sub also included this comment.
thin& this is going to drive people craD# wh# not rename the sheets with thecustomer name or at least !uild a ta!le o" contents with in"ormation. See
m# !uildtoc.htm page all #ou need to do is add content o" cell %0 "rom eachsheet. Oou will also "ind in"ormation there "or sorting sheetnames .
Scroll for &osition in window of active cell (#scroll)
,is&la* Cell A+ in u&&er left corner of sheet
he "ollowing was to protect sheet "rom accidentall# deleting an# data in theactive range. Sub )$cro;?(
'Select cell without /$t$ $n/ /is2l$ to2 le t corner o sheet ' i% l$strow As 6on& 'l$strow = Cells.S2eci$lCells( l6$stCell .Row 'R$n&e(Cells(l$strow + 1, 1 , Cells(l$strow + 1, 1 .Select ActiveWin/ow.ScrollColu%n = 1 ActiveWin/ow.ScrollRow = 1 n/ Sub
,is&la* Active cell in u&&er left corner of sheet
http://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/buildtoc.htm#sortallsheetshttp://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/buildtoc.htm#sortallsheets -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
11/31
Sub )$cro;?( ActiveWin/ow.ScrollColu%n = ActiveCell.Colu%n ' ActiveWin/ow.ScrollColu%n = 1 ActiveWin/ow.ScrollRow = ActiveCell.Row n/ Sub
avigations within a sheet %ased on another sheet(#navigation)
he "ollowing would use another sheet with the address to navigate to.
Gote !oth %1, and %1? re"er !ac& to N1,$ so #ou cannot write a programdirectl# reversi!le as where to go !ac&wards in the macro "rom N1,.
Sheet =Sheet =Sheet
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
12/31
rr.#u%ber B 7 Then rr.Cle$r A22lic$tion.Co%%$n/*$rs(!Workbook T$bs! .Show8o2u2 n/ 0n rror @oTo 7
n/ Sub
ore on Sheet navigation on m# ool!ars and uild >' pages$ and o" coursethe -ore Sheets listing is easier to wor& with i" the sheets have !een sortedalpha!eticall# !# sheet name.7 Sort%llSheets 8.
Oou can hold the shi"t &e# down clic& on the wor&sheet ta! navigation arrowsto shi"t over to the next set o" wor&sheet ta!s currentl# out o" sight.
Some oo&s through Worksheets (#loo&sheets)
hese are example onl#$ the msg!ox will sort o" "ail on #ou i" #ou have man#
sheets$
he Loop hru which resem!les #our original will reach a limit on characters inthe msg!ox
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
13/31
A22lic$tion.Screen92/$tin& = True A22lic$tion.C$lcul$tion = lAuto%$tic ' l N u2 use
lC$lcul$tionAuto%$ticn/ Sub
8ublic Sub 6oo2Shorter( i% ws As Worksheet For $ch ws n ThisWorkbook.Worksheets )s&*o ws.#$%e ' ws.8rotect 2$sswor/>=!An Thin&! #e t ws
n/ Sub
Sub )$rtin77 ( '2l$ce ')$rtin 7'EA1 v$lue into /istinct sheets i% As $ri$nt i% sht As $ri$nt = Arr$ (!)$rtin 1!, !Sheet !, !)SFT! Sheets(!)$rtin 7! .Activ$te For $ch sht n
Sheets(sht .8$&eSetu2.6e tOe$/er = ActiveSheet.Cells(1, 1 #e t sht n/ Sub
Rename Sheet (#rename)
Sub RSO T( 'Ren$%e Sheet, .)cRitchie, 771-7 rr.Cle$r
Str = n2ut*o (!Su22l new n$%e or ol/ sheet, bl$h,! 3 " !bl$h,bl$h!, !Ren$%e Sheet!, ActiveSheet.#$%e Str = !! Then it Sub 0n rror Resu%e #e t ActiveSheet.#$%e = Str rr.#u%ber B 7 Then )s&*o rr.#u%ber " ! ! " rr. escri2tion rr.Cle$r @oTo retr n/ 0n rror @oTo 7 '... continue...... n/ Sub
,elete Active Sheet if name %egins with .sheet.(#,elete/hisSheet)
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
14/31
o help 2uic&l# clean up some testing. Aill delete sheets with names !eginning sheet without veri"ication. >ther sheets will !e deleted onl# when#ou repl# to the normal message.
%n exception "or sheets with program code
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
15/31
i% sh As Worksheet 'Ron /e *ruin, 2ro&r$%%in&, 77 -1 - LFor $ch sh n ThisWorkbook.Worksheets
A22lic$tion.WorksheetFunction.CountA(sh.Cells = 7 Then A22lic$tion. is2l$ Alerts = F$lse sh. elete A22lic$tion. is2l$ Alerts = True n/ #e t
n/ Sub
inked files in "'cel (#linked)
he "irst line is the wa# a lin& displa#s$the second line is how #ou might t#pe it inthe third line is how #ou might code the line "or use in G9 3E'the "ourth line invo&es a user de"ined "unction 7@9F8 >G% ol/erGe celGTA: SG% ile. ls - checkin&E*477
/>G% ol/erGe celGt$ esG% ile. lsJcheckin&KEb477 =!/>G% ol/erGe celGt$ esG! " A1< " !J!" A1? " !KE!"!b477! =% ile. lsE&et or%ul$(*
VBA gotocell reference (#gotocell)
Similar V % code is used to goto a cell 7see uild >' 8$ go to a macro7see No o'ell 8. A22lic$tion.@oto Re erence>=!sheet$$EC1
VBA goto su%routine$macro$function (#goto) ref
See No oSu!
%pplication.Noto 3e"erence:)%ctive'ell.Value
%pplication.Noto 3e"erence:) id7%ctive'ell.Formula$ 1$ i < 18
Coding a link address and friendl* name using 230"R 4 5 Worksheetfunction (#h*&erlink)
his topic was copied "rom !uildtoc.htm $ where additional similar in"ormationcan !e "ound$ including in"ormation on how to displa# h#perlin& in"ormation.
e sure to see the next topic down on this page ta&ing the re"erences out "romwithin 2uotes.Aor&sheet code that created a h#perlin& =O58 R6 # (!Jc>Gte%2 ol/erG $bc. lsK'Sheet 0ne'EDCD?!,!Sheet 0ne! J htt2>HHwww.%v2s.or&H/%critchieHe celHe cel.ht% K!,!) cel 8$&es!=O58 R6 # (! ile>GGGc>Gte%2G $vi/ )cRitchieG$.t t!,!thisone is &oo/!=O58 R6 # (!%$ilto> )cRitchieP 7P4 sn.co%!,! $vi/ )cRitchie!
http://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/code/gotostuff.txthttp://dmcritchie.mvps.org/excel/buildtoc.htm#gotosubhttp://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/sheets.htm#hyperlink_refhttp://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/code/gotostuff.txthttp://dmcritchie.mvps.org/excel/buildtoc.htm#gotosubhttp://dmcritchie.mvps.org/excel/buildtoc.htmhttp://dmcritchie.mvps.org/excel/sheets.htm#hyperlink_ref -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
16/31
t appears that even i" the lin& is to a cell in the same wor&sheet #ou mustinclude the !oo&name including .QLS extension$ as well as the sheetname. )HO(E3L GI7 [A Game.xls] ichael-'B $ ichael 8
@ntil someone pointed out that #ou can use the pound sign 7+8
# including the + the pathname can !e le"t o""$ these wor&: in Excel 1;;;$and as understand "rom Excel C and up$ !ut these re"erences are "roDenwithin dou!le 2uotes. See the ne't to&ic to ta&e the re"erences out "rom withinthe 2uotes.
=O58 R6 # (! C?!,C?=O58 R6 # (! Sheet;
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
17/31
o h#perlin& to a more descriptive longer note on the same row$ !ut in the column o" the otes wor&sheet.
=O58 R6 # (! !"C 66(!$//ress!,0FFS T( Notes E*1,R0W( -1,7 , !Click ornote!
o h#perlin& to the Gote in Column B same row on Gotes sheet$ !ut displa#the short description in Column A o" the Gotes sheet.=O58 R6 # (! !"C 66(!$//ress!,0FFS T(#otesE B1 ,R0W( -1,7 ,
0FFS T(#otesE A1 ,R0W( -1,7
o re"er to a cell in 'olumn 9 7col ?8 o" the same sheet at a speci"ic 3ow>FFSE to the row
=O58 R6 # (! ! " A R SS(R0W( + 41 , 1 ,< ,!ne t week! ta&es #ou down :+ rows to the first column$ so the "ormula can !e in an# cellon the row. he ? indicates a relative address result$ !ut it reall# doesn*t matteri" a!solute or relative since it is created during recalculation. Aill allow #ou to
put the "ormula on an# cell in the row $ the lin& will ta&e #ou ?0 rows !elowand to the "irst column.
Some alternatives to H#perlin&s within a wor&sheet or within a wor&!oo&
%n Event macro such as dou!le
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
18/31
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
19/31
)HO(E3L GI7 + J%993ESS7 % 'H7 x#D $ 0: 0,8$18$ x#D 8
Oou are loo&ing "or the value x#D 7not case sensitive8 in 0: 0,
i" #ou don*t want to see +G/% then insert the "ailing part o" the "ormula and the"ormula as "ollows:) F7 SE33>37 % 'H7 x#D $ 0: 0,$;88$ G/% $HO(E3L GI7 + J%993ESS7 % 'H7 x#D $ 0: 0,$;8$18$ x#D 88
9on*t as& me wh# the second "ormula h#perlin& wor&s !ut does not show up asa h#perlin&$ don*t &now.
Oou should loo& up %993ESS Aor&sheet Function and the % 'HAor&sheet Function in HEL( "or some details.
Jsing 230"R 4 5 Worksheet Function to also &ull in associated link(#vlooku&)
%dd another column to #our ta!le with the h#perlin&. Oou can extract theh#perlin& on #our Loo&up sheet with a @ser 9e"inedFunction h#perlin&address . . >n loo&up sheet
0: =2erson$l. lsEh 2erlink$//ress(A1
>n #our sheet with VL>>I@(=O58 R6 # ( 600 98(*L,6ooku2EA1>6ooku2EU1 7;,17,FA6S ,
600 98(*L,6ooku2EA1>6ooku2EU1 7;,;,FA6S
Additional "'am&les with mailtoE (#mailto)
=O58 R6 # (!)$ilto>! " * , A =O58 R6 # (!)$ilto>! " * ,!J K! = F( S RR0R(S ARCO(!V!,* ,!!,!%$ilto>! " O58 R6 # (* = F( S RR0R(S ARCO(!V!,* ,!!,O58 R6 # (!%$ilto>! " * ,!J K!
o extract a h#perlin& see h#perlin&address and other @ser 9e"ined Functionsin !uildtoc.htm+url .
Jsing 230"R 4 5 Worksheet Function where work%ook name canchange (#internal)
%s aread# mentioned #ou must include the "ilename$ even i" it re"ers to a cell inthe same wor&!oo&. Oou can use this code to generate the wor&!oo& name.%s mentioned on m# (athname page #ou must include the cell re"erence to get avalid result "rom the 'ELL Aor&sheet Function.
http://dmcritchie.mvps.org/excel/buildtoc.htm#hyperlinkaddresshttp://dmcritchie.mvps.org/excel/buildtoc.htm#urlhttp://dmcritchie.mvps.org/excel/pathname.htmhttp://dmcritchie.mvps.org/excel/buildtoc.htm#hyperlinkaddresshttp://dmcritchie.mvps.org/excel/buildtoc.htm#urlhttp://dmcritchie.mvps.org/excel/pathname.htm -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
20/31
,:) 97'ELL7 "ilename $%08$F G97 [ $'ELL7 "ilename $%08$08P0$F G97 ] $'ELL7 "ilename $%08$08
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
21/31
8riv$te Sub Worksheet3*e ore oubleClick(* $l 3 T$r&et As R$n&e, C$ncel As *oole$n 'to inst$ll -- ri&ht-click on the sheett$b o the sheet to ' be use/ in $n/ choose 'view co/e'. 8$ste this Worksheet ' event %$cro into the %o/ule. C$ncel = True '@et out o e/it %o/e i% As Strin& = ! 'Oel2 )e'E 4! ActiveWorkbook.FollowO 2erlink A//ress>= , #ewWin/ow>=F$lse n/ Sub
ink to a Chart or Sha&e! faked %* an "vent -acro H#perlin&s in Excel $ on (eltier$ some notes on h#perlin&s including how to "a&ea lin& to a chart 7or other shapes8.
/o Return to original worksheetE%s mentioned at the !eginning #ou can return with the Ie#!oard Shortcut
A /KArrow eft or with the ool!ar BAC5 %utton that can !einstalled on #our tool!ar as previousl# descri!ed.
his topic was a little hard to decide whether to put on this we!page7sheets.htm8$ or on m# uild a!le o" 'ontents we!page$ or on Event
acros we!page as another example$ or even perhaps the Shortcuts we!page !ecause o" the %'I !utton.
ink "'cel into 2/- or -S Word (#linke'cel)
"or S Aord as ':4 # 9ocuments4excel4!oo&0.xls+sheet0-c"or H L as Ua hre") c:4temp4m#!oo&.xls+sheet0-c wor&sheet 0U/a
'an*t get the a!ove H L lin& to wor& in Fire"ox$ let alone with a space in thesheetname with Fire"ox. Ua hre") "ile:///':4!&04h4excel1&41;;?
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
22/31
the "ollowing will S@ the cells 9,:N, in each o" the sheets named in the cellsL,:(, 79ominic 1;;B8;"!'E ;>@;! ,! B!
Some Worksheet "'am&les using 4 ,4R"C/(#indirect)
='Worksheet 1'E@; = # R CT(!Worksheet 1'! " !E! " !@;! = # R CT(!'Worksheet 1'! " !E@! " h< = # R CT(!'c>G) ocu%entsGJWTA ! " 5e$r(#ow( -1 " !. lsKSheet1'ED*D
he a!ove is hard to read$ single 2uotes surround the wor&sheet name !ecauseit includes a !lan&. %ll the rest are dou!le 2uotes.
G9 3E' will not wor& "or closed "iles.
Extended one step "urther using the sheetname in cell %0:
= # R CT(!'! " A1 " !'! " !E! " !@! " ODAD17,R0W(1>1
4 ,4R"C/ to refer to a cell in another work%ook 7actuall# used samewor&!oo&$ can #ou tell58
% ' 9 E F N
0 *h:
4
excel1
&4
[vloo&up.x
ls]
sheet0
,
*-
c?
Examp
le
*h:4excel1&4
[vloo&up.xls]sheet0,*-c?
1 h: excel1& vloo&up.xls
sheet0, '?
Example
*h:4excel1&4[vloo&up.xls]sheet0,*-'?
,
http://groups.google.co.uk/groups?threadm=domenic22-92BF6E.21183705052005@msnews.microsoft.comhttp://dmcritchie.mvps.org/excel/fillhand.htmhttp://dmcritchie.mvps.org/excel/fillhand.htmhttp://groups.google.co.uk/groups?threadm=domenic22-92BF6E.21183705052005@msnews.microsoft.comhttp://dmcritchie.mvps.org/excel/fillhand.htmhttp://dmcritchie.mvps.org/excel/fillhand.htm -
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
23/31
? "'am&le
Gote cell %0 and E0 show a single 2uote !ut #ou must t#pe in an additionalsingle 2uote !e"ore the one #ou see.
%0 )get"ormula7 G9 3E' 7%C88
A+ *h:4 a h:
%+ excel1&4 % excel1&
c+ [vloo&up.xls] c vloo&up.xls
d+ sheet0, d sheet0,
e+ *-c? e '?
f+) G9 3E' 7%0J 0J'0J90JE08 f
) G9 3E' 7 * J %1 J 4 J 1J 4[ J'1 J ] J91J *-JE18
g+ )%0J 0J'0J90JE0 g ) * J %1 J 4 J 1 J 4[ J'1J ] J91J *- JE1
f+; Example f ; Example
g+; *h:4excel1&4[vloo&up.xls]sheet0,*-c? g ;*h:4excel1&4[vloo&up.xls]sheet0,*-'?
another e'am&leESheetnames are in 'olumn ' 7cells '0:'B;8$ he sheets that hae a modelnum!er o" odel% in their %, cell are to have the value o" their H,0 cellstotalled in the "ormula. [ o! (hillips$ .excel$ 1;;=
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
24/31
he last sheet updated can !e identi"ied with the crippled )'ELL7 Filename 8until a recalcuation occurs$ !ut that is a ver# unsta!le "orm and expect it isrelated to serious pro!lems . here is no pro!lem with)'ELL7 "ilenname $%08 which returns the Filename (including full path) ofthe file that contains reference, as text. Returns empty text ("") if the worksheetthat contains reference has not yet been saved.
Referencing a Relative Worksheet ame in aWork%ook (#relsheet)
he "ollowing "ormula will o!tain the sheetname in the current wor&!oo&.Function RelSheet(sht As 6on& As Strin& RelSheet = Worksheets(sht .#$%e
n/ Function
Function W*3Sheet3cell(wb As Strin&, sht As 6on&, cell As Strin& AsStrin& W*3Sheet3cell = Workbooks(wb .Worksheets(sht .R$n&e(cell
n/ Function )w!TsheetTcell7 (0.xls $0$ % 1 8where (0.xls is #our other wor&!oo&where 0 is the relative wor&sheet num!erwhere % 1 is the cell in the other wor&!oo&*s relative sheet 0.
,is&la* Formula
>' )relsheet708
* >'*-!1 ) * J relsheet708J * J -!1
#pe ) G9 3E' 7 * J relsheet708J * J -!1 8
#pe )w!TsheetTcell7 martinTh#perlin&s.xls $0$ !1 8
':4temp4[martinTh#perlin&s.xls]Sheet1;
)'ELL7 "ilename $%08
+V%L@E- )w!TsheetTcell7'ELL7 "ilename $%08$0$ !1 8
#pe
)w!TsheetTcell7 97'ELL7 "ilename $%08$F G97 [ $'ELL7 "ilename $%08$08P0$F G97 ] $'ELL7 "ilename $%08$08
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
25/31
Ua hre")"ile:///':/temp/rls;M1B.xls+*savehistor#*-a, cell %,U/a
m# own test: cell %,
Work%ook (#work%ook)
his we!page is "or sheet coding$ !ut here is some code "or wor&!oo&s.
pen wor&!oo&s in current range down "rom %0.For $ch cell n R$n&e(!A1!, R$n&e(!$1! . n/( l own Workbooks.02en (cell " !. ls!#e t
Conditionall*
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
26/31
?enerate Sheets with sheetnames named from current selection of cells
Set up to generate sheetnames "or dates$ !ut easil# changed to Rust do constantswith activesheet.name)cell.text as needed.Sub &enWSt$bn$%es(
i% cell As R$n&e i% new#$%e As Strin&, As Strin& rr. escri2tion = !! 0n rror Resu%e #e t For $ch cell n Selection Worksheets.A// rr. escri2tion B !! Then it Sub )s&*o For%$t(cell. $lue, ! -%%-//! rr. escri2tion = !! new#$%e = For%$t(cell. $lue, ! -%%-//! ActiveSheet.#$%e = new#$%e rr. escri2tion B !! Then '-- $ile/ to ren$%e, 2rob$bl sheetn$%e $lre$/ e ists... = )s&*o (!F$ile/ to ren$%e inserte/ worksheet ! " 3 vb6 " 3 ActiveSheet.#$%e " ! to ! " new#$%e " vb6 " 3 rr.#u%ber " ! ! " rr. escri2tion, vb0 C$ncel, 3 !F$ilure to Ren$%e Worksheet>! '--eli%in$te $lre$/ cre$te/ sheet th$t w$s to be ren$%e/... A22lic$tion. is2l$ Alerts = F$lse ActiveSheet. elete A22lic$tion. is2l$ Alerts = True '--check or i%%e/i$e c$ncell$tion... = vbC$ncel Then it Sub rr. escri2tion = !! n/ #e t cell
n/ Sub
-iscellaneous (#misc)
he "ollowing is Rust some code that did not want to lose trac& o":
%nd i" the macro is exited with an# o" these things in e""ect is there an# wa# o"telling what*s wrong or Rust resetting it$
A22lic$tion. nter$ctive = F$lse '2revents user inter erencewith the %$cro A22lic$tion. is2l$ Alerts = F$lse 'su22ress 2ro%2ts $n/ $lerts A22lic$tion.Screen92/$tin& = F$lse 'su22ress screen re2$intin& ActiveWin/ow. isible = F$lse 'hi/es the $ctive win/ow
thought the "ollowing would put things !ac& to normal$ !ut a"ter rereading theswitches$ don*t thin& it would change. turned o"" macros and "ixed theinteractive once$ !ut more interested in i" #ou don*t &now what happened.
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
27/31
':4(rogram Files4 icroso"t >""ice4>""ice4Excel.exe /regserver change pathas needed or Rust use Excel.exe /regserver
1 01MM < Startup Switches "or icroso"t Excel 71;;1: 1 01MM$ 1;;;: 100?M0 C:0B ?C?8 7S%FE ode$ 3e
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
28/31
See V E Help "or Visi!le (ropert# having to do with wor&sheets and there isalso a xlver#hidden Worksheets(!Sheet1! . isible = F$lseWorksheets(!Sheet1! . isible = Truesht.visible = True
-issing Scroll%ars or Worksheet /a%s (#missing)
ools ptions '8 primaril# deals with isting Sheetnames $h#perlin&s$ and other documentation things.
uild a!le o" 'ontents$ with optional sorting o" sheets List o" Functions and Su!routines List o" wor&sheet names Enumerate%dd ns78 List the add
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
29/31
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
30/31
aster Sheet$ creation 7+mastersheet8
uild a!le o" 'ontents$ similar listings$ wor&ing with H#perlin&s and lessmacro oriented variation uild >' %nother %pproach . >n this pagesee Looping through sheets
3on de ruin has a we! page speci"icall# "or a aster Sheetgeneration: 'op# cells "rom each sheet in the wor&!oo& $ topics include:'op# a range o" each sheet$ 'op# the 'urrent3egion o" a cell o" eachsheet$ 'op# the @sed3ange o" each sheet$ 'op# a row or rows o" eachsheet$ 'op# "rom row5 till the last row with data$ and 'op# a column orcolumns "rom each sheet.
'ells $ 'ells related coding
(athGame
Slow 3esponse
Some items to speed up Excel o create a !utton on tool!ar see m# page
-
8/13/2019 Worksheets in VBA Coding and in Worksheet Formulas
31/31
ove$ 'op#$ 3eplace$ (opulate >ptimiDing V % $ 'hip (earson (athname in headings$ "ooters$ and cells (rint tips "or Excel $ 3on de ruin 3e"erencing Aor&sheets From Formulas 7'. (earson8$ V % procedure that
#ou can use to re"er to the "irst$ next$ previous$ or last wor&sheet in awor&!oo&$ called directl# "rom wor&sheet cells.
Summar# o" ethods "or Gavigating !etween wor&sheets that #ou will "indhere in # Excel (ages .
'reate a wee&l#Tchronolog#.xls 79. c3itchie$ 1;;C