VBA Is Short For Visual Basic For Application.: History Hint

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. VBA is short for Visual Basic for Application.

History hint: Excel 5 was the first application on the market to feature Visual

Basic for Applications (VBA).VBA is best thought of as Microsofts common


application scripting language, and its included
with most Office 2010 applications and even in applications from other vendors.
Therefore, if you
master VBA by using Excel, youll be able to jump right in and write macros for
other Microsoft
(and some non-Microsoft) products. Even better, youll be able to create complete
solutions that
use features across various applications.
a. Following is a quick-and-dirty summary of what VBA is all about:
1. Code: You perform actions in VBA by executing VBA code. You write (or record)
VBA
code, which is stored in a VBA module.
2. Module: VBA modules are stored in an Excel workbook file, but you view or edit
a module
by using the Visual Basic Editor (VBE). A VBA module consists of procedures.
3. Procedures: A procedure is basically a unit of computer code that performs
some action.
VBA supports two types of procedures: Sub procedures and Function procedures.
4. Sub: A Sub procedure consists of a series of statements and can be executed in
a
number of ways. Heres an example of a simple Sub procedure called Test: This
procedure
calculates a simple sum and then displays the result in a message box.
Demo:
/*******************************
Sub Test()
Sum = 1 + 1
MsgBox The answer is & Sum
End Sub
/********************************
5. Function: A VBA module can also have Function procedures. A Function
procedure
returns a single value (or possibly an array). A Function can be called from
another VBA procedure or used in a worksheet formula. Heres an example of a
Function named AddTwo:
/*******************************
Function AddTwo(arg1, arg2)
AddTwo = arg1 + arg2
End Function
/*******************************
b. All about objects
Object

Workbook, Worksheet, Chart, Cell, Range, PivotTable, Shape, Application,..


Classes
In VBA, a class is defined in class module and serves as a template for an object
Object hierarchy
/*******************************
Application.Workbooks(Book1.xlsx).Worksheets(Sheet1).Range(A1)

/*******************************
Collections
Like objects form a collection.
Active objects
If not specified ,the active objects is enabled
Objects properties
/*******************************
Worksheets(Sheet1).Range(A1).Value

/*******************************
Objects Methods
/*******************************
Range(A1).ClearContents

/*******************************
Events
Some objects recognize specific events, and you can write VBA code that is executed
when the event occurs. For example, opening a workbook triggers a Workbook_
Open event. Changing a cell in a worksheet triggers a Worksheet_Change event.
b. The work space
VBE: Visual Basic Editor (Alt + F11 or Developer Ribbon->Visual Basic)

1. Project Explorer

Adding an module (Right click on Project->insert->Module) Rename it in Property


window.
2. Code window
In general, a code window can hold four types of code:
1. Sub procedures: A procedure is a set of instructions that performs some action.
2. Function procedures: A function is a set of instructions that returns a single value
or an
array (similar in concept to a worksheet function, such as SUM).
3.Property procedures: These are special procedures used in class modules.
4. Declarations: A declaration is information about a variable that you provide to
VBA. For
example, you can declare the data type for variables you plan to use.
Practice: write your first procedure
/*******************************

Sub SayHello()
Msg = "Is your name " & Application.UserName & "?"
Ans = MsgBox(Msg, vbYesNo)
If Ans = vbNo Then
MsgBox "Oh, never mind."
Else
MsgBox "I must be clairvoyant!"
End If
End Sub

/*******************************
Press F5 to run.

You might also like