Saturday, November 14, 2009

VBA grammar.......(Excel Macro) Part I

Today we are going to discuss about Visual Basic Grammar. I called it VBA Grammar.

Objects:-

Visual Basic is an object-oriented language. This means that all the items in Excel are thought of as objects. There are more than a hundred of them.
Few Examples of objects are:
The Excel application(the largest object)
A Workbook
A worksheet
A Range
A Chart

A object is just like as noun. A object can contain other objects.

Few Examples.
Application.Workbooks("myexcel.xls")
The above line refer the workbook by name.

Application.workbooks("myexcel.xls").worksheets("sheet1").range("A2")
The above line refer the range of the sheet1 from the workbook "myexcel.xls".

Sheets("Mysheet") refers to the sheet called Mysheet.
Charts(1) refers to the first chart sheet on the tab bar.


Methods:-

Objects have methods that perform actions on them.
Methods are just like the verb of English Grammar.
We can think about it just like

Noun.Verb
Object.Method

If you were considering the Range object, then examples of methods would be:
Activate
Clear
Copy
Cut
Delete
Select

Few Examples

Sheet1.select
Range("B#").select
Selection.copy



Properties

Each object has its own characteristics. In general, properties control the appearance of objects.

Properties are just like Adjective of English Grammar.
Noun.Adjective = value

Object.Property = value

One thing must be remember that each object has its own set of methods and properties.


Few Examples

Range("C3").ColumnWidth = 14
sets the column width of cell C3 to 14.(Excel's default column width is 8.43 characters.)

Range("A1").columnwidth = Range("C3").columnwidth + 5
takes the value of the column width of cell C3. Adds 5 to it. and then assigns that value to the column width of cell A1.

So this three are the most important things in VBA Grammar.
Object
Methods
Property

And the other important things which are left for discuss they are

Variables
Dim
Constants
Arrays
Using Set

No comments:

Subscribe Now: From your browser

Search:

Learn Office Excel