Documente Academic
Documente Profesional
Documente Cultură
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.
The prospect of learning new languages scares us a lot. But worry not, VBA is
much easier to learn than French, Spanish, German or Chinese. The main reason
why VBA is easy to learn is because it almost looks like plain English.
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.
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
6. Email it to boss
When we execute or run this macro, we end up generating the report &
mailing it.
Basics of VBA & Macros – Writing our First Macro
In this section, lets build our first macro. We will write a MakeMeRed() macro,
that paints red color in any selected cell(s). Like this,
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.
2.
Using Excel’s Built-in Macro Recorder
In order to write your first VBA program (or Macro), you need to know the
language first. This is where Excel’s tape recorder will help us.
Tape Recorder?!?
Yes. Excel has a built-in tape recorder, that listens and records everything you
do, in Excel’s own language, ie VBA.
Since we don’t 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.
Then right click on the rectangle shape and go to Assign Macro. And select the
MakeMeRed macro from the list shown. Click ok.