Public Sub ReadText1()
' Openステートメントを使用
' 行単位で読み込み
Dim FileNumber As Long
Dim FilePath As String
Dim TextLine As String
Dim ar As Variant
Dim i As Long
Dim gyo As Long
FileNumber = FreeFile
FilePath = ThisWorkbook.Path & "\test.txt"
Open FilePath For Input As #FileNumber
gyo = 1
Do While Not EOF(FileNumber)
Line Input #FileNumber, TextLine
ar = Split(TextLine, ",")
For i = 0 To UBound(ar)
Cells(gyo, i + 1).Value = ar(i)
Next i
gyo = gyo + 1
Loop
Close #FileNumber
End Sub
Public Sub ReadText2()
' Openステートメントを使用
' 項目単位で読み込み
Dim FileNumber As Long
Dim FilePath As String
Dim ar(1 To 7) As String
Dim i As Long
Dim gyo As Long
FileNumber = FreeFile
FilePath = ThisWorkbook.Path & "\test.txt"
Open FilePath For Input As #FileNumber
gyo = 1
Do While Not EOF(FileNumber)
Input #FileNumber, ar(1), ar(2), ar(3), ar(4), ar(5), ar(6), ar(7)
For i = 1 To UBound(ar)
Cells(gyo, i).Value = ar(i)
Next i
gyo = gyo + 1
Loop
Close #FileNumber
End Sub
Public Sub ReadText3()
' FileSystemObjectを利用
' テキストファイルの読み込み(行単位、カンマ区切り)
Dim FSO As Object
Dim TSO As Object
Dim TextLine As String
Dim FilePath As String
Dim ar As Variant
Dim i As Long
Dim gyo As Long
FilePath = ThisWorkbook.Path & "\test.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
gyo = 0
Set TSO = FSO.GetFile(FilePath).OpenAsTextStream
With TSO
Do Until .AtEndOfLine
TextLine = .ReadLine
gyo = gyo + 1
ar = Split(TextLine, ",")
For i = 0 To UBound(ar)
Cells(gyo, i + 1).Value = ar(i)
Next i
Loop
.Close
End With
End Sub
Public Sub ReadText4()
' FileSystemObjectを利用
' テキストファイルの読み込み(行単位、タブ区切り)
Dim FSO As Object
Dim TSO As Object
Dim TextLine As String
Dim FilePath As String
Dim ar As Variant
Dim i As Long
Dim gyo As Long
FilePath = ThisWorkbook.Path & "\test.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
gyo = 0
Set TSO = FSO.GetFile(FilePath).OpenAsTextStream
With TSO
Do Until .AtEndOfLine
TextLine = .ReadLine
gyo = gyo + 1
ar = Split(TextLine, vbTab)
For i = 0 To UBound(ar)
Cells(gyo, i + 1).Value = ar(i)
Next i
Loop
.Close
End With
End Sub
Public Sub WriteText1()
' Openステートメントを使用
' テキストファイルの書き込み(行単位、上書き)
Dim FileNumber As Long
Dim FilePath As String
Dim TextLine As String
Dim gyo As Long
FileNumber = FreeFile
FilePath = ThisWorkbook.Path & "\test.txt"
Open FilePath For Output As #FileNumber
gyo = 1
Do While Cells(gyo, 1).Value <> ""
TextLine = Cells(gyo, 1).Value
Print #FileNumber, TextLine
gyo = gyo + 1
Loop
Close #FileNumber
End Sub
Public Sub WriteText2()
' Openステートメントを使用
' テキストファイルの書き込み(行単位、追記)
Dim FileNumber As Long
Dim FilePath As String
Dim TextLine As String
Dim gyo As Long
FileNumber = FreeFile
FilePath = ThisWorkbook.Path & "\test.txt"
Open FilePath For Append As #FileNumber
gyo = 1
Do While Cells(gyo, 1).Value <> ""
TextLine = Cells(gyo, 1).Value
Print #FileNumber, TextLine
gyo = gyo + 1
Loop
Close #FileNumber
End Sub
Public Sub WriteText3()
' Openステートメントを使用
' テキストファイルの書き込み(項目単位、上書き)
Dim FileNumber As Long
Dim FilePath As String
Dim gyo As Long
FileNumber = FreeFile
FilePath = ThisWorkbook.Path & "\test.txt"
Open FilePath For Output As #FileNumber
gyo = 1
Do While Cells(gyo, 1).Value <> ""
Write #FileNumber, Cells(gyo, 1).Value, Cells(gyo, 2).Value
gyo = gyo + 1
Loop
Close #FileNumber
End Sub
Public Sub WriteText4()
' FileSystemObjectを利用
' テキストファイルの書き込み(行単位、カンマ区切り)
Dim FSO As Object
Dim OSB As Object
Dim FilePath As String
Dim gyo As Long
FilePath = ThisWorkbook.Path & "\test.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
'2:上書きモード TRUE:ファイルが存在しなければ新規作成
Set OSB = FSO.OpenTextFile(FilePath, 2, True)
gyo = 1
Do Until Cells(gyo, 1).Value = ""
OSB.WriteLine Cells(gyo, 1).Value & "," & Cells(gyo, 2).Value
gyo = gyo + 1
Loop
OSB.Close
End Sub
|