Excel Data Entry Tips and Techniques

Embed Size (px)

Citation preview

  • 8/18/2019 Excel Data Entry Tips and Techniques

    1/87

    Excel Data Entry Tips and Techniques

    [Ctrl]-[Home] takes you to A1

    [Ctrl]-[End] takes you to the last data cell in your worksheet

    Move to next sheet — [Ctrl]-[P!n]

    Move to "revious sheet — [Ctrl]-[P#"]

     $he %C& 'uttons to the le(t o( the sheet ta's let you naviate throuh

    sheets)

     *ou can right-click  on the %C& 'uttons and (rom the resultin menu select

    the sheet you want to view)

     $o move to the next window +o"en work'ook, — [Ctrl]-[$a']

     $ools ."tions Edit ta' has a dro"down (or s"eci(yin the direction thecell "ointer moves when ta""in [Enter] a(ter enterin data in a cell)

     $he /tatus 0ar at the very 'ottom o( the screen usually says &eady in the

    lower le(thand corner) 2t "rovides use(ul in(ormation) .ne use(ul (eature is

    that when a 'lock o( cells is selected the /#M o( the cells will a""ear in the

    /tatus 0ar) &iht-click the /#M in the /tatus 0ar and you can choose

    another (unction to a""ly to the selected cells) *ou can3t do anythin with

    this result however exce"t view it in the /tatus 0ar)

    4indow /"lit or 4indow 5ree6e Panes will divide the window a'ove and

    to the le(t o( the current cell "ointer "osition) $his will allow column and7or

    row headers to remain dis"layed in one section o( the window while you

    scroll and move throuh data in another section o( the window)

     *ou can also s"lit the window 'y drain the little ray 'ar a'ove

    the u" arrow in the vertical scroll 'ar and7or the little ray 'ar to the

    riht o( the riht arrow in the hori6ontal scroll 'ar)

    Selecting Cells

    Holdin [Ctrl] while usin mouse selection lets you select a non-

    contiuous 'lock o( cells

    Click the mouse once in the u""er le(thand corner cell o( a 'lock o( data

    you want to select then hold the [/hi(t] key down when you click the

    mouse on the cell that de8nes the lower rihthand corner o( your 'lock

  • 8/18/2019 Excel Data Entry Tips and Techniques

    2/87

    #se the key'oard — [/hi(t] com'ined with any key'oard movement

    selects

    [/hi(t]-arrows

    [/hi(t]-[Ctrl]-arrows

    /elect the current 'lock o( data — [Ctrl]-9

    .nce you3ve selected a 'lock o( cells "ointin at the 'order o( the

    selection will turn the mouse "ointer to an arrow this is the move cursor)

     $o move cells click and dra clickin when you have the move

    cursor7arrow)

    2( you hold [Ctrl] 'e(ore click-dra movin cells you can co"y the cells)

    Entering Patterned Data

     $y"e the 8rst two values in a se:uence select the two cells click and dra

    usin the 5ill Handle to extend the "attern

    2( you riht-click and dra with the 5ill Handle when you release the

    mouse you3ll 'e "resented with a list o( o"tions (or the ty"e o(

    "attern7se:uence you want to use)

    /elect a 'lock o( cells +contiuous or noncontiuous, ty"e the value to

    enter [Ctrl]-Enter will "ut that value in all the selected cells

    #se a (ormula and then once all the data is entered select the results use

    Edit Co"y click in the u""er le(thand corner o( the destination and use

    Edit Paste /"ecial as %alues

    2( you3d like to randomize your data one o"tion is to create a column o(

    random num'ers usin the &A;!+, (unction and then sort your data with

    the random num'er column as your sort 8eld)

    Excel Functions

     $he 25 (unction is (re:uently use(ul —IF(CONDITION, action or value if CONDITION is true, action or value if

    CONDITION is false)

    If you have standard text to enter, maybe one of 5!" #ossible entries, maybe

    usin$ the IND%& function 'ould hel#

  • 8/18/2019 Excel Data Entry Tips and Techniques

    3/87

    2( you want the 8rst value you enter 1 in 01 ?rd value in the list

    enter ? @

    Co"y this (ormula down Column C — 2;!EB+E1=E> 01,

    .nce you have the values you want in Column C Add a column and

    then use Co"y Paste /"ecial %alues to co"y the (ormula results to

    your new column

    *TC+ #rovides the com#limentary o#eration for IND%& $iven a value you

    'ant to find, loo- it u# in an array of #ossible values, and return the index number

    for that value.s location in the array

    MA$CH +location o( "ossi'le values looku" value match ty"e,

    2( matchDty"e is omitted it is assumed to 'e 1)

    2( matchDty"e is 1 array o( values must 'e sorted in ascendin order

    and

    MA$CH 8nds the larest value that is less than or e:ual to

    looku"Dvalue)

    2( matchDty"e is MA$CH 8nds the 8rst value that is exactly e:ual to

    looku"Dvalue)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    4/87

     *ou3d still have to convert this num'er to a value in another column

    usin the 2;!EB (unction discussed a'ove)

     $he <

    3#ecial and choose the Conditional (ormats radio 'utton) $o 8nd cells with

  • 8/18/2019 Excel Data Entry Tips and Techniques

    5/87

    identical conditional (ormats to the selected cellclic- 3ame belo' Data

    validation To find cells 'ith any conditional formats, clic- *ll belo' Data

    validation

    Tools < O#tions < Calculation < ;recision as Dis#layed chec-box could #revent

    you from fallin$ into a #otentially embarrassin$ roundin$ error6 situation

    ?ant to trans#ose your columns to ro's or ro's to columns@ The trans#ose

    feature in Office AB""" is in the %dit < ;aste 3#ecial dialo$ box

    1) /elect the cells to trans"ose Edit Co"y

    G) &iht-click on the u""er-le(thand corner o( a rane o( em"ty cells

    that can accommodate the trans"osed result)

    ?) Choose Paste /"ecial and check the trans"ose check'ox

    3electin$ cells and ta##in$ the Delete -ey only deletes the cell contents,

    e/uivalent to %dit < Clear < Contents

    Edit !elete Icho"s outJ the cell and asks you how you3d like to 8ll

    in (or the missin cells

    Edit Clear 5ormats will let you delete the (ormattin o( the cell

    e)) 'orders (onts numeric (ormattin @

    G There may be certain #ortions of the 'or-sheet that you.d li-e to #rotect from any

    #ossible chan$es 1y default all the cells in the 'or-sheet are loc-ed but the loc-s

    are i$nored Tools < ;rotection < ;rotect 3heet activates reco$nition of the loc-s

    1efore usin$ ;rotect 3heet you 'ould unloc- all the cells you 'ant to be able to

    edit 'hen the rest of sheet is #rotected E 3elect Cells, Format < Cells < ;rotection

    tab and unchec- the default loc- Then use Tools < ;rotection < ;rotect 3heet

    If most of your cells are $oin$ to be un#rotected 'ith ust a fe' #rotected E

    1) #se [Ctrl]-A to select all the cells in the sheet

    G) 5ormat Cells Protection ta' and uncheck the de(ault lock

    ?) /elect the cells you !. want to "rotectK) 5ormat Cells Protection ta' and CHECF the lock 'ack on

    L) $ools Protection Protect /heet

    Miscellaneous

  • 8/18/2019 Excel Data Entry Tips and Techniques

    6/87

    Electronic Post-2t notes miht 'e use(ul to make comments on your

    worksheet 2nsert Comment)

    ."tions (or "rintin comments with the sheet are in= 5ile Pae /etu"

    /heet Comments o"tion dro"down

    !ata $ext to Columns is a use(ul tool (or se"aratin into multi"le columns

    text that is currently contained in one column) 5or exam"le may'e you

    have social security num'ers in column A and you ust need the last (our

    diits o( the //;) !ata $ext to Columns would let you se"arate the //;

    into two columns)

    /ometimes you may want to com'ine into one column text that is

    currently contained in two columns) .ne way to do this is to write a

    (ormula in a third column which uses the concatenation o"erator the

    am"ersand) $his (ormula in C1 would return the contents o( A1 (ollowed

    'y the contents o( 01= =!"#!

    2( you want to include a literal character say a s"ace use the

    (orm= =!"$ %"#!

     $he 4i6ard used in !ata $ext to Columns is the same tool you have

    availa'le (or o"enin text 8les you want to im"ort into Excel) 5ile ."en

    with some text 8le will 'rin you to /te" 1 o( the !ata 2m"ort 4i6ard)

    #se 5ile Pae /etu" Pae ta' 5it to check'ox i( your "rintout is

    extendin ust 'eyond one "ae

     $o move or co"y an entire worksheet to a new work'ook=

    1) ."en the oriinal work'ook and choose Edit Move .r Co"y /heet

    G) 4hen the Move .r Co"y dialo 'ox o"ens click the arrow at the

    riht side o( the $o 0ook list 'ox) Choose ;ew 0ook (rom the list)

    ?) 2( you want to co"y the sheet select the Create A Co"y check 'ox

    and click .F)K) Excel moves or co"ies the selected sheet to a new work'ook)

    /ometimes codes used in data entry need to 'e sorted in some order that

    is not al"ha'etical or numeric) *ou can de8ne a custom sort order in $ools

    ."tions Custom

  • 8/18/2019 Excel Data Entry Tips and Techniques

    7/87

    ."tions 'utton has a dro"down where you can choose your list (or the sort

    order)

    &o' to Create a Drop Do'n(ist in Excel)Having a drop-down list in your Microsoft Excel spreadsheet can often improve the

    eciency of your data entry, while also limiting data entry to a specic set of items or

    data made available in the drop-down list.

    !ro"-down lists can reatly (acilitate data entry) Here3s a look at how to use

    Excel3s data validation (eature to create handy lists within your worksheets)

    2n Access you can limit user entries 'y (orcin users to choose a value (rom a

    list control) .Nce a""lications use the same (unctionality in 'uilt-in dro"-down

    lists) 5or instance the Hihliht and 5ont Color controls on most 5ormattin

    tool'ars use this Oexi'le tool) /im"ly click the small trianle to the riht o( the

    icon to dis"lay a list o( choices)

     *ou can create the same ty"e o( control (or your users in an Excel sheet 'ut the"rocess isn3t intuitive) $he o"tion is in the !ata %alidation (eature) 5ortunately

    once you know the (eature exists it3s easy to im"lement) *ou need only two

    thins= a list and a data entry cell) $he (ollowin sheet shows a sim"le dro"-

    down list in an Excel sheet)

    #sers click the dro"-down arrow to dis"lay a list o( items (rom A1=AK) 2( a user

    tries to enter somethin that isn3t in the list Excel reects the entry) $o add this

    dro"-down list to a sheet do the (ollowin=

    1) Create the list in cells A1=AK) /imilarly you can enter the items in a sinle

    row such as A1=!1)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    8/87

    G) /elect cell E?) +*ou can "osition the dro"-down list in most any cell or

    even multi"le cells),

    ?) Choose %alidation (rom the !ata menu)

    K) Choose

  • 8/18/2019 Excel Data Entry Tips and Techniques

    9/87

    CT:( 7 ; to display 9ormulas3 4hile in the "rocess o( mis'ehavin

    trou'leshootin num'ers have a o at the (ormulas 8rst) /ee the (ormula

    used in the cell 'y sim"ly hittin the two keys= Ctrl S T +also called the

    acute accent key, which is the (urthest to le(t on the row with num'er keys)

    2t is tilde +U, when you "ress shi(t)

    Excel Tips " Tricks 9or a Success9ul and E

  • 8/18/2019 Excel Data Entry Tips and Techniques

    10/87

    Dou0le clicking 9ormat painter3 4ith a sim"le mouse click you can

    now du"licate a (ormat in other cells with only a click) Many excel users

    use it 'y dou'le clickin on the (ormat "ainter to co"y the (ormat into

    multi"le cells) *ayV

    @ Simple Tricks to Make Smart and 8eat Excel raphs

    o these were some ama!ing excel tips that are deemed to come extremely handy in

    times when you need to save even an itsy amount of time. "t is always easier when you

    ma#e your data easier to manage.

    a#ing the time to learn some Excel tips and tric#s will li#ely help you boost your

    productivity and streamline your spreadsheets.

    $e previously shared some Excel insights. Here are ve more tips to consider%

    !A 4se 8um0er Formatting Shortcuts

    &or circumstances when you need to format a large amount of data, Excel o'ers time-

    saving shortcuts for many common formatting functions. Experiment with these handy

    ones%

    5ormat num'ers to include two decimal "laces= CtrlS/hi(tS1 5ormat as time= CtrlS/hi(tSG

    5ormat as date= CtrlS/hi(tS?

    5ormat as currency= CtrlS/hi(tSK

    5ormat as "ercentae= CtrlS/hi(tSL

    5ormat in scienti8c7ex"onential (orm= CtrlS/hi(tS

    (lso )ead% Com"lete

  • 8/18/2019 Excel Data Entry Tips and Techniques

    11/87

    wor#sheet as your data range. par#lines can help you easily display trends in your

    data in a compact format.

    (lso )ead% 1 /im"le $i"s $o Make *our Excel Charts /exier

    A Manipulate Data 'ith Piot Ta0les

    $hen you have a large, detailed data set, pivot tables allow you to easily manipulate

    your data. hese tables are interactive and can help you analy!e data, detect patterns

    and ma#e comparisons. 3reating a pivot table is as easy as using the built-in 4ivotable

    and 4ivot3hart $i!ard, located in the 15ata2 drop-down menu. he wi!ard helps you

    choose the data to include in your 4ivot3hart and format that information in a

    meaningful manner. &or more information on creating pivot tables, refer to

    this Pivot$a'le $utorials.A Moe #et'een Formulas and :esults

    o eciently switch between the cell data and formula, use the 3trl6tilde 789 #eystro#e.

    his allows you to rapidly chec# formulas when wor#ing in a large spreadsheet.

    A &ide Gero Halues

    Hiding !ero values can be helpful within large data sets by allowing you to see data

    more clearly. o hide !ero values, you simply need to change the options in your Excel

    setup. :avigate to this function by clic#ing the 1&ile2 drop-down menu and choose

    1;ptions.2 hen choose 1(dvanced2 from the left-hand menu and unchec# the box for

    1how a !ero in cells that have !ero value.2 7Mac users%

  • 8/18/2019 Excel Data Entry Tips and Techniques

    12/87

    =ut it turns out that there0s a lot of data munging you can do in a plain old Excel

    spreadsheet > if you #now how to craft the proper formulas.

    "n a presentation at the recent *+? 3omputer (ssisted )eporting 73()9 conference,

    Mary@o $ebster, senior data reporter with 5igital &irst Media > a newspaper group in

    :ew Aor# > shared some of her favorite Excel tric#s. he goal of these tips, $ebster

    said% Bearn at least one new thing that will ma#e you say, 1$hy didn0t " #now this

    beforeC2

    Date 9unctions

    Tip !3 Split dates into separate Ields

    Aou can extract the year, month and day into separate elds from a date eld in Excelby using formulas DAear73ell$ith5ate9, DM;:H73ell$ith5ate9 and

    D5(A73ell$ith5ate9. plitting dates this way > by year, month and day of month >

    wor#s in Microsoft (ccess as well, $ebster said.

    "n addition, you can also get the day of the wee# for any date in Excel with

    D$EE5(A73ell$ith5ate9. he default returns numbers, not names of the days of

    wee#, with for unday, * for Monday and so on.

    o display the name of the wee#day instead of a number, apply a custom format to the

    cells with the wee#day numbers, using &ormat cells F 3ustomG then type ddd in the

    ype text box to get three-day abbreviations or dddd for the full day name.

    Tip B3 Find someoneJs current age

    "f you have someone0s date of birth, you can nd his or her current age on whatever

    day you open the spreadsheet with the D5(E5"&79 and D;5(A79 functions. ;5(A79,

    as you might guess, gives the current date. 5(E5"&79 gives the di'erence between two

    dates in units of years 71y29, months 71m29 or days 71d29, using the syntax%

    D5(E5"&75ate, 5ate*, nit of measure9

    o, to get current age in years, use the formula%

  • 8/18/2019 Excel Data Entry Tips and Techniques

    13/87

    D5(E5"&73ell$ith=irthday,;5(A79, 1y29

    :ote that the years unit returns ages in whole numbers and does not round up.

    :eshaping dataTip 3 Create multiple ro's out o9 only one

    ometimes you need data in a format with one row for each observation, but what you

    already have comes with multiple observations for each row instead. "n $ebster0s

    example of ('ordable 3are (ct Exchange plan pricing, there is a column for prices in

    each age group% -*+ years old, * years old, ** years old and so on. However, some

    visuali!ation and analysis tools reIuire one rowfor each plan/price combination,

    not one row with multiple prices.

    Sometimes you need data in a format with one row for each observation, but what you already

    have comes with multiple observations for each row instead. Here’s one way to resolve it.

    ableau visuali!ation software is one such tool that needs one data point per row, not

    multiple data points per row, so the vendor created a ableau )eshaper ool that

    wor#s with recent versions of Excel on $indows.

    Aou can download this (ree tool (rom the $a'leau we'site. (lthough one add-in says

    it0s for Excel *++, it wor#ed ne with Excel *+ on my $indows J 43.

    http://kb.tableausoftware.com/articles/knowledgebase/addin-reshaping-data-excelhttp://kb.tableausoftware.com/articles/knowledgebase/addin-reshaping-data-excelhttp://kb.tableausoftware.com/articles/knowledgebase/addin-reshaping-data-excel

  • 8/18/2019 Excel Data Entry Tips and Techniques

    14/87

    everal 3() attendees said they0ve spent hours reshaping large data sets by manually

    cutting and pasting, and the free ableau tool will save them a lot of time. Aou don0t

    need to have other ableau software installed on your system to use it.

    he columns you0re #eeping as row "5 columns should be placed on the left, and all

    your data columns on the right. o use the reshaper tool, put your cursor on the rst

    cell with data that you want transformed. hen go to the ableau menu and choose

    reshape data. ay ;. Aou can watch a brief example below.

    Tip 3 Create more easily sorta0le data

    (nother common data format problem is when you get a 1spreadsheet2 that0s less li#e

    a sortable table of data and more li#e a $ord document with column headers. ;ne

    example% a spreadsheet with the name of a team on one row followed by all the

    players on that team, then the name of another team right below followed by the

    players and so on. "t0s dicult to analy!e a wor#sheet where column headers are

    interspersed with data, since you can0t easily sort, lter or visuali!e data by team.

    ;ne way to deal with this is to add a new column with the team name for each player.

    It’s difficult to analyze a worksheet where column headers are interspersed with data, since you

    can’t easily sort, filter or visualize data by team. One way to deal with this is to add a new

    column with, in this case, the team name for each player.

  • 8/18/2019 Excel Data Entry Tips and Techniques

    15/87

    1he tric# is that you need to have a pattern to follow,2 according to $ebster. "n the

    example above, the position column is empty for the team name rows but lled in for

    the player rows. =y lling in Kust the rst cell with the team name manually, you can

    then use this formula to automatically ll in the rest%

    25+0?ZZA?CG,

    hat says% "f cell = is blan#, ll in the value of the cell in the rst column of the same

    row 7in this case (9. ;therwise, ll in the value from the cell that0s Kust above it 7in this

    case 3*, which should be the team name from the row above for all the player rows9.

    Ma#e sure to start with the rst player row after having manually entered the rst

    header row.

    Search and replaceTip 3 Create a ne' column

    Aou probably #now that you can do a search and replace in Excel with a typical text-

    editor control-& nd-and-replace. =ut did you #now that you can also create an entirely

    new column in Excel based on search-and-replace on an existing columnC hat needs

    the D="E function, using the syntax%

    /#0/$2$#$E+Cell4ith$ext ZoldtextZ ZnewtextZ,

    (nd for lots more on getting the most out of Excel%

     

    Excel H# Tutorial Part ! - H# Code Presentation

    4hen learnin to "roduce %0A code it is im"ortant (rom the very

    start that you ado"t ood ha'its in terms o( code "resentation sothat it is easy to read the code and understand how it works)

    4hile you may at the time you are writin the code have a clear

    idea o( what it does and how it works you need to consider what the

    code will look like when you return to it in months time) 4orse

  • 8/18/2019 Excel Data Entry Tips and Techniques

    16/87

    still what i( someone else needs to work on your code and has to

    8ure out how it worksR

     $his "ae discusses comments code indentation and line 'reaks all

    o( which will assist in makin your code clearer and easier tointer"ret)

    Comments

     $he sinle most im"ortant "ractice (or writin clear deci"hera'le

    code is to add (re:uent comments)

    Comments are lines in your code which act as notes to yoursel( or

    others to ex"lain what the code means or what it is doin)

    Comments are not executed durin the runnin o( the "roram so

    have no im"act on the result your macro) %0A considers any line

    that starts with an a"ostra"he +, to 'e a comment and the Excel

    %0A editor hihlihts these lines 'y colourin them in reen so you

    can see at a lance that they are comments and will not 'e

    executed)

    /ee the exam"le 'elow which shows comments used to clari(y the

    details o( a sim"le /u' "rocedure=

    /u' "rocedure to search cells A1-A1 o( the current active

    sheet and 8nd the cell containin the su""lied strin

    /u' 5indD/trin+s5ind$ext As /trin,

    !im i As 2nteer  2nteer used in 5or loo"

    !im i&ow;um'er As 2nteer  2nteer to store result in

    i&ow;um'er

  • 8/18/2019 Excel Data Entry Tips and Techniques

    17/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    18/87

    2t is easy to et la6y a'out addin comments to your code 'ut it

    really is worth makin the eXort) $he minutes invested in ensurin

    your code is well documented could save you hours o( (rustration in

    the lon term)

    Code /ndentation

    Another way o( assistin in makin your code reada'le is 'y addin

    indentations to it) *ou can see how in the exam"le a'ove the code

    has 'een indented within the main /u' "rocedure and then indented

    (urther inside individual 'locks o( code) $hese indented sectionsena'le you to easily see where each 'lock o( code starts and ends)

    (ine #reaks

     *our code can also 'e made more reada'le 'y insertin line 'reaks

    in the middle o( lon lines o( code) 2n %0A i( you want to s"lit a line

    u" you need to add a s"ace (ollowed 'y an underscore + D, ust

    'e(ore the line 'reak) $his tells the %0A com"iler that the current

    line o( code continues on the (ollowin line)

     $he (ollowin exam"le shows how sim"le line 'reaks can 'e used to

    make lon lines o( code much easier to read and understand)

    Consider the (ollowin 2( statement=

    2(  +index 1 And sColor1 ZredZ, .r +index G And sColor1

    Z'lueZ, .r +index ? And sColor1 ZreenZ, .r +index

    K And sColor1 Z'rownZ, $hen

  • 8/18/2019 Excel Data Entry Tips and Techniques

    19/87

    0y addin line 'reaks the same 2( statement can 'e "resented as

    (ollows=

    2(  +index 1 And sColor1 ZredZ, .r D 

      +index G And sColor1 Z'lueZ, .r D   +index ? And sColor1 ZreenZ, .r D 

      +index K And sColor1 Z'rownZ, $hen

    4hen the 2( statement is 'roken u" over (our lines you can see the

    diXerent conditions within the 2( statement much more clearly) $his

    exam"le illustrates how "resentin your code in a clear way canhel" you to "roduce reada'le code which will hel" you to avoid

    introducin 'us or errors)

    Excel H# Tutorial Part B - H# Haria0les "

    Constants

    2n %0A as in any "rorammin lanuae %aria'les and Constants

    are names that re"resent values) As suested 'y the names the

    values o( %aria'les can chane while Constants enerally have 8xed

    values)

    5or exam"le you miht use the constant ZPiZ to store the value

    ?)1K1L>GL))) $he value o( ZPiZ will not chane throuhout the

    course o( your "roram 'ut it is use(ul to store this value in a

    Constant (or ease o( use)

    Also we miht use a varia'le named Zs%A$D&ateZ to store the %A$

    &ate to 'e "aid on "urchased oods) $he value o( s%A$D&ate may

    vary de"endin on the ty"e o( oods)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    20/87

    Data Types

    All varia'les and constants have a data ty"e) $he (ollowin ta'le

    shows the %0A data ty"es alon with a descri"tion o( each ty"e and

    the rane o( "ossi'le values)

    Data Type

    0yte

    0oolean

    2nteer

  • 8/18/2019 Excel Data Entry Tips and Techniques

    21/87

    /trin

    %ariant

    5rom the a'ove ta'le it is clear that you can save on memory 'y

    usin s"eci8c data ty"es +e)) 2nteers rather than

  • 8/18/2019 Excel Data Entry Tips and Techniques

    22/87

    !im s%A$D&ate As /inle

    !im i As 2nteer

    Constants are declared in a similar way exce"t a constant shouldalways 'e assined a value when it is declared) Exam"les o( the

    declaration o( constants in %0A are=

    Const iMaxCount L

    Const iMax/core 1

    &easons to !eclare %aria'les

    Excel does not (orce you to declare varia'les) 0y de(ault all

    varia'les in Excel will have the %ariant ty"e and can 'e assined a

    num'er or text)

     $here(ore at any "oint durin your "roram you can use any

    varia'le name +even i( it has not 'een declared, and Excel will

    assin the %ariant ty"e to it) However this is not ood "rorammin

    "ractice (or the (ollowin reasons=

    1) Memory " Calculation Speed3 2( you do not declare a

    varia'le to have a data ty"e it will 'y de(ault have the %ariant

    ty"e) $his takes u" more memory than many o( the other data

    ty"es)

    4hile a (ew extra 'ytes "er varia'le miht not seem to 'e a lot o(

    memory it is not uncommon (or "rorammers to have thousandso( varia'les in their "rorams +es"ecially when you start to use

    arrays,) $here(ore the additional memory used to store %ariants

    instead o( 2nteers or /inles can add u" sini8cantly)

    %ariant data ty"es also take more time to "rocess than some o(

  • 8/18/2019 Excel Data Entry Tips and Techniques

    23/87

    the other data ty"es so i( you have thousands o( unnecessary

    %ariant data ty"es this can slow down your calculations)

    G) Preention o9 KTypoK #ugs3 2( you always declare your

    varia'les then you can use the %0A ."tion Ex"licit +see 'elow,to (orce you to declare varia'les)

     $his will "revent you (rom introducin 'us into your code 'y

    accidentally ty"in a varia'le name incorrectly) 5or exam"le you

    miht 'e usin a varia'le called Zs%A$D&ateZ and when assinin

    a value to this varia'le you miht accidentally ty"e Z%A$&ate

    )1QLZ) 5rom this "oint onwards you are ex"ectin your varia'le

    Zs%A$D&ateZ to have the value )1QL 'ut o( course it doesnt)However i( you were usin the %0A o"tion to (orce you to declare

    all varia'les 'e(ore usin them this error would 'e hihlihted 'y

    the %0A com"iler as the varia'le Z%A$&ateZ would not have 'een

    declared)

    ?) &ighlighting 4nexpected Data Halues3 2( you declare a

    varia'le to have a s"eci8c data ty"e and you attem"t to assin

    the wron ty"e o( data to it this will enerate an error in your

    "roram which i( not handled within your code can cause your

    "roramme to crash)

    4hile this may initially seem to 'e a ood reason not to declare

    varia'les it is actually use(ul to know as soon as "ossi'le i( your

    varia'le receives an unex"ected data ty"e) .therwise i( the

    "roram continues to run you could end u" with incorrect or

    unex"ected results at a later time when it is likely to 'e much

    more diNcult to detect the causes o( the errors)

    Also it is "ossi'le that the macro may com"lete with incorrect

    results and you may not notice the error at all- and continue to

    work with incorrect dataV

  • 8/18/2019 Excel Data Entry Tips and Techniques

    24/87

    2t is there(ore "re(era'le to detect the unex"ected data ty"e at an

    early stae and add code to handle this a""ro"riately)

     $here(ore it is recommended that you always declare all varia'les

    when "rorammin in %0A and use the ."tion Ex"licit +see 'elow,to ensure that all varia'les in the code are declared)

    ."tion Ex"licit

     $he o"tion Ex"licit (orces you to declare all varia'les that you use

    in your %0A code 'y hihlihtin any undeclared varia'les as errorsdurin com"ilation +'e(ore the code will run,) $o use this o"tion

    sim"ly ty"e the line

    ."tion Ex"licit

    at the very to" o( your %0A 8le)

    2( you want to always include the o"tion Ex"licit at the to" o( every

    new %0A module that you o"en u" this can 'e done automaticallyvia the &e:uire %aria'le !eclaration o"tion o( your %0A editor)

     $o do this=

    • 2n the %isual 0asic Editor select ToolsLptionsAAA

    • Ensure the Editor ta' is selected

    • Check the 'ox next to the o"tion :equire Haria0le

    Declaration and click .F)

    .nce the &e:uire %aria'le !eclaration o"tion is selected

    the ."tion Ex"licit will automatically 'e inserted at the to" o( all

    new modules)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    25/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    26/87

    ."tion Ex"licit

    5unction $otalDCost+, As

    !ou'le

    !im s%A$D&ate As

    /inle

      )

      )

      )

    End 5unction

    2( you declare Zs%A$D&ateZ at the start o( the

     $otalDCost (unction the sco"e o( this varia'le will

    'e the whole o( this (unction +i)e) Zs%A$D&ateZ

    will 'e reconised throuhout the $otalDCost

    (unction 'ut not outside o( this (unction,)

     $here(ore i( you attem"t to use Zs%A$D&ateZ in

    any other "rocedure the %0A com"iler will raise

    an error as the varia'le has not 'een declared

    outside o( the $otalDCost (unction +and the ."tion

    Ex"licit is in use,)

    2n the a'ove exam"le the module level varia'le has 'een declared

    usin the !im keyword) However it is "ossi'le that you may want

    to declare varia'les that can 'e shared with other modules) $his can

    'e s"eci8ed 'y usin the keyword Public in the declaration instead

    o( !im)

    ;ote that (or a module-level varia'le the !im keyword could also

    'e re"laced with the keyword Private to indicate that the sco"e o( 

    the varia'le is limited to the current module)

    Constants can also use the Pu'lic and Private keywords 'ut in this

    case the Pu'lic or Private keyword is used in addition to the

    Const keyword +not instead o(,)

     $he (ollowin exam"les show the Pu'lic and Private keywords

    a""lied to varia'les and constants=

  • 8/18/2019 Excel Data Entry Tips and Techniques

    27/87

    ."tion Ex"licit

    Pu'lic s%A$D&ate As

    /inle

    Pu'lic Const iMaxDCount

    L

    )

    )

    )

     $his exam"le shows the Pu'lic keyword used to

    declare the varia'le Zs%A$D&ateZ and the

    constant ZiMaxDCountZ) $he sco"e o( these two

    declarations is the whole o( the current "roect)

     $here(ore Zs%A$D&ateZ and ZiMaxDCountZ can 'e

    accessed (rom any "rocedure in any module in

    the "roect)

    ."tion Ex"licit

    Private s%A$D&ate As

    /inle

    Private

    Const iMaxDCount

    L

    )

    )

    )

     $his exam"le shows the Private keyword used

    to declare the varia'le Zs%A$D&ateZ and the

    constant ZiMaxDCountZ) $he sco"e o( these two

    declarations is the current module)

     $here(ore Zs%A$D&ateZ and ZiMaxDCountZ can 'e

    accessed (rom any "rocedure in the current

    module 'ut can not 'e accessed (rom

    "rocedures that reside in diXerent modules)

    Excel %0A $utorial Part ? - Excel %isual 0asicArrays

    Excel %isual 0asic arrays are structures which are used to store a set

    o( related varia'les o( the same ty"e) Each o( the entries in the

    array can 'e accessed 'y an index num'er)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    28/87

    5or exam"le i( you had G mem'ers o( a team and you wanted to

    store all the names (or use in your %0A code) *ou could declare G

    varia'les to hold the team mem'er names as (ollows=

    !im $eamDMem'er1 As /trin

    !im $eamDMem'erG As /trin

    AAA

    !im $eamDMem'erG As /trin

    Alternatively you could use the much sim"ler and more oranised

    method o( storin the $eam mem'ers in an array o( G /trin

    varia'les=

    !im $eamDMem'ers+1 $o G, As /trin

    .nce you have declared the array as a'ove each entry o( the array

    is "o"ulated as (ollows=

     $eamDMem'ers+1, Z^ohn /mithZ

    AAA

    A (urther advantae o( storin your data in an array rather than in

    individual varia'les arises i( you want to "er(orm the same action

    on every mem'er o( the list) 2( your team mem'er names were

  • 8/18/2019 Excel Data Entry Tips and Techniques

    29/87

    stored in G individual varia'les you would need G lines o( code to

    carry out a s"eci8c action on each name) However i( you have

    stored your names in an array you can use a sim"le loo" to carry

    out the action (or each entry in the array)

     $his is shown in the exam"le code 'elow which "rints out each

    name in the $eamDMem'ers array to a cell in Column A o( the

    current Excel 4orksheet=

    5or i 1 $o G

    Cells+i 1,)%alue $eamDMem'ers+i,

    ;ext i

    2t is clear (rom the a'ove that handlin an array o( G names is

    much less cum'ersome and more oranised than handlin G

    individual varia'les 'ut imaine i( you had 1 names to storeV

    And imaine you wanted to store /urnames se"arately (rom

    5orenamesV 2t is clear that it would soon 'ecome almost im"ossi'leto handle this amount o( data without the use o( Arrays in your %0A

    code)

    Multi-Dimensional Excel Hisual #asic rrays

     $he %isual 0asic Arrays discussed a'ove are one-dimensional in

    that they re(er to one list o( ;ames) However arrays can have

    multi"le dimensions) An array havin two dimensions acts as a rid

    o( values)

    5or exam"le imaine that you want to store daily sales 8ures (or

    the month o( ^anuary (or L diXerent teams) *ou would need a G-

  • 8/18/2019 Excel Data Entry Tips and Techniques

    30/87

    dimensional array consistin o( L sets o( 8ures over ?1 days) *ou

    would then declare the array as (ollows=

    !im ^anD/alesD5iures+1 $o ?1 1 $o L, As Currency

    2n order to access the entries in the array ^anD/alesD5iures you

    need to use two indices re(erin to the day o( the month and the

    team num'er) 5or exam"le the sales 8ures (or $eamG on ^anuary

    1Lth would 'e re(erenced as=

     ^anD/alesD5iures+1L G,

     *ou can declare arrays with ? or more dimensions in the same way -

    i)e) 'y addin (urther dimensions into the declaration and usin a

    (urther index to re(erence the array entries)

    Declaring Excel Hisual #asic rrays

     $he a'ove sections have already iven some exam"les o( %isual

    0asic Array declarations 'ut it is worth discussin this (urther) As

    seen a'ove a one-dimensional array can 'e declared as (ollows=

    !im $eamDMem'ers+1 $o G, As /trin

     $his declaration tells the %0A com"iler that the array

    $eamDMem'ers has G varia'les which are re(erenced 'y indices 1

  • 8/18/2019 Excel Data Entry Tips and Techniques

    31/87

    to G) However we could also decide to num'er our array varia'les

    (rom to 1> in which case the declaration would 'e=

    !im $eamDMem'ers+ $o 1>, As /trin

    2n (act the de(ault (orm o( array indexin is to start at so i( you

    omit the start index (rom the declaration and sim"ly declare the

    array as=

    !im $eamDMem'ers+1>, As /trin

     $hen the %0A com"iler will understand this to 'e an array o( G

    varia'les which are indexed (rom to 1>)

     $he same rules are a""lied to declarations o( multi-dimensional

    %isual 0asic arrays) As shown in the "revious exam"le a two-

    dimensional array is declared 'y se"aratin the dimension indices'y a comma=

    !im ^anD/alesD5iures+1 $o ?1 1 $o L, As Currency

    However i( we omit the start indices (rom 'oth dimensions as

    (ollows=

    !im ^anD/alesD5iures+?1 L, As Currency

  • 8/18/2019 Excel Data Entry Tips and Techniques

    32/87

    this is understood to 'e a two-dimensional array in which the 8rst

    dimension has ?G entries indexed (rom to ?1 and the second

    dimension has entries indexed (rom to L)

    Dynamic rrays

    2n the a'ove exam"les the arrays all have 8xed dimensions)

    However in many cases we dont know how 'i an array is oin to

    'e 'e(ore run time) 4e could solve this 'e declarin a hue array in

    an attem"t to cover the maximum "ossi'le si6e needed 'ut this

    would use u" an unnecessarily lare amount o( memory and couldslow down your "roram) A 'etter o"tion would 'e to use

    a !ynamic array which is an array that can 'e si6ed and re-si6ed as

    many times as you like durin the execution o( a macro)

    A dynamic array is declared with em"ty "arentheses as (ollows=

    !im $eamDMem'ers+, As /trin

     *ou then need to declare the dimension o( the array durin the

    execution o( the code usin the &e!im statement=

    &e!im $eamDMem'ers+1 $o G,

    2( durin the execution o( the code you need to extend the si6e o( 

    your array you can use&e!im aain=

    2(  $eamD/i6e G $hen

  • 8/18/2019 Excel Data Entry Tips and Techniques

    33/87

    &e!im $eamDMem'ers+1 $o $eamD/i6e,

    End 2( 

    2t should 'e noted that resi6in a dynamic array in this way will

    result in the loss o( all the values that had "reviously 'een stored in

    the array) 2( you want to avoid this loss and kee" the values that

    have "reviously 'een assined to the array you need to use the

    ZPreserveZ keyword as shown 'elow=

    2(  $eamD/i6e G $hen

    &e!im Preserve $eamDMem'ers+1 $o $eamD/i6e,

    End 2( 

     $he disadvantae o( usin the ZPreserveZ keyword when resi6in

    %isual 0asic Arrays is that you can only chane the u""er 'ound o( an array not the lower 'ound) Also i( you have a multi-dimensional

    array the use o( the ZPreserveZ keyword limits you to chanin only

    the last dimension o( the array)

    Excel %0A $utorial Part K - %0A 5unction \ /u'Procedures

    4ser-DeIned H# Function " Su0 Procedures

    2n Excel %isual 0asic a set o( commands to "er(orm a s"eci8c task is

    "laced into a "rocedure which can 'e a 5unction"rocedure or

    a /u' "rocedure +also known as (unctions and su'routines,)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    34/87

     $he main diXerence 'etween a %0A 5unction "rocedure and a /u'

    "rocedure is that a 5unction "rocedure returns a result whereas a

    /u' "rocedure does not)

     $here(ore i( you wish to "er(orm a task that returns a result +e))summin o( a rou" o( num'ers, you will enerally use a 5unction

    "rocedure 'ut i( you ust need a set o( actions to 'e carried out +e))

    (ormattin a set o( cells, you miht choose to use a /u' "rocedure)

    rguments

    %0A "rocedures can 'e "assed data viaarguments which are

    declared in the "rocedure de8nition) 5or exam"le you miht have a

    %0A /u' "rocedure that adds an 2nteer to every cell in the current

    rane) *ou could su""ly the value o( the inteer to the /u' via an

    arument as (ollows=

    /u' Add$oCells+i As 2nteer,

      A

      A

      A

    End /u'

    ;ote that it is not essential (or %0A 5unctions or /u's to havearuments) /ome "rocedures may not re:uire them)

    ."tional Aruments

  • 8/18/2019 Excel Data Entry Tips and Techniques

    35/87

     *ou can also de8ne %0A "rocedures to have ."tional aruments)

     $hese are aruments that the user can su""ly i( they want 'ut i( 

    they are omitted the "rocedure will assin a de(ault value to them)

     $o return to the exam"le a'ove i( we wanted to make the su""liedinteer arument o"tional this would 'e declared as (ollows=

    /u' Add$oCells+."tional i As 2nteer  ,

    2n this case the su""lied inteer i has a de(ault value o( )

     *ou can use multi"le ."tional aruments in a %0A "rocedure aslon the ."tional aruments are all "ositioned at the end o( the

    arument list)

    Passin Aruments 0y %alue and 0y &e(erence

    4hen aruments are "assed to %0A "rocedures they can 'e "assedin two ways=

    • #yHal - $he arument is "assed 'y %alue) $his means that ust

    the value +i)e) a co"y o( the arument, is "assed to the "rocedure

    and there(ore any chanes that are made to the arument inside

    the "rocedure will 'e lost when the "rocedure is exited)

    • #y:e9  - $he arument is "assed 'y &e(erence) $his means that

    the address o( the arument is "assed to the "rocedure) Any

    chanes that are made to the arument inside the "rocedure will

    'e remem'ered when the "rocedure is exited)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    36/87

     *ou can s"eci(y whether an arument is "assed to a %0A "rocedure

    'y value or 'y re(erence 'y usin the 0y%al or the 0y&e(  keyword

    when de8nin the "rocedure) $his is shown 'elow=

    /u' Add$oCells+0y%a

    l i As 2nteer,

      A

      A

      A

    End /u'

    2n this case the inteer i is "assed 'y %alue) Any

    chanes that are made to i will 'e lost when you exit

    the /u' "rocedure)

    /u' Add$oCells+0y&e

    (  i As 2nteer,

      A

      A

      A

    End /u'

    2n this case the inteer i is "assed 'y &e(erence)

    4hen you exit the /u' any chanes that have 'een

    made to i will 'e remem'ered 'y the varia'le that

    was "assed into the /u' "rocedure)

    2t should 'e noted that 'y de(ault in %0A aruments are "assed 'y

    &e(erence) $here(ore i( you do not use the 0y%al or

    the 0y&e(  keyword the aruments will 'e "assed 'y &e(erence)

    0e(ore discussin (urther "ro"erties o( %0A 5unction and /u'

    "rocedures it is use(ul to look at the two ty"es o( "rocedure

    individually) $he (ollowin two sections "rovide a 'rie( discussion o( 

    %0A 5unction and %0A /u' "rocedures alon with sim"le exam"les)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    37/87

    H# Function Procedures

     $he %0A editor reconises a 5unction "rocedure 'ecause the

    commands are "ositioned 'etween the (ollowin start and end

    commands=

    5unction

    A

    A

    A

    End 5unction

    As "reviously mentioned %0A (unction "rocedures +unlike su'

    "rocedures, return a value) $he return values have the (ollowin

    rules=

    •  $he data ty"e o( the returned value must 'e declared in the5unction header)

    •  $he value to 'e returned must 'e assined to a varia'le havin

    the same name as the 5unction) $his varia'le does not need to 'e

    declared as it already exists as a "art o( the (unction)

     $his is illustrated in the (ollowin exam"le)

    %0A 5unction Procedure Exam"le= Per(orm a Mathematical ."eration on ?

    ;um'ers

  • 8/18/2019 Excel Data Entry Tips and Techniques

    38/87

     $he (ollowin code shows an exam"le o( a sim"le %0A 5unction

    "rocedure that receives three aruments each o( which are

    !ou'les +dou'le "recision Ooatin "oint num'ers,) $he 5unction

    returns a (urther !ou'le which is the sum o( the 8rst two

    aruments minus the third arument=

    5unction /umMinus+d;um1 As !ou'le d;umG As !ou'le d;um? As !ou'le, As

    !ou'le

    /umMinus d;um1 S d;umG - d;um?

    End 5unction

     $he a'ove very sim"le %0A 5unction "rocedure illustrates the way in

    which data aruments are su""lied to a "rocedure) 2t is also seen

    that the 5unction "rocedure return ty"e is de8ned as 'ein a

    !ou'le +i)e) 'y the term ZAs !ou'leZ which is included a(ter the

    5unction aruments,)

     $he a'ove exam"le also shows how the 5unction "rocedure result is

    stored in a varia'le that has the same name as the 5unction)

    Callin %0A 5unction Procedures

    2( the a'ove sim"le 5unction "rocedure is ty"ed into a Module in the

    %isual 0asic Editor it will then 'e availa'le to 'e called (rom other

    %0A "rocedures or to 'e used in the worksheets o( your Excel

    work'ook)

    Callin a %0A 5unction Procedure 5rom Another Procedure

  • 8/18/2019 Excel Data Entry Tips and Techniques

    39/87

     *ou can call a 5unction "rocedure (rom within another %0A

    "rocedure 'y sim"ly assinin the 5unction to a varia'le) $he

    (ollowin exam"le shows a call to the sim"le /umMinus (unction that

    was de8ned a'ove=

    /u' main+,

    !im total as !ou'le

    total /umMinus+L K ?,

    End /u'

    Callin a %0A 5unction Procedure 5rom A 4orksheet

     *ou can call %0A 5unction "rocedures (rom an Excel 4orksheet in

    the same way as you can call any o( the 'uilt-in Excel (unctions)

     $here(ore you could call the /umMinus 5unction "rocedure 'y

    ty"in the (ollowin into any cell o( your worksheet=

    /umMinus+1 L G,

    H# Su0 Procedures

     $he %0A editor reconises a /u' "rocedure 'ecause the commandsare "ositioned 'etween the (ollowin start and end commands=

    /u'

  • 8/18/2019 Excel Data Entry Tips and Techniques

    40/87

    A

    A

    A

    End /u'

    %0A /u' Procedure Exam"le 1= Center and A""ly 5ont /i6e to a /elected

    &ane o( Cells

     $he (ollowin code shows an exam"le o( a sim"le %0A /u'

    "rocedure that a""lies (ormattin to the current selected cell rane)

     $he cells are (ormatted to 'e alined centrally +'oth hori6ontally and

    vertically, and to have a user-su""lied (ont si6e=

    /u' 5ormatDCenteredDAndD/i6ed+."tional i5ont/i6e As 2nteer 1,

    /election)Hori6ontalAlinment xlCenter

    /election)%erticalAlinment xlCenter

    /election)5ont)/i6e i5ont/i6e

    End /u'

     $he a'ove exam"le illustrates how /u' "rocedures "er(orm actions

    'ut do not return values)

     $his exam"le also includes the ."tional arument i5ont/i6e) 2( 

    i5ont/i6e is not su""lied to the /u' then the de(ault (ont si6e o( 1

  • 8/18/2019 Excel Data Entry Tips and Techniques

    41/87

    is used) However i( i5ont/i6e is su""lied to the /u' then the current

    rane is set to have the user-su""lied (ont si6e)

    %0A /u' Procedure Exam"le G= Center and A""ly 0old 5ont to a /elected

    &ane o( Cells

     $he (ollowin code is similar to exam"le 1 'ut instead o( su""lyin

    a (ont si6e to the selected rane the cells are set to have a 'old

    (ont) $his exam"le has 'een included to show a /u' "rocedure that

    does not receive any aruments =

    /u' 5ormatDCenteredDAndD0old+,

    /election)Hori6ontalAlinment xlCenter

    /election)%erticalAlinment xlCenter

    /election)5ont)0old  $rue

    End /u'

    Callin Excel %0A /u' Procedures

    Callin a %0A /u' Procedure 5rom Another Procedure

     *ou can call a %0A /u' "rocedure (rom another %0A "rocedure 'yty"in the Call keyword (ollowed 'y the /u' name and then the /u'

    "rocedure aruments enclosed in 'rackets) $his is shown in the

    exam"le 'elow=

  • 8/18/2019 Excel Data Entry Tips and Techniques

    42/87

    /u' main+,

    Call 5ormatDCenteredDAndD/i6ed+ G ,

    End /u'

    2( the 5ormatDCenteredDAndD/i6ed /u' "rocedure had more than

    one arument these would 'e se"arated 'y commas) E))

    /u' main+,

    Call 5ormatDCenteredDAndD/i6ed+ ar1 arG ))) ,

    End /u'

    Callin a %0A /u' Procedure 5rom A 4orksheet

    /u' "rocedures cannot 'e ty"ed directly into a 4orksheet in Excel

    in the same way that %0A 5unction "rocedures can 'ecause /u'

    "rocedures dont return a value) However "rovided they have no

    aruments +and are Pu'lic - see 'elow, Excel %0A /u'

    "rocedures are availa'le to the user o( a s"readsheet) $here(ore i( 

    the a'ove sim"le /u' "rocedures are ty"ed into a Module in the

    %isual 0asic Editor the 5ormatDCenteredDAndD0old /u' "rocedurewill 'e availa'le to 'e used in the worksheets o( your Excel

    work'ook 'ut the 5ormatDCenteredDAndD/i6ed will not +as it has an

    arument,)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    43/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    44/87

    Scope o9 H# Procedures

    2n Part G o( this tutorial we discussed the sco"e o( varia'les and

    constants and the role o( thePu'lic and Private keywords) $hese

    keywords have the same meanin when a""lied to %0A "rocedures =

    Pu'lic

    /u' Add$oCells+i

    As 2nteer,

      A

      A

      A

    End /u'

    2( a "rocedure declaration is "receded 'y the

    keyword Pu'lic this makes the "rocedure accessi'le to

    all other modules in the %0A Proect)

    Private

    /u' Add$oCells+i

    As 2nteer,

      A

      A  A

    End /u'

    2( a "rocedure declaration is "receded 'y the

    keyword Private this makes the "rocedure only availa'le

    to the current module) 2t cannot 'e accessed (rom any

    other modules or (rom the Excel work'ook)

    2( no keyword is inserted at the start o( a %0A 5unction or /u'

    declaration then the de(ault settin is (or the "rocedure to

    'e Pu'lic +i)e) to 'e accessi'le (rom anywhere in the %0A Proect,)

     $his is diXerent to varia'le declarations which are Private 'y

    de(ault)

    http://www.excelfunctions.net/VBA-Variables-And-Constants.htmlhttp://www.excelfunctions.net/VBA-Variables-And-Constants.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    45/87

    Early Exit From H# Function " Su0 Procedures

    2( you want to exit a %0A 5unction or /u' "rocedure 'e(ore it has run

    to the end you can do this usin the Exit 5unction or the Exit

    /u' command) $his is illustrated 'elow in a sim"le 5unction"rocedure that ex"ects to receive a "ositive value to work with) 2( 

    the value received is not "ositive the (unction cannot continue so it

    hihlihts the error to the user and exits the "rocedure immediately=

    5unction %A$DAmount+s%A$D&ate As /inle, As /inle

    %A$DAmount

    2(  s%A$D&ate `  $hen

      Ms0ox ZEx"ected a Positive value o( s%A$D&ate 'ut &eceived Z \ s%A$D&ate

      Exit 5unction

    End 2( 

      A

      A

      A

    End 5unction

    ;ote that 'e(ore exitin the %A$DAmount 5unction "rocedure the

    a'ove code uses the 'uilt-in %0A Ms0ox (unction to dis"lay a 'ox

    containin a warnin messae to the user)

    Excel %0A $utorial Part L= %0A Conditional/tatements

     $he main Excel %0A Conditional /tatements are the 2( )))

     $henstatement and the /elect Casestatement) 0oth o( these

    http://www.excelfunctions.net/vba-msgbox-function.htmlhttp://www.excelfunctions.net/vba-msgbox-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    46/87

    evaluate one or more conditions and de"endin on the result

    execute s"eci8c actions)

     $he two Conditional /tatement ty"es are discussed individually

    'elow)

    The Hisual #asic /9 AAA ThenStatement

     $he 2( ))) $hen statement tests a condition and i( it evaluates to $rue

    carries out a set o( actions) 2( the condition evaluates to 5alse a

    diXerent set o( actions are carried out)

     $he (ormat o( the 2( ))) $hen statement is=

    2(  Condition1  $hen

     Actions if Condition1 ealuates to True

    Else2(  Condition!  $hen

     Actions if Condition! ealuates to True

    Else

     Actions if none of the preious conditions ealuate to True

    End 2( 

    2n the a'ove 2( statement the Else2(  and the Else "arts o( the

    conditional statement can 'e le(t out i( desired)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    47/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    48/87

    The Hisual #asic Select Case Statement

     $he /elect Case statement is similar to the 2( ))) $hen statement in

    that it tests an ex"ression and carries out diXerent actions

    de"endin on the value o( the ex"ression)

     $he (ormat o( the /elect Case statement is=

    /elect Case "#pression

    Case Value1

     Actions if "#pression matches Value1

    Case Value!

     Actions if "#pression matches Value!

    )

    )

    )

    Case Else

     Actions if e#pression does not match any of listed cases

    End /elect

    2n the a'ove code 'lock the Case Else "art o( the conditional

    statement is o"tional)

    2n the exam"le 'elow the /elect Case statement is used to color the

    current active cell de"endin on the value o( the cell contents)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    49/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    50/87

    Case 1 $his is an exam"le o( the 'asic test o( whether

    your ex"ression evaluates to a s"eci8c value

    Case Else  $his is an exam"le o( the Else condition which

    is executed i( your ex"ression hasnt matched

    any o( the "revious cases

    ;ote that as soon as one case in the /elect Case statement is

    matched and the corres"ondin actions executed the whole /elect

    Case statement is exited) $here(ore you will never et entry into

    more than one o( the listed cases)

    Excel %0A $utorial Part - %0A

  • 8/18/2019 Excel Data Entry Tips and Techniques

    51/87

    The Hisual #asic For (oop

     $he %isual 0asic 5or loo" takes on two se"arate (orms) $hese are

    the 5or ))) ;ext loo" and the 5or Each loo")

     $he 5or ))) ;ext

  • 8/18/2019 Excel Data Entry Tips and Techniques

    52/87

    ;ext d

    2n the a'ove 5or loo" 'ecause the ste" si6e is s"eci8ed as )1 the

    value o( the varia'le d is set to the values ) )1 )G )? ))) >)>

    1) (or each execution o( the %0A code inside the loo")

     *ou can also use neative ste" si6es in the %0A 5or loo" as is

    illustrated 'elow=

    5or i 1 $o 1 /te" -1

      iArray+i, i

    ;ext i

    2n this exam"le the ste" si6e is s"eci8ed as -1 and so the loo" sets

    the varia'le i to have the values 1 > _ ))) 1)

     $he 5or Each

  • 8/18/2019 Excel Data Entry Tips and Techniques

    53/87

    !im w/heet As 4orksheet

    5or Each w/heet in 4orksheets

      Ms0ox Z5ound 4orksheet= Z \ w/heet);ame;ext w/heet

     $he Exit 5or /tatement

    2( you want to exit a 5or

  • 8/18/2019 Excel Data Entry Tips and Techniques

    54/87

    The Hisual #asic Do Nhile (oop

     $he !o 4hile loo" re"eatedly executes a section o( code while a

    s"eci8ed condition continues to evaluate to $rue) $his is shown in

    the (ollowin /u' "rocedure where a !o 4hile loo" is used to "rint

    out all values o( the 5i'onacci /e:uence until the values exceed

    1=

    /u' "rocedure to list the 5i'onacci series (or all values 'elow 1/u' 5i'onacci+,

    !im i As 2nteer  counter (or the "osition in the series

    !im i5i' As 2nteer  stores the current value in the series

    !im i5i'D;ext As 2nteer  stores the next value in the series

    !im i/te" As 2nteer  stores the next ste" si6e

    2nitialise the varia'les i and i5i'D;ext

    i 1i5i'D;ext

    !o 4hile loo" to 'e executed as lon as the value o( the

    current 5i'onacci num'er exceeds 1

    !o 4hile i5i'D;ext ` 1

    2(  i 1 $hen

    /"ecial case (or the 8rst entry o( the series

    i/te" 1

    i5i'

    Else

    /tore the next ste" si6e 'e(ore overwritin the

  • 8/18/2019 Excel Data Entry Tips and Techniques

    55/87

    current entry o( the series

    i/te" i5i'

    i5i' i5i'D;ext

    End 2( 

    Print the current 5i'onacci value to column A o( the

    current 4orksheet

    Cells+i 1,)%alue i5i'

    Calculate the next value in the series and increment

    the "osition marker 'y 1

    i5i'D;ext i5i' S i/te"i i S 1

  • 8/18/2019 Excel Data Entry Tips and Techniques

    56/87

    !o

    )

    ))

  • 8/18/2019 Excel Data Entry Tips and Techniques

    57/87

    2n the a'ove exam"le since the condition 2sEm"ty+Cells+i&ow 1,, is

    at the start o( the !o #ntilloo" the loo" will only 'e entered i( the

    8rst cell encountered is non-'lank)

    However as illustrated in the !o 4hile loo" you may on someoccasions want to enter the loo" at least once reardless o( the

    initial condition) 2n this case the condition can 'e "laced at the end

    o( the loo" as (ollows=

    !o

    )

    )

    )

  • 8/18/2019 Excel Data Entry Tips and Techniques

    58/87

     $he main Mathematical %0A o"erators are listed in the ta'le 'elow)

     $he "recedences that are listed alonside the o"erators are the

    de(aults which are a""lied in the a'sence o( 'rackets) However the

    order in which the %0A o"erators are a""lied can 'e controlled 'yaddin 'rackets to an ex"ression=

    perator

    9

    7

    b

    Mod

    S

    -

    /trin ."erators

     $he concatenate o"erator \ can 'e used to oin toether strins=

  • 8/18/2019 Excel Data Entry Tips and Techniques

    59/87

    perator

    \

    %0A Com"arison ."erators

    Com"arison o"erators com"are two num'ers or strins and return a

    loical +$rue or 5alse, result) $he main Excel %0A com"arison

    o"erators are listed in the ta'le 'elow=

  • 8/18/2019 Excel Data Entry Tips and Techniques

    60/87

    %0A

  • 8/18/2019 Excel Data Entry Tips and Techniques

    61/87

    0s&eturns the a'solute value o( a su""lied num'er)

    e)) 0s5-B*6 returns the value B*

    0s5B*6 returns the value B*)

    Chr&eturns the character associated with a su""lied

    character code)

    e)) Chr5O@6 returns the character ZaZ)

    Chr5!*6 returns a line (eed

    Date &eturns the current system date)

    Dated

    d Adds a s"eci8ed time "eriod to a su""lied date)

     $he (unction has the (ormat Datedd5/nteral1

    8um0er1 Date6

    where the 2nterval arument s"eci8es the ty"e o( 

    time interval to 'e added to the su""lied date)

     $his can takes on one o( the (ollowin values=

    /nteral :epresents

    yyyy year

    : :uarter

    http://www.excelfunctions.net/vba-abs-function.htmlhttp://www.excelfunctions.net/vba-chr-function.htmlhttp://www.excelfunctions.net/VBA-Date-Function.htmlhttp://www.excelfunctions.net/vba-dateadd-function.htmlhttp://www.excelfunctions.net/vba-dateadd-function.htmlhttp://www.excelfunctions.net/vba-abs-function.htmlhttp://www.excelfunctions.net/vba-chr-function.htmlhttp://www.excelfunctions.net/VBA-Date-Function.htmlhttp://www.excelfunctions.net/vba-dateadd-function.htmlhttp://www.excelfunctions.net/vba-dateadd-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    62/87

    m month

    y day o( the year

    d day

    w weekday

    ww week

    h hour

    n minute

    s second

    e))

     

    Datedd5d1 B1 *!>*!>B*!6 adds ?G days to th

    date Z1717G1LZ and so returns the date

    Z*B>*B>B*!

    Z)

    Datedd5''1 Q1 *!>*!>B*!6 adds __ weeks to

    the date Z1717G1LZ and so returns the date

    Z*O>*O>B*!Z)

    DateDi

    Calculates the num'er o( s"eci8ed intervals 'etween

    now and a su""lied date

    e))

     

    DateDiR5d1 *!>*!>B*!1 *B>*B>B*!6calculat

    the num'er o( days 'etween the dates Z1717G1LZ

    http://www.excelfunctions.net/vba-datediff-function.htmlhttp://www.excelfunctions.net/vba-datediff-function.htmlhttp://www.excelfunctions.net/vba-datediff-function.htmlhttp://www.excelfunctions.net/vba-datediff-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    63/87

    and ZG7G7G1LZ and so returns the value B)

    DateDiR5'1 *!>*!>B*!1

    *>*>B*!6calculates the num'er o( weeks 'etwee

    the dates Z1717G1LZ and Z?7?7G1Z and so

    returns the value !)

    Day &eturns the day o( the month (or a su""lied date

    e)) Day5*!>BO>B*!6 returns the value BO)

    &our &eturns the Hour "ortion o( a su""lied time

    e)) &our5BB33**6 returns the value BB)

    /nStr

    2( su""lied with an inteer n and G strins the 2n/tr(unction returns the "osition o( the second strin

    within the 8rst strin startin the search (rom the

    nth "osition in the 8rst strin)

    E))

     

    /nStr5!1 &ere is the search'ord1

    search'ord6 returns the value !)

    /nStr5!1 &ere is the search'ord 0ut here is

    another search'ord1 search'ord6 returns the

    value )

    http://www.excelfunctions.net/VBA-Day-Function.htmlhttp://www.excelfunctions.net/VBA-Hour-Function.htmlhttp://www.excelfunctions.net/vba-instr-function.htmlhttp://www.excelfunctions.net/VBA-Day-Function.htmlhttp://www.excelfunctions.net/VBA-Hour-Function.htmlhttp://www.excelfunctions.net/vba-instr-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    64/87

    /nt&eturns the inteer "art o( a numeric value)

    e)) /nt5A@O6 returns the value )

    /sDate&eturns $rue i( a su""lied value is a date and 5alse

    otherwise)

    e)) /sDate5*!>*!>B*!6  returns the valueTrue

    /sDate5!**6 returns the value False)

    /sError &eturns $rue i( a su""lied value is an error and 5alse

    otherwise)

    /sMissi

    ng

    2( su""lied with the name o( an o"tional arument to

    a (unction the 2sMissin (unction returns $rue i( no

    arument value was "assed to the (unction)

    /s8um

    eric

    &eturns $rue i( a su""lied value can 'e evaluated as a

    num'er or 5alse otherwise)

    (e9t&eturns a s"eci8ed num'er o( characters (rom the

    start o( a su""lied strin)

     $he (ormat o( the (unction is (e9t5String1 (ength6

    where String is the oriinal text strin and (ength is

    the num'er o( characters to 'e returned (rom the

    http://www.excelfunctions.net/vba-int-function.htmlhttp://www.excelfunctions.net/vba-isdate-function.htmlhttp://www.excelfunctions.net/vba-iserror-function.htmlhttp://www.excelfunctions.net/vba-ismissing-function.htmlhttp://www.excelfunctions.net/vba-ismissing-function.htmlhttp://www.excelfunctions.net/vba-isnumeric-function.htmlhttp://www.excelfunctions.net/vba-isnumeric-function.htmlhttp://www.excelfunctions.net/vba-left-function.htmlhttp://www.excelfunctions.net/vba-int-function.htmlhttp://www.excelfunctions.net/vba-isdate-function.htmlhttp://www.excelfunctions.net/vba-iserror-function.htmlhttp://www.excelfunctions.net/vba-ismissing-function.htmlhttp://www.excelfunctions.net/vba-ismissing-function.htmlhttp://www.excelfunctions.net/vba-isnumeric-function.htmlhttp://www.excelfunctions.net/vba-isnumeric-function.htmlhttp://www.excelfunctions.net/vba-left-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    65/87

    start o( the su""lied /trin)

    E)) (e9t5a0cde9ghi?klm1 6 returns the strin Za0cdZ

    (e9t5a0cde9ghi?klm1 !6 returns the strin ZaZ)

    (en &eturns the lenth o( a su""lied text strin)

    E)) (en5a0cde9g6 returns the value @)

    Month&eturns the month num'er (or a su""lied date)

    e)) Month5*!>BO>B*!6 returns the value !)

    Mid&eturns a s"eci8ed num'er o( characters (rom the

    middle o( a su""lied strin)

     $he (ormat o( the (unction is Mid5String1 Start1

    (ength6

    where String  is the oriinal text strin Start is the

    start "osition o( the section o( /trin that is to 'e

    returned and (ength is the lenth o( the returned

    strin)

    E)) Mid5a0cde9ghi?klm1 1 6 returns the strin

    Zde9ghZ

    http://www.excelfunctions.net/vba-len-function.htmlhttp://www.excelfunctions.net/vba-month-function.htmlhttp://www.excelfunctions.net/vba-mid-function.htmlhttp://www.excelfunctions.net/vba-len-function.htmlhttp://www.excelfunctions.net/vba-month-function.htmlhttp://www.excelfunctions.net/vba-mid-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    66/87

    http://www.excelfunctions.net/VBA-Minute-Function.htmlhttp://www.excelfunctions.net/vba-now-function.htmlhttp://www.excelfunctions.net/vba-right-function.htmlhttp://www.excelfunctions.net/VBA-Second-Function.htmlhttp://www.excelfunctions.net/vba-sqr-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    67/87

    E)) Sqr56 returns the value B

    Sqr5!6 returns the value )

    Time &eturns the current system time)

    4#oun

    d

    &eturns the u""er su'scri"t o( a su""lied array)

     ,ear &eturns the year o( a su""lied date)

    E))  ,ear5*!>BO>B*!6 returns the value B*!

    Excel %0A $utorial Part _ - Excel .'ects

     $he term Excel 0?ects+collectively re(erred to as the Excel0?ect Model, re(ers to the entities that make u" an Excel

    work'ook such as 4orksheets &ows Columns Cell &anes and the

    Excel 4ork'ook itsel() Each o'ect in Excel has a num'er

    o( Properties which are stored as a "art o( that o'ect)

    5or exam"le an Excel 4orksheets "ro"erties include the

    4orksheets ;ame Protection %isi'le Pro"erty /croll Area etc)

     $here(ore i( durin the execution o( a macro we wanted to hide an

    Excel worksheet we could do this 'y accessin the 4orksheet

    o'ect and alterin the %isi'le "ro"erty)

    Excel %0A has a s"ecial ty"e o( o'ect called a Collection) As the

    name suests a Collection re(ers to a rou" +or collection, o( Excel

    http://www.excelfunctions.net/VBA-Time-Function.htmlhttp://www.excelfunctions.net/vba-ubound-function.htmlhttp://www.excelfunctions.net/vba-ubound-function.htmlhttp://www.excelfunctions.net/vba-year-function.htmlhttp://www.excelfunctions.net/VBA-Time-Function.htmlhttp://www.excelfunctions.net/vba-ubound-function.htmlhttp://www.excelfunctions.net/vba-ubound-function.htmlhttp://www.excelfunctions.net/vba-year-function.html

  • 8/18/2019 Excel Data Entry Tips and Techniques

    68/87

    o'ects) 5or exam"le the :o's collection is an o'ect containin all

    the rows o( a 4orksheet)

     $he main Excel .'ects can all 'e accessed +directly or indirectly,

    (rom the 4ork'ooks o'ect which is a collection o( all the currentlyo"en 4ork'ooks) Each 4ork'ook o'ect contains the /heets o'ect

    +consistin o( all the 4orksheets and Chart sheets in the 4ork'ook,

    and in turn each 4orksheet o'ect contains a &ows o'ect

    +consistin o( all &ows in the 4orksheet, and a Columns o'ect

    +consistin o( all Columns in the 4orksheet, etc)

     $he (ollowin ta'le lists some o( the more commonly used Excel

    o'ects) 5or a more com"rehensive list o( Excel %0A o'ects seethe Microso(t .Nce !evelo"er we'site)

    0?ect

    Type

    Description

    A""licati

    on

     $he current Excel A""lication)

    4ork'oo

    ks

     $he 4ork'ooks o'ect is a collection o( all o( the

    o"en Excel 4ork'ooks in the current Excel

    A""lication)

    An individual 4ork'ook can 'e extracted (rom

    the 4ork'ooks o'ect 'y usin an individual

    4ork'ook index num'er or name +i)e)4ork'ooks+1, or 4ork'ooks+Z0ook1Z,,)

    4ork'oo

    k

    A 4ork'ook o'ect can 'e accessed (rom the

    4ork'ooks Collection 'y usin a 4ork'ook index

    http://msdn.microsoft.com/en-us/library/office/ff194068(v=office.15).aspxhttp://msdn.microsoft.com/en-us/library/office/ff194068(v=office.15).aspx

  • 8/18/2019 Excel Data Entry Tips and Techniques

    69/87

    num'er or a 4ork'ook name +e)) 4ork'ooks+1,

    or 4ork'ooks+Z0ook1Z,,) *ou can also use

    Active4ork'ook to access the current active

    4ork'ook)

    5rom the 4ork'ook o'ect you can access the

    /heets o'ect which is a collection o( all the

    /heets +4orksheets and Chart /heets, in the

    4ork'ook and you can also access the

    4orksheets o'ect which is a collection o( all the

    4orksheets in the 4ork'ook)

    /heets $he /heets o'ect is a collection o( all the /heets

    in a 4ork'ook) $hese /heets can 'e 4orksheets

    or Charts) An individual /heet can 'e extracted

    (rom the /heets o'ect 'y usin an individual

    /heet index num'er or name +i)e) /heets+1, or

    /heets+Z/heet1Z,,)

    4orkshe

    ets

     $he 4ork/heets o'ect is a collection o( all the

    4ork/heets in a 4ork'ook +i)e) all the /heets

    exce"t the Charts,) An individual 4orksheet can

    'e extracted (rom the 4orksheets o'ect 'y

    usin an individual 4orksheet index num'er or

    name +i)e) 4orksheets+1, or

    4orksheets+Z4ksheet1Z,,)

    4orkshe

    et

    A 4orksheet o'ect can 'e accessed (rom the

    /heets or the 4orksheets o'ect 'y usin a

    /heet or 4orksheet index num'er or a /heet or

    4orksheet name +e)) /heets+1, 4orksheets+1,

  • 8/18/2019 Excel Data Entry Tips and Techniques

    70/87

    /heets+Z/heet1Z, or 4orksheets+Z4ksheet1Z,,)

     *ou can also use Active/heet to access the

    current active /heet)

    5rom the 4orksheet o'ect you can access the

    &ows and Columns o'ects which are collections

    o( &ane o'ects relatin to the &ows and

    Columns o( the 4orksheet) *ou can also access

    an individual cell or any &ane o( contiuous

    cells on the 4orksheet)

    &ows $he &ows o'ect is a collection o( all the &ows o(

    a 4orksheet) A &ane o'ect consistin o( an

    individual 4orksheet row can 'e accessed 'y

    usin an index num'er +i)e) &ows+1,,

    Columns $he Columns o'ect is a collection o( all the

    Columns o( a 4orksheet) A &ane o'ect

    consistin o( an individual 4orksheet column can'e accessed 'y usin an index num'er +i)e)

    Columns+1,,

    &ane $he &ane o'ect re"resents any num'er o(

    contiuous cells on a 4orksheet) $his can 'e ust

    one cell or it can 'e all the cells on the

    4orksheet)

    A rane consistin o( ust one cell can 'e

    returned (rom a 4orksheet usin the Cells

    "ro"erty +i)e) 4orksheet)Cells+11,,)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    71/87

    Alternatively a rane can 'e re(erenced 'y

    s"eci(yin either a cell rane or a start and end

    cell +e)) 4orksheet)&ane+ZA1=01Z, .&

    4orksheet)&ane+ZA1Z Z01Z, .&4orksheet)&ane+Cells+11, Cells+1G,,,)

    ;ote that i( the second cell re(erence is omitted

    (rom the &ane +e)) 4orksheet)&ane+ZA1Z, .&

    4orksheet)&ane+Cells+11,, this will return a

    rane that consists o( only one cell)

     $he a'ove ta'le descri'es how you access Excel o'ects via "arent

    o'ects) 5or exam"le a rane o( cells may 'e re(erenced 'y the

    ex"ression=

    4ork'ooks+Z401Z,)4orksheets+Z4/1Z,)&ane+ZA1=01Z,

    ssigning an 0?ect to a Haria0le

    Another "oint to note when workin with Excel o'ects is that when

    an o'ect is 'ein assined to a varia'le in your v'a code you must

    use the /et keyword as (ollows=

    !im !ata4' As 4ork'ook/et !ata4' 4ork'ooks+Z!ata)xlsxZ,

  • 8/18/2019 Excel Data Entry Tips and Techniques

    72/87

    The ctie 0?ect

    At any one time Excel will have an  Actie 4ork'ook which is the

    work'ook that is currently selected) /imilarly there will 'e

    an  Actie 4orksheet and an  Actie &ane etc)

     $he current active 4ork'ook or /heet can 'e re(erred to in your v'a

    code as Actie$or%boo%  or  Actie&heet  and the current active

    rane can 'e accessed 'y re(errin to&election)

    2( in your %0A code you re(er to a worksheet without re(errin to a

    s"eci8c work'ook Excel de(aults to the current Active 4ork'ook)

    /imilarly i( you re(er to a rane without re(errin to a s"eci8c

    work'ook or worksheet Excel de(aults to the current Active

    4orksheet in the current Active 4ork'ook)

     $here(ore i( you wish to re(er to rane A1=01 on the current Active

    4orksheet within the current Active 4ork'ook you can sim"ly ty"e=

    &ane+ZA1=01Z,

    Chanin the Current Active .'ect

    2( durin the execution o( your code you wish to chane the current

    Active 4ork'ook 4orksheet &ane etc this can 'e done usin the

    Activate or /elect methods as (ollows=

    4ork'ooks+Z0ook1)xlsmZ,)Activate

    4orksheets+Z!ataZ,)/elect

    &ane+ZA1Z Z01Z,)/elect

    .'ect Methods such as the Activate and /elect methods used

    a'ove are discussed in more detail 'elow)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    73/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    74/87

    0?ect Methods

    %0A o'ects also have methods that "er(orm s"eci8c actions) .'ect

    methods are "rocedures that are associated to a s"eci8c o'ect

    ty"e) 5or exam"le the 4ork'ook o'ect has the methods ActivateClose /ave and many more)

    An .'ect Method can 'e called 'y re(errin to the o'ect name

    (ollowed a dot and then the method name) 5or exam"le the current

    active 4ork'ook can 'e saved usin the code=

    Active4ork'ook)/ave

  • 8/18/2019 Excel Data Entry Tips and Techniques

    75/87

    A list o( excel o'ects with their "ro"erties and methods are

    "rovided in the .'ect 0rowser within the %isual 0asic Editor) $o

    dis"lay this sim"ly "ress 5G (rom within the %isual 0asic Editor)

    Examples

    Exam"le 1

     $he (ollowin %0A code sni""et was "reviously used to illustrate theuse o( the 5or Each loo") 2t is now use(ul to re-visit this code to

    examine the re(erences to the 4orksheets o'ect +taken (rom the

    current active 4ork'ook 'y de(ault, and the re(erence to each

    individual 4orksheet) ;ote that the 4orksheet 8ame "ro"erty is

    accessed to dis"lay the name o( each 4orksheet)

    Cycle throuh each 4orksheet in the current 4ork'ook

    and dis"lay the 4orksheet name in a messae 'ox

    !im w/heet As 4orksheet

    5or Each w/heet in 4orksheets

    Ms0ox Z5ound 4orksheet= Z \ w/heet);ame

    ;ext w/heet

    Exam"le G

  • 8/18/2019 Excel Data Entry Tips and Techniques

    76/87

     $he (ollowin section o( %0A code has 'een included to illustrate

    how you can access 4orksheets and &anes (rom other 4ork'ooks

    and how the current Excel .'ects are accessed 'y de(ault i( no

    s"eci8c o'ect is re(erenced) $his exam"le also illustrates the use o( 

    the /et keyword to assin an Excel o'ect to a varia'le)

     $he code also shows the Paste/"ecial method 'ein called (or the

    &ane o'ect) $his method sets the Paste arument to the value

    xlPaste%alues)

    Co"y a rane o( cells (rom /heet1 o( another 4ork'ook +named Z!ata)xlsxZ,

    and "aste the values into the Z&esultsZ 4orksheet o( the current 4ork'ook

    +named ZCurr4')xlsmZ,

    !im data4' As 4ork'ook

    /et !ata4' 4ork'ooks)."en+ZC=b!ataZ,

    ;ote that !ata4' is the current Active 4ork'ook)

    $here(ore the (ollowin accesses the /heets .'ect in !ata4')

    /heets+Z/heet1Z,)&ane+ZA1=01Z,)Co"y

    Paste the values (rom the co"ied &ane into the Z&esultsZ 4orksheet o( 

    the current 4ork'ook) ;ote that as Curr4' is not the current Active

    4ork'ook we need to s"eci(y this 4ork'ook)

    4ork'ooks+ZCurr4'Z,)/heets+Z&esultsZ,)&ane+ZA1Z,)Paste/"ecial

    Paste=xlPaste%alues

    Exam"le ?

     $he (ollowin section o( %0A code shows how the Columns

    +collection, o'ect can 'e accessed (rom the 4orksheet o'ect) 2t is

  • 8/18/2019 Excel Data Entry Tips and Techniques

    77/87

    also seen that when a cell or cell rane on the current active

    4orksheet is accessed the re(erence to the 4orksheet can 'e

    omitted) Aain the code "rovides an illustration o( the use o( 

    the /et keyword to assin a &ane o'ect to the varia'le Col)

     $he code also includes an exam"le o( how to access and chane the

    &ane o'ects %alue "ro"erty)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    78/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    79/87

    G) Click on the dro"-down menu at the to" le(t o( the code

    window and select the o'ect ty"e +e)) 4orksheet,) $he dro"down

    menu in the riht window will then dis"lay the events that are

    de8ned (or that o'ect) $he imae 'elow shows the events that are

    de8ned (or an Excel 4orksheet=

    ?) /elect an event (rom the to" riht dro"-down menu) $his

    causes a /u' "rocedure to 'e automatically inserted into the code

    window (or that o'ect) $he aruments that Excel automatically

  • 8/18/2019 Excel Data Entry Tips and Techniques

    80/87

    (eeds into that (unction +i( there are any, are included into the /u'

    header - you then ust need to add the v'a code to de8ne what you

    want to do when that event is 8red)

    Example

     $he (ollowin exam"le dis"lays a messae 'ox every time the cell

    01 in the 4orksheet Z/heet1Z is selected)

    5or this action we need to use the 4orksheet Event

    Z/electionDChaneZ which 8res every time a diXerent cell or raneo( cells is selected) $he Z/electionDChaneZ (unction receives as an

    arument a &ane o'ect called Z$aretZ) $his tells you the rane o( 

    cells that has 'een selected)

    As the Z/electionDChaneZ event relates to any  new selection we

    need to check the su""lied &ane $aret as soon as the (unction

    is called to make sure that the re:uired actions are "er(ormed only

    when cell 01 is selected) $he code (or this is shown 'elow=

    Code to dis"lay a Messae 0ox i( Cell 01 o( the current 4orksheet

    is selected)

    Private /u' 4orksheetD/electionChane+0y%al $aret As &ane,

    Check i( the selection is cell 01

    2(  $aret)Count 1 And $aret)&ow 1 And $aret)Column G $hen

    $he selection 2/ cell 01 so carry out re:uired actionsMs0ox Z*ou have selected cell 01Z

    End 2( 

    End /u'

  • 8/18/2019 Excel Data Entry Tips and Techniques

    81/87

    Excel %0A $utorial Part 1 - %0A Error $y"es

    H# Error Types

     $here are three ty"es o( %0A error that you may encounter when

    executin an Excel macro) $hese are=

    • Com"ile Errors

    • &untime Errors

  • 8/18/2019 Excel Data Entry Tips and Techniques

    82/87

    the error in red de"endin on the settin o( the Auto /yntax Check

    o"tion +see riht,)

    Alternatively a com"ile error may 'e detected when the code is

    com"iled ust 'e(ore 'ein executed)

    A com"ile error is enerally easy to 8x as the %0A com"iler "o"s u"

    a messae 'ox which "rovides in(ormation on the nature o( the

    error)

    5or exam"le i( you et the messae "Compile error: Variable not 

    defned" when you attem"t to run your code this indicates that

    you are attem"tin to use or access a varia'le that has not 'eendeclared in the current sco"e) +$his error is only enerated when

    you are usin ."tion Ex"licit,)

    :untime Errors

  • 8/18/2019 Excel Data Entry Tips and Techniques

    83/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    84/87

     $he diXerent runtime error codes are ex"lained on the Microso(t

    /u""ort 4e'site) However some o( the more common %0A error

    messaes are shown in the ta'le 'elow=

    L - 2nvalid "rocedure call

    Q - .ut o( memory

    > -/u'scri"t out o( rane

    +this error arises i( you attem"t to

    access elements o( an array outside o(

    the de8ned array si6e - e)) i( you

    de8ne an array indexed (rom 1 to 1

    then attem"t to access entry no) 11,

    1

    1

    - !ivision 'y 6ero

    1

    ?

    - $y"e mismatch

    +this error arises when you attem"t to

    assin the wron ty"e o( value to a

    varia'le - e)) de8ne i as an inteer

    then attem"t to assin the strin ZtextZ

    to i,

    https://support.microsoft.com/en-us/kb/142138https://support.microsoft.com/en-us/kb/142138https://support.microsoft.com/en-us/kb/142138https://support.microsoft.com/en-us/kb/142138

  • 8/18/2019 Excel Data Entry Tips and Techniques

    85/87

  • 8/18/2019 Excel Data Entry Tips and Techniques

    86/87

    ."en the !ata 4ork'ook

    /et !ata4ork'ook 4ork'ooks)."en+ZC=b!ocuments and /ettinsb!ataZ,

    /et the varia'les %al1 and %alG (rom the data in !ata4ork'ook

    %al1 /heets+Z/heet1Z,)Cells+1 1,

    %alG /heets+Z/heet1Z,)Cells+1 G,

    !ata4ork'ook)Close

    Exit /u'

    ErrorHandlin=

    2( the 8le is not (ound ask the user to "lace it into

    the correct directory and then resume

    Ms0ox Z!ata 4ork'ook not (oundZ \ D 

      ZPlease add the work'ook to C=b!ocuments and /ettins and click .FZ

    &esume

    End /u'

    2n the code a'ove the code attem"ts to o"en the Excel 5ile !ata

    and i( it (ails to 8nd the 8le "rom"ts the user to "lace the data 8le

    into the correct (older) .nce the user does this and clicks .F the

    code is resumed and a (urther attem"t is made to o"en the 8le) 2( 

    desired instead o( re-tryin the 8le the /u' "rocedure could 'e

    terminated at this "oint 'y usin theExit /u' command)

  • 8/18/2019 Excel Data Entry Tips and Techniques

    87/87

    (ogical Errors