vba printf code example

Example: vba printf

'Printf-like function
Public Function PrintF(ByVal pTemplate As String, ByVal pPattern As String, _
    ParamArray pParameters()) As String
    Dim lNParam As Long
    Dim sSearched As String
    Dim sReturn As String
    Dim sValues
    PrintF = pTemplate
    For Each sValues In pParameters
        lNParam = lNParam + 1
        sSearched = pPattern & lNParam & StrReverse(pPattern)
        If InStrB(pTemplate, sSearched) Then PrintF = Replace(PrintF, sSearched, sValues)
    Next
End Function
'------------------------------------------------------------------------------
Sub TestMe()
    Dim sTemplate As String: sTemplate = "It is |%1%| on |%2%|."
    Dim sPattern As String: sPattern = "|%"
    Debug.Print PrintF(sTemplate, sPattern, "cold", "Monday")
End Sub

Tags:

Vb Example