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†
- テキスト.txt
あいうえお
かきくけこ
さしすせそ
- Excel VBA
Option Explicit
Private Enum InOutMode
ReadOnly = 1
Create = 2
Append = 8
End Enum
Public Sub ReadTextFile()
Dim filePath As String: filePath = ThisWorkbook.Path & "\テキスト.txt"
Dim fileSystem As Object: Set fileSystem = CreateObject("Scripting.FileSystemObject")
Dim readingFile As Object: Set readingFile = fileSystem.OpenTextFile(filePath, InOutMode.ReadOnly, False)
Do Until (readingFile.AtEndOfStream)
Debug.Print readingFile.ReadLine
Loop
readingFile.Close
Set readingFile = Nothing
Set fileSystem = Nothing
End Sub
文字列を埋め込む†
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