Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1596to1600
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 speichern

Excel Zeilen als einzelne Text-Dateien speichern
16.12.2017 08:11:01
Robert
Hallo zusammen, ich bin am verzweifeln.
https://www.herber.de/bbs/user/118365.xlsx
- Ich habe ein Excel Dokument mit 11 Zeilen und 7 Spalten (die aber noch erweitert werden sollen).
- Ich möchte jede Zeile von C4:K4 als eine .txt Datei in einem Ordner speichern.
- Der Name der .txt-Datei soll der Zelleninhalt von C4 sein.
- Ich möchte diese Liste ständig erweitern, so dass alle Änderungen auch in dem Skript mit berücksichtigt werden.
Wäre echt super, wenn mir da jemand helfen könnte. Habe echt nicht viel Erfahrung in VBA daher wäre ein komplettes Skript wirklich sehr hilfreich.
Viele Dank schon einmal im Voraus.
Robert

27
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
16.12.2017 08:38:32
Robert
Nachtrag:
Falls man im Dokument ein "OK" Button noch integrieren könnte der erst beim klicken die Zeilen neu speichert bzw. überschreibt. Wäre das wirklich sehr sehr hilfreich.
AW: Excel Zeilen als einzelne Text-Dateien speichern
16.12.2017 15:03:40
Robert
Folgendes Skript habe ich schon versucht:
Aber leider werden die Spalten von D4 bis K4 nicht berücksichtigt.
Sub ErstelleDateien()
Ziel = "C:\Users\Robert\Desktop\Skriptcode"
Typ = ".txt"
AbZeile = 3
Spalte = "C"
Zeile = AbZeile
Nr = 1000001
Set fso = CreateObject("Scripting.FileSystemObject")
If Right(Ziel, 1)  "\" Then Ziel = Ziel & "\"
Do While Cells(Zeile, Spalte).Value  ""
fso.CreateTextFile(Ziel & Cells(Zeile, Spalte) & Typ).Write Cells(Zeile, Spalte).Value
Zeile = Zeile + 1
Nr = Nr + 1
Loop
End Sub

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
16.12.2017 16:56:54
Sepp
Hallo Robert,
' **********************************************************************
' 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

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 08:59:51
Robert
Hallo Sepp, vielen Dank für dein Skript, doch leider bekomme ich eine Fehlermeldung, wenn ich das Skript so bei mir einfüge.
Fehlermeldung:
Laufzeitfehler '76'
Pfad nicht gefunden
Kannst du mir da nochmal weiter helfen?
Vielen lieben Dank.
Robert
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 09:04:30
Sepp
Hallo Robert,
dann musst du bei 'strPath =' den richtigen Pfad angeben!
Gruß Sepp

AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 09:15:02
Robert
Sepp, großartig Danke dir!!! Echt spitze!
Kannst du mir vielleicht noch sagen, wie man das Skript ausführen kann (Vllt einen OK Button) ohne in das Skript ständig rein gehen zu müssen.
Wirklich danke nochmal.
Gruß Robert
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 10:22:30
Robert
Hi Sepp, kannst du mir noch schnell sagen, wie man das Trennzeichen "," in ein "Zeilenumbruch" ändern kann?
strDivider = ","
Dort hätte ich gerne ein Zeilenumbruch.
Gruß Robert
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 10:24:51
Sepp
Hallo Robert,
strDivider = vbLf
Gruß Sepp

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 10:32:47
Robert
Hab ich versucht, wenn ich den Befehl wie oben beschrieben anstatt "," eingebe, sind keine Trennzeichen in der .txt Datei. Der Text bleibt quasi in der gleichen Spalte nur ohne Trennzeichen.
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 10:52:48
Robert

strDivider = vbCrLf
damit hat es geklappt.
Teilst du das dann bitte auch im VBA-Forum mit.
17.12.2017 11:04:07
Werner
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 15:56:28
Robert
Hi Sepp,
Wie kann ich mit diesem Skript die .txt Dateien auf einem FTP-Server hoch laden?
Ein einfaches ändern
strPath = "ftp://....."
Ist leider nicht möglich.
Hast du da noch eine Idee?
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 17:16:12
Sepp
Hallo Robert,
direkt speichern nicht, aber hinterher hochladen.
https://www.herber.de/bbs/user/118384.xlsm
Im Modul2 musst du die Daten deines FTP-Servers eintragen!
Gruß Sepp

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 19:12:05
Robert
Wow danke dir für das ausführlich Skript!
Aber i-was scheint noch nicht ganz zu funktionieren.
Fehlermeldung:
C:\Users\Rober\AppData\Local\Name1.txt;ftp://....mein Zielordner
Error: Unknown.
(Im C:\Users\Rober.... ist die Datei vorhanden)
Im Modul1 muss ich doch noch mein ftp://Zielordner angeben oder?
Also hier:
strFTPFolder = "ftp://...."
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 20:00:55
Sepp
Hallo Robert,
du musst den Ordner angeben und nicht den kompletten Pfad zum Server.

strFTPFolder = "Zielordner"
Die Serverdaten musst du im Modul2 angeben.
Gruß Sepp

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 20:45:07
Robert
Und wo wird dann der Ziel Pfad/Verzeichnis eingegeben?
Ich muss doch dem Skript sagen wo sich der Ordner befindet oder nicht?
Wenn ich nur den Zielordner angebe geht es leider immer noch nicht.
Würde ja dann nur so im Modul1 aussehen.
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
Dim strFiles() As String, strFTPFolder As String, lngIndex As Long
strFTPFolder = "raspi1" 'Zielverzeichnis FTP
strPath = Environ("TEMP") 'Zielpfad

If Right(strPath, 1) "\" Then strPath = strPath & "\"
strDivider = vbCrLf 'Trennzeichen der Textdatei - Anpassen
lngStartRow = 4 'Erste Zeile mit Daten
lngStartCol = 3 'Erste Spalte (Dateiname)
......
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 20:50:11
Sepp
Hallo Robert,
ich kenne deine Ordnerstruktur auf deinem FTP-Server nicht!
Wie sieht den der gesamte FTP-Pfad aus?
Gruß Sepp

AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 20:53:07
Robert
ftp://ic-visit.de/httpdocs/team-drop/raspi1/
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 21:13:46
Sepp
Hallo Robert,
mal ins Blaue.

"httpdocs/team-drop/raspi1"
Gruß Sepp

AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 21:43:04
Robert
leider auch nicht.
i-wo stimmt da was noch nicht. Hab fast alles schon durch getestet.
Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
17.12.2017 21:47:44
Sepp
Hallo Robert,
Benutzername und Passwort stimmen?
Gruß Sepp

AW: Excel Zeilen als einzelne Text-Dateien speichern
18.12.2017 08:56:35
Robert
Ja alles gecheckt.
Und alles mögliche schon probiert. Er will es einfach nicht auf den Server laden.
AW: Excel Zeilen als einzelne Text-Dateien speichern
18.12.2017 15:08:12
Sepp
Hallo Robert,
auch mit Backslash probiert?
strFTPFolder = "httpdocs\team-drop\raspi1" 'Zielverzeichnis FTP

Gruß Sepp

Anzeige
AW: Excel Zeilen als einzelne Text-Dateien speichern
18.12.2017 16:46:54
Robert
nein, hab alles versucht, klappt leider nicht
AW: Excel Zeilen als einzelne Text-Dateien speichern
18.12.2017 18:06:42
Sepp
Hallo Robert,
tausche den Code in Modul2 gegen folgenden.
Du musst natürlich wieder deine Daten in Modul2 eingeben!
' **********************************************************************
' Modul: Modul2 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const OpenAsDefault = -2
Private Const FailIfNotExist = 0
Private Const ForReading = 1
Private Const ForWriting = 2


Sub PutFTPFile(ByRef FilesToLoad() As String)
Dim strReturn As String
Dim lngIndex As Long, strMsg As String

'------------- Einstellungen START -----------------------
Const cstrFTPServer = "ftp.deinserver.com" 'ftp-Server
Const cstrUser = "username" 'Benutzername
Const cstrPassword = "passwort" 'Passwort
'------------- Einstellungen ENDE ---------------------------

For lngIndex = 0 To UBound(FilesToLoad)
  strReturn = FTPUpload(cstrFTPServer, cstrUser, cstrPassword, _
    Split(FilesToLoad(lngIndex), ";")(0), Split(FilesToLoad(lngIndex), ";")(1))
  strMsg = strMsg & Format(lngIndex + 1, "00") & vbTab & FilesToLoad(lngIndex) & _
    vbTab & strReturn & vbLf
Next

MsgBox strMsg

End Sub

Private Function FTPUpload(sSite, sUsername, sPassword, sLocalFile, _
  sRemotePath) As String


Dim oFTPScriptFSO As Object, oFTPScriptShell As Object
Dim sFTPScript As String, sFTPTemp As String, sFTPTempFile As String
Dim sFTPResults As String, sResults As String
Dim fFTPScript As Variant, fFTPResults As Variant
Dim varPath As Variant, lngI As Long

'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

sRemotePath = Trim(sRemotePath)
sLocalFile = Trim(sLocalFile)

'----------Path Checks---------
'Here we willcheck the path, if it contains
'spaces then we need to add quotes to ensure
'it parses correctly.

'added by j.ehrensberger 2017/12/18
sRemotePath = Replace(sRemotePath, "/", "\")

If InStr(1, sRemotePath, "\") > 0 And Len(sRemotePath) > 1 Then
  varPath = Split(sRemotePath, "\")
  For lngI = 0 To UBound(varPath)
    If InStr(varPath(lngI), " ") > 0 Then
      If Left(varPath(lngI), 1) <> """" And Right(varPath(lngI), 1) <> """" Then
        varPath(lngI) = """" & varPath(lngI) & """"
      End If
    End If
    sRemotePath = "cd " & varPath(lngI) & vbCrLf
  Next
Else 'end add
  If InStr(sRemotePath, " ") > 0 Then
    If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
      sRemotePath = """" & sRemotePath & """"
    End If
  End If
  
  'Check to ensure that a remote path was
  'passed. If it's blank then pass a "\"
  If Len(sRemotePath) = 0 Then
    'Please note that no premptive checking of the
    'remote path is done. If it does not exist for some
    'reason. Unexpected results may occur.
    sRemotePath = "\"
  End If
  sRemotePath = "cd " & sRemotePath & vbCrLf
End If
'Check the local path and file to ensure
'that either the a file that exists was
'passed or a wildcard was passed.
If InStr(sLocalFile, "*") Then
  If InStr(sLocalFile, " ") Then
    FTPUpload = "Error: Wildcard uploads do not work if the path contains a " _
      & "space." & vbCrLf
    FTPUpload = FTPUpload & _
      "This is a limitation of the Microsoft FTP client."
    Exit Function
  End If
ElseIf Len(sLocalFile) = 0 Or Dir(sLocalFile) = "" Then
  'nothing to upload
  FTPUpload = "Error: File Not Found."
  Exit Function
End If

If InStr(sLocalFile, " ") > 0 Then
  If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then
    sLocalFile = """" & sLocalFile & """"
  End If
End If

'--------END Path Checks---------

'build input file for ftp command
sFTPScript = sFTPScript & "USER " & sUsername & vbCrLf
sFTPScript = sFTPScript & sPassword & vbCrLf
sFTPScript = sFTPScript & sRemotePath
sFTPScript = sFTPScript & "binary" & vbCrLf
sFTPScript = sFTPScript & "prompt n" & vbCrLf
sFTPScript = sFTPScript & "put " & sLocalFile & vbCrLf
sFTPScript = sFTPScript & "quit" & vbCrLf & "quit" & vbCrLf & "quit" & vbCrLf


sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

'Write the input file for the ftp command
'to a temporary file.
Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
fFTPScript.WriteLine (sFTPScript)
fFTPScript.Close
Set fFTPScript = Nothing

oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite & _
  " > " & sFTPResults, 0, True

Sleep 1000

'Check results of transfer.
Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _
  FailIfNotExist, OpenAsDefault)
sResults = fFTPResults.ReadAll
fFTPResults.Close

oFTPScriptFSO.DeleteFile (sFTPTempFile)
oFTPScriptFSO.DeleteFile (sFTPResults)

If InStr(sResults, "226 Transfer complete") > 0 Then
  FTPUpload = "Done"
ElseIf InStr(sResults, "File not found") > 0 Then
  FTPUpload = "Error: File Not Found"
ElseIf InStr(sResults, "cannot log in") > 0 Then
  FTPUpload = "Error: Login Failed."
Else
  FTPUpload = "Error: Unknown."
End If

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function



Private Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, _
  sRemotePath, sRemoteFile) As String


Dim oFTPScriptFSO As Object, oFTPScriptShell As Object
Dim sFTPScript As String, sFTPTemp As String, sFTPTempFile As String
Dim sFTPResults As String, sResults As String
Dim sOriginalWorkingDirectory As String
Dim fFTPScript As Variant, fFTPResults As Variant

'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

sRemotePath = Trim(sRemotePath)
sLocalPath = Trim(sLocalPath)

'----------Path Checks---------
'Here we will check the remote path, if it contains
'spaces then we need to add quotes to ensure
'it parses correctly.
If InStr(sRemotePath, " ") > 0 Then
  If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
    sRemotePath = """" & sRemotePath & """"
  End If
End If

'Check to ensure that a remote path was
'passed. If it's blank then pass a "\"
If Len(sRemotePath) = 0 Then
  'Please note that no premptive checking of the
  'remote path is done. If it does not exist for some
  'reason. Unexpected results may occur.
  sRemotePath = "\"
End If

'If the local path was blank. Pass the current
'working direcory.
If Len(sLocalPath) = 0 Then
  sLocalPath = oFTPScriptShell.CurrentDirectory
End If

If Not oFTPScriptFSO.FolderExists(sLocalPath) Then
  'destination not found
  FTPDownload = "Error: Local Folder Not Found."
  Exit Function
End If

sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
oFTPScriptShell.CurrentDirectory = sLocalPath
'--------END Path Checks---------

'build input file for ftp command
sFTPScript = sFTPScript & "USER " & sUsername & vbCrLf
sFTPScript = sFTPScript & sPassword & vbCrLf
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf
sFTPScript = sFTPScript & "binary" & vbCrLf
sFTPScript = sFTPScript & "prompt n" & vbCrLf
sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCrLf
sFTPScript = sFTPScript & "quit" & vbCrLf & "quit" & vbCrLf & "quit" & vbCrLf


sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

'Write the input file for the ftp command
'to a temporary file.
Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
fFTPScript.WriteLine (sFTPScript)
fFTPScript.Close
Set fFTPScript = Nothing

oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & sSite & _
  " > " & sFTPResults, 0, True

Sleep 1000

'Check results of transfer.
Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _
  FailIfNotExist, OpenAsDefault)
sResults = fFTPResults.ReadAll
fFTPResults.Close

'oFTPScriptFSO.DeleteFile(sFTPTempFile)
'oFTPScriptFSO.DeleteFile (sFTPResults)

If InStr(sResults, "226 Transfer complete.") > 0 Then
  FTPDownload = "Done"
ElseIf InStr(sResults, "File not found") > 0 Then
  FTPDownload = "Error: File Not Found"
ElseIf InStr(sResults, "cannot log in.") > 0 Then
  FTPDownload = "Error: Login Failed."
Else
  FTPDownload = "Error: Unknown."
End If

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function

Gruß Sepp

AW: Excel Zeilen als einzelne Text-Dateien speichern
19.12.2017 13:59:32
Robert
Hi Sepp,
es funktioniert!!! Ich habe beim Namen des FTP-Server das .de weg gelassen und siehe da es funktioniert.
Das Erste Skript das du mir in die Excel Tabelle rein gemacht hast ist das richtige!
Beim 2. Skript speichert er mir die Daten direkt auf den Server und berücksichtigt kein Unterverzeichnis, egal ob ich eines in Modul1 benenne oder nicht.
Eingaben werden so httpdocs/team-drop/raspi1/ erkannt!
Sepp, ich muss mich wirklich nochmal bei dir bedanken!!!
Klasse wie du mir da geholfen hast!!!
Respekt! Und Danke nochmal!

306 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige