Saturday, September 15, 2012

How to Write a VBA Script


1. Open the Microsoft Office application that you want to write a script for. For example, open Microsoft Excel, Word or PowerPoint.
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.
3. Click on the 'Insert' tab and then click on 'Module' to open a blank window. This is where you will type your script.
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.
5. Press 'F5' to run the script.

Blogger news