VBA MsgBox Ok Only – vbOKOnly Button

VBA-MsgBox-Ok-Only

VBA MsgBox Ok Only Message Box helps you to display a prompt with only OK button to the user. It is the default message box button and user can click on the Ok button to close the message box.

VBA MsgBox Ok Only

VBA MsgBox with OK Only button is very helpful to show the important message to the user. We can prompt the user with a message using VBA MsgBox with Ok button to get the user attention. And ask the user to press the Okay button to continue to the remaking process.

Usage

We can use Message Box with Ok button Option to show verity of messages to the user. You can display the message at beginning , in between or at end of a sub procedure.

  • Message box with Ok button is useful to show some message to the user.
  • We can notify the user about the result of a specific procedure
  • Helps to display the error value or error number to the user
  • Progress of the current process can be shown to the user
  • Display the if the macro is successfully created the output
  • VBA Developers can use the Ok Only message box to display the completion of the process, once the macro is successfully done the given task.

Syntax

Here is the VBA syntax of the Message Box to display with Ok Only button. You can pass the vbOKOnly constant for the button argument of the VBA MsgBox Function.

MsgBox (prompt, [ buttons:= vbOKOnly, ] [ title, ] [ helpfilecontext ])

You can also use VBA MsgBox Enumeration Value for vbOKOnly constant as shown below:

MsgBox (prompt, [ buttons:= 0, ] [ title, ] [ helpfilecontext ])

Here 0 is the enumeration value of the vbOKOnly message box constant

Examples

Following is the Example VBA Code to display a message box with OK button. The example below shows the VBA MsgBox with OK button using VBA constant and ENUM value.

1. vbOKOnly Constant: The following example is the VBA code using VBA constant to display a message box with Ok button only.

Sub VBA_MsgBox_vbOKOnly_Button_Constant()
MsgBox "Your Prompt and message to display in the Message Box", vbOKOnly, "Titile of the Message Box"
End Sub

Here, we have provided the second argument buttons as vbOKOnly constant to display the message with Ok button.

2. vbOKOnly ENUM: Here is the VBA Macro using VBA Message Box Enumeration Value for Ok button. 0 is the Enum value of vbOKOnly MsgBox constant.

Sub VBA_MsgBox_vbOKOnly_Button_Enum()
MsgBox "Your Prompt and message to display in the Message Box", 0, "Titile of the Message Box"
End Sub

3. Message Box at End of the Process (Real-time scenario): Most of the developers use the Message box with Ok button to display the message to the user at end of the process.

Sub VBA_MsgBox_vbOKOnly_Button_CompletedMessage()
MsgBox "Given Task is completed Successfully", vbOKOnly, "Done!"
End Sub

4. Display Message before continuing the Process (Real-time scenario): You can inform the user about any important information before Start running the Actual Procedure.

Sub VBA_MsgBox_vbOKOnly_Button_BeforeStartingTheProcessing()
MsgBox "This Process May take 30 minutes to finish, Please do not close the Excel File", vbOKOnly, "Please Note!"
End Sub

5. Show the Result of an expression (Real-time scenario): We can use the Ok button message box to display the result of the calculations.

Sub VBA_MsgBox_vbOKOnly_Button_ToDisplayResult()
Dim intQuntity As Integer
Dim intPrice As Integer

intQuntity = Range("A1")
intPrice = Range("B1")
MsgBox "Total Value of the Products = " & intQuntity * intPrice, vbOKOnly, "Please Note!"
End Sub

Instructions

Here are the simple step by step instructions to use and run the VBA MsgBox OK button Macros. You can copy entire procedure or a statement to insert into your existing procedure.

  • Open the VBA Editor by pressing Alt+F11 or from Developer Tab of the Ribbon.
  • Insert a new Code Module from Insert menu of VBE
  • Copy the VBA MsgBox OK Only Code and Paste in the Module
  • Press F5 or Click on the Run Sub (green button) to execute the Macro
  • Output: You will see a Message Box with OK button

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment