2013年1月15日火曜日

VBA Excelマクロ(csvファイルを読み込み)

csvテキストファイルを読み込んでシートに書き込むExcelのマクロのサンプルです。








Sub test1()

    Dim fso As Object

    Dim tstream As Object, buf As String, buf_a As Variant

    Dim k As Integer, j As Integer

    Dim delimiter As String

 

    delimiter = ","

    'delimiter = Chr(9)

 

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set tstream = fso.OpenTextFile("C:\mdb\sample.csv")

    j = 1

 

    Do While tstream.AtEndOfStream = False

        buf = tstream.ReadLine

        buf_a = Split(buf, delimiter)

        k = UBound(buf_a) + 1

        Sheet1.Range(Sheet1.Cells(j, 1), Sheet1.Cells(j, k)).Value = buf_a

        j = j + 1

    Loop

    tstream.Close

    Set tstream = Nothing

    Set fso = Nothing



End Sub

0 件のコメント:

コメントを投稿