VbaFunctionsBeginner

MsgBox

Displays a dialog box and can return which button the user clicked.

Review the syntaxStudy the examplesOpen the coding app
response = MsgBox(prompt, buttons, title)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Displays a dialog box and can return which button the user clicked.

MsgBox is useful for simple confirmations, warnings, and quick diagnostics while building workbook automation. It is convenient for low-friction user prompts, but it becomes a poor choice once a macro needs structured forms or repeated input.

Quick reference

Syntax

response = MsgBox(prompt, buttons, title)

Inputs

Parameters

promptString · Message shown to the user.
buttons (optional)VbMsgBoxStyle · Optional button and icon combination.
title (optional)String · Optional dialog title.

See it in practice

Examples

1

Confirm before overwriting a report

If MsgBox("Overwrite the existing report?", vbYesNo + vbQuestion, "Weekly refresh") = vbYes Then
    Call RefreshReport
End If

Use the return value to gate destructive workbook actions.

Debug faster

Common Errors

1

LogicError

Cause: Showing a prompt but ignoring the returned button value.

Fix: Store or compare the return value when the user's choice matters.

Runtime support

Compatibility

Excel desktop VBA

Source: Microsoft Learn Office VBA reference

Common questions

Frequently Asked Questions

Displays a dialog box and can return which button the user clicked.

prompt: Message shown to the user. buttons: Optional button and icon combination. title: Optional dialog title.

LogicError: Store or compare the return value when the user's choice matters.