Anda di halaman 1dari 6

1

infotica
vbafaculty@gmail.com

Visual Basic for Applications (VBA)

VBA is based on Visual Basic, a programming language derived from BASIC. BASIC stands
for Beginners All-Purpose Symbolic Instruction Code. BASIC is designed to be user-friendly,
because it employs recognizable English words (or quasi-recognizable variations on them)
rather than abstruse and incomprehensible programming terms. In addition to its English-like
diction, BASICs designers endeavored to keep its punctuation and syntax as simple and natural
as possible.
Visual Basic is visual in that it offers drag-and-drop programming techniques and many
graphical elements.
Visual Basic for Applications consists of Visual Basic implementations that share a common
core of objects and commands, but also include application-specific objects and language
elements.

Visual Basic for Applications is a programming language created by Microsoft that can be built
into applications. You use VBA to automate operations in applications that support it. All the
main Office applications Word, Excel, PowerPoint, Outlook, Access, and Project support
VBA, so you can automate operations through most Office applications. In addition,
implementations of VBA can be found in some third-party applications, including Corels
WordPerfect and Autodesks AutoCAD.
VBA allows you to streamline many tasks, avoid burdensome repetition, and improve your
efficiency in many ways.
You can use VBA to automate those repetitive tasks you perform frequently. For example, if the
layout of your monthly report rarely changes, you can use VBA to set up your report each
month.

VBA is a programming language; however, you do not have to be a programmer to automate


the tasks you perform in Excel. You can also automate a task by using the macro recorder to
create a macro. A macro is a recording of the steps you want to automate. You just click a
button to turn on the macro recorder and begin performing the steps as you normally would.
Excel records each step and creates the VBA code. When you finish, you click the Stop Record
button. When you select your macro in the Macro dialog box and then click the Run button,
Excel plays back the steps you recorded. For example, if you record the steps necessary to set
up your monthly report, all you have to do each month thereafter is click a button and Excel
automatically sets up your report.
VBA Features:

Minimizes Manual Working Hours


Easy To Understand & Develop

infotica
vbafaculty@gmail.com

No Prior Programming Knowledge Is Reqd.


Bundled With Application Software Itself
Supports A Variety Of Functions
Tightly Integrated With Ms-Office
Auto Syntax Display

Automation Of Other Applications


Ado (Activex Data Objects Library) : Connect & Work With All Types Of Databases Like
Oracle,Sql Server,Mysql,Access Etc,
Auto Complete Word(Ctrl+Sp_Bar)
In-Built Interpreter (Automatic): Checks Each Line Of Code Automatically After Moving
From The Line
Object Browser (F2) : To Find Required Objects, Properties & Methods
Udf's (User Defined Functions)
Reusability, Accuracy & More Productivity
Vba Ide (Vbe) (Integrated Dev. Environment)
o Designing, Coding, Compiling, Debugging & Running
Supports Gui(Graphical User Interface) Programming (Userforms)
Make Use Of Dll's(Dynamic Link Libraries

infotica
vbafaculty@gmail.com

Enabling Developer Tab :


(From Excel 2007 onwards)

Click Office Button


Click Excel Options
Select 'Show Developer Tab In The Ribbon' option
Click OK

Opening Visual Basic Editor:

Click 'Visual Basic' button in the code group of the developer tab (Alt+F11)
Note : Alt+F11 is also used to switch between Excel and VB-Editor

Note : Alt+ Q closes the VBE (VB-Editor)

Macro Security :

Because of increasing problems with computer


viruses, specifically macro viruses, the default
Excel macro security setting disables all macros
when you open a workbook and enables you to decide on
a case-by-case basis whether you want to enable them.
This is true whether you created the macros or someone
else created them.
You can click 'Macro Security' option in the code group of Developer tab
to choose an option from the available.
You can change the Excel macro
security by choosing one of four options.
The Disable All Macros without Notification option
disables all macros. This option does not provide you
with any security alerts to let you know macros exist.
The Disable All Macros with Notification option is the
default setting. It notifies you if macros are present so
you can enable them on a case-by-case basis.
The Disable All Macros except Digitally Signed Macros
option disables all macros except those digitally signed by
a trusted publisher.
The Enable All Macros (Not Recommended; Potentially

infotica
vbafaculty@gmail.com

Dangerous Code Can Run) option enables you to run all


macros. Because potentially dangerous code can run,
Microsoft does not recommend this option.
Changes you make to macro security in Excel do not
change the macro security in other Office programs.

Recoding a macro:
Starting the Macro Recorder
Start the Macro Recorder by opening the Developer tab on the Ribbon, then clicking the Record
Macro button. Or click the Macro Record button on the status bar at the bottom of the application.
As soon as you start the Macro Recorder, the RecordMacro dialog box opens. You see a default
macro name (Macro1,Macro2, and so on) that you can accept or change. You can add a description
if you wish its optional.
In the Record Macro dialog box, enter a name for the macro youre about to record.
If desired, type a concise but helpful description in the Description box.
This is the Record Macro dialog box for Word.
You can also stop the Macro Recorder by clicking the blue button that appears
on the status bar during recording. Once the Recorder is stopped, the blue button is replaced with
an icon that will start recording a new macro.

Naming the Macro


Next, enter a name for the new macro in the Macro Name text box in the RecordMacro dialog box.
The name

Must start with a letter; after that, it can contain both letters and numbers.
Can be up to 255 characters long.
Can contain underscores, which are useful for separating words, such as File Save.
Cannot contain spaces, punctuation, or special characters, such as ! or *.

Assigning a Way to Run the Macro in Excel

To assign a Ctrl shortcut key to run the macro youre recording:


1. Start recording the macro, then press the key in the Shortcut Key text box. (Press the Shift
key if you want to include Shift in the shortcut key.)
2. In the Store Macro In dropdown list, specify where you want the Macro Recorder to store
the macro. Your choices are as follows:
ThisWorkbook stores the macro in the active workbook. This option is useful for macros
that belong to a particular workbook and do not need to be used elsewhere.
New Workbook causes Excel to create a new workbook for you and store the macro in
it. This option is useful for experimental macros that youll need to edit before unleashing
them on actual work.
Personal Macro Workbook stores the macro in the Personal Macro Workbook, a
special workbook named PERSONAL.XLSB. By keeping your macros and other

infotica
vbafaculty@gmail.com

customizations in the Personal Macro Workbook, you can make them available to any
of your procedures.
If the Personal Macro Workbook does not exist yet, the Macro Recorder
creates it automatically.
3. Click OK to start recording the macro.

Recording the Actions in the Macro


When you close the Record Macro dialog box, the Macro Recorder begins recording the macro.
For most applications, the Macro Recorder displays the Stop Recording button in the status bar at
the bottom of the screen (and a Stop Recording button on the Developer tab on the Ribbon).
Now you should perform the sequence of actions you want to record. What exactly you can do
varies from application to application, but in general, you can use the mouse to select items, make
choices in dialog boxes, and select defined items (such as cells in spreadsheets) in documents.

Do some actions.
To stop recording, click the Stop Recording button (or Stop Recorder, depending on the application),
or click the Stop Recording button on the status bar.

Running a Macro
Press Alt+F8 to display the Macros dialog box, select the macro, and then click the
Run button. (Alternatively, double-click the macro name in the list box.)

How to Stop an Executing Macro


To stop a running macro, press Ctrl+Break (Break is usually the un-Shifted Pause key on the
keyboard). VBA stops running the code and displays a dialog box telling you that code execution has
been interrupted. Click the End button to dismiss this dialog box.

Components in VBE :
Project Explorer
displays all open workbooks as vbaprojects.
MS-Excel Objects is the default category that contains all sheets & thisworkbook.
VBA Objects such as Modules, Userforms and Classmodules can be added/removed optionally
Note : Excel Objects can not be added/removed from this window directly
Immediate Window

infotica
vbafaculty@gmail.com

Allows you to:


Type or paste a line of code and press ENTER to run it.
Copy and paste the code from the Immediate window into the Code window but does not allow you to save
code in the Immediate window.
The Immediate window can be dragged and positioned anywhere on your screen unless you have made it a dockable
window from the Docking Tab of the Options dialog box.
You can close the window by clicking the Close box. If the Close box is not visible, double-click the Title bar to make the
Close box visible, then click it.

Properties Window

Lists the design-time properties for selected objects and their current settings. You can change these properties at design
time. When you select multiple controls, the Properties window contains a list of the properties common to all the selected
controls.

Window Elements
Object Box
Lists the currently selected object. Only objects from the active form are visible. If you select multiple objects, the
properties common to the objects and their settings, based on the first object selected, appear on the Properties List
tabs.
Properties List Tabs
Alphabetic Tab Alphabetically lists all properties for the selected object that can be changed at design time, as
well as their current settings. You can change the property setting by selecting the property name and typing or
selecting the new setting.
Categorized Tab Lists all properties for the selected object by category. For example, BackColor, Caption,
and ForeColor are in the Appearance category. You can collapse the list so that you see the categories or you
can expand a category to see the properties. When you expand or collapse the list, you see a plus (+) icon or
minus (-) icon to the left of the category name.

Anda mungkin juga menyukai