dank der guten Hilfe hier im Forum hatte ich vor langer Zeit schon mal folgenden Code zusammenbasteln, können, der mir eine Textdatei einliest und sauber ins Excel auflistet:
Option Explicit
Sub sbTxtImp() 'Startef File-Import mit Datei-Öffnen Dialog
Dim lvarTxtImp, lstrTxt As String, larstrTxt() As String, liIdx As Integer, larstrEntry() _
As String, lloRow As Long, lloCol As Long, liIdx2 As Integer
lvarTxtImp = Application.GetOpenFilename(fileFilter:="Txt-Dateien, *.txt")
Open lvarTxtImp For Binary Access Read As #1
lstrTxt = Space(LOF(1))
Get #1, , lstrTxt
Close #1
larstrTxt = Split(lstrTxt, vbCrLf)
For liIdx = 0 To UBound(larstrTxt)
If InStr(larstrTxt(liIdx), "Aufteil.") = 0 And _
Not IsDate(Left(larstrTxt(liIdx), 10)) And _
Left(larstrTxt(liIdx), 1) "-" And _
larstrTxt(liIdx) "" Then
lloRow = IIf(Cells(Rows.Count, 1).End(xlUp).Row = 1 And Range("A1"). _
Value = "", 1, Cells(Rows.Count, 1).End(xlUp).Row + 1)
Do Until InStr(larstrTxt(liIdx), " ") = 0
larstrTxt(liIdx) = Replace(larstrTxt(liIdx), " ", " ")
Loop
larstrEntry = Split(larstrTxt(liIdx), " ")
lloCol = 1
For liIdx2 = 1 To 5
Cells(lloRow, lloCol).Value = Replace(larstrEntry(liIdx2), _
vbTab, "")
lloCol = lloCol + 1
Next
For liIdx2 = 7 To UBound(larstrEntry)
Cells(lloRow, lloCol).Value = Cells(lloRow, lloCol).Value & _
larstrEntry(liIdx2) & " "
Next
Cells(lloRow, lloCol).Value = Trim(Cells(lloRow, lloCol).Value)
End If
Next
End Sub
die Text-Datei welche eingelesen wird schaut nach folgendem Muster aus:
https://www.herber.de/bbs/user/93995.txt
Soweit so gut!
der Code lief nun einige Zeit problemlos. Doch nun ... stehe ich vor folgendem Problem:
Die Zahlen in der Spalte "Aufteil." können nun künftig auch 7 stellig sein.
Das Textfile kommt genau so wie im Beispiel gezeigt aus einem anderen Programm daher.
https://www.herber.de/bbs/user/93996.txt
Nun hab ich das Problem, dass ich im obigen Code also anpassen müsste, dass nun neu auch korrekt 7-stellige Zahlen aus der Spalte "Aufteil." gelesen werden können.
Oder anders gesagt:
die folgende Zeile aus meinem obigen Code macht nun Probleme:
larstrEntry = Split(larstrTxt(liIdx), " ")
denn im Textfile waren ja die Zahlen anfänglich immer um eine Leerstelle eingerückt, nun - bei 7 - stellen - sind diese nicht mehr eingerückt sondern beginnen ganz links im Textfile an erster Zeichenstelle.
Wie könnte ich also meinen Code anpassen, dass dieser nun die auch die 7 stellen einliest ?
(ggf. vor dem Splitting bei allen 7-stelligen Zahlen im Textfile vorab eine Leerstelle erzeugen ?)
bin für jeden Tip sehr dankbar !