Gruppe
Extern
Bereich
TextExport
Thema
Tabellenblattinhalt in seqentielle Textdatei ein- und auslesen
Problem
Wie kann ich den Inhalt der Spalten A:B in eine Textdatei ein- und später wieder auslesen?
Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.
StandardModule: basMain
Sub IntTextDatei()
Dim rng As Range
Dim iRow As Integer, iCol As Integer, iFile As Integer
Dim sFile As String, sTxt As String
Set rng = Range("A1").CurrentRegion
sFile = Application.Path & "/testtext.txt"
iFile = FreeFile
Open sFile For Output As iFile
For iRow = 1 To rng.Rows.Count
For iCol = 1 To rng.Columns.Count
sTxt = sTxt & Cells(iRow, iCol).Value & ","
Next iCol
sTxt = Left(sTxt, Len(sTxt) - 1)
Print #iFile, sTxt
sTxt = ""
Next iRow
Close iFile
rng.ClearContents
MsgBox "Habe die Daten in eine Textdatei eingelesen!"
End Sub
Sub AusTextDatei()
Dim iRow As Integer, iFile As Integer
Dim sFile As String, sTxtA As String, sTxtB As String
sFile = Application.Path & "/testtext.txt"
If Dir(sFile) = "" Then
MsgBox "Die Daten wurden nicht eingelesen!"
Exit Sub
End If
iFile = FreeFile
Open sFile For Input As iFile
Do Until EOF(iFile)
Input #iFile, sTxtA, sTxtB
iRow = iRow + 1
Cells(iRow, 1).Value = sTxtA
Cells(iRow, 2).Value = sTxtB
Loop
Close
Kill sFile
MsgBox "Habe die Daten ausgelesen!"
End Sub