Anzeige
Archiv - Navigation
1808to1812
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

Excel Zeilen als einzelne Text-Dateien

Excel Zeilen als einzelne Text-Dateien
08.02.2021 15:00:58
Marco
Hallo,
ich habe im Archiv ein Makro gefunden, welches jede Zeile in jeweils eine txt-Datei schreibt.
https://www.herber.de/forum/archiv/1596to1600/1597522_Excel_Zeilen_als_einzelne_TextDateien_speichern.html#1597567

' Modul: Modul1 Typ: Allgemeines Modul
Option Explicit
Sub ErstelleDateien()
Dim strPath As String, strText As String, strDivider As String, strFileName As String
Dim lngRow As Long, lngStartRow As Long, lngLastRow As Long
Dim lngCol As Long, lngStartCol As Long, lngLastCol As Long
Dim FF As Integer
strPath = "C:\Users\Robert\Desktop\Skriptcode" 'Zielpfad
If Right(strPath, 1)  "\" Then strPath = strPath & "\"
strDivider = ";" 'Trennzeichen der Textdatei - Anpassen
lngStartRow = 4 'Erste Zeile mit Daten
lngStartCol = 3 'Erste Spalte (Dateiname)
With Sheets("Speichern der Datei1") 'Tabellenname - Anpassen
lngLastRow = Application.Max(lngStartRow, .Cells(.Rows.Count, 4).End(xlUp).Row) 'letzte Zeile
lngLastCol = Application.Max(lngStartCol, .Cells(lngStartRow, .Columns.Count).End(xlToLeft). _
Column) 'letzte Spalte
For lngRow = lngStartRow To lngLastRow
strFileName = strPath & .Cells(lngRow, lngStartCol) & ".txt"
strText = ""
For lngCol = lngStartCol + 1 To lngLastCol
strText = strText & .Cells(lngRow, lngCol) & strDivider
Next
strText = Left(strText, Len(strText) - Len(strDivider))
FF = FreeFile
Open strFileName For Output As #FF
Print #FF, strText
Close #FF
Next
End With
End Sub
Gruß Sepp
Habe es für meine Bedürfnisse angepasst.
Allerdings möchte ich, dass in der txt-Datei den Inhalt jeder Zelle in "XXXX" Anführungszeichen steht.
Z.B. "abcdefg";"hijklmn";"opqrstuv"
Das habe ich leider nicht hinbekommen.
Kann mir da bitte jemand helfen?
Vielen Dank

5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien
08.02.2021 15:23:32
Rudi
Hallo,
versuch mal
strText = strText & chr(34) & .Cells(lngRow, lngCol) & chr(34) & strDivider

Gruß
Rudi
AW: Excel Zeilen als einzelne Text-Dateien
08.02.2021 15:25:37
Nepumuk
Hallo Marco,
teste mal:
Option Explicit

Public Sub ErstelleDateien()
    Dim strPath As String, strText As String, strDivider As String, strFileName As String
    Dim lngRow As Long, lngStartRow As Long, lngLastRow As Long
    Dim lngCol As Long, lngStartCol As Long, lngLastCol As Long
    Dim FF As Integer
    
    
    strPath = "H:\" ' "C:\Users\Robert\Desktop\Skriptcode" 'Zielpfad
    
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    
    strDivider = Chr$(34) & ";" & Chr$(34) 'Trennzeichen der Textdatei - Anpassen
    
    lngStartRow = 4 'Erste Zeile mit Daten
    
    lngStartCol = 3 'Erste Spalte (Dateiname)
    
    With Sheets("Speichern der Datei1") 'Tabellenname - Anpassen
        lngLastRow = Application.Max(lngStartRow, .Cells(.Rows.Count, 4).End(xlUp).Row) 'letzte Zeile
        lngLastCol = Application.Max(lngStartCol, .Cells(lngStartRow, .Columns.Count).End(xlToLeft).Column) 'letzte Spalte
        For lngRow = lngStartRow To lngLastRow
            strFileName = strPath & .Cells(lngRow, lngStartCol) & ".txt"
            strText = ""
            For lngCol = lngStartCol + 1 To lngLastCol
                strText = strText & .Cells(lngRow, lngCol) & strDivider
            Next
            strText = Chr$(34) & Left(strText, Len(strText) - Len(strDivider) + 1)
            FF = FreeFile
            Open strFileName For Output As #FF
            Print #FF, strText
            Close #FF
        Next
    End With
    
End Sub

Gruß
Nepumuk
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien
09.02.2021 06:54:48
Marco
Guten Morgen,
vielen Dank für die Antworten.
Beide Antworten funktionieren genau so, wie ich es haben wollte.
Wobei die Zeile von Rudi die für mich einfachere oder verständlichere Variante ist.
Vielen Dank und schön gesund bleiben.
Grüße Marco
AW: Excel Zeilen als einzelne Text-Dateien
10.02.2021 10:51:26
Marco
Hallo, ich hab da noch etwas.
ich würde gerne noch die Einträge in der zweiten Spalte der jeweiligen Zeile direkt im Dateinamen hinten anfügen.

' Modul: Modul1 Typ: Allgemeines Modul
' Jede Zeile wird in eine einzelne Txt-Datei geschrieben und
' gespeichert. Dateiname ist jeweils der Inhalt in der ersten Spalte
' Das Trennzeichen kann angepasst werden.
Option Explicit
Sub ErstelleDateien()
Dim strPath As String, strText As String, strDivider As String, strFileName As String
Dim lngRow As Long, lngStartRow As Long, lngLastRow As Long
Dim lngCol As Long, lngStartCol As Long, lngLastCol As Long
Dim FF As Integer
strPath = "C:\Temp\Import" 'Zielpfad
If Right(strPath, 1)  "\" Then strPath = strPath & "\"
strDivider = ";" 'Trennzeichen der Textdatei - Anpassen
lngStartRow = 1 'Erste Zeile mit Daten
lngStartCol = 1 'Erste Spalte (Dateiname)
With Sheets("Daten für Nav zum importieren") 'Tabellenname - Anpassen
lngLastRow = Application.Max(lngStartRow, .Cells(.Rows.Count, 4).End(xlUp).Row) 'letzte Zeile
lngLastCol = Application.Max(lngStartCol, .Cells(lngStartRow, .Columns.Count).End(xlToLeft). _
Column) 'letzte Spalte
For lngRow = lngStartRow To lngLastRow
strFileName = strPath & .Cells(lngRow, lngStartCol) & ".txt"
strText = ""
For lngCol = lngStartCol + 0 To lngLastCol + 1
strText = strText & Chr$(34) & .Cells(lngRow, lngCol) & Chr$(34) & strDivider 'Chr$(34)  _
ist das Zeichen "
Next
strText = Left(strText, Len(strText) - Len(strDivider))
FF = FreeFile
Open strFileName For Output As #FF
Print #FF, strText
Close #FF
Next
End With
End Sub
Das ist im Moment das aktuelle Makro.
Danke
Grüße Marco
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien
10.02.2021 11:35:18
Marco
Ich hab es hinbekommen.
strFileName = strPath & .Cells(lngRow, lngStartCol) & .Cells(lngRow, lngNextCol) & ".txt"
lngNextCol weiter oben als Long und eine Zeile, welche Spalte verwendet werden soll.
Grüße Marco

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige