つらつら Excel VBA

私の備忘録です。

テキスト一括出力タブ区切り

Sub テキストファイルタブ区切り一括出力()
    
    Dim buf As String '1行
    Dim delimiter As String '区切り文字
    Dim row As Range, clm As Range
    Dim ws As Worksheet
    
    Dim outputTxt As String '全データ
    Dim outputFilePath As String '出力パス
    Dim fileNo As Integer
    
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    delimiter = vbTab 'タブ区切り
    
    For Each row In ws.UsedRange.Rows
        
        buf = ""
        For Each clm In row.Columns
            buf = buf & clm.Value
            If clm.Column < row.Columns(row.Columns.Count).Column Then
                buf = buf & delimiter '最後の列でなければ区切り文字を追加
            End If
        Next
        
        If outputTxt = "" Then
            outputTxt = buf
        Else
            outputTxt = outputTxt & vbCrLf & buf
        End If
        
    Next
    
    
    '出力処理
    outputFilePath = "C: emp est.txt"
    fileNo = FreeFile()
    Open outputFilePath For Output As #fileNo
    
    Print #fileNo, outputTxt
    
    Close #fileNo
    
End Sub