Hello World with Microsoft Excel VBA

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.

  1. Open Microsoft Excel
  2. Create a new, macro-enabled workbook: File » Save As » Excel Macro-Enabled Workbook (*.xlsm)
  3. Name the Workbook Hello World and save it.
  4. Navigate to the Developer menu and click on the Visual Basic button
  5. In the Project window on the left, double-left click on the This Workbook object
  6. Type Option Explicit at the top
  7. At the top of the code window, select the Workbook option from the drop-down list (which currently displays (General))
  8. The default Procedure is Workbook_Open()
  9. Type: MsgBox "Hello World!"
  10. 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.

By foxbeefly

PHP / MySQL Developer. HTML, CSS and some JavaScript.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.