AW: Inhalt von Listbox aus Userform exportieren
18.06.2007 02:06:00
Listbox
Hallo Till,
die Daten der Listbox kannst du auch ohne den Umweg über eine Arbeitsmappe direkt in ein Text/CSV-File schreiben.
Private Sub Image9_Click()
MsgBox "Einen Moment bitte," & vbLf & "die Daten werden geschrieben.", vbInformation, " "
Dim i As Long
Dim j As Long
Dim sFile As String, stext$, sTime$, sSep As String
iFilenr = FreeFile
sSep = ";"
sTime = Format(Now, "YYYYMMDD_hhmmss")
With Verkehr.ListBox1
sFile = ThisWorkbook.Path & Application.PathSeparator _
& "Datenexport_" & sTime & "_" & .List(0, 1) & ".csv"
Open sFile For Output As iFilenr
For i = 0 To .ListCount - 1
stext = .List(i, 0)
For j = 1 To .ColumnCount - 1
stext = stext & sSep & .List(i, j)
Next
Print #iFilenr, stext
stext = ""
Next
Close iFilenr
End With
MsgBox "Datei wurde angelegt:" & vbLf & sFile, vbInformation, " "
End Sub
Das Poblem bei deinem Code ist unter anderem das Einfügen des Datums in den Dateinamen. Punkte/ _
Doppelpunkte sind unzulässige Zeichen in Dateinamen. Mit folgenden Anpassungen sollte dein Code laufen:
Private Sub Image9_Click()
MsgBox "Einen Moment bitte," & vbLf & "die Daten werden geschrieben.", vbInformation, " "
Dim i As Long
Dim j As Long
Dim sFile As String, abook, bbook, dtTime As Date, sSep As String
Dim wbThis As Workbook, wbTemp As Workbook
Set wbThis = ThisWorkbook
Workbooks.Add
Set wbTemp = ActiveWorkbook
With Verkehr.ListBox1
For i = 0 To .ListCount - 1
For j = 0 To .ColumnCount - 1
Cells(i + 1, j + 1) = .List(i, j)
Next
Next
End With
dtTime = Now
abook = wbThis.Worksheets("Wertetabelle").Range("F1").Value ' VDE...
bbook = wbThis.Worksheets("Wertetabelle").Range("E5").Value ' Datum
sFile = wbThis.Path & Application.PathSeparator _
& "Datenexport_" & Format(dtTime, "YYYYMMDD_hhmmss") & "_" & ".csv" '& bbook & "_" _
& abook & "_" & ".csv"
wbTemp.SaveAs FileName:=sFile, FileFormat:=xlCSV
sSep = ";"
wbTemp.Close
MsgBox "Datei wurde angelegt:" & vbLf & sFile, vbInformation, " "
End Sub