I find Visual Basic for Applications (“VBA”) most useful in Excel. I have created many small enhancements to make my larger spreadsheets easier to use. Here follows a simple “Hello World!” introduction to VBA for Excel. In this exercise, we are simply going to have a pop-up message appear when we first open an Excel Workbook. This simple task will allow me to point out one or two important basics.
To access the VBA Editor you need to enable the Developer toolbar.
- Open Microsoft Excel
- Create a new, macro-enabled workbook: File » Save As » Excel Macro-Enabled Workbook (*.xlsm)
- Name the Workbook Hello World and save it.
- Navigate to the Developer menu and click on the Visual Basic button
- In the Project window on the left, double-left click on the This Workbook object
Option Explicitat the top
- At the top of the code window, select the Workbook option from the drop-down list (which currently displays (General))
- The default Procedure is Workbook_Open()
MsgBox "Hello World!"
- Save and close the Workbook
Option Explicit Private Sub Workbook_Open() MsgBox "Hello World!" End Sub
When you re-open the Workbook, click the Enable Content button and the message should appear in a pop-up.
You are now ready for the next step: VBA for Excel: add a little logic.