Visual Basic for Applications (VBA) is Microsoft's attempt to create a common macro language for Windows products. VBA is based on the highly successful Visual Basic (VB) product. This second-generation version of Visual Basic is as easy to use as VB and shares most of its core features. Here are some of the shared features:
These features help make VBA a more robust language for shared code across multiple applications. However, VBA's ties to VB make it a mature language, even though it was first released in 1993.
![]()
For the first time ever, Microsoft has made it possible for any company to purchase the rights to include VBA (and the VBE environment) in that company's own applications. This means your current VB/VBA code can be ported to other applications in addition to those developed by Microsoft. Look for upcoming ads in your favorite computer magazines for new applications that will be using VBE/A.
Although VB and VBA are similar, they differ in some important ways. Perhaps the most important difference is the definition of the two languages. provides these definitions.
Table 25.1 - Defining VB and VBA
Language | Definition |
---|---|
Visual Basic | Beginning with Visual Basic 5.0, VB now contains a shared version of VBA 97. The most important feature of VB is that it enables you to create a stand-alone executable or automation server that you can distribute to users who do not own a Microsoft application. |
Visual Basic for Applications | Introduced in the fall of 1993 as a replacement for Microsoft Excel's XLM macro language, VBA 97 is now included in Access, Excel, PowerPoint, and Word. Additional products will also be released with VBA. The most significant difference between VBA and VB is that VBA requires the product in which code was developed in order to run. In addition, its form and control support is significantly different from the traditional versions you'll find in Visual Basic 5.0. |
All this is rather confusing, but there's a simple way to differentiate between VB and VBA: VB is a separate programming product that enables you to make executables, and VBA is a macro language for applications. As of VB 4.0, both products can share elements (type libraries) through the Object Browser.
Using Visual Basic for Applications
Starting with Visual Basic 5.0, VB has included all the features of VBA (in the form of a shared type library); code written in pure VBA can be shared across all products that use the VBA 97 type library. For example, the code in could appear in either VB or VBA.
Listing 25.1 - GREETINGS.TXT - The Greetings Routine Is Compatible with Both VB and VBA
Sub Greetings(YourName As String) Dim Reply As Integer Reply = MsgBox("Hello " & YourName & _ ", are you ready to compute?", vbQuestion + vbYesNo) If Reply = vbYes Then MsgBox "Well then, let's get busy!", vbInformation Else MsgBox "I'm sorry to hear that, but you must.", _ vbInformation End If End Sub
From looking at the code in Listing 25.1 in its editor, it is difficult to determine which language was used. shows an example of VBA in Microsoft Excel, and shows an example of Visual Basic 5.0. Unlike in the past, viewing the editor doesn't tell you right away which product is being used, because both VBA and VB share the same common user-interface: VBE.
Here is the VBA Code in Microsoft Excel 97.
The same code (as shown in Figure 25.1) in Visual Basic 5.0.
![]()
The Web site for this book contains an implemented sample of Greetings that will work with Access, Excel, PowerPoint, Word, and Visual Basic 5.0.