0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

エクセルからテキスト出力するVBAをChatGPTに聞いた

Posted at

エクセルからテキスト出力するVBA

Sub ExportDataToTextFile()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim outputString As String
    Dim filePath As String
    Dim fileNumber As Integer
    
    ' 出力先のファイルパスを指定
    filePath = "C:\出力先のファイルパス\output.txt"
    
    ' プログラム一覧シートを取得
    Set ws = ThisWorkbook.Sheets("プログラム一覧")
    
    ' 最終行を取得
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    ' テキストファイルを開く
    fileNumber = FreeFile
    Open filePath For Output As #fileNumber
    
    ' ループしてデータを結合して書き出す
    For i = 2 To lastRow
        outputString = ws.Cells(i, "B").Value & " " & ws.Cells(i, "C").Value
        Print #fileNumber, outputString
    Next i
    
    ' テキストファイルを閉じる
    Close #fileNumber
    
    MsgBox "データをテキストファイルに出力しました。"
End Sub
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?