Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Excel VBA: String Format

The functions string.Format or StringBuilder.AppendFormat are two very usefull functions for formatting strings and increasing the readability of your .NET code. The Format function in VBA unfortunately works in a quite different way than the string.Format function in .NET. As far as I know there is no built-in function in VBA to acomplish the exact same as string.Format. In VBA the functionality can be achieved the following way:

' Format string using the .NET way
Public Function StringFormat(ByVal strValue As String, ParamArray arrParames() As Variant) As String
    Dim i As Integer

    ' Replace parameters  
    For i = LBound(arrParames()) To UBound(arrParames())
        strValue = Replace(strValue, "{" & CStr(i) & "}", CStr(arrParames(i)))
    Next
   
    ' Get the value    StringFormat = strValue
End Function



This post first appeared on LazerWire.com, please read the originial post: here

Share the post

Excel VBA: String Format

×

Subscribe to Lazerwire.com

Get updates delivered right to your inbox!

Thank you for your subscription

×