Sunday, September 16, 2012

Visual Basics for Applications

If you have studied computer science in your academics then you will be aware of Visual Basic. Visual Basic is a programming language used in Microsoft Visual Studio for developing software applications. There are many versions of Visual Basic. We normally call it as VB script. Its is used in both stand alone and web based applications. Stand alone application is a software which is stored and executed in a local system. Web Based application is a software which is stored in remote servers and runs on a internet browser. VB is equally wast as other computer languages like C, C++ and a very interesting, easy language to learn.

A typical VB script looks like:

Main Form code
Button1.Name = "Click Here"
* here we specify all the design properties. How a window should look like, how many command
buttons it should have, what are all the lables it should have , etc*
End of main form code

Private Sub FuncName( ByVal Name as String )
Label1.Caption = "Your name"
Label2.Caption = Name
End Sub

Sub "FuncName" is the Sub routine which is similar to a typical C, C++ void function. Main difference is it will not Return Values. No need to specify parenthesis while calling a sub.

VB also has Functions which can return the values.

Private Function sum(ByVal a as integer, ByVal b as integer)
End Function

In above function the value is returned by assigning the return value the name of function. This is similar ti user defined functions in C, C++.

Above Sub is called as below:

Private Sub Button1_Click()
FuncName "John"
End Sub

Above Function is called as below:

Private Sub Button2_Click()   - This will get called when the button is clicked.
Dim total as Integer
msgbox total
End Sub

Above we have seen the code written for a click event. Normally it is left mouse click. Similarly we can write code for every event that includes keyboard events, mouse events, etc.

Now lets know About VBA

VBA is a version of VB script used in MS excel for writing Macros.

What is a Macro?

Generally in Micro Processors Macro is a piece of code which is written to reuse multiple times in a module. Procedures are also for the same purpose, but there is a huge difference between procedures, functions and macros. Procedures, Macros, Functions these are mainly used for modularity, re usability and memory efficiency purposes. Macros are written to perform a task. Whenever that task needs to be performed, we can call it required number of times. Whenever a macro is called, the call is replaced by the code written for that macro. This replacement happens during level 1 parsing of code. Where as whenever a  Procedure or a Function is called, execution jumps to the place where the procedure or function is written and again jumps back when the execution of procedure or function is complete. So it is cumbersome for the processor in second case. Benefit of using procedure or function is it saves lot of memory while degrading the execution speed. Macros will eat up lots of memory while execution speed will be good as the execution is contiguous.

What is a VBA Macro ?

VBA means Visual Basic for Applications. This terminology is used when the VB script is used in MS excel. In MS Excel we store the data in row's and column's. We call it as spreadsheets. In some cases we need to perform some calculations on the data present in the sheet and in other cases we need to extract only required portion of data from sheets containing large amount of data. Normally we perform calculations by storing formulas in formula bar for specific Cells and we extract data by copy paste operations. Doing these operation multiple times will consume lot of time and it leads to human errors most of the times. Hence we write a piece of code that does all the actions multiple times accurately. That piece of code is called VBA Macro in MS Excel.

So the meaning of Macro is similar in all subjects and it is used in different ways.

How does a typical VBA Macro look like ?

Sub Macro1()
With Activesheet
End With
End Sub

In above macro "Macro1" is the name of the Macro.
Activesheet will point to the tab which is currently open.
Everything in Excel is Cells. It is a part of sheet defined by row and column. 5 is row and 1 is column.
"With" block is used to do many operations for a single context.
All the loops of programming such as for,while,do while and if conditions are possible in VBA.

Same VB script is used in Excel to write Macros. But the difference in VBA is there are many excel specific functions like Activesheet, Worksheets etc. These excel specific functions are invoked inside VB script.

This is a overview of VBA. You will find many sources for detail information about writing macros in VBA. If you have any errors or questions regarding VBA, just post a comment and I will try solving it for you...:)

                           *Now Do you know what is VBA..??*

No comments:

Post a Comment