How to Write a VBA Script

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

Visual Basic for Applications (VBA) is a programming language used in Microsoft Office applications like Excel and PowerPoint. You can use VBA to write scripts that will customize your Office applications to suit your needs. Although VBA was designed with the novice programmer in mind, writing your first script can seem like a daunting task. However, once you've navigated around the Visual Basic Editor (VBE), you'll have your program written in no time.


Step 1

Open the Microsoft Office application that you want to write a script for. For example, open Microsoft Excel, Word or PowerPoint.

Video of the Day

Step 2

Click on the "Developer" tab and then click on "Visual Basic." The Visual Basic Editor (VBE) screen will open. Alternatively, you can press the "Alt" and "F11" keys to open the VBE.


Step 3

Click on the "Insert" tab and then click on "Module" to open a blank window. This is where you will type your script.

Step 4

Type your script into the open window. You should start by typing "Function function name" if your script is a function or "Sub subroutine" if your script is a subroutine. Type the body of your script, then end with either "End Function" or "End Sub." Use a function if you want to return something (like a value) or a subroutine if your script performs a task without returning anything (like formatting a document). Microsoft's website offers an extensive help section for writing VBA code.


Step 5

Press "F5" to run the script.

Video of the Day




Report an Issue

screenshot of the current page

Screenshot loading...