Option Explicit
Sub speichern_als_textdatei()
Dim i As Long, n As Long
Dim colCount As Integer, rowCount As Integer
Dim f1L As Integer, f2L As Integer, f3L As Integer, f4L As Integer
Dim myExportFile As String, exportStr As String, tmpStr As String
Dim myStart As Date, myEnd As Date
myExportFile = "D:\Demo.txt"
Close #1
Open myExportFile For Output As #1
'Schleifenbegrenzungen definieren
'Anzahl Zeilen
rowCount = Cells(Rows.Count, 4).End(xlUp).Row
'Anzahl Spalten zum exportieren
colCount = 4
'Länge der jeweiligen Datenfelder in Anzahl Zeichen
'Wie sie in der Exportdatei stehen sollen
f1L = 8
f2L = 8
f3L = 31
f4L = 60
exportStr = ""
'Zeitkontrolle
myStart = Now
Debug.Print myStart
'Feld-Überschriften exportieren
'hier Zeile 1
For i = 1 To colCount
exportStr = exportStr & "" & Cells(1, i) & ""
Next i
Write #1, exportStr
'Zeilenschleife
'beginnt ab Zeile 3
For i = 3 To rowCount
exportStr = ""
'Spaltenschleife
tmpStr = ""
'Für Feld1 z.B. mit Leerzeichen auffüllen
For n = 1 To f1L - Len(Cells(i, 1))
tmpStr = tmpStr & " "
Next n
exportStr = exportStr & "" & tmpStr & Cells(i, 1).Text
tmpStr = ""
'Für Feld 2 z.B. mit Underline auffüllen
For n = 1 To f2L - Len(Cells(i, 2))
tmpStr = tmpStr & " "
Next n
exportStr = exportStr & "" & tmpStr & Cells(i, 2)
tmpStr = ""
'Für Feld 3 z.B. mit 0 (Null) auffüllen
For n = 1 To f3L - Len(Cells(i, 3))
tmpStr = tmpStr & " "
Next n
exportStr = exportStr & "" & tmpStr & Cells(i, 3)
tmpStr = ""
'Für Feld 2 z.B. mit Underline auffüllen
For n = 1 To f4L - Len(Cells(i, 4))
tmpStr = tmpStr & " "
Next n
exportStr = exportStr & "" & tmpStr & Cells(i, 4)
tmpStr = ""
'Schreiben in die Datei
Write #1, exportStr & ""
Next i
Close #1
Debug.Print Now
Debug.Print Format(Now - myStart, "hh:mm:ss")
MsgBox "erfolgreich gespeichert!"
End Sub ActiveWorkbook.SaveAs Filename:="dateiname.txt", _
FileFormat:=xlTextMSDOS, CreateBackup:=FalseDim f1[b]R[/b] As Integer, f2[b]R[/b] As Integer, f3[b]R[/b] As Integer, f4[b]R[/b] As Integer
Dim f1L As Integer, f2L As Integer, f3L As Integer, f4L As IntegerDim f1L As String, f2L As String, f3L As String, f4L As String exportStr = exportStr & "" & tmpStr & Cells(i, 1).Text
exportStr = exportStr & "" & Cells(i, 1).Text& tmpStr For i = 1 To colCount
exportStr = exportStr & "" & Cells(1, i) & ""
Next i
exportStr = exportStr & "" & Cells(i, 1).Text& tmpStr'Feld-Überschriften exportieren
'hier Zeile 1
For i = 1 To colCount
exportStr = exportStr & "" & Cells(1, i) & ""
Next i
Write #1, exportStr 'Feld-Überschriften exportieren
'hier Zeile 1
For i = 1 To colCount
Select Case i
Case 1, 2
x = 8
Case 3
x = 31
Case 4
x = 60
End Select
For n = 1 To x - Len(Cells(1, i))
tmpStr = tmpStr & " "
Next n
exportStr = exportStr & Cells(1, i).Text & tmpStr
Next i
Write #1, exportStr
Dim i, n As Long
Dim colCount, rowCount, f1L, f2L, f3L, f4L, x As Integer
Dim myExportFile, exportStr, tmpStr As String
Dim myStart, myEnd As Date
export-ntdll.dll
rw1978 31.07.2007 - 178 Hits - 1 Antwort
ICQ - Text falsch ausgerichtet
Ecki.. 03.01.2008 - 28 Hits - 1 Antwort
Textdatei Analysieren
Ralfman 19.04.2008 - 57 Hits - 2 Antworten
gescannter Briefes in eine Textdatei umwandeln
Sue_S. 15.05.2008 - 75 Hits - 20 Antworten