Output to a dialog

Tags
Summary

Use a MsgBox to show a dialog box.

Situation

You want to show a message to a user. Could be a variable's value, an error message, or something else.

Needs

A message to show. Might include a variable's value.

Provides

Happy user.

Action

Use the MsgBox function.

  • MsgBox THE MESSAGE

THE MESSAGE is a string, or a number.

Explanation

Example:

  • MsgBox sAverage

Use a string expression. & is concatenation, meaning "stick things together":

  • MsgBox "The average is " & sAverage & "."

You can put the message into a variable first, and then show it.

  • tMessage = "The average is " & sAverage & "."
  • MsgBox tMessage

That often works well with Ifs.

  • If sCount = 0 Then
  •     tMessage = "No data. Cannot compute an average."
  • Else
  •     tMessage = "The average is " & sTotal / sCount & "."
  • End If
  • MsgBox tMessage

You can break the message across multiple lines.

  • MsgBox "First name: " & tGoatFirstName & vbCrLf & "Last name: " & tGoatLastName