MsgBox Help

M

millwalll

Hi all need some help I have got message box the code for the box is

MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially")

And I want to add a Vbokonly and vbinformation to it so it only display a ok
button and the information image.
(vbOKOnly + vbInformation)

The code I thought would be somthing like this.
MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially",vbOKOnly + vbInformation)

But this is not working so any help or advice thanks all :)
 
M

millwalll

millwalll said:
Hi all need some help I have got message box the code for the box is

MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially")

And I want to add a Vbokonly and vbinformation to it so it only display a ok
button and the information image.
(vbOKOnly + vbInformation)

The code I thought would be somthing like this.
MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially",vbOKOnly + vbInformation)

But this is not working so any help or advice thanks all :)

p.s is there way to change the msgbox name at mins is says microsoft excel ??
 
D

Dave Peterson

MsgBox Prompt:=Sheet2.Range("a2").Value _
& " has the most Expensive Phone to buy Initially", _
buttons:=vbOKOnly + vbInformation, _
title:="what's the title"

Check VBA's help for more options.

ps. If you want to add a new line:

MsgBox Prompt:=Sheet2.Range("a2").Value & vbnewline _
& " has the most Expensive Phone to buy Initially", _
buttons:=vbOKOnly + vbInformation, _
title:="what's the title"
 
P

Per Jessen

Hi all need some help I have got message box the code for the box is

MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially")

And I want to add a Vbokonly and vbinformation to it so it only display a ok
button and the information image.
(vbOKOnly + vbInformation)

The code I thought would be somthing like this.
MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially",vbOKOnly + vbInformation)

But this is not working so any help or advice thanks all :)

msg = MsgBox(Sheet2.Range("a2").Value & " has the most Expensive Phone
to buy Initially ", vbOKOnly + vbInformation)
 
S

Steve Yandl

MsgBox Sheet2.Range("A2").Value _
& " has the most Expensive Phone to buy Initially", 64, "Cool Title"


Steve Yandl
 
R

Rick Rothstein \(MVP - VB\)

Hi all need some help I have got message box the code for the box is
MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially")

And I want to add a Vbokonly and vbinformation to it so it only display a
ok
button and the information image.
(vbOKOnly + vbInformation)

The code I thought would be somthing like this.
MsgBox (Sheet2.Range("a2").Value & " has the most Expensive Phone to buy
Initially",vbOKOnly + vbInformation)

But this is not working so any help or advice thanks all :)

As others have shown without specifically pointing to the reason, your
problem stems from the parentheses. MsgBox is a function and when used to
return a value, such as like this...

Answer = MsgBox("Here is a question", vbYesNo Or vbQuestion)

the parentheses are required. When no value is returned, the function
effectively becomes a subroutine. There are two proper ways to call a
subroutine...

Call MsgBox("Here is a question", vbYesNo Or vbQuestion)

and

MsgBox "Here is a question", vbYesNo Or vbQuestion

Note that when Call is used, the parentheses are required; however, when the
Call keyword is omitted, the parentheses are syntactically incorrect. So you
are probably asking, "Why did it work when I didn't add the 2nd argument?"
Good question. It seems that when parentheses are used in a statement that
are not required by syntax, VB assumes it has an expression to evaluate and,
well, it attempts to evaluate the contents of the parentheses. When you
specified only a single text argument, VB could evaluate it... it simply
evaluates as itself. But, when you added the 2nd argument, VB has no
mechanism to evaluate two expressions without an operator of some kind
between them (the comma is not an expression operator), so it generated an
error. The bottom line is to use parentheses in a subroutine statement
**only** when they are required by syntax (do not use them to "pretty"
things up as doing that will create problems in more situations than the one
I just outlined).

Rick
 
M

millwalll

Rick Rothstein (MVP - VB) said:
As others have shown without specifically pointing to the reason, your
problem stems from the parentheses. MsgBox is a function and when used to
return a value, such as like this...

Answer = MsgBox("Here is a question", vbYesNo Or vbQuestion)

the parentheses are required. When no value is returned, the function
effectively becomes a subroutine. There are two proper ways to call a
subroutine...

Call MsgBox("Here is a question", vbYesNo Or vbQuestion)

and

MsgBox "Here is a question", vbYesNo Or vbQuestion

Note that when Call is used, the parentheses are required; however, when the
Call keyword is omitted, the parentheses are syntactically incorrect. So you
are probably asking, "Why did it work when I didn't add the 2nd argument?"
Good question. It seems that when parentheses are used in a statement that
are not required by syntax, VB assumes it has an expression to evaluate and,
well, it attempts to evaluate the contents of the parentheses. When you
specified only a single text argument, VB could evaluate it... it simply
evaluates as itself. But, when you added the 2nd argument, VB has no
mechanism to evaluate two expressions without an operator of some kind
between them (the comma is not an expression operator), so it generated an
error. The bottom line is to use parentheses in a subroutine statement
**only** when they are required by syntax (do not use them to "pretty"
things up as doing that will create problems in more situations than the one
I just outlined).

Rick

Thanks all :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top