「Excel VBA」Appendでエクセルのセルの値をテキストに追記で出力する

2022年1月13日

環境
Windows 10 64bit
Excel 2013
ディレクトリ:C:\test10
ファイル名:Book1.xlsx result.txt
セルのB3に「横浜」、B4に「川崎」があります

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Option Explicit
Sub funA()
'ファイル変数名
Dim fileName As String
'1 出力するファイルの場所
fileName = ActiveWorkbook.Path & "\result.txt"
'2 対象のファイルをAppendモードで開く
Open fileName For Append As #1
'B3セルの値を指定ファイルに出力
Print #1, Cells(3, 2).Value
'B4の値を指定ファイルに指定
Print #1, Cells(4, 2).Value
'4 対象のファイルを閉じる
Close #1
End Sub
Option Explicit Sub funA() 'ファイル変数名 Dim fileName As String '1 出力するファイルの場所 fileName = ActiveWorkbook.Path & "\result.txt" '2 対象のファイルをAppendモードで開く Open fileName For Append As #1 'B3セルの値を指定ファイルに出力 Print #1, Cells(3, 2).Value 'B4の値を指定ファイルに指定 Print #1, Cells(4, 2).Value '4 対象のファイルを閉じる Close #1 End Sub
Option Explicit
Sub funA()
   'ファイル変数名
    Dim fileName As String

    '1 出力するファイルの場所
    fileName = ActiveWorkbook.Path & "\result.txt"
    
    '2 対象のファイルをAppendモードで開く
    Open fileName For Append As #1
    
    'B3セルの値を指定ファイルに出力
    Print #1, Cells(3, 2).Value
    'B4の値を指定ファイルに指定
    Print #1, Cells(4, 2).Value
     
    '4 対象のファイルを閉じる
    Close #1

End Sub

実行結果
「result.txt」に以下の内容が表示されます。出力されるファイルの文字コードはShift_JISです。
横浜
川崎

Excel VBA

Posted by arkgame