1

Background

VBA has a Format() function which converts any value to its textual representation. This is useful for numbers and Dates and times, but it can also be used on Strings.

Debug.Print Format("1234567890abc", "(@@@) @@@-@@@@ ext. @@@")
(123) 456-7890 ext. abc

Now what I want to do is something like this...

Debug.Print Format("example text", "\E\n\t\i\r\e\ \s\t\r\i\n\g\:\ \""@\""\.")
Entire string: "example text".

...but instead I get the following result. Note how only a single character displays where intended. Also, the other characters are stuck onto the end.

Entire string: "e".xample text

The documentation suggests we can only display individual characters in sequence. Unlike printf() with %s and friends, this prevents dynamic formatting of the text as a whole, and forces us to hard-code the number of characters into our format.

Character Description
@ Character placeholder. Display a character or a space. If the string has a character in the position where the at symbol (@) appears in the format string, display it; otherwise, display a space in that position. Placeholders are filled from right to left unless there is an exclamation point character (!) in the format string.
& Character placeholder. Display a character or nothing. If the string has a character in the position where the ampersand (&) appears, display it; otherwise, display nothing. Placeholders are filled from right to left unless there is an exclamation point character (!) in the format string.
< Force lowercase. Display all characters in lowercase format.
> Force uppercase. Display all characters in uppercase format.
! Force left to right fill of placeholders. The default is to fill placeholders from right to left.

Question

Is there a native option to Format() an entire string, rather than its individual characters?


Technically, we could implement our own UDF called FormatString(). This identifies a designated character (like *) in the format which is not escaped (\*), and substitutes as many placeholders ("@@@…@") as there are characters in the input. Then it passes the input and modified format to the native Format().

However, this is fraught with challenges like identifying escape sequences: * versus \* versus \\* versus \\\* and so forth. As such, I absolutely desire a native solution rather than a UDF, which introduces instability and complexity.

14
  • 1
    Could you give a few more examples of what you're expecting, and what is happening. In your example did you want just quotes at beginning and end of entire string? Also, not sure if this helps, but there's some some newer regex excel functions which might be more appropriate which you could use. So something evaluate("=REGEXTEST(...") of course quotes get annoying... Commented Oct 10 at 17:27
  • 3
    For your example: Format("example text", "\E\n\t\i\r\e\ \s\t\r\i\n\g\:\ \""" & string(100,"&") & "\""\.") would work but it's unclear what you actual use cases are and it seems like there could be simpler ways to do what you want - some of that "complexity" (escape sequences etc) seems to be the result of (mis)using Format ? Similar post: stackoverflow.com/questions/17233701/… Commented Oct 10 at 17:58
  • 1
    I don't think the size of the Expression is the biggest limiting factor here. The biggest limitation I see is that the Format argument can't take more than 255 characters, so the highest you can go is string(219,"&") in this example: debug.print Format(string(999999999,"a"), "\E\n\t\i\r\e\ \s\t\r\i\n\g\:\ \""" & string(219,"&") & "\""\.") Commented Oct 10 at 20:38
  • 1
    With example text entered in a cell in the UI, to which the custom number format ;;;"Entire String: "\"@\""." has been applied, calling .Text on that cell would return Entire String: "example text".. Of course, the 32,767 character limit for a cell's contents is tiny in terms of how large a VBA string can be, but there are billions of cells available on a worksheet. Commented Oct 11 at 0:15
  • 2
    Even though the VBA library has Format the object model also has Text, e.g. Debug.Print WorksheetFunction.Text("example string",";;;""Entire String: ""\""@\"""".""") (i.e. just doubling up on the quotes from the custom-number format - the 3 semicolons can actually be deleted) Commented Oct 11 at 12:08

1 Answer 1

2

More of a workaround than a solution, but you can do this with the Text method of the WorksheetFunction object, i.e.

Debug.Print WorksheetFunction.Text("example text","""Entire String: ""\""@\"""".""")

Screenshot of VBA Immediate Window, illustrating effect of suggested syntax

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.