Thursday, September 1, 2011

Message and Input Boxes (MsgBox, InputBox) in Excel


In VBA for Excel the message box (MsgBox) is the primary tool to interact with the user. For example you might want to tell the user that a long macro has finished running.

Step 1: Open a new workbook and use the ALT/F11 keys to move to the Visual Basic Editor.

Step 2: Copy/Paste the following macro from here into the code window of any sheet.

Sub InputBoxTest()
Sheets("Sheet1").Select
Range("A1").Value = 695
MsgBox "The macro has finished running!"
End Sub

Notice the space following MsgBox and the use of quotation marks surrounding the text

Step 3: Use the ALT/F11 keys to go back to Excel and run the macro InputBoxTest.

The value 695 is entered in cell A1 and the following message box appears.

Step 4: Delete the macro in the Visual Basic Editor and the value 695 from cell A1

No comments: