SoftwareEngineering

ファイルに書き込む

Print文

Dim freeFileNo As Integer

' ファイルの空き番号を取得
freeFileNo = FreeFile


Open "D:\UDF\VBA\ファイル書き込みテスト.txt" For Output As #freeFileNo

Print #freeFileNo, "あいうえお"
Print #freeFileNo, "かきくけこ"
Print #freeFileNo, "さしすせそ"

Close #freeFileNo

引数の数を可変にする方法

Public Sub Test_sprintf()
    Debug.Print sprintf("_{0}_{1}_{2}_{0}_{1}_{2}", "A", "b", "C")
End Sub

Private Function sprintf(template As String, ParamArray args() As Variant) As String
    Dim index As Integer
    
    value = template

    index = 0
    For Each arg In args
        value = Replace(value, "{" & index & "}", arg)
        
        index = index + 1
    Next
    
    sprintf = value
End Function

Scripting.FileSystemObject

Option Explicit

文字列を埋め込む

Public Function FormatString(format As String, ParamArray args() As Variant) As String
    Dim index As Integer
    
    FormatString = format
    
    For index = 0 To UBound(args())
        FormatString = Replace(FormatString, "{" & index & "}", args(index))
    Next index
    
End Function



Public Sub TestFormatString()

    Debug.Print FormatString("引数1=[{0}], 引数2=[{1}], 引数3=[{2}]", "param1", "param2", "param3")

End Sub

トップ   一覧 検索 最終更新   ヘルプ   最終更新のRSS