+ All Categories
Home > Documents > Advanced Functions Add Ins and VBA

Advanced Functions Add Ins and VBA

Date post: 03-Apr-2018
Category:
Upload: traisimdo
View: 237 times
Download: 0 times
Share this document with a friend

of 20

Transcript
  • 7/28/2019 Advanced Functions Add Ins and VBA

    1/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Advanced Functions, Add-Ins & VBA

    Prepared For By

    IALEIA SWOC Joseph Ryan Glover

    November Excel Workshop Police Analyst.com

    November 28, 2012

    Contact

    416-538-0659

    [email protected]

    www.policeanalyst.com

    This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

    To view a copy o this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/.

    http://-/?-http://-/?-http://-/?-http://-/?-
  • 7/28/2019 Advanced Functions Add Ins and VBA

    2/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover 2

    Table of Contents

    Advanced Functions, Add-Ins & VBA 3

    Advanced Functions? 3

    The FREQUENCY Function 4The Microsot Analysis ToolPak Add-In 8

    Perorming a T-Test to Determine Statistical Signicance 9

    Microsot Are Not the Only People Who Make Add-Ins 13

    The PatternUI Add-In 13

    Making Your Own Add-In 16

    The DAYOFYEAR Function 16

  • 7/28/2019 Advanced Functions Add Ins and VBA

    3/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Advanced Functions, Add-Ins & VBA

    The plan or this workshop is to start by demonstrating an example o an advanced unction in Excel (specically the FREQUENCY

    unction), then well move to learning how to use the Analysis ToolPak Add-in that Microsot provides or ree with Excel, then Ill

    show you how to install Add-ins that you can nd on the web and nally Ill show you how to create your own Add-In with your own

    unctions using Visual Basic or Applications (VBA). While this material is more advanced than the last workshop Im condent o yoursuccess. I we continue to do the examples together well all learn some cool Excel tricks that can help us do our jobs more produc-

    tively. Good luck and have un.

    Advanced Functions?

    When you start talking about advanced unctions in Excel you are moving into the realm o packaged unctionality that achieves some

    kind o complicated eect. That is, the work done behind the scenes by the unction is more complicated than just counting how long

    a string is (i.e. LEN) or putting together a date (i.e. DATE). Advanced unctions ask the user to have a stronger understanding o what

    the unction is designed to do and, in return, the unction works to make hard things easier to accomplish.

    3

  • 7/28/2019 Advanced Functions Add Ins and VBA

    4/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    The FREQUENCY Function

    For this workshop I am going to demonstrate how to use the FREQUENCY unction as iy is useul or dierent kinds o analysis that law

    enorcement analysts might tackle.

    FREQUENCY is used to tabulate requency data, which is used to create histogram charts, rom a series o data points and bin sizes. A

    histogram is basically a column char t that counts how many data points all within each o several bins. The bins are rangeslike1-5, 6-10, 11-15, etcthat cover the entire range o the data set. The idea is that ater binning the data you can plot the bins versus

    their counts and gain urther insights into the character o your data.

    To get star ted have a look in the Excel le Advanced_Functions.xlsx or a sheet named Frequency. It contains in column A a list o

    100 numbers the represent daily call or service numbers. This column will serve as the data that we want binned. In order to use

    FREQUENCY we need to gure out the ull range o the data and to do that were going to use the MIN and MAX unctions to nd the

    minimum and maximum values o the data set, just like in the screen shot below.

    Continued on the next page ...

    4

  • 7/28/2019 Advanced Functions Add Ins and VBA

    5/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    From the screen shot we can see that the min value is 205 and the max value is 295. This indicates that bins that cover the range 200

    300 will suit us ne. To make things manageable I think the bins should go by 10s, so 201 210, 211 220, 221 230, etc. I set

    up the bins in column G the screen shot below.

    Notice that the bin values have not been entered as ranges (like 201-210) but simply as the largest value each bin will hold. Excel is

    smart enough to understand that these values represent the bin maximums.

    Continued on the next page ...

    5

  • 7/28/2019 Advanced Functions Add Ins and VBA

    6/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Now well use the FREQUENCY unction to put each o the 100 data points in column A into one o the 10 bins dened in column G.

    The FREQUENCY unction is a little dierent than what youre used to, betting its advanced status, as its whats known as an array

    unction. Unlike traditional Excel unctions array unctions are passed multiple arrays o values (really just multiple ranges) and the

    unction outputs the results across multiple cells.

    In order to use FREQUENCY we need to rst highlight all the cells the results will ll, and or this example that will be cells H2 throughH11 and then type, in the ormula bar at the top o the page, =FREQUENCY(A2:A101, G2:G11) just as in the screen shot below. But,

    beore you hit the enter key, a key element o array unctions is that when you are nished typing them in you need to hit the CTRL,

    SHIFT and ENTER all at the same time. I you do this properly you will see two things: the rst is that each highlighted cell in the range

    H2:H11 will get a value and the unction in the ormula bar will be surrounded by curly braces like this {}.

    Ater successully entering the unction youll see 10 values in H2 through H11. Each number corresponds to the number o data points

    that alls within each o the bins we dened and their sum is the total number o data points in our data set (in this example there area hundred).

    Continued on the next page ...

    6

  • 7/28/2019 Advanced Functions Add Ins and VBA

    7/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    To get a quick look at the histogram highlight columns H2 through H11 and hit the Insert->Charts->Column Charts. Excel will spit

    out a chart that has the 10 columns and that reveals that the daily number o calls or service ollow a normal or bell curve, which, i

    youre looking to do urther analysis on calls or service, is very valuable inormation. Screen shot below.

    So there you have it, an example o an Excel advanced unction that not only works in a dierent way than other unctions (i.e. its

    an array unction) but outputs a complex result that could not otherwise easily be achieved (try binning by hand, it takes orever,

    especially i you have a lot o data points). I admit, even using FREQUENCY is a little complicated and well see in the next section how

    Microsot and Excel have worked to make producing these kind o results even easier.

    7

  • 7/28/2019 Advanced Functions Add Ins and VBA

    8/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    The Microsoft Analysis ToolPak Add-In

    The Analysis ToolPak is a ree Add-In provided by Microsot as part o the deault installation o Excel. Add-Ins are a kind o Excel le,

    typically with a .xlam extension, that package together a bunch o related unctionality. Add-Ins can be made by big companies like

    Microsot but they are also be made by third parties (there are number o companies whose whole business is making and selling

    Add-Ins) and by every day users o Excel (Ill show you how to make an Add-In later in this work shop).To get star ted with the Analysis ToolPak we need to check i it is installed. You may or may not have it already installed but to check i

    you do you click the Datatab in Excel and look at the ar right. I you see Data Analysis as an option, its already active (see screen

    shot).

    I you dont see it that means we need to turn it on. First, click the green Filetab (or the Excel jewel i youre in 2007) to call up the

    Save/Open/Close menu. Look around the option named Optionsand click it. This will open up a window with a menu on the let.

    Click on the menu option named Add-Ins on the let hand side. The screen will change and at the bottom, beside the word Manage,

    will be a select box and a button labeled Go. Click the Go button to open yet another window and you will see a series o check-

    boxes. Fingers crossed that one o them says Analysis ToolPak and is unchecked. Click the checkbox and click ok. Now go back to Data

    tab, look at the ar right and youll see Data Analysis. Youre set.

    I you click the Data Analysis button in the menu youre going to get a new window with a lot o options. Its not possible to cover

    everything thats in there in one day (or one week). What Im going to do is demonstrate one o the tools that I use oten to determine

    i a result is real or not: its called t-Test: Two-Sample Assuming Unequal Variances. Yes, its a mouthul, dont worry, Ill walk youthrough it. (As a side note, did you see the Histogram tool in the checkbox list? It does exactly what we did above manually with

    FREQUENCY.)

    8

  • 7/28/2019 Advanced Functions Add Ins and VBA

    9/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Perorming a T-Test to Determine Statistical Signifcance

    The value o the t-Test is that i t can tell you i a dierence you are seeing in two measurements is legitimate or i the dierence is likely

    to be just rom randomness. In the example below I am going to use the t-Test to determine i the dierence in the number o daily

    motor vehicle collisions during the winter is meaningully dierent then the number during the summer. This is just one o a near-

    innite number o uses.

    First, you need some background to understand what it is were doing. In statistics we are always comparing two hypotheses. The rst

    is named the null hypothesis and the second is named the alternate hypothesis. We almost always want to reject the null hypothesis

    and accept the alternate one because in that situation it means something interesting has occurred. For our motor vehicle collision

    example we want to determine i there is a meaningul dierence in the number o daily motor vehicle collisions during the rst quar-

    ter o the year (i.e. the winter) versus the third quarter o the year (i .e. the summer). Now, you may be thinking obviously theres a

    dierence, the weather but lets, or the sake o education, assume that we need to demonstrate statistically that there is a dierence.

    In this study the null hypothesis would be that there is no dierence between the quarters, they are the same while the alternate

    hypothesis would be yes, there is a statistically signicant dierence between the two quarters.

    The term statistically signicant needs some explanation. When we want something to be statistically signicant we want to be able

    to say, with a particular level o condence, that the results we are seeing are not just due to chance. First we pick our condencelevel or how sure we want to be and then, through the magic o statistics, we are provided a number that our results have to beat in

    that we can be that sure. In this example were going with the 95% condence level which means that i we nd a dierence between

    the average number o MVCs during the winter and summer we want to be 95% sure its not due to chance. We can up the condence

    interval to 99% i we want but, as well see, well need to adhere to even stricter conditions. Whatever we choose just remember that

    signicant in statistically signicant doesnt mean important (as signicant typically means in everyday conversation) it just means

    not due to randomness.

    Continued on the next page ...

    9

  • 7/28/2019 Advanced Functions Add Ins and VBA

    10/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    How do we go about demonstrating that the two quarters have real, non-random dierences in the number o daily motor vehicle

    collisions? The rst thing we do randomly select rom the rst and third quarters 30 dierent days worth o MVC counts. Ive provided

    this or you in a sheet named MVCs in the Advanced_Functions.xlsx spreadsheet and screen grabbed the start and end o the data

    below.

    Notice that we there are two columns o data, one marked Winter and one marked Summer and each has 30 entries. Also notice that

    at the bottom o each column is a number labeled Mean. In those cells I have used Excels AVERAGE unction to nd the mean o all

    the numbers (add them all up, divide by 30). And behold, the means are dierent. That proves the quarters are dierent, right? Not

    quite. We still have to deal with the peskiness that is statistical signicance. Its possible, ater all, that its just through chance those

    means are dierent. As discussed above we want to be 95% sure that they arent.

    This is where the Data Analysis Add-In and the t-Test: Two-Sample Assuming Unequal Variances tool comes into play. We are going

    to eed the two sets o 30 numbers through the tool and its going to spit out a bunch o results then were going to zero in on the

    result that gives us the thumbs up or thumbs down on meaningul dierence.

    Continued on the next page ...

    10

  • 7/28/2019 Advanced Functions Add Ins and VBA

    11/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    So, click open the Data Analysis window. Click t-Test: Two-Sample Assuming Unequal Variances in the list and click the OK but-

    ton. This opens up a new window (check the screen shot below) with some options. For the Variable 1 Range click the little arrow

    button and select all o the Winter data, which is in B2:31. For Variable 2 Range select all the Summer data rom C2:C31. For the

    Hypothesized Mean Dierence enter 0 (since were trying to prove they are the same, the dierence o the mean should be 0) and

    everything else can stay the same. However, notice that there is eld named Alpha that has the value 0.05. Its not a coincidence that

    0.05 = 1 0.95. Alpha is another way o asking how sure we want to be.

    Click the OK button.

    Continued on the next page ...

    11

  • 7/28/2019 Advanced Functions Add Ins and VBA

    12/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Excel will open up a new worksheet populated with a bunch o labels and values just like the one in the screen shot below.

    So what are we looking at? Well, there are the means we saw. Next comes the variances, which are measures o how spread out values

    are, and they are quite dierent (which is why we needed to use the test that assumed unequal variances) and a little urther down

    we see our t Stat and it has a value o about 4.35. Skip down two lines to the value labeled t Critical one-tail and note that it is

    about 1.68. Thats good news or us because the rule is that i your t Stat is larger than your t Critical value then the null hypothesis

    can be rejected and our results are thereore statistically meaningul. This means that we can now state, with the power o math

    backing us up, that we are 95% sure that the daily average number o MVCs in the winter are greater than the daily average number o

    MVCs in the summer.

    But what, exactly, does this demonstration o the Analysis ToolPak Add-In have to do with advanced unctions? The answer lies in the

    sheet labeled Manual t-Test in the Advanced_Functions.xlsx le. On this sheet I worked out all the bits and bobs o the t-Test solu-tion using traditional unction calls and look at what a mess it is. I had to Google all over to remind mysel how to do this manually

    and it still looks like gibberish to me. And thats the power o Add-Ins: they package up messiness and make it easier to work with.

    They make it so non-experts can still use powerul tools.

    12

  • 7/28/2019 Advanced Functions Add Ins and VBA

    13/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Microsoft Are Not the Only People Who Make Add-Ins

    There are a thousand dierent Add-Ins out there. Some are insanely complicated, like SpatialXL (an Add-In that puts a unctional

    GIS inside o Excel) and cost an arm and a leg (SpatialXL is $1000+) and some are available or ree and solve a simple problem that

    someone had. Were going to have a look at one o the latter kind o Add-Ins now

    The PatternUI Add-In

    The Add-In that were going to install is named PatternUI.xlam. To get the le you can either download it o the web rom http://

    oceblogs.net/excel/PatternUI.zip or you can nd it on your memory stick. However you get it, drag it to your My Documents older

    on your computer.

    To install an Add-In open up Excel and click the on the green File tab. Just like turning on the Analysis ToolPak Add-In go to Options-

    >Add-Ins and click the GO button near the Manage label. However, this time, when the little window with the checkboxes opens

    up click the Browse button to open the le dialogue. Browse to where you dropped the PatternUI.xlam le, click OK, make sure the

    checkbox beside the name is checked and click OK again.

    Continued on the next page...

    13

  • 7/28/2019 Advanced Functions Add Ins and VBA

    14/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    PatternUI is used or putting patterns on charts so go back or a moment to the t-Test page and highlight the rst 5 or so values under

    the Winter label and click Insert->Column to create a new column chart. Click on the chart and then click the Chart Tools tab named

    Format to see the button or the new Add-In named Patterns like in the screen shot below.

    Continued on next page ...

    14

  • 7/28/2019 Advanced Functions Add Ins and VBA

    15/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    I you click the columns and then the Patterns button youll be oered a number o dierent hatching patterns that, i you select one,

    will be applied to the columns (see screen shot below). I use this Add-In requently at work because my charts are usually printed in

    black and white and using patterns instead o colours really allows the dierences to stand out in a way that just doesnt happen with

    colour.

    So there you have it. Really useul unctionality, bundled into an Add-In and provided or ree by a kind soul on the Internet. I you are

    having a chronic problem in Excel that you think could be solved by an Add-In chances are someone else has had the same problem

    and possibly published their solution. Hop on Google, look around, and see i you can nd a solution to your problem that has been

    turned into an Add-In by some Good Samaritan.

    15

  • 7/28/2019 Advanced Functions Add Ins and VBA

    16/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Making Your Own Add-In

    Sometimes youll nd that other people havent solved your problem and maybe youll want to roll up your sleeves and solve it your-

    sel. Now its time to make your own Add-In.

    The DAYOFYEAR Function

    Close all your Excel workbooks (its best to star t resh) and open a new one but dont save anything yet. The rst thing youll need to

    do is add the Developer tab to your workbook. This process is a li ttle dierent in Excel 2007 and Excel 2010 so I am going to explain

    how to do it in 2010 rst. Once again, go back to the green File table and select Excel Options. In the menu at the let select Cus-

    tomize Ribbon. You should see something similar to the screen shot below. From the drop down on the let select All Tabs and scan

    in the let hand list or a checkbox that says Developer. Click on it and click the Add >> button in the middle o the page. This will

    shit the Developer checkbox over to the right hand column. Click OK.

    In 2007 its quite a bit easier to activate the Developer tab. Click on the Excel jewel in the top let hand corner and nd the Excel

    Options button near the bottom o the window. Click it. The new window that opens up has a menu where the topmost option is

    Popular. Click that i it isnt already selected. There will be a set o our checkboxes and number three will be labelled Show Devel-

    oper tab in the R ibbon. Click that checkbox, click ok, and youre good to go.

    You now have the developer tools active in Excel. The developers tools contain all you need to create your own Add-In.

    Continued on the next page ...

    16

  • 7/28/2019 Advanced Functions Add Ins and VBA

    17/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Back in your workbook youll see the new Developer tab at the top o the page Click it. At the ar let you will see a button labeled

    Visual Basic (see the screen shot below).

    Click the Visual Basic button and a whole new window will open up. This is the Visual Basic developer window and a screen shot is

    below.

    Now dont be intimated, most o the stu is unimportant. But what is important is in the let hand column in the little window named

    Project. There you will see an entry named Book2 or whatever the name o your current worksheet is. Click the plus symbol to open

    it up. In there youll see your worksheets and not much else. Right click on the project name and select Insert->Module. All the code

    you are going to write or this Add-In is going to reside in this module, which is really just a text le with a ancy name.

    Continued on the next page ...

    17

  • 7/28/2019 Advanced Functions Add Ins and VBA

    18/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Click your cursor inside the text area to let and lets solve a problem. In my rst session we briefy talked about how to determine the

    day o the year rom a date. I told you there was no built in Excel unction to do that work and we had to make an ugly ormula to get

    the answer. I also told you I would x that problem later. This is that later.

    We are going to write a unction named DAYOFYEAR that, when given a date, will return the day o the year that date represents.

    Because this unction is going to live in your Add-In it will be available to all your Excel worksheets in the uture.

    The ollowing screen shot lists the entire code or this Add-In. The discussion is below.

    Ill break down the code line by line.

    In the rst line we are dening the unction we are going to make. We need to tell Excel that a) were making a new unction, b) that

    its name is DAYOFYEAR and c) that were going to pass in a value and that value is going to be a date.

    In the second line I use the Year unction, which works the same in VBA as it does in Excel, to get the year o the date that was passedin. I save that year in a variable named date_in_year.

    In the third line I create a new date using the VBA unction DATESERIAL, which behaves exactly the same as Excels DATE unction, and

    I give it the previously determined date_in_year, the month o January (1) and a day o 0.

    In the ourth line I set the variable DAYOFYEAR to be equal to the date passed in date_in minus the start_o_year date I calculated in

    line three. Since the variable in line our is named DAYOFYEAR thats what Excel is going to send back rom the unction call.

    The th and last line just tells VBA that the unction is over.

    Continued on the next page ...

    18

  • 7/28/2019 Advanced Functions Add Ins and VBA

    19/20

    Advanced Functions, Add-Ins & VBA. CC BY-NC-SA 2012 Joseph Ryan Glover

    Back in your workbook enter a date into cell A2 where you know the day o the year (try something early in January). Then enter into

    cell B2 the unction =DAYOFYEAR(A2) and see i you get something like in the screen shot below. I you get an error like #NAME or

    #VALUE instead o the right answer then its likely that you have a typo in your code. Go back into the Visual Basic editor and double

    check that the code is correct.

    Try a ew dierent dates where you know the day o year to prove to yoursel that this unction you just created does what its sup-

    posed to.

    Continued on the next page ...

    19

  • 7/28/2019 Advanced Functions Add Ins and VBA

    20/20

    Advanced Functions Add Ins & VBA CC BY NC SA 2012 Joseph Ryan Glover

    Now we need to save our Add-In. We do this by saving the le as we normally would. Click the save button and when the Save As

    dialogue comes up change the Save as type drop down to read Excel Add-In and name it MyAddIn. Put it in your My Documents

    older and click the Save button. The screen shot demonstrates the options.

    Once its saved, close all o Excel again. Now open a new worksheet. Type in a date in cell A1 and use DAYOFYEAR in cell B1. Did it

    work? Not likely. Thats because your Add-In hasnt been installed yet. Just like the PatternUI.xlam le you need to install MyAddIn.

    xlam the rst time beore you can use it (it worked beore because you were actually in the le). So, once again, click File->Options-

    >Add-Ins. When the Add-Ins window opens, click Browse and locate your Add-In. Click OK and OK. Now try the DAYOFYEARunction call again. Perect. I you close out o Excel and open it back up, the unction call will work without needing to install the

    Add-In since it will be active until you uninstall it.

    Congratulations on making your rst Excel Add-In.

    This concludes the Advanced Functions, Add Ins & VBA workshop. Thank you.

    20


Recommended