Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1252to1256
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
CSV Spalten Aufteilen
Manuela
Hallo,
ich brauche noch mal Eure Profihilfe! :-)
Ich möchte das bestimmte Spalten aus einer CSV in meiner Excel-Lise verteilt werden. Soweit funktioniert das auch ganz gut, allerdings wenn ich es um einen Öffnen-Dialog erweitern wird geht alles kaputt..
Mit diesem Code funktioniert es:
Sub GetData()
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, arrSrc
strSrcFile = "C:\test.csv"
strDelimit = ";"
intFile = FreeFile()
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
arrSrc = Split(strTmp, strDelimit)
Cells(i + 5, 1) = arrSrc(1)
Cells(i + 5, 2) = arrSrc(4)
Cells(i + 5, 4) = arrSrc(4)
Cells(i + 5, 7) = arrSrc(0)
i = i + 1
Loop
Close
End Sub
Wenn ich ihn jetzt um einen Öffnen-Dialog erweitere, dann gibts einen Laufzeitfehler.
Sub GetData()
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, arrSrc
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = ""
.Filters.Add "CSV-Dateien", "*.csv", 1
.Filters.Add "Alle Dateien", "*.*", 2
If .Show = -1 Then
strSrcFile = .SelectedItems(1)
End If
End With
If strSrcFile  "" Then
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
arrSrc = Split(strTmp, strDelimit)
Cells(i + 5, 1) = arrSrc(1)
Cells(i + 5, 2) = arrSrc(4)
Cells(i + 5, 4) = arrSrc(4)
Cells(i + 5, 7) = arrSrc(0)
i = i + 1
Loop
Close
End If
End Sub
Kann mir jemand bei der Lösung helfen? Was mache ich falsch?
Laufzeitfehler: 52
Dateiname oder -nummer falsch
Sry, aber meine VBA-Fähigkeiten sind eher... ja... Ihr wisst schon.
Danke und Gruß,
Manu

7
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: CSV Spalten Aufteilen
02.03.2012 21:31:37
Fettertiger
Hallo Manu,
wie wäre es wenn Du erst mal intFile=freefile setzt:
Sub GetData()
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, arrSrc
intFile=freefile
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = ""
.Filters.Add "CSV-Dateien", "*.csv", 1
.Filters.Add "Alle Dateien", "*.*", 2
If .Show = -1 Then
strSrcFile = .SelectedItems(1)
End If
End With
If strSrcFile  "" Then
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
arrSrc = Split(strTmp, strDelimit)
Cells(i + 5, 1) = arrSrc(1)
Cells(i + 5, 2) = arrSrc(4)
Cells(i + 5, 4) = arrSrc(4)
Cells(i + 5, 7) = arrSrc(0)
i = i + 1
Loop
Close
End If
End Sub
Gruss
Theo
Anzeige
AW: CSV Spalten Aufteilen
02.03.2012 21:45:36
Manuela
Hallo Theo!
Danke für deine Hilfe, aber ich bekomme dafür jetzt an anderer Stelle einen Laufzeitfehler:
         Cells(i + 5, 1) = arrSrc(1)

Laufzeitfehler 9
Index außerhalb des gültigen Bereichs
Hättest ne Idee was da falsch gelaufen sein könnte?
Gruß,
Manu
AW: CSV Spalten Aufteilen
02.03.2012 22:00:47
dan
Hallo Manu,
da fehlt noch: strDelimit = ";"
Mfg dan, cz.
AW: CSV Spalten Aufteilen
03.03.2012 12:57:04
Manuela
Vielen Dank!
Jetzt funktinoert es wunderbar, allerdings ist mir noch was aufgefallen. Könntet Ihr mir dabei Helfen den Code so abzuändern, dass die 1. Zeile der CSV(Überschrift) nicht importiert wird?
Mein Code:
Sub CSVimport()
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, arrSrc
strDelimit = ";"
intFile = FreeFile
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = ""
.Filters.Add "CSV-Dateien", "*.csv", 1
.Filters.Add "Alle Dateien", "*.*", 2
If .Show = -1 Then
strSrcFile = .SelectedItems(1)
End If
End With
If strSrcFile  "" Then
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
arrSrc = Split(strTmp, strDelimit)
Cells(i + 4, 1) = arrSrc(1)
Cells(i + 4, 2) = arrSrc(2)
Cells(i + 4, 3) = arrSrc(3)
Cells(i + 4, 5) = arrSrc(4)
Cells(i + 4, 7) = arrSrc(5)
Cells(i + 4, 9) = arrSrc(7)
Cells(i + 4, 10) = arrSrc(8)
Cells(i + 4, 11) = arrSrc(9)
Cells(i + 4, 12) = arrSrc(10)
i = i + 1
Loop
Close
End If
End Sub
lg,
Manu
Anzeige
AW: CSV Spalten Aufteilen
03.03.2012 17:22:37
Manuela
Keiner ne Idee? :-(
lg,
Manu
AW: CSV Spalten Aufteilen
03.03.2012 17:47:27
Christian
Hallo Manu,
ein Vorschlag:
Option Explicit
Sub CSVimport()
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, k&, arrSrc
strDelimit = ";"
intFile = FreeFile
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = ""
.Filters.Add "CSV-Dateien", "*.csv", 1
.Filters.Add "Alle Dateien", "*.*", 2
If .Show Then
strSrcFile = .SelectedItems(1)
End If
End With
If strSrcFile  "" Then
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
If i > 0 Then
arrSrc = Split(strTmp, strDelimit)
Cells(i + 4, 1) = arrSrc(1)
Cells(i + 4, 2) = arrSrc(2)
Cells(i + 4, 3) = arrSrc(3)
Cells(i + 4, 5) = arrSrc(4)
Cells(i + 4, 7) = arrSrc(5)
Cells(i + 4, 9) = arrSrc(7)
Cells(i + 4, 10) = arrSrc(8)
Cells(i + 4, 11) = arrSrc(9)
Cells(i + 4, 12) = arrSrc(10)
End If
i = i + 1
Loop
Close
End If
End Sub

Gruß
Christian
Anzeige
AW: CSV Spalten Aufteilen
03.03.2012 20:56:47
Manuela
Vielen Dank!
Gruß,
Manu

301 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige