Excel Msgbox Function

 

Excel MsgBox Function

 

 

The Excel MsgBox function is a useful function to use to present output messages to the user. It also allows you to obtain input from the user, which you can build into your function.

 

 

 

The Basics:

 

 

MsgBox(Message to the user, Buttons you require, Title for the message box, Help file index if you want tie the message to a help file)

 

 

Now for an example. Let’s assume we want to reverse a string on the active Excel worksheet. However we first want to ask the user if he/she wants to proceed with the execution of the code prior to running the code module.

 

 

 

Code


 

 Dim StringToProcess As String   'The variable that will contain the string to be evaluated

 

Dim Response As Long    'The variable to receive the user input

 

StringToProcess = ActiveSheet.Cells(2, 1).Value   'Assigns the value contained in the active cell to the string variable

 

 

Response = MsgBox("You are about to reverse the string. Are you sure you want to continue?", vbYesNo, "Question")

 

 

If Response = vbYes Then

 

 

StringToProcess = StrReverse(StringToProcess)

ActiveSheet.Cells(2, 2).Value = StringToProcess  ‘Output is written to a cell adjacent to the intput


End If


 

Output

 

 

 

Should our Excel user have selected the ‘No’ value then the code would not have executed.


 

 


 


 

 

 

 
Sitemap | Privacy | Terms
Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation.
Copyright 2011 ExcelHelp.com All rights reserved
More Info (+)