Simple Excel sprintf visual basic (vba) macro

Simple Excel sprintf visual basic (vba) macro.

Example:  "=printf("Some text '%s', more text: '%s'", A1, A2)"

Public Function Printf(mask As String, ParamArray tokens()) As String

    Dim i As Long

     For i = 0 To UBound(tokens)

          mask = Replace$(mask, "%s", tokens(i), , 1)

     Next

     Printf = mask

End Function

See http://stackoverflow.com/questions/17233701/is-there-an-equivalent-of-printf-or-string-format-in-excel and http://www.freevbcode.com/ShowCode.asp?ID=5014 for an more elaborate implementation.

Tags: , ,

No comments yet.

Leave a Reply