Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
920to924
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
920to924
920to924
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Ordner und Datei anlegen?

Ordner und Datei anlegen?
27.10.2007 14:38:00
Heiko
Hi
ich habe eine Datei mit ca. 50 Registern (sheets)
Jedes Register hat in der Zelle D7 einen "Verantwortlichen"
Jetzt möchte ich das im Ordner Eigene Dateien ein Ordner angelegt wird mit dem Namen "Auswertung aktuelles Datum"
in diesem Ordner soll jetzt für jeden " Verantwortlichen" eine Exceldatei angelegt werden, die den Namen trägt und in dem Ordner sollen alle Sheets der 50 Stück sein ,die dem Verantwortlichen sind!
Wie bekomme ich so was hin? Geht das?

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Ordner und Datei anlegen?
27.10.2007 14:55:00
Josef
Hallo Heiko,
kopiere den Code in ein Modul deiner Datei.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
    (ByVal lpPath As String) As Long

Sub CopySheets()
Dim objWs As Worksheet
Dim strDir As String, strPath As String

On Error GoTo ErrExit
GMS

strDir = "F:\Temp" 'Zielverzeichnis - Anpassen

If Right(strDir, 1) <> "\" Then strDir = strDir & "\"

strDir = strDir & "Auswertung_" & Format(Date, "yyyymmdd") & "\"

For Each objWs In ThisWorkbook.Worksheets
    If objWs.Range("D7") <> "" Then
        strPath = strDir & objWs.Range("D7") & "\" & objWs.Name & ".xls"
        If MakeSureDirectoryPathExists(strPath) <> 0 Then
            objWs.Copy
            ActiveWorkbook.SaveAs strPath
            ActiveWorkbook.Close
        End If
    End If
Next

ErrExit:
GMS True
Set objWs = Nothing
End Sub

Sub GMS(Optional ByVal Modus As Boolean = False)
Static lngCalc As Long

With Application
    .ScreenUpdating = Modus
    .EnableEvents = Modus
    .DisplayAlerts = Modus
    .EnableCancelKey = IIf(Modus, 1, 0)
    If Modus Then
        .Calculation = lngCalc
    Else
        lngCalc = .Calculation
        .Calculation = xlCalculationManual
    End If
    .Cursor = IIf(Modus, -4143, 2)
    .CutCopyMode = False
End With

End Sub

Gruß Sepp

Anzeige
AW: Ordner und Datei anlegen?
27.10.2007 15:03:00
Heiko
Danke klappt super!
Geht aus auch das im Ordner des verantwortlichen nur eine Datei liegt und da alle Sheets drin sind?

AW: Ordner und Datei anlegen?
27.10.2007 15:11:00
Josef
Hallo Jens,
klar, geht auch.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
    (ByVal lpPath As String) As Long

Sub CopySheets()
Dim objWs As Worksheet, objWB As Workbook
Dim strDir As String, strPath As String

On Error GoTo ErrExit
GMS

strDir = "F:\Temp" 'Zielverzeichnis - Anpassen

If Right(strDir, 1) <> "\" Then strDir = strDir & "\"

strDir = strDir & "Auswertung_" & Format(Date, "yyyymmdd") & "\"

For Each objWs In ThisWorkbook.Worksheets
    If objWs.Range("D7") <> "" Then
        strPath = strDir & objWs.Range("D7") & "\" & objWs.Range("D7") & ".xls"
        
        On Error Resume Next
        Set objWB = Workbooks.Open(strPath)
        On Error GoTo ErrExit
        
        If MakeSureDirectoryPathExists(strPath) <> 0 Then
            If objWB Is Nothing Then
                objWs.Copy
                ActiveWorkbook.SaveAs strPath
                ActiveWorkbook.Close
            Else
                objWs.Copy after:=objWB.Sheets(objWB.Sheets.Count)
                objWB.Close True
            End If
        End If
    End If
Next

ErrExit:
GMS True
Set objWs = Nothing
Set objWB = Nothing
End Sub

Sub GMS(Optional ByVal Modus As Boolean = False)
Static lngCalc As Long

With Application
    .ScreenUpdating = Modus
    .EnableEvents = Modus
    .DisplayAlerts = Modus
    .EnableCancelKey = IIf(Modus, 1, 0)
    If Modus Then
        .Calculation = lngCalc
    Else
        lngCalc = .Calculation
        .Calculation = xlCalculationManual
    End If
    .Cursor = IIf(Modus, -4143, 2)
    .CutCopyMode = False
End With

End Sub

Gruß Sepp

Anzeige
Oder besser so.
27.10.2007 15:18:00
Josef
Hallo nochmal,
nimm diesen Code.
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
    (ByVal lpPath As String) As Long

Sub CopySheets()
Dim objWs As Worksheet, objWB As Workbook
Dim strDir As String, strPath As String

On Error GoTo ErrExit
GMS

strDir = "F:\Temp" 'Zielverzeichnis - Anpassen

If Right(strDir, 1) <> "\" Then strDir = strDir & "\"

strDir = strDir & "Auswertung_" & Format(Date, "yyyymmdd") & "\"

For Each objWs In ThisWorkbook.Worksheets
    If objWs.Range("D7") <> "" Then
        strPath = strDir & objWs.Range("D7") & "\" & objWs.Range("D7") & ".xls"
        
        If MakeSureDirectoryPathExists(strPath) <> 0 Then
            If Dir(strPath) = "" Then
                objWs.Copy
                ActiveWorkbook.SaveAs strPath
                ActiveWorkbook.Close
            Else
                Set objWB = Workbooks.Open(strPath)
                objWs.Copy after:=objWB.Sheets(objWB.Sheets.Count)
                objWB.Close True
            End If
        End If
    End If
Next

ErrExit:
GMS True
Set objWs = Nothing
Set objWB = Nothing
End Sub

Sub GMS(Optional ByVal Modus As Boolean = False)
Static lngCalc As Long

With Application
    .ScreenUpdating = Modus
    .EnableEvents = Modus
    .DisplayAlerts = Modus
    .EnableCancelKey = IIf(Modus, 1, 0)
    If Modus Then
        .Calculation = lngCalc
    Else
        lngCalc = .Calculation
        .Calculation = xlCalculationManual
    End If
    .Cursor = IIf(Modus, -4143, 2)
    .CutCopyMode = False
End With

End Sub

Gruß Sepp

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige