10
5/ 15/13 Geek School: Learn Ho w to Use Excel Ma cr os to Automate T edi ous T ask s ww w.h ow togeek.com /1629 75/geek -sc hool-l earn-how-to-use-excel-macr os-to- autom ate-tedious-tasks/?utm _source=newsletter&utm_m edium=em ail&utm_cam p… 1/10 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks One of t he more powerful , but seldom used functions of Ex cel i s t he abil ity to very easi ly crea te automated tas ks and cus tom logic within macros. Macros provide an ideal way to save time on predictable, repetitive tasks as well as standardize document formats – many times without having to write a single line of code. If you are curious what macros are or how to actually create them, no problem – we will walk you through the entire process. What is a Macro? A Mi crosof t Office Macro (as t his functionali ty applies to several of the MS Office Applications) is simply Visual Basic for Applications (VBA) code saved inside a document. For a comparable analogy, think of a document as HTML and a macro as Javascript. In much of the same way that Javascript can manipulate HTML on a webpage, a macro can manipulate a document. Macros are incredibly powerful a nd can do pretty much anything your imagi nation can conjure. As a (very ) short list of funct ions you can do with a ma cro: Apply st yle a nd for matting. Manipulate data and text. Communicate with data so urces (database, tex t f iles, etc.). Create entirely new documents. Any combinati on, in any order, o f any of the above.  Creating a Macro: An Explanation by Example We s tart with your garden varie ty CSV fil e. Nothing specia l here , just a 10×20 set of numbers betwee n 0 and 100 with both a row and column heade r. Our goal is t o pr oduce a well forma tted, presentable data sheet which incl udes s umma ry to tals for each row. How-To Geek

Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

  • Upload
    rocky35

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 1/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp…

Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

One of the more powerful, but seldom used functions of Excel is the ability to very easily create automated tasks and custom logicwithin macros. Macros provide an ideal way to save time on predictable, repetitive tasks as well as standardize document formats –many times without having to write a single line of code.

If you are curious what macros are or how to actually create them, no problem – we will walk you through the entire process.

What is a Macro?

A Microsoft Office Macro (as this functionality applies to several of the MS Office Applications) is simply Visual Basic for Applications(VBA) code saved inside a document. For a comparable analogy, think of a document as HTML and a macro as Javascript. In muchof the same way that Javascript can manipulate HTML on a webpage, a macro can manipulate a document.

Macros are incredibly powerful and can do pretty much anything your imagination can conjure. As a (very) short list of functionsyou can do with a macro:

Apply style and formatting.

Manipulate data and text.

Communicate with data sources (database, text f iles, etc.).

Create entirely new documents.

Any combination, in any order, of any of the above.

 

Creating a Macro: An Explanation by Example

We start with your garden variety CSV file. Nothing special here, just a 10×20 set of numbers between 0 and 100 with both a rowand column header. Our goal is to produce a well formatted, presentable data sheet which includes summary totals for each row.

How-To Geek

Page 2: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 2/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 2

 

As we stated above, a macro is VBA code, but one of the nice things about Excel is you can create/record them with zero codingrequired – as we will do here.

To create a macro, go to View > Macros > Record Macro.

Assign the macro a name (no spaces) and click OK.

Once this is done, all of your actions are recorded – every cell change, scroll action, window resize, you name it.

Page 3: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 3/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 3

There are a couple of places which indicate Excel is record mode. One is by viewing the Macro menu and noting that Stop Recordinghas replaced the option for Record Macro.

The other is in the bottom right corner. The ‘stop’ icon indicates it is in macro mode and pressing here will stop the recording(likewise, when not in record mode, this icon will be the Record Macro button, which you can use instead of going to the Macrosmenu).

Now that we are recording our macro, let’s apply our summary calculations. First add the headers.

Next, apply the appropriate formulas (respectively):

=SUM(B2:K2)

=AVERAGE(B2:K2)

=MIN(B2:K2)

=MAX(B2:K2)

=MEDIAN(B2:K2)

Now, highlight all the calculation cells and drag the length of all our data rows to apply the calculations to each row.

Page 4: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 4/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 4

Once this is done, each row should display their respective summaries.

Now, we want to get the summary data for the entire sheet, so we apply a few more calculations:

Page 5: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 5/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 5

Respectively:

=SUM(L2:L21)

=AVERAGE(B2:K21) *This must be calculated across all data because the average of the row averages does not necessarily equal the average of all the values.

=MIN(N2:N21)

=MAX(O2:O21)

=MEDIAN(B2:K21) *Calculated across all data for the same reason as above.

 

Now that the calculations are done, we will apply the style and formatting. First apply general number formatting across all the cellsby doing a Select All (either Ctrl + A or click the cell between the row and column headers) and select the “Comma Style” icon under

the Home menu.

Page 6: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 6/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 6

Next, apply some visual formatting to both the row and column headers:

Bold.

Centered.

Background fill color.

And finally, apply some style to the totals.

When all is finished, this is what our data sheet looks like:

 

Since we are satisfied with the results, stop the recording of the macro.

Congratulations – you have just created an Excel macro.

 

In order to use our newly recorded macro, we have to save our Excel Workbook in a macro enabled file format. However, before wedo that, we first need to clear all the existing data so that it is not embedded in our template (the idea being every time we use thistemplate, we will import the most up-to-date data).

To do this, select all cells and delete them.

Page 7: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 7/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 7

With the data now cleared (but the macros still included in the Excel file), we want to save the file as a macro enabled template(XLTM) file. It is important to note that if you save this as a standard template (XLTX) file then macros willnot be able to be runfrom it. Alternately, you can save the file as a legacy template (XLT) file, which will allow macros to be run.

Once you have saved the file as a template, go ahead and close Excel.

 

Using an Excel Macro

Before covering how we can apply this newly recorded macro, it is important to cover a few points about macros in general:

Macros can be malicious.

See the point above.

VBA code is actually quite powerful and can manipulate files outside of the scope of the current document. For example, a macrocould alter or delete random files in your My Documents folder. As such, it is important to make sure you only run macros fromtrusted sources.

To put our data format macro to use, open the Excel Template file which was created above. When you do this, assuming you havestandard security sett ings enabled, you will see a warning across the top of the workbook which says that macros are disabled.Because we trust a macro created by ourselves, click the ‘Enable Content’ button.

Up next, we are going to import the latest data set from a CSV (this is the source the worksheet used to create our macro).

Page 8: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 8/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 8

To complete the import of the CSV file, you may have to set a few options in order for Excel to interpret it correctly (e.g. delimiter,headers present, etc.).

 

Once our data is imported, s imply go to the Macros menu (under the View tab) and select View Macros.

In the resulting dialog box, we see the “FormatData” macro we recorded above. Select it and click Run.

Once running, you may see the cursor jump around for a few moments, but as it does you will see the data being manipulatedexactly as we recorded it. When all is said and done, it should look just like our original – except with different data.

Page 9: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 9/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

www.howtogeek.com/162975/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks/?utm_source=newsletter&utm_medium=email&utm_camp… 9

 

Looking Under the Hood: What Makes a Macro Work

As we have mentioned a couple of times, a macro is driven by Visual Basic for Applications (VBA) code. When you “record” a macro,Excel is actually translating everything you do into its respective VBA instructions. To put it s imply – you don’t have to write anycode because Excel is writing the code for you.

To view the code that makes our macro run, from the Macros dialog click the Edit button.

The window that opens displays the source code that was recorded from our actions when creating the macro. Of course, you canedit this code or even create new macros entirely inside of the code window. While the recording action used in this article will likelyfit most needs, more highly customized actions or conditional actions would require you to edit the source code.

 

Taking Our Example One Step Farther…

Hypothetically, assume our source data file, data.csv, is produced by an automated process which always saves the file to the samelocation (e.g. C:\Data\data.csv is always the most recent data). The process of opening this file and importing it can be easily made

Page 10: Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

7/30/2019 Geek School_ Learn How to Use Excel Macros to Automate Tedious Tasks

http://slidepdf.com/reader/full/geek-school-learn-how-to-use-excel-macros-to-automate-tedious-tasks 10/10

5/15/13 Geek School: Learn How to Use Excel Macros to Automate Tedious Tasks

h t k /162975/ k h l l h t l t t t t di t k /? t l tt & t di il& t 10

Jason Faulkner is a developer and IT professional who never has a hot cup of coffee far away. Interact with him on Google+

Published 05/14 /13

7,532 Views

What To Do If You Get aVirus on Your Computer  From Plastic to Smartphone:When Will Digital WalletsTake Ove r?

How to Create a PortableVersion of Windows 8Without Extra Software

HTG Explains: Do You Nee dthe Professional Edition of Windows 8?

HTG Explains: Why It’s GoodThat Your Computer’s RAMIs Full

Stupid Gee k Tricks: RunCommands in the WindowsExplorer Address Bar 

into a macro as well:

Open the Excel Template file containing our “FormatData” macro.

Record a new macro named “LoadData”.

With the macro recording, import the data file like you normally would.

Once the data is imported, s top recording the macro.

Delete all the cell data (select all then delete).

Save the updated template (remember to use a macro enabled template format).

Once this is done, whenever the template is opened there will be two macros – one which loads our data and the other which

formats it.

 

If you really wanted to get your hands dirty with a bit of code editing, you could easily combine these actions into a single macro bycopying the code produced from “LoadData” and inserting it at the beginning of the code from “FormatData”.

 

Download this Template

For your convenience, we have included both the Excel template produced in this article as well as a sample data file for you to playaround with.

Download Excel Macro Template from How-To Geek

RECOMMENDED POSTS