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
5 answers

IF statements in VBA

I am using an if statement which is giving the the error message End if without block if Ive tried placing the End if just before the sub and just after the if statement. I also tried placing 2 end if statements at the end of each IF statement. If…
user3013325
  • 211
  • 2
  • 4
  • 13
11
votes
2 answers

Snap ticks to decades

I have a chart with some data with a linear y-axis and a logarithmic x-axis. The question is about the logarithmic (x-)axis. I want the logarithmic ticks on the x-axis to align with exact decades (powers of 10), but I don't want the axis to…
brimborium
  • 9,362
  • 9
  • 48
  • 76
11
votes
3 answers

Close userform with escape button

I have 2 questions. When I pressed esc button then close Userform1 When I input open in TextBox1 then Userform2 should show. Also clear TextBox1 in Userform1 automatically. I have tried the below code: Private Sub TextBox1_KeyPress(ByVal KeyAscii…
Tomz
  • 139
  • 2
  • 3
  • 9
11
votes
6 answers

Excel Hide/Show all tabs on Ribbon except custom tab

How can I hide and show all the standard Excel ribbon tabs using VBA (not XML). I do not want to hide the whole ribbon (as is asked here: VBA minimize ribbon in Excel) just the tabs. I know how to use startFromScratch using XML so please do not…
Brett
  • 3,296
  • 5
  • 29
  • 45
11
votes
2 answers

VBA 03 - Application Path - get to Parent Folder

Application: Excel Left(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") - 1) I need to go back at least 2 Folders from the Workbook Path. I cannot use Paths like "C:/Folder1", because the Application will be moved multiple times.
Dennis
  • 183
  • 1
  • 1
  • 10
11
votes
4 answers

Using a wildcard to open an excel workbook

I want to use a wildcard to open a workbook stored in the same folder as my macro workbook. In the folder is a file named 302113-401yr-r01.xlsm. Here is my code: Workbooks.Open filename:=ActiveWorkbook.Path & "\302113*.xlsm" However, it tells me…
DanW
  • 303
  • 3
  • 4
  • 13
11
votes
11 answers

How to compare two entire rows in a sheet

I am new to VBA. I have job in my hand to improve performance of VBA code. To improve performance of the code, I have to read entire row and compare it with another row. Is there any way to do this in VBA? Pseudocode: sheet1_row1=read row1 from…
Vicky
  • 1,215
  • 6
  • 22
  • 45
11
votes
3 answers

Exporting Data into a CSV - Excel VBA

Say I have a function that generates some data into cells into the current worksheet like : Cells(1, "A").Value = ... Cells(2, "A").Value = ... Cells(3, "A").Value = ... Cells(4, "A").Value = ... Instead of the being the current worksheet in the…
Thatdude1
  • 905
  • 5
  • 18
  • 31
11
votes
8 answers

Save as CSV with semicolon separator

I'm currently use this function for saving, but I have a problem with it: Private Sub spara() ActiveWorkbook.SaveAs Filename:="T:\filepath+ ActiveWorkbook.Name", _ FileFormat:=xlCSV, CreateBackup:=False End Sub It automatically saves with ,…
saknar namn
  • 139
  • 1
  • 1
  • 10
11
votes
1 answer

Multiple input box excel VBA

Right now I've been using multiple Input boxes to get user input for a couple of strings i need: Dim U As String Dim P As String U = Application.InputBox("Enter Username", "Username", Type:=1)) P = Application.InputBox("Enter Pwd", "pwd",…
Thatdude1
  • 905
  • 5
  • 18
  • 31
11
votes
3 answers

C# like List in VBA

I'd like to create a List on VBA like you create on C#, there is any way I can do that? I looked for questions about it here on SO, but I could not find any.
Zignd
  • 6,896
  • 12
  • 40
  • 62
11
votes
5 answers

Use cell's color as condition in if statement (function)

I am trying to get a cell to perform a function based on the hilight color of a cell. Here is the function I currently…
1337Atreyu
  • 223
  • 2
  • 5
  • 12
11
votes
8 answers

Excel VBA Project has generated multiple Workbook objects

I am responsible for a very large Excel 2010 spreadsheet with links to all sorts of external data sources including Bloomberg, 65 worksheets with vba modules and references to other vba add-ins. I have noticed that the VBA project has acquired…
Alex Berry
  • 113
  • 1
  • 1
  • 4
11
votes
4 answers

Perform HTTP Post from within Excel and Parse Results

I have access to an API. The API takes an XML post as input and then returns an XML response with the relevant data. I want to Send the HTTP Post to the Server (Authentication and Request will be sent together) Receive the response (One of the…
Scott
  • 877
  • 4
  • 12
  • 24
11
votes
2 answers

Excel VBA function returning an array

Can you create an Excel VBA function that returns an array in the same manner as LINEST does, for example? I would to create one that, given a supplier code, returns a list of products for that supplier from a product-supplier table.
user220894
1 2 3
99
100