Visual Basic for Applications (VBA)Visual Basic for Applications (VBA) is an implementation of Microsoft's Visual Basic for use in Microsoft Office host applications such as Excel, Word, Outlook, and PowerPoint. This programming language is script like and enables users to write macros for automating tasks in Microsoft Office.
Launching the VBA Editor in ExcelTo access the VBA editor in Excel, simply follow the steps below:
1. Launch Microsoft Excel. Go to File->Options->Customize Ribbon.
2. Ensure that the Developer tab is enabled as shown in the screenshot below:
3. Next, click on the Developer tab followed by the "Visual Basic" button in the ribbon. You should see the Visual Basic for Applications editor.
4. Click on "Sheet 1 (Sheet 1)" and enter the following:
Sub HelloWorld() MsgBox "Hello World" End Sub
5. The VBA above creates a macro (subroutine) that displays a message box that shows "Hello World".
6. Go back to Excel, click on the Macros button. In the macro dialog, select "Sheet1.HelloWorld" and then click on the Run button. You should see the following output in Excel.
VBA Programming ConstructsThe following section focuses on commonly used Visual Basic for Applications (VBA) scripts/programming constructs in Microsoft Excel.
- For loop
- For Each loop
- Do While loop
- While Do loop
- If Then Else
- Excel Range
- Cells in Excel
- On Error Goto/Resume
- Copy Paste
- Cut Paste