Live-Forum - Die aktuellen Beiträge
Datum
Titel
29.03.2024 13:14:12
28.03.2024 21:12:36
28.03.2024 18:31:49
Anzeige
Archiv - Navigation
1268to1272
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

.txt's mit VBA bearbeiten

.txt's mit VBA bearbeiten
Horst
Hallo Excel-Freunde,
ich bin auf der Suche nach einem Makro, dass folgendes bewerkstelligt: Eine .txt soll in einer .xls geöffnet werden, die ersten beiden Spalten sowie die ersten fünf Zeilen gelöscht und danach wieder als .txt gespeichert werden.
Besten Dank vorab,
Gruß, Horst
AW: .txt's mit VBA bearbeiten
30.06.2012 14:34:30
Horst
Alternativ könnten übrigens auch alle Werte aus der "bestehenden .txt" ab der sechsten Zeile und ab der dritten Spalte ausgelesen und in eine "neue .txt" gespeichert werden. Um Daten aus einer .txt zu lesen, habe ich folgendes Codebeispiel angefügt (hier wird der letzte Wert zurückgegeben). Eventuell kann man das Makro so umbauen, dass es mit den Werten ab der 6. Zeile und der 3. Spalte eine neue .txt generiert:
Function LeseZeile1(strPfad As String) As Double
Dim sLine As String
Dim F As Integer
F = FreeFile
Open strPfad For Input As #F
Do
Line Input #F, sLine
Loop Until EOF(F)
Close #F
sLine = Replace(sLine, " ", vbTab)
If InStr(sLine, vbTab) > 0 Then
sLine = Right$(sLine, Len(sLine) - InStr(sLine, vbTab))
End If
If IsNumeric(sLine) Then
LeseZeile1 = CDbl(sLine)
End If
End Function

Gruß, Horst
Anzeige
AW: .txt's mit VBA bearbeiten
30.06.2012 14:53:46
Josef

Hallo Horst,
dazu müsste man wissen, wie die Textdatei aufgebaut ist.

« Gruß Sepp »

AW: .txt's mit VBA bearbeiten
30.06.2012 20:34:59
Horst
Hallo Josef!
Anbei die .txt, so wie sie von einem externen Programm standardmäßig produziert wird. Wie gesagt, die ersten fünf Zeilen und die ersten zwei Spalten sollen weg, damit lediglich die Datenmatrix (die hinsichtlich Zeilen und Spalten unterschiedlich groß sein kann) übrig bleibt und das Ergebnis als neue .txt gespeichert werden kann.
https://www.herber.de/bbs/user/80816.txt
Wäre super, wenn du das hinbekommen würdest, habe in der Vergangenheit schon gute Erfahrung mit deinen Programmierkünsten gemacht ;-))
LG, Horst
Anzeige
AW: .txt's mit VBA bearbeiten
30.06.2012 20:49:00
Josef

Hallo Horst,
teste mal, die Datei wird unter dem selben Namen gespeichert.
Sub changeTXT_File()
  Dim strFile As String, strTmpFile As String, strTmp As String
  Dim lngIndex As Long, lngCol As Long
  Dim vntTmp As Variant
  Dim FF1 As Integer, FF2 As Integer
  
  strFile = "E:\Temp\Test.txt" 'Dateipfad - Anpassen!
  
  strTmpFile = Environ("TEMP") & "\Temp.txt"
  
  FF1 = FreeFile
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 Then
      vntTmp = Split(strTmp, vbTab)
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
        Next
      End If
      strTmp = Left(strTmp, Len(strTmp) - 1)
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strFile
  Name strTmpFile As strFile
  
End Sub



« Gruß Sepp »

Anzeige
AW: .txt's mit VBA bearbeiten
30.06.2012 23:03:32
Horst
Hallo Sepp!
Super - funktioniert wie immer einwandfrei!
Abschließend noch eine Sache: Ich möchte die kopierten Werte in Excel automatisiert weiterverarbeiten, um sie als Input für ein externes Programm, dass nur Textdateien lesen kann, aufzubereiten. Dazu gibt's nun zwei Möglichkeiten:
A (einfache Variante):
Zwecks Weiterverarbeitung speichere ich das Ergebnis des obigen Makros als .xls ab. Wie gebe ich das im Code am besten an?
B (effizienteste, aber programmiertechnisch vermutlich komplexe Variante):
Nach der letzten Spalte der vom Makro gespeicherten Datenmatrix müsste eine Spalte hinzugefügt werden, die sich aus der 124. Spalte dreier bereits bestehender .txt-Dateien ("train.txt", "retrain.txt" u. "test.txt", siehe Anhang) ergibt.
"retrain.txt": https://www.herber.de/bbs/user/80817.txt
"test.txt": https://www.herber.de/bbs/user/80818.txt
Die Anzahl der Zeilen der 124. Spalte der drei Dateien ergibt in Summe die Anzahl der Zeilen der vom obigen Makro gespeicherten Datenmatrix, wobei die Zeilen der 124. Spalte der "test.txt" unter jene der "retrain.txt" u. diese unter jene der "train.txt" kopiert werden. Nach Hinzufügen dieser letzten Spalte soll die so entstandene, neue Datenmatrix dann erneut zeilenmäßig in eine "train.txt", "retrain.txt" (vorletzte Zeile bis n-51) und "test.txt" (letzte Zeile n) zerlegt werden.
Besten Dank vorab, Horst
Anzeige
AW: .txt's mit VBA bearbeiten
01.07.2012 16:56:56
Josef

Hallo Horst,
absolut ungetestet!
Sub changeTXT_File2()
  Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As String
  Dim strTmpFile As String, strTmp As String
  Dim lngIndex As Long, lngCol As Long, lngC As Long
  Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
  Dim FF1 As Integer, FF2 As Integer
  
  strTrainFile = "E:\Temp\train.txt" 'Dateipfad train.txt - Anpassen
  strReTrainFile = "E:\Temp\retrain.txt" 'Dateipfad retrain.txt - Anpassen
  strTestFile = "E:\Temp\test.txt" 'Dateipfad test.txt - Anpassen
  strFile = "E:\Temp\datei.txt" 'Dateipfad Eingabe-Datei - Anpassen!
  
  strTmpFile = Environ("TEMP") & "\Temp.txt"
  
  '## train.txt einlesen
  FF1 = FreeFile
  
  Open strTrainFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, vbTab)
    Redim Preserve strTrain(lngIndex)
    strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
  Loop
  Close #FF1
  
  '## retrain.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strReTrainFile For Input As #FF1
  lngIndex = 0
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, vbTab)
    Redim Preserve strReTrain(lngIndex)
    strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
  Loop
  Close #FF1
  
  '## test.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strTestFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, vbTab)
    Redim Preserve strTest(lngIndex)
    strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
  Loop
  Close #FF1
  
  '## train.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 Then
      vntTmp = Split(strTmp, vbTab)
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
        Next
      End If
      strTmp = strTmp & strTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTrainFile
  Name strTmpFile As strTrainFile
  
  '## retrain.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strReTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + 1 Then
      vntTmp = Split(strTmp, vbTab)
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
        Next
      End If
      strTmp = strTmp & strReTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strReTrainFile
  Name strTmpFile As strReTrainFile
  
  '## test.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTest)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + UBound(strReTrain) + 2 Then
      vntTmp = Split(strTmp, vbTab)
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
        Next
      End If
      strTmp = strTmp & strTest(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTestFile
  Name strTmpFile As strTestFile
  
End Sub



« Gruß Sepp »

Anzeige
AW: .txt's mit VBA bearbeiten
01.07.2012 23:01:00
Horst
Hallo Sepp,
besten Dank für deine Bemühungen! Das Makro bearbeitet die Dateien "train.txt", "retrain.txt" und "test.txt", scheint hier einiges zu löschen, die eigentlich zu bearbeitende "instrum.txt" bleibt allerdings unverändert. Woran könnte es liegen?
Gruß, Horst
Sub changeTXT_File2()
Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As  _
String
Dim strTmpFile As String, strTmp As String
Dim lngIndex As Long, lngCol As Long, lngC As Long
Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
Dim FF1 As Integer, FF2 As Integer
strTrainFile = "C:\Programme\Signal_Pro\update\train_one.txt" 'Dateipfad train.txt - Anpassen
strReTrainFile = "C:\Programme\Signal_Pro\update\retrain_one.txt" 'Dateipfad retrain.txt -  _
Anpassen
strTestFile = "C:\Programme\Signal_Pro\update\test_one.txt" 'Dateipfad test.txt - Anpassen
strFile = "C:\Programme\Signal_Pro\update\instrum.txt" 'Dateipfad Eingabe-Datei - Anpassen!
strTmpFile = Environ("TEMP") & "\Temp.txt"
'## train.txt einlesen
FF1 = FreeFile
Open strTrainFile For Input As #FF1
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, vbTab)
ReDim Preserve strTrain(lngIndex)
strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
Loop
Close #FF1
'## retrain.txt einlesen
FF1 = FreeFile
lngIndex = 0
Open strReTrainFile For Input As #FF1
lngIndex = 0
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, vbTab)
ReDim Preserve strReTrain(lngIndex)
strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
Loop
Close #FF1
'## test.txt einlesen
FF1 = FreeFile
lngIndex = 0
Open strTestFile For Input As #FF1
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, vbTab)
ReDim Preserve strTest(lngIndex)
strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
Loop
Close #FF1
'## train.txt erstellen
FF1 = FreeFile
lngIndex = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 Then
vntTmp = Split(strTmp, vbTab)
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
Next
End If
strTmp = strTmp & strTrain(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strTrainFile
Name strTmpFile As strTrainFile
'## retrain.txt erstellen
FF1 = FreeFile
lngIndex = 0
lngC = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 + UBound(strTrain) + 1 Then
vntTmp = Split(strTmp, vbTab)
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
Next
End If
strTmp = strTmp & strReTrain(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strReTrainFile
Name strTmpFile As strReTrainFile
'## test.txt erstellen
FF1 = FreeFile
lngIndex = 0
lngC = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 + UBound(strTrain) + UBound(strReTrain) + 2 Then
vntTmp = Split(strTmp, vbTab)
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & vbTab
Next
End If
strTmp = strTmp & strTest(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strTestFile
Name strTmpFile As strTestFile
End Sub

Anzeige
AW: .txt's mit VBA bearbeiten
03.07.2012 18:36:25
Josef

Hallo Horst,
ich hab mich daran gehalten.
"Nach Hinzufügen dieser letzten Spalte soll die so entstandene, neue Datenmatrix dann erneut zeilenmäßig in eine "train.txt", "retrain.txt" (vorletzte Zeile bis n-51) und "test.txt" (letzte Zeile n) zerlegt werden.
"

sonst musst du das ganze noch mal ausführlicher erklären und vier entsprechende TXT-Dateien hochladen.

« Gruß Sepp »

Anzeige
AW: .txt's mit VBA bearbeiten
03.07.2012 21:39:23
Horst
Hallo Sepp!
Bei mir schrumpft das Makro die "train_one.txt", "retrain_one.txt" und "test_one.txt" auf 1Kb = 1 Zeile zusammen, die "instrum.txt" ändert sich nicht.
instrum.txt: jene Datei, bei der die aus den Dateien "train_one.txt", "retrain_one.txt" und "test_one.txt" chronologisch auszulesende 124. Spalte hinzugefügt und anschließend die komplette Datenmatrix nach der Anzahl der Zeilen wiederum als "train_one.txt" (Zeile 1 bis n-52), "retrain_one.txt"(n-51) und "test_one.txt" (letzte Spalte n) gespeichert werden soll (der Name der abzuspeichernden Dateien kann klarerweise auch anders lauten)
Die vier Dateien habe ich aufgrund der Größe extern speichern müssen; du findest sie unter folgendem Link:
http://members.kabsi.at/e0104/herber/
Besten Dank vorab!
Horst
Anzeige
AW: .txt's mit VBA bearbeiten
04.07.2012 20:49:53
Josef

Hallo Horst,
ich hatte einen kleinen Fehler im Code, aber deine Dateien sind auch anders aufgebaut als die welche du vorher hochgeladen hattest.
Teste mal.
Sub changeTXT_File2()
  Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As String
  Dim strTmpFile As String, strTmp As String
  Dim lngIndex As Long, lngCol As Long, lngC As Long
  Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
  Dim FF1 As Integer, FF2 As Integer
  
  strTrainFile = "E:\Temp\Test3\train_one.txt" 'Dateipfad train.txt - Anpassen
  strReTrainFile = "E:\Temp\Test3\retrain_one.txt" 'Dateipfad retrain.txt - Anpassen
  strTestFile = "E:\Temp\Test3\test_one.txt" 'Dateipfad test.txt - Anpassen
  strFile = "E:\Temp\Test3\instrum.txt" 'Dateipfad Eingabe-Datei - Anpassen!
  
  strTmpFile = Environ("TEMP") & "\Temp.txt"
  
  '## train.txt einlesen
  FF1 = FreeFile
  
  Open strTrainFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTrain(lngIndex)
    strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## retrain.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strReTrainFile For Input As #FF1
  lngIndex = 0
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strReTrain(lngIndex)
    strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## test.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strTestFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTest(lngIndex)
    strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## train.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTrainFile
  Name strTmpFile As strTrainFile
  
  '## retrain.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strReTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + 1 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strReTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strReTrainFile
  Name strTmpFile As strReTrainFile
  
  '## test.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTest)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + UBound(strReTrain) + 2 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTest(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTestFile
  Name strTmpFile As strTestFile
  
End Sub



« Gruß Sepp »

Anzeige
AW: .txt's mit VBA bearbeiten
05.07.2012 23:13:26
Horst
Hallo Sepp,
das Makro arbeitet superschnell, jetzt müsste lediglich noch eine Kleinigkeit ausgebessert werden. Die neu generierte "test.txt" ist leer. Wie ich bemerkt habe, liegt dies daran, dass die Datenmatrix um eine Zeile verschoben ist: die letzte Zeile der neu generierten "retrain.txt" entspricht der "test.txt", die erste Zeile der "retrain.txt" sollte die letzte Zeile der "train.txt" sein. Wäre super, wenn du das noch korrigieren könntest. Hab dir ein .xls mitgeschickt.
ftp://members.kabsi.at/herber/Sepp.xls
Gruß, Horst
AW: .txt's mit VBA bearbeiten
05.07.2012 23:16:01
Horst
Sorry, der Link lautet natürlich: http://members.kabsi.at/e0104/herber/
AW: .txt's mit VBA bearbeiten
05.07.2012 23:53:18
Josef

Hallo Horst,
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Sub changeTXT_File2()
  Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As String
  Dim strTmpFile As String, strTmp As String
  Dim lngIndex As Long, lngCol As Long, lngC As Long
  Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
  Dim FF1 As Integer, FF2 As Integer
  
  strTrainFile = "E:\Temp\Test3\train_one.txt" 'Dateipfad train.txt - Anpassen
  strReTrainFile = "E:\Temp\Test3\retrain_one.txt" 'Dateipfad retrain.txt - Anpassen
  strTestFile = "E:\Temp\Test3\test_one.txt" 'Dateipfad test.txt - Anpassen
  strFile = "E:\Temp\Test3\instrum.txt" 'Dateipfad Eingabe-Datei - Anpassen!
  
  strTmpFile = Environ("TEMP") & "\Temp.txt"
  
  '## train.txt einlesen
  FF1 = FreeFile
  
  Open strTrainFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTrain(lngIndex)
    strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## retrain.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strReTrainFile For Input As #FF1
  lngIndex = 0
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strReTrain(lngIndex)
    strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## test.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strTestFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTest(lngIndex)
    strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## train.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTrainFile
  Name strTmpFile As strTrainFile
  
  '## retrain.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strReTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strReTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strReTrainFile
  Name strTmpFile As strReTrainFile
  
  '## test.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC <= UBound(strTest)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + UBound(strReTrain) + 1 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTest(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTestFile
  Name strTmpFile As strTestFile
  
End Sub



« Gruß Sepp »

AW: .txt's mit VBA bearbeiten
06.07.2012 15:23:50
Horst
Hallo Sepp,
wir sind fast am Ziel. Lediglich in der neugenerierten "train.txt" muss die letzte Zeile weg, die ist überflüssig, da sie zugleich die erste Zeile der "retrain.txt" darstellt (siehe: http://members.kabsi.at/e0104/herber/Sepp.xls).
Danke vorab,
Horst
AW: .txt's mit VBA bearbeiten
06.07.2012 21:08:45
Josef

Hallo Horst,
dann so.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Sub changeTXT_File2()
  Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As String
  Dim strTmpFile As String, strTmp As String
  Dim lngIndex As Long, lngCol As Long, lngC As Long
  Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
  Dim FF1 As Integer, FF2 As Integer
  
  strTrainFile = "E:\Temp\Test3\train_one.txt" 'Dateipfad train.txt - Anpassen
  strReTrainFile = "E:\Temp\Test3\retrain_one.txt" 'Dateipfad retrain.txt - Anpassen
  strTestFile = "E:\Temp\Test3\test_one.txt" 'Dateipfad test.txt - Anpassen
  strFile = "E:\Temp\Test3\instrum.txt" 'Dateipfad Eingabe-Datei - Anpassen!
  
  strTmpFile = Environ("TEMP") & "\Temp.txt"
  
  '## train.txt einlesen
  FF1 = FreeFile
  
  Open strTrainFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTrain(lngIndex)
    strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## retrain.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strReTrainFile For Input As #FF1
  lngIndex = 0
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strReTrain(lngIndex)
    strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## test.txt einlesen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strTestFile For Input As #FF1
  Do While Not EOF(FF1)
    Line Input #FF1, strTmp
    vntTmp = Split(strTmp, Chr(9))
    Redim Preserve strTest(lngIndex)
    strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
    lngIndex = lngIndex + 1
  Loop
  Close #FF1
  
  '## train.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC < UBound(strTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTrainFile
  Name strTmpFile As strTrainFile
  
  '## retrain.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC < UBound(strReTrain)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strReTrain(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strReTrainFile
  Name strTmpFile As strReTrainFile
  
  '## test.txt erstellen
  FF1 = FreeFile
  lngIndex = 0
  lngC = 0
  
  Open strFile For Input As #FF1
  FF2 = FreeFile
  Open strTmpFile For Output As #FF2
  Do While Not EOF(FF1) And lngC < UBound(strTest)
    Line Input #FF1, strTmp
    lngIndex = lngIndex + 1
    If lngIndex > 5 + UBound(strTrain) + UBound(strReTrain) + 1 Then
      vntTmp = Split(strTmp, Chr(9))
      strTmp = ""
      If UBound(vntTmp) > 1 Then
        For lngCol = 2 To UBound(vntTmp)
          strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
        Next
      End If
      strTmp = strTmp & strTest(lngC)
      lngC = lngC + 1
      Print #FF2, strTmp
    End If
  Loop
  Close #FF2
  Close #FF1
  
  Kill strTestFile
  Name strTmpFile As strTestFile
  
End Sub



« Gruß Sepp »

AW: .txt's mit VBA bearbeiten
06.07.2012 22:37:59
Horst
Hallo Sepp!
Jetzt stimmt die "train.txt", dafür ist die "test.txt" aber leer (0 Kb) und bei der "retrain.txt" fehlt die letzte Zeile.
Ich glaube am besten ist es, den Code (vorletzte Version) im Anhang zu nehmen und einfach die letzte Zeile der "train.txt" zu löschen.
Danke vorab,
Gruß, Horst
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************
Option Explicit
Sub changeTXT_File2()
Dim strFile As String, strTrainFile As String, strReTrainFile As String, strTestFile As  _
String
Dim strTmpFile As String, strTmp As String
Dim lngIndex As Long, lngCol As Long, lngC As Long
Dim vntTmp As Variant, strTrain() As String, strReTrain() As String, strTest() As String
Dim FF1 As Integer, FF2 As Integer
strTrainFile = "E:\Temp\Test3\train_one.txt" 'Dateipfad train.txt - Anpassen
strReTrainFile = "E:\Temp\Test3\retrain_one.txt" 'Dateipfad retrain.txt - Anpassen
strTestFile = "E:\Temp\Test3\test_one.txt" 'Dateipfad test.txt - Anpassen
strFile = "E:\Temp\Test3\instrum.txt" 'Dateipfad Eingabe-Datei - Anpassen!
strTmpFile = Environ("TEMP") & "\Temp.txt"
'## train.txt einlesen
FF1 = FreeFile
Open strTrainFile For Input As #FF1
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, Chr(9))
Redim Preserve strTrain(lngIndex)
strTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
lngIndex = lngIndex + 1
Loop
Close #FF1
'## retrain.txt einlesen
FF1 = FreeFile
lngIndex = 0
Open strReTrainFile For Input As #FF1
lngIndex = 0
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, Chr(9))
Redim Preserve strReTrain(lngIndex)
strReTrain(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
lngIndex = lngIndex + 1
Loop
Close #FF1
'## test.txt einlesen
FF1 = FreeFile
lngIndex = 0
Open strTestFile For Input As #FF1
Do While Not EOF(FF1)
Line Input #FF1, strTmp
vntTmp = Split(strTmp, Chr(9))
Redim Preserve strTest(lngIndex)
strTest(lngIndex) = Trim$(Application.Clean(vntTmp(123)))
lngIndex = lngIndex + 1
Loop
Close #FF1
'## train.txt erstellen
FF1 = FreeFile
lngIndex = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 Then
vntTmp = Split(strTmp, Chr(9))
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
Next
End If
strTmp = strTmp & strTrain(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strTrainFile
Name strTmpFile As strTrainFile
'## retrain.txt erstellen
FF1 = FreeFile
lngIndex = 0
lngC = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 + UBound(strTrain) Then
vntTmp = Split(strTmp, Chr(9))
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
Next
End If
strTmp = strTmp & strReTrain(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strReTrainFile
Name strTmpFile As strReTrainFile
'## test.txt erstellen
FF1 = FreeFile
lngIndex = 0
lngC = 0
Open strFile For Input As #FF1
FF2 = FreeFile
Open strTmpFile For Output As #FF2
Do While Not EOF(FF1) And lngC  5 + UBound(strTrain) + UBound(strReTrain) + 1 Then
vntTmp = Split(strTmp, Chr(9))
strTmp = ""
If UBound(vntTmp) > 1 Then
For lngCol = 2 To UBound(vntTmp)
strTmp = strTmp & Trim$(Application.Clean(vntTmp(lngCol))) & Chr(9)
Next
End If
strTmp = strTmp & strTest(lngC)
lngC = lngC + 1
Print #FF2, strTmp
End If
Loop
Close #FF2
Close #FF1
Kill strTestFile
Name strTmpFile As strTestFile
End Sub

AW: .txt's mit VBA bearbeiten
30.06.2012 22:17:43
bst
Auch Hallo,
es gibt Dinge die sich M.E. leichter auf einer Kommandozeile lösen lassen. Insbesondere wenn man da noch ein paar Linux-Kommandozeilen-Tools verfügbar hat.
Ich würde das denn hier mit einem Einzeiler lösen...
Just my 0,02€,
Bernd
--
$ sed '1,5d' 80816.neu

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige