0

I've seen a lot of ways to do this but how I'm starting with vba I'd like to know all the ways to do this for after I can choose the way I liked the most.

I saw this example of code in Microsoft Oficcial page:

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue ?"    ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
Title = "MsgBox Demonstration"    ' Define title.
Help = "DEMO.HLP"    ' Define Help file.
Ctxt = 1000    ' Define topic context. 
        ' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)

I wanted to do the same but without variables, i mean, this:

MsgBox("Do you want to continue?", vbYesNo + vbCritical + vbDefaultButton2,"MsgBox Demostration ")

but this returns me an error, I think the example is doing exactly the same than me but without variables so I dont know why this doesn't work.

I saw another people doing this:

MsgBox("Do you want to continue?",msgboxstyle.vbYesNo,"tittle")

But this doesn't work for me either.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • 5
    Use parenthesys only if you are getting the return value, if not remove them. `x = MsgBox(...)` or `MsgBox ...` or `Call MsgBox(...)` are valid calling syntax, but not `MsgBox(...)` – Vincent G Aug 21 '19 at 10:18
  • You are not setting the return value from it to any variable. – Mikku Aug 21 '19 at 10:19
  • Possible duplicate of [Title of Message box not setting up](https://stackoverflow.com/q/30962795/11683) – GSerg Aug 21 '19 at 11:04
  • Your last example is VB.NET. It is a different language. – GSerg Aug 21 '19 at 11:06

1 Answers1

1

If you just want the message box to appear, you can do it like this:

Sub message()
x = MsgBox("Do you want to continue?", vbYesNo + vbCritical + vbDefaultButton2,"MsgBox Demostration ")
End Sub

In the Microsoft Official example you also have this part:

If Response = vbYes Then    ' User chose Yes.
    MyString = "Yes"    ' Perform some action.
Else    ' User chose No.
    MyString = "No"    ' Perform some action.
End If

If you want anything to happen when somebody clicks "yes" or "no", you need to include it here.

So for example, in your case:

Sub message()
If MsgBox("Do you want to continue?", vbYesNo + vbCritical + vbDefaultButton2,"MsgBox Demostration ") = vbYes Then
'any action you would like it to perform if yes
Else
'any action you would like it to perform if no
End If
End Sub
bajun65537
  • 498
  • 3
  • 14