Thursday, February 19, 2015

VBA Code to show MsgBox in MS Excel

In this tutorial we are going to explain you how you show the MsgBox in Microsoft Excel using the VBA Code. The MsgBox is a dialog box in Excel VBA, you can use To inform the users of your program. Place a command button On your worksheet and add the following code lines: 

  1. Code To show a simple message in the MsgBox:
  2.     MsgBox "This Is fun"
    

    Result when you click the command button On the sheet: 

    msg box image

  3. Code To show a little more advanced message. First, enter a number into cell A1 And Then run the below code:
  4.     MsgBox "Entered value Is " & Range("A1").Value
    
    Result when you click the command button On the sheet:



    msg box with value image


    Note: we used the & operator To concatenate (join) two strings. Although Range("A1").value Is Not a string, it works here.

  5. To start a new line in a message, use vbNewLine.
  6.     MsgBox "Line 1" & vbNewLine & "Line 2"
    
    Result when you click the command button On the sheet:
msg box in multiple lines image


No comments:

Post a Comment