Creating sentences and lists from a series of items
- John
- 1 day ago
- 2 min read
Often, especially when creating text to show to a user in a dialog, I have a Collection or Array of Strings that I want to use to inform the user of some information. Code to compile those Strings into "clean" user-friendly text can be a pain to write ... for example, in a sentence, making sure the penultimate and last items are separated by "and" (or "or" etc) instead of a comma especially when you don't know how many Strings will be included from the off.
To simplify this, I have two Functions that I include in many of my projects. One is called ToSentence ... it creates a single sentence from a Collection or Array of Strings. The other is called ToList ... it creates a bulleted or numbered list from a Collection or Array of Strings. The code of both of these Functions are "English-language centric" ... however, if you're working with clients that use another language, it should be quite simple to change the English words contained within the Functions ("and" in both of them and "more" in ToList) to being another language.
This is ToSentence ...
The code itself is fairly straight-forward and each parameter is documented to explain its purpose. Note that either a Collection or an Array can be passed in for the vEnumerable parameter (as both Collections and Arrays can be enumerated over using For Each ... Next).
Examples of using ToSentence are ...
... which results in ...
... and ...
... which results in ...
This is ToList ...
Again, the code is fairly straight-forward and each parameter is documented to explain its purpose. Again, either a Collection or an Array can be passed in for the vEnumerable parameter.
Examples of using ToList are ...
... which results in ...
... and ...
... which results in ...
A final note is that if you want the user to be able to select one String (rather than just showing all of the Strings to the user in a dialog) then you might instead want to see the Multiple choice UserForm in VBA using a ComboBox post.