Thursday, June 14, 2012

How to use Message Box (MsgBox) ?

MsgBox() is a very common function in VBA. It is used to display a user defined message on the screen.
We can use a MsgBox in different manners. Examples are as follow :

1 - Simple Message Box


A message box with a single argument is called Simple Message Box. We need to pass the message as a string in MsgBox() funtion. It displays the default Title and Button.

Code :
MsgBox "Welcome To VBA"


2 - Message Box with Title


A message box with an user-defined message and a title.


Code:
MsgBox "This is a message box with a title", "Message Box Title"


3 - Message Box with Title and Buttons


A message box with user defined message, title and buttons.

Code:



Private Sub btnHome_Click()
  Dim result As VbMsgBoxResult
  result = MsgBox("Do you want to navigate to Home sheet ?", vbQuestion + vbYesNo, "Switch to Home Sheet")
  If (result = vbYes) Then
    Sheets("Home").Activate
  End If
End Sub

No comments:

Post a Comment