LoginSignup
1
2

More than 5 years have passed since last update.

VBA_ログファイル作成

Posted at

1.概要

・VBAのログファイル関数を作成。
・書き込みは追記式

2.ソースコード

Sub LogTest()
    Dim i As Long
    For i = 0 To 5
       Call DebugPrintFile("テスト")
    Next
End Sub

'ログファイルを作成。書き込みは追記式、ファイル名、フォルダ名は省略可
Function DebugPrintFile(arg_Data As Variant, Optional arg_filename As String, Optional arg_folderpath As String)
    Dim lngFileNum As Long 'ログファイルのファイル番号
    Dim strLogFile As String 'ログファイルのパス

    'ファイル名の指定があれば適用
    If arg_filename = "" Then
        arg_filename = "Log_Debug.txt"
    Else
        arg_filename = arg_filename & ".txt"
    End If

    'フォルダの指定が無ければ直下
    If arg_folderpath = "" Then
        arg_folderpath = ThisWorkbook.Path
    End If

    'ログファイルのパスを作成
    strLogFile = arg_folderpath & "\" & arg_filename

    '現在使用されていないファイル番号を取得
    lngFileNum = FreeFile()

    '追記式(Append)でファイルを開く(中身が消えていいならOutPut)
    Open strLogFile For Append As #lngFileNum

    '取得したファイル番号にarg_Dataを書き込み
    Print #lngFileNum, arg_Data

    'ファイルを閉じる
    Close #lngFileNum

End Function

3.出力結果

Log_Debug.txt

テスト
テスト
テスト
テスト
テスト
テスト

4.出典

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2