Echo Function

There's no Echo function in VBA. There are, however, Debug.Print and MsgBox functions which do the following:

From Excel Help:

  • Debug.Print – Prints text in the Immediate window.
  • MsgBox – Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.

For a MsgBox alternative, see Custom VBA Message Box and A VBA MsgBox Replacement.

I've written my own Echo function that lets you specify where you want the output to go. Just place the following into a standard module (being sure to place the Enum section at the top, or in its own module).

Public Enum outputType
  ImmediateWindow
  MessageBox
End Enum

Function Echo(inputString As String, outputString As outputType) As String

  Select Case outputString
    Case 0  ' debug.print
      Debug.Print inputString
    Case 1  ' msgbox
      MsgBox inputString
  End Select

End Function

Sample usage

The following sample procedure displays a message box with "Hello World!" in it, then displays "Hello World!" in the Immediate Window.

Sub TestEcho()

Echo "Hello World!", MessageBox
Echo "Hello World!", ImmediateWindow

End Sub

Suppose you want to display text as message boxes, then switch to Debug.Print for the production copy. The calls to the Echo function would specify MessageBox as the output type. Before releasing the app, just switch the order of MessageBox and ImmediateWindow in the Enum section. All existing Echo calls would output to the Immediate Window instead. Of course, that would require you to remember what you did.

Site last updated: April 19, 2014

Peltier Tech Charting Utilities for Excel