The sprintf() family is popular across many programming languages for conveniently displaying information in an attractive way. Despite vocal demand, neither VBA nor Excel support such features—until now.
Introducing the sPrinter module for Excel VBA! Simply write a template for your message, and use curly braces {…} to embed data inside.
vPrint "You have a meeting with {1} {2} at {3} on {4}.", "John", "Doe", Time(), Date()
' ˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄˄ ˄˄˄˄˄˄ ˄˄˄˄˄ ˄˄˄˄˄˄ ˄˄˄˄˄˄
' Message Template DataYou have a meeting with John Doe at 1:30:00_PM on 1/1/26.
You can make this look even nicer, by applying format codes to your data!
vPrint "You have a meeting with {1} {2} at {3:h:MM AM/PM} on {4:dddd, mmmm d}.", "John", "Doe", Time(), Date()
' ˄˄˄˄˄˄˄˄˄˄ ˄˄˄˄˄˄˄˄˄˄˄˄˄
' Time Format Date FormatYou have a meeting with John Doe at 1:30 PM on Thursday, January 1.
See here for a detailed guide to syntax for message templates. You may construct a template from these components:
- Field: Use curly braces
{…}to embed a data field in your message. - Plaintext: Everything outside a field is displayed verbatim as regular text.
Tip
To display a regular curly brace as plaintext, simply neutralize it with a backslash: \{
You may fine-tune a field by specifying these things:
- Index: Identify the value in the data, either by position or by name.
- Format: Adjust how the value is displayed, using a format code like
m/d/yyyythat is native1 to Excel/VBA.
' Index Name
' ˅ ˅˅˅˅˅˅˅˅˅˅
{2:m/d/yyyy} {"birthday":m/d/yyyy}
' ˄˄˄˄˄˄˄˄ ˄˄˄˄˄˄˄˄
' Format FormatYou may specify the index in several ways:
-
Position: The (numeric) location of the value within the data. So
1is the first value, and2is the second.{1} and {2}Use a negative number to count from the end. So
-1is the last value, and-2is the second-to-last.{-2} and {-1} -
Name: The (textual) name of the value2 within the data. You must wrap this in quotes like
"birthday"or in further braces like{birthday}.{"birthday"} or {{birthday}} -
Auto: Simply omit the index altogether, and it uses the next available value.
{} and {}
Important
If you use braces inside your index or format, without \ to neutralize them, then you must keep them balanced. Every active { must (eventually) be followed by }.
Here are the features provided by sPrinter, which are useful in VBA and (mostly) in Excel. If you are a developer, and wish to hide these functions from your users in Excel, then look here to activate Option Private for the sPrinter.bas module.
Describe the module itself.
MOD_NAME: The name of the module.MOD_VERSION: Its current version.MOD_REPO: The URL to its repository.
Generate messages by embedding data in a template…
xMessage(): Source flexibledatafrom any structure, like an array orCollectionorDictionary.vMessage(): Supply anonymous values as single arguments…iMessage(): …or identify them with name-value pairs.
…and print them to the console.
xPrint(): Print the output fromxMessage()…vPrint(): …or fromvMessage()…iPrint(): …or fromiMessage().
Break down a template into an array of its components.
Parse(): Translate the textual template into aParserElementarray.
Perform broadly useful tasks.
Enum_Has(): Test if anEnumeration combo3 contains one of multiple options.
Num_Cardinal(): Represent a number as a cardinal like1,234…Num_Ordinal(): …and as an ordinal like1,234th.
Arr_Rank(): Count4 the dimensions of an array.Arr_Length(): Get the length of an array.
ChrX(): Safely get the character5 for a code, regardless of platform.Txt_Crop(): Remove a fixed number of characters from the end(s) of aString.Txt_List(): Format a set ofStrings as a bulleted list.
Footnotes
-
You may choose between the codes used by
Format()in VBA, or those used byTEXT()in Excel. ↩ -
This may be a key in a
CollectionorDictionarysupplied toxMessage()in VBA; or a name paired with a value and supplied toiMessage(). ↩ -
With a bitwise
Enumeration likeVbMsgBoxStyle, you may layer multiple options using the+andOroperators. ↩ -
In VBA an array may have at most 60 dimensions. ↩
-
The native
Chr*()family does not reliably support Unicode on Mac. ↩