does msgbox return value?

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I trying to understand how to call functions which return value and that which do not. I thought msgbox is a function that return value, so I tried to assign that value to a variable x but when I msgbox out x the value of x was 1? Any idea why. Thank you so much.

Code:
Sub wk_open()
    Dim x As Integer
    x = MsgBox(5)
    MsgBox (x)
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The value returned by message box is based on the button pressed. A value of 1 is vbOK, vbCancel is equal to 2 and so on. You can refer to the value or the constant name. A quick example...

Code:
Dim LResponse As Integer

LResponse = MsgBox("Do you wish to continue?", vbYesNo, "Continue")

If LResponse = vbYes Then
   {...statements...}
Else
   {...statements...}
End If

Edit: Use "Input" rather than "Msgbox" if you want a custom user input.
 
Last edited:
Upvote 0
You can also use TextBox to return values that can be input by users. Or even more sophisticated ComboBox.

BTW, MsgBox is not a function but is a application dialog box call.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top