You are on page 1of 13

By Rohit Hambar

What is VBA ? Macros Example

Visual Basic for Applications (VBA) is an implementation of Microsoft's event driven programming language Visual Basic 6 and its associated integrated development environment (IDE), which are built into most Microsoft Office applications. VBA enables building user defined functions, automating processes and accessing Windows API and other low-level functionality through dynamic-link libraries (DLLs).

It can be used to control many aspects of the host application, including manipulating user interface features, such as menus and toolbars, and working with custom user forms or dialog boxes. VBA can also be used to create import and export filters for various file formats, such as OpenDocument (ODF).

If you need to do repeated tasks in excel every day, then it is always recommend to automate such tasks so that you can save time and energy. Macros are a great way of automating tasks in Excel, we have already covered a basic tip on recording and using Macros. Macros basically add VB code to your excel sheet. Whenever you create a new Macro, Excel adds visual basic code at the back-end, so whenever you run a Macro, this VB code is executed and you get the results on the screen.

To open Google Search page in IE using VBA for input string. First thing to know is how to open IE .The best way is probably to create the object and make it visible. Like this:
Dim i As Long Dim IE As Object Dim objElement As Object Dim objCollection As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True

Navigating from webpage to webpage can be done like this:


IE.Navigate "http://www.google.co.in/"

Wait when IE is loading


IE.Busy Application.Wait DateAdd("s", 1, Now)

You may also want to manipulate some data while you're browsing the internet. This all has to be done by using HTML code.

Values are stored in a variety of elements in HTML. They can be in tables, textboxes, links, and others. Regardless of where the target value is, you need to know how to reference an HTML control. The most common way is probably by it's ID.
Find 2 input tags: ' 1. Text field ' <input type=hidden name="hl" value="en"> <input type=hidden name="output" value="search"> <input type=hidden name="sclient" value="psy-ab"> ' ' 2. Button ' <button id=gbqfb aria-label="Google Search" class=gbqfb name=btnG> <button id=gbqfba aria-label="Google Search" name=btnK class="gbqfb gbqfba">

How do I put a value into a texbox? Say you want to dynamically put a value in the Google search box. You would need to use this code.

Set objCollection = IE.document.getElementsByTagName("input") i=0 While i < objCollection.Length If objCollection(i).Name = "s" Then

' Set text for search objCollection(i).Value = "india"


Else If objCollection(i).Type = "submit" And _ objCollection(i).Name = "" Then ' "Google Search" button is found Set objElement = objCollection(i)

End If End If i=i+1 Wend


objElement.Click = i

Wait when IE is busy.


Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop ' Show IE IE.Visible = True

Clean objects which created.


' Clean up Set IE = Nothing Set objElement = Nothing Set objCollection = Nothing Application.StatusBar = "" End Sub

This example display Google Search results for input string India.

You might also like