Questions tagged [vba]

Visual Basic for Applications (VBA) is an event-driven, object-oriented programming language for writing macros, used for the entire Office suite as well as other applications. VBA is not equivalent to VB.NET, or VBS; if you're working in Visual Studio use [vb.net]. If your question is specifically about programming any MS Office application, also use the appropriate tag: [excel], [ms-access], [ms-word], [outlook], [visio], or [ms-project].

Visual Basic for Applications (VBA) is an event-driven programming language which was first introduced by Microsoft in 1993 to give Excel 5.0 a more robust object-oriented language for writing macros and automating the use of Excel. It was introduced to Access, PowerPoint and Word in Office 97. The language and its runtime quickly matured and began being licensed and used in products beyond Microsoft Office applications.

Tag usage

  • VBA is not equivalent to VB.NET; if you're working in Visual Studio use .
  • If your question is specifically about programming Excel, Access, Word, Outlook, PowerPoint or Project, also use the appropriate application tag: , , , , or .
  • While code written in VBA is typically called a 'macro', the tag should not be used for general VBA questions. Please see the wiki entry page for more information about correct usage.
  • If your code is specific to an Office application (Excel, Word, Outlook, PowerPoint, etc.) then tag it with one of the application-specific tags found below, in addition to this one.
  • It is expected that questions tagged should either contain VBA code or ask how to use specific VBA code.

Overview

VBA derives from the classic Visual Basic programming language (version 5.0 and later 6.0) and uses a subset of that language's functionality. It is, however, totally dependent on the host application interface for execution and cannot run outside that environment. Unlike classic VB, VBA cannot create independent executables or DLLs. Visual Basic Libraries for interaction with Windows and its services (such as Printers or the Clipboard) are not part of VBA.

VBA 6 was shipped in 1998 and includes a myriad of licensed hosts, among them: Office 2000 - 2007, AutoCAD and PI Processbook. VBA 6 code will run equally well on any host, though the underlying objects native to each host will vary. Though VBA is still built into Microsoft Office applications, its "parent", classic VB, ceased to be an integral of part of Microsoft's development platform when Visual Basic .NET shipped with the first version of the .NET framework in 2002.

VBA has been rarely updated since only including new features to allow it to remain compatible with x64 versions of Windows and Office. Its application-specific object models are regularly supplemented to support new functionality added to the host Office applications. VBA 7 was released in 2010 to address the new 64-bit version of Microsoft Office, which shipped in 2010. There are several important changes made to VBA 7 that make it different from VBA 6, namely compatibility with both 32 and 64-bit versions of Office. Applications using VBA 7 must address both backwards compatibility and 64-bit-safe issues.

In some Starter editions of Office applications, the VBA Editor (VBE) is not available, and programming in VBA cannot be done. VBA support can also be locked out (not installed) by network administrators as a security measure.

It was removed from Office for Mac 2008, however, Microsoft returned VBA to Office 2011. Microsoft has continually been questioned about whether or not VBA will be removed altogether from Office and has repeatedly replied "no", the main reason being a large amount of legacy code in use.

This still holds true, even after the introduction of Visual Studio Tools for Office (VSTO) that allowed the use of C# and VB.NET to automate MS Office, and the JavaScript APIs in Office 2013, although VBA is supported only in the desktop application versions of Office.

VBA inherits much of its syntax from the BASIC programming language, where language features tend to be explicitly expressed in words, e.g. If ... Then ... End If, Function ... End Function. It also has many object-oriented features, such as classes and interfaces, and even has some dynamic features in Variant and Object types. Below is a simple subroutine, which generates a message box and prints a message to the Immediate window:

Sub HelloWorld()
    ' This is a comment
    Dim message As String
    message = "Hello World"
    MsgBox message
    Debug.Print "I just made a message box that says """ & message & """!"
End Sub

Related Tags:

Frequently Asked Questions:

Beginner Resources:

References:

Additional Reading:

General VBA Information and History:

211298 questions
11
votes
2 answers

How to get the MD5 hex hash for a file using VBA?

How can I get the MD5 hex hash for a file using VBA? I need a version that works for a file. Something as simple as this Python code: import hashlib def md5_for_file(fileLocation, block_size=2**20): f = open(fileLocation) md5 =…
aF.
  • 64,980
  • 43
  • 135
  • 198
11
votes
5 answers

Excel VBA Input box

I have following Input box for excel file. I don't want to show typing characters and need to show input box characters * , how to do this? Private Sub Workbook_Open() Dim UserName As String UserName = InputBox("Please Enter Your USER…
Hewage
  • 175
  • 2
  • 3
  • 12
11
votes
2 answers

How do you correctly set document properties using VBA?

The problem I'm having some trouble setting document properties using VBA in Word 2010. I have a document containing several Heading 1 sections and I use a macro to extract a selected section (along with it's contents) and paste it to a new…
David Gard
  • 11,225
  • 36
  • 115
  • 227
11
votes
1 answer

How do I save a workbook using xlwings?

I have an excel worksheet, some buttons and some macros. I use xlwings to make it work. Is there a way to save the workbook through xlwings ? I want to extract a specific sheet after doing an operation, but the saved sheet is the extracted sheet…
IordanouGiannis
  • 4,149
  • 16
  • 65
  • 99
11
votes
3 answers

Bug found while replacing text in formulas

I discovered a very interesting bug today i.e if it is a bug. Can you please confirm if you can replicate it? If it is a bug and has been not reported then I can file it as such. I am also ok if any of the Excel-MVPs want to file it as a bug. Let's…
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
11
votes
8 answers

Alternating coloring groups of rows in Excel

I have an Excel Spreadsheet like this id | data for id | more data for id id | data for id id | data for id | more data for id | even more data for id id | data for id | more data for id id | data for id id | data for id | more data…
Marijn Deé
  • 886
  • 2
  • 13
  • 21
11
votes
6 answers

Calling a Sub or Function contained in a module using "CallByName" in VB/VBA

It is easy to call a function inside a classModule using CallByName How about functions inside standard module? ''#inside class module ''#classModule name: clsExample Function classFunc1() MsgBox "I'm class module 1" End Function ''#…
Kratz
  • 766
  • 5
  • 10
  • 17
11
votes
2 answers

Save the cursor position in a document and return there later on

I have a macro that scans my document for Heading 1 styles, and consequently the cursor is moved to after the last match. I'm trying to capture the location of the cursor before this scan occurs, and then return to that position after it has…
David Gard
  • 11,225
  • 36
  • 115
  • 227
11
votes
2 answers

How not to count page numbers for hidden slides in PPT?

In presentation mode, I want only unhidden slides to appear with consecutive page numbers. How can I avoid that hidden slides are counted?
Maximilian Kohl
  • 632
  • 1
  • 9
  • 23
11
votes
2 answers

Excel VBA to open word template, populate, then save as .docx file somewhere else

I created a word template with placeholders such as <> that I am then able to replace automatically with my excel macro. When I tried this process again, the word document now opens saying it is a read only document. How am I supposed to save my…
Brian
  • 715
  • 4
  • 16
  • 37
11
votes
4 answers

Add new lines in VBA email

I'm trying to send an email automatically through Excel, but the new line commands aren't working! I've tried
, vbCrLf and vbNewLine .HTMLbody = "Hello" & vbNewLine & "Please find attached the above invoices and backup" & vbNewLine & _ …
DannyBland
  • 483
  • 3
  • 10
  • 26
11
votes
4 answers

vba Loop over a non-contiguous range

I have a non-contiguous range on rows (example address of myRange: $2:$2,$4:$205,$214:$214) and I would like to access a specific row and column within the range. I have tried the following: 'Get the value of the 2nd row, 1st column within the…
Jeffrey
  • 1,068
  • 2
  • 15
  • 25
11
votes
3 answers

Array as a Class Member

I'm designing a dynamic buffer for outgoing messages. The data structure takes the form of a queue of nodes that have a Byte Array buffer as a member. Unfortunately in VBA, Arrays cannot be public members of a class. For example, this is a no-no…
Blackhawk
  • 5,984
  • 4
  • 27
  • 56
11
votes
2 answers

Excel VBA Won't Keep Leading Zeroes

I can't seem to find a way to save leading zeros in my VBA code. The zeros are necessary since they correspond to unique IDs. I've tried changing the number format to text and 0000.... in excel and the same approach in my actual code: …
11
votes
2 answers

VBA difference between public variable and property

What is the difference between Public Variable As Integer and Private pVariable As Integer Public Property Let Variable(ByVal lVariable As Integer) pVariable = lVariable End Property Public Property Get Variable() Variable = pVariable End…
Toxiro
  • 528
  • 1
  • 3
  • 12