FileRead()は、指定のテキストファイルから1行づつ読み込んで返す関数です。
これを利用して、1行づつチェックして書き出すのが一つの方法です。
もっと、簡単なのは、FileReadArray() で一気に変数に呼び込むことです。
UBound()などで配列に呼び込んだ行数を取得してチェックして不要行を削除します。
FileWrite()で書き出します。
Public Function FileRead(ByVal FileName As String, Optional isStop As Boolean = False) As String
On Error GoTo Err_FileRead
Static isOpen As Boolean
Static fso As FileSystemObject
Static fil As File
Static txs As TextStream
If Not isOpen Then
isOpen = True
Set fso = New FileSystemObject
Set fil = fso.GetFile(FileName)
Set txs = fil.OpenAsTextStream(ForReading, TristateUseDefault)
End If
FileRead = IIf(fil.Name = FileName, txs.ReadLine, "")
Exit_FileRead:
If Len(FileRead) = 0 Or isStop Then
isOpen = False
Set txs = Nothing
Set fil = Nothing
Set fso = Nothing
End If
Exit Function
Err_FileRead:
Resume Exit_FileRead
End Function
Public Function FileReadArray(ByVal FileName As String) As String()
On Error GoTo Err_FileReadArray
Dim fso As FileSystemObject
Dim fil As File
Dim txs As TextStream
Dim strText As String
Dim strTexts() As String
Set fso = New FileSystemObject
Set fil = fso.GetFile(FileName)
Set txs = fil.OpenAsTextStream(ForReading, TristateUseDefault)
strText = txs.ReadAll
strTexts = Split(strText, Chr$(13) & Chr$(10))
Exit_FileReadArray:
FileReadArray = strTexts()
Exit Function
Err_FileReadArray:
MsgBox Err.Description & "(FileReadArray)", vbExclamation, " 関数エラーメッセージ"
strTexts() = Split("")
Resume Exit_FileReadArray
End Function
Public Function FileWrite(ByVal FileName As String, _
ByVal Text As String) As Boolean
On Error GoTo Err_FileWrite
Dim fso As FileSystemObject
Dim txs As TextStream
Set fso = New FileSystemObject
Set txs = fso.CreateTextFile(FileName, True)
txs.Write Text
FileWrite = True
Exit_FileWrite:
Exit Function
Err_FileWrite:
MsgBox Err.Description & "(FileWrite)", vbExclamation, " 関数エラーメッセージ"
Resume Exit_FileWrite
End Function
お礼
ありがとうございます。 良く理解できました。