Excel information

Most Popular Excel Questions

Message Box to display a cell value when a criteria is met.

Posted by davesexcel on March 28, 2012 at 5:00 AM

When A1=>10, I want a message box to pop up telling me the value of A1 and then a yes or no button.

A1 is the Sun of B1:B3.


You can get the msgbox code straight from excels VBA help file.

The worksheet_Calculate event can be used in this situation.

Right click on the Sheet tab and select View Code,

Copy and paste the code there.

The code will only activate if A1 is 10 or greater.

Private Sub Worksheet_Calculate()

    If Range("A1") < 10 Then Exit Sub

    Dim Msg, Style, Title, Help, Ctxt, Response, MyString

    Msg = "A1 = " & Range("A1") & " , Do you want to continue ?"   ' Define message.

    Style = vbYesNo + vbInformation + vbDefaultButton2    ' Define buttons.

    Title = "Hello"    ' Define title.

    Help = "DEMO.HLP"    ' Define Help file.

    Ctxt = 1000    ' Define topic

    ' context.

    ' Display message.

    Response = MsgBox(Msg, Style, Title, Help, Ctxt)

    If Response = vbYes Then    ' User chose Yes.

        MsgBox "You selected Yes"    ' Perform some action.

    Else    ' User chose No.

        MsgBox "You selected No"    ' Perform some action.

    End If

End Sub

 Another Message Box example can be found here.



Categories: None

Post a Comment


Oops, you forgot something.


The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register