Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text and a variable in a messagebox

I just need to know how to have plain text and a variable in a messagebox.

For example:

I can do this: MsgBox(variable)

And I can do this: MsgBox("Variable = ")

But I can't do this: MsgBox("Variable = " + variable)

like image 600
Mark Kramer Avatar asked Dec 25 '11 15:12

Mark Kramer


People also ask

How do you use variables in Messagebox?

with VBA, follow these steps: Create a message box with the MsgBox function (MsgBox(…)). Specify the buttons to be displayed in the message box (Buttons:=ButtonsExpression). Assign the value returned by the MsgBox function to a variable (Variable = MsgBox(…)).

How do I show a variable in VBA?

In the VBA editor go to the View menu and click on Locals Window. Each time F8 is pressed one line of VBA is executed. When the range MyRange is set, you can see that values appear for it in the Locals window. By examining the MyRange object you can drill down to see the values in it.


1 Answers

As has been suggested, using the string.format method is nice and simple and very readable.

In vb.net the " + " is used for addition and the " & " is used for string concatenation.

In your example:

MsgBox("Variable = " + variable)

becomes:

MsgBox("Variable = " & variable)

I may have been a bit quick answering this as it appears these operators can both be used for concatenation, but recommended use is the "&", source http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.100).aspx

maybe call

variable.ToString()

update:

Use string interpolation (vs2015 onwards I believe):

MsgBox($"Variable = {variable}")
like image 182
Ric Avatar answered Sep 23 '22 15:09

Ric