7
10

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 5 years have passed since last update.

【Excel VBA】セルの内容をテキストファイルに出力するサンプル

Posted at

エクセルと同じパスに「yyyymmdd.txt」のファイル名で
テキストファイルを作成し、エクセルの内容を出力するVBAのサンプルです。

VBA
Sub MakeText()
    '変数を定義します
    Dim FSO As Object
    Dim lStr As String

    '変数に値を代入します
    bookName = ThisWorkbook.Name
    'シート名を設定します
    sheetName = "XXX"
    Workbooks(bookName).Activate
    Sheets(sheetName).Select

    'オブジェクトを作ります
    Set FSO = CreateObject("Scripting.FileSystemObject")
    tTime = Left(Replace(Now(), "/", ""), 8)
    
    With FSO.CreateTextFile(ThisWorkbook.Path & "¥" & tTime & ".txt")
            lStr = Cells(X, Y).Value
            .writeline lStr
        .Close
    End With
    Set FSO = Nothing
End Sub

基本となる形ですので、シート名、ファイル名、出力するセルなどをカスタマイズして使いましょう。

7
10
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
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?