Sei sulla pagina 1di 5

1. What is VBA?

Excel VBA allows you to automate various activities you do in Excel. We use Excel to analyze
data, create reports, prepare charts & presentations, do calculations and understand information.
When we are doing all these, we spend quite some time, repeating same steps.
For example, we may prepare and email same type of report every week with different data.
By using Excel VBA, we can cut down the time we spend repeating these steps and improve our
productivity.
VBA stands for Visual Basic for Applications. Just like you and I have a language, computer
programs too have their own language. VBA happens to be the language in which Excel speaks. For
that matter, VBA is also the language of MS Word, PowerPoint, Access and other MS Office
applications.
For example the following line says hello to you:

MsgBox "hello"

Just like any language, VBA too has its own rules, grammatical structures & nuances. Once you
understand these, speaking VBA with Excel becomes as easy as chatting with your friend over a drink.

A macro is a sequence of instructions that can be automatically executed in


order to automate frequent or complicated tasks.
Macros are written in a programming language called Visual Basic and can be
created by recording a task or by writing the Visual Basic program or by a
combination of the two.

2. What is a Macro?
While VBA is the language which Excel speaks (and understands), Macro is like a paragraph. In other
words, a Macro is a set of instructions given to Excel to accomplish something.
For example, this is a macro for generating a report (written in plain English, not VBA)
1.

Open data.xls

2.

Take last 30 days of data

3.

Prepare a bar chart

4.

Copy the chart to a new workbook

5.

Save the workbook as a PDF

6.

Email it to boss

When we execute or run this macro, we end up generating the report & mailing it.

3.How is it useful?
It lets you get the most out of your software investment (or, more likely, your employers
software investment). Using Excel without knowing VBA is sort of like buying a TV set and watching
only the odd-numbered channels.
It will improve your productivity and Save your time (eventually). Mastering VBA definitely
takes some time, but youll more than make up for this in the amount of time you ultimately save
because youre more productive. Sort of like what they told you about going to college.
It helps you to reduce the manual errors.
Visibility in the Company. Like it or not, Microsofts applications are extremely popular. You may
already know that all applications in Microsoft Office support VBA. The more you know about VBA, the
better your chances for advancement in your job.
Its fun (well, sometimes). Some people really enjoy making Excel do things that are otherwise
impossible. By the time you finish this training, you just might be one of those people.

4. Basics of VBA & Macros Writing our First Macro

Enabling Developer Ribbon in Excel


In order to record and use macros (and other developer features), the first step is to activate
Developer Ribbon (or Developer Toolbar). This is done by,
Excel 2007:
1. Click on Office button (top left)
2. Go to Excel Options
3. Go to Popular
4. Check Show Developer Tab in Ribbon (3rd Check box)
5. Click ok.
Excel 2010:
1. Click on File Menu (top left)
2. Go to Options
3. Select Customize Ribbon
4. Make sure Developer tab is checked in right side area
5. Click ok.

Using Excels Built-in Macro Recorder


In order to write your first VBA program (or Macro), you need to know the language first. This is
where Excels tape recorder will help us.

Since we dont know any VBA, we will use this recorder to record our actions and then we will see
recorded instructions (called as code in computer lingo) to understand how VBA looks like.
Step 1: Select any cell & start macro recorder
This is the easiest part. Just select any cell and go to Developer Ribbon & click on Record Macro
button.

Step 2: Give a name to your Macro


Specify a name for your macro. I called mine MakeMeRed. You can choose whatever you want. Just
make sure there are no spaces or special characters in the name (except underscore)
Click OK when done.
Step 3: Fill the current cell with red color
This is easy as eating pie. Just go to Home ribbon and fill red color in the current cell.
Step 4: Stop Recording
Now that you have done the only step in our macro, its time to stop Excels tape recorder. Go to
Developer ribbon and hit stop recording button.

Step 5: Assign your Macro to a button


Now go to Insert ribbon and draw a nice rectangle. Then, put some text like click me to fill red in it.
Then right click on the rectangle shape and go to Assign Macro. And select the MakeMeRed macro
from the list shown. Click ok.

Step 6: Go ahead and play with your first macro


That is all. Now, we have linked the rectangle shape to your macro. Whenever you click it, Excel would
drop a bucket of red paint in the selected cell(s).
Go ahead and play with this little macro of ours.

Important Shortcuts for Excel VBA


Please remember these shortcuts & use them to be productive while using Excel VBA.

ALT+F11: To view VBA Editor (or to switch back to Excel)

ALT+F8: To display all macros


These shortcuts will work only in VBA Editor (also known as VBE):

ALT+Q: To close VBA Editor and return to Excel

F5: To run a Macro

F2: Display Object Browser

F7: Display code editor

CTRL+G: Open immediate window

F1: Display help

Potrebbero piacerti anche