As its name suggests, VBA is closely related to Visual Basic, but can normally only run code within a host application rather than as a standalone application. It can, however, be used to control one application from another using OLE Automation. For example, it is used to automatically create a Word report from Excel data, in turn automatically collected by Excel from polled observation sensors.
VBA is functionally rich and extremely flexible but it does have some important limitations, including limited support for function pointers which are used as callback functions in the Windows API. It has the ability to use (but not create) (ActiveX/COM) DLLs, and later versions add support for class modules.
.xls) independent of the document streams. The intermediate code is then executed by a virtual machine (hosted by the hosting application). Despite its resemblance to many old BASIC dialects (particularly Microsoft BASIC, from which it is indirectly derived), VBA is not compatible with any of them except Microsoft Visual Basic, where source-code of VBA Modules can be directly imported, and which shares the same library and virtual machine. Compatibility ends with Visual Basic version 6; VBA is not compatible with VB.NET. VBA is proprietary to Microsoft and, apart from the COM interface, is not an open standard.
Interaction with the host application uses OLE Automation. Typically, the host application provides a type library and API documentation which document how VBA programs can interact with the application. This documentation can be examined from inside the VBA development environment using its Object Browser.
VBA programs which are written to use the OLE Automation interface of one application cannot be used to automate a different application, even if that application hosts the Visual Basic runtime, because the OLE Automation interfaces will be different. For example, a VBA program written to automate Microsoft Word cannot be used with a different word processor, even if that word processor hosts VBA.
Conversely, multiple applications can be automated from the one host by creating Application objects within the VBA code. References to the different libraries must be created within the VBA client before any of the methods, objects, etc. become available to use in the application. These application objects create the OLE link to the application when they are first created. Commands to the different applications must be done explicitly through these application objects in order to work correctly.
For example: In Microsoft Access, users automatically have access to the Access library. References to the Excel, Word, and Microsoft Outlook libraries can also be created. This will allow creating an application that runs a query in Access, exports the results to Excel, formats the text, then writes a Mail merge document in Word that it automatically e-mails to each member of the original query through Outlook. (In this example, it is important to note that Microsoft Outlook contains a security feature that forces a user to allow, disallow, or cancel an e-mail being sent through an automated process with a forced 5 second wait. Information on this can be found at the Microsoft website.)
VBA programs can be attached to a menu button, a macro, a keyboard shortcut, or an OLE/COM event, such as the opening of a document in the application. The language also provides a user interface in the form of UserForms, which can host ActiveX controls for added functionality.
Like any common programming language, VBA macros can be created with a malicious intent. Using VBA, most of the security features lie in the hands of the user, not the author. The VBA 'host-application' options are accessible to the user. The user who runs any document containing VBA macros can preset the software with user preferences, much like those for internet browsers. End-users can protect themselves from attack by disabling macros from running in an application if they do not intend to use documents containing them, or only grant permission for a document to run VBA code if they are sure the source of the document can be trusted.
VBA is useful for automating database tasks such as traversing a table:
VBA is useful for automating repeated actions in rows of a spreadsheet. For example, using the following code example, the built-in iterative solver Goal Seek is applied automatically to each row in a column array, avoiding repeated use of manual menu entry. Below a column variable "C_M" determines the values of another column variable "Target" in some nonlinear fashion. The built-in nonlinear solver Goal Seek is called to find the value of "C_M" that brings "Target" to value one. The subroutine is inserted into the workbook using the VBA editor and command Insert Module. It is called directly from the VBA editor, or by using a "hot key" or keyboard shortcut. Values on the spreadsheet automatically update as the rows are scanned.
It is useful to note that subroutines have the power to update variables on the spreadsheet; functions do not - they simply report their evaluation.
Line Option Explicit is not part of the subroutine: it sets a compiler option that forces identification of all variables that have not been specified in Dim statements, which avoids possible knotty debugging problems that can arise due to typos. Notation (' ) in the following code denotes a comment, and (_) line continuation. The code uses NAMED variables: a form of cell reference in which cells are assigned names of user choice, rather than the standard cell designation referring to specific row and column numbers. Naming is accomplished on the worksheet using the Excel "Name Manager", or menu Insert Name: Create.
In the example below VBA is used to get an array from a cell range, manipulate the array, and then set the values back in a different range. This works many times faster than directly setting the cell values one-by-one.
VBA can be used to create a user defined function (UDF) for use in a Microsoft Excel workbook:
Example of how to add an external application object (The user must have the application library referenced in the application before this):
As of July 1, 2007, Microsoft no longer offers VBA distribution licenses to new customers. Microsoft intended to replace VBA with .NET-based languages ever since the release of the .NET Framework. The .NET Framework versions 1.0 and 1.1 included a scripting runtime technology known as Script for the .NET Framework . Also, Visual Studio .NET 2002 and 2003 SDK contained a separate scripting IDE called Visual Studio for Applications (VSA) that supported VB.NET . One of its significant features was that the interfaces to the technology were also available via Active Scripting (VBScript and JScript), allowing even .NET-unaware applications to be scripted using .NET languages. However, VSA was deprecated in version 2.0 of the .NET Framework , leaving no clear upgrade path for applications desiring Active Scripting support (although "scripts" can be created in C#, VBScript, and other .NET languages, which can be compiled and executed at run-time via libraries installed as part of the standard .NET runtime).
Support for VBA in the Mac OS X version of Microsoft Office was dropped with the release of Microsoft Office 2008 for Mac. . The official reason given was that VBA relied heavily on machine code written for the PowerPC architecture, and that rewriting this code for dual PowerPC/Intel architectures would have added another 2 years to the development of the suite. However, the office suite can to an extent be automated using AppleScript. In a press statement released on May 13, 2008, Microsoft's Macintosh Business Unit (Mac BU) announced that VBA will be returning in the next version of Office for Mac. Microsoft has also clearly stated that they have no plans to remove VBA from the Windows version of Office.
On 1st September 2008, a UK based firm, iTech Masters, released a library for VBA6 that adds global error handling possibilities and also allows programmatic access to the VBA call stack at runtime.
With the release of Visual Studio 2005, Microsoft announced Visual Studio Tools for Applications (VSTA), an application customization toolkit based on the .NET Framework 2.0 and built on the same architecture as Visual Studio Tools for Office (VSTO). Some of the technology developed for VSA was incorporated within VSTA. VSTA consists of an SDK and a customized developer IDE, based on the Visual Studio 2005 IDE, and a runtime that can be embedded in applications to expose its features via the .NET object model. It also includes an end-user IDE incorporating Visual Basic .NET and C#. VSTA also features 64-bit support, macro recoding and other usual Visual Studio 2005 IDE features, but does not incorporate Active Scripting support.
The first CTP was released in April 2006 and version 1.0 was released to manufacturing along with Office 2007 . It is included with Office 2007, and the SDK is available separately. VSTA is licensed from Microsoft depending on the usage scenarios for redistribution with applications. Office 2007 applications continue to integrate with VBA, except for InfoPath 2007 which integrates with VSTA.
The next version of VSTA, based on Visual Studio 2008 will be released in mid-2008 . The second version of VSTA will be significantly different from the first version, including features such as dynamic programing and support for WPF, WCF, WF, LINQ, and .NET 3.5. A beta release is currently available, titled Visual Studio Tools For Applications 2.0 .
Visual Basic is a landmark for Windows would-be programmers. (Microsoft's program development software for Windows)(includes a related article on follow-on products for Visual Basic) (Software Review) (evaluation)
Oct 01, 1991; IBM In the weeks following its announcement, Microsoft's Visual Basic programming language got more ecstatic press than any other...
Visual Basic with Professional Toolkit. (Microsoft Corp.'s program development software for Microsoft Windows) (Software Review) (Paul Bonner on Windows) (Evaluation)
Jul 01, 1992; Microsoft Visual Basic has proven to be a remarkable programming tool. Since it was released in May 1991, hundreds of thousands...