AW: Zeilen in neue Mappen kopieren
29.12.2007 18:38:33
Erich
Hi Steffen,
das mit dem Fehler 1004 verstehe ich nicht - dafür stand vor dem SaveAs doch "On Error Resume Next".
Besser ist es ohnehin, ohne diese Fehlersteuerung auszukommen.
Ob unter dem Namen schon eine Datei existiert, kann man vor dem Speicherversuch prüfen.
Das macht die Version 5:
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
(ByVal PFAD As String) As Long
Sub tst5()
Dim iRow As Long, lngP As Long, strN As String, intE As Integer, strPath As String
strPath = ThisWorkbook.Path & "\" & Format(Date, "yyyy-mm-dd") & "\"
If MakeSureDirectoryPathExists(strPath) = 0 Then
MsgBox "Das Verzeichnis" & vbLf & strPath & vbLf _
& "konnte nicht erstellt werden!", vbCritical, "Abbruch"
Exit Sub
End If
With Worksheets("T_LW_Vorlage")
.Select
.Cells(1, 1).Select
iRow = 2
Do Until IsEmpty(.Cells(iRow, 1))
Workbooks.Add xlWBATWorksheet ' neue Mappe mit 1 leerem Blatt
.Range(.Cells(1, 1), .Cells(1, 25)).Copy Cells(1, 1) ' Überschrift
lngP = 0 ' Anzahl Zeilen bestimmen
While .Cells(iRow, 24) = .Cells(iRow + lngP + 1, 24)
lngP = lngP + 1
Wend
.Range(.Cells(iRow, 1), .Cells(iRow + lngP, 25)).Copy Cells(2, 1) ' Zeile(n)
' ' speichern
strN = strPath & Cells(2, 4) & " " & Cells(2, 24)
If UCase(Right(strN, 4)) ".xls" Then strN = strN & ".xls"
If Dir(strN) > "" Then
intE = MsgBox(strN & vbLf & "existiert bereits." & vbLf & vbLf _
& "Soll die Datei überschrieben werden?", vbYesNoCancel + vbQuestion)
Select Case intE
Case vbYes
Kill strN
ActiveWorkbook.SaveAs strN
ActiveWorkbook.Close
Case vbCancel
Exit Sub
Case Else ' bei "Nein" wird die Mappe nicht gespeichert und bleibt offen
End Select
Else
ActiveWorkbook.SaveAs strN
ActiveWorkbook.Close
End If
iRow = iRow + lngP + 1
Loop
End With
End Sub
Das kannst du gleich in der Mappe ausprobieren:
https://www.herber.de/bbs/user/48698.xls
Rückmeldung wäre nett! - Grüße von Erich aus Kamp-Lintfort