Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1624to1628
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

Dateien im Verzeichnis zählen

Dateien im Verzeichnis zählen
21.05.2018 10:08:19
sigrid
Guten Morgen,
ich such ein Makro für das Zählen der vorhandenen Dateien im
Verzeichnis. Egal Excel oder auch DOCX.
In der aktuellen Datei: Mappe "Bestand"
Mein Verzeichnis: "C:\Lager\Osram\Bestand\2017\01 Januar\"
Im Jahr stehen die Monate von 01 Januar bis 12 Dezember
Schön wäre es wenn man das Verzeichnis auswählen könnte.
mit freundlichen Gruß
sigrid

14
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Dateien im Verzeichnis zählen
21.05.2018 11:44:06
Sepp
Hallo Sigrid,
Modul Modul1
Option Explicit 
 
Sub test() 
  Dim strPath As String 
 
  With Application.FileDialog(msoFileDialogFolderPicker) 
    .InitialFileName = "D:\" 
    .Title = "Dateien zählen - Ordnerauswahl" 
    .ButtonName = "Zählen..." 
    .InitialView = msoFileDialogViewList 
    If .Show = -1 Then 
      strPath = .SelectedItems(1) 
      If Right(strPath, 1) <> "\" Then strPath = strPath & "\" 
    End If 
  End With 
 
  If Len(strPath) Then 
 
    MsgBox countFiles(Directory:=strPath, SubFolders:=True) 
 
  End If 
 
End Sub 
 
Private Function countFiles(ByVal Directory As String, Optional ByVal FileName As String = "", Optional ByVal SubFolders As Boolean = False) As Long 
  Dim objFSO As Object, objFolder As Object, objFile As Object, objSubF As Object 
  Dim lngCount As Long 
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  Set objFolder = objFSO.GetFolder(Directory) 
 
  If Len(FileName) Then 
    For Each objFile In objFolder.Files 
      If objFile.Name Like FileName Then lngCount = lngCount + 1 
    Next 
  Else 
    lngCount = objFolder.Files.Count 
  End If 
 
  If SubFolders Then 
    For Each objSubF In objFolder.SubFolders 
      lngCount = lngCount + countFiles(objSubF.Path, FileName, SubFolders) 
    Next 
  End If 
 
  countFiles = lngCount 
 
  Set objSubF = Nothing 
  Set objFile = Nothing 
  Set objFolder = Nothing 
  Set objFSO = Nothing 
End Function 

VBA/HTML-CodeConverter, AddIn für Office 2002-2016 - in VBA geschrieben von Lukas Mosimann. Projektbetreuung:RMH Software & Media

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0


 ABCDEF
1Gruß Sepp
2
3

Anzeige
Danke für die Hilfe ! Bleibt stehen...
21.05.2018 14:41:09
sigrid
Hallo Sepp,
bleibt hier stehen, msoFileDialogFolderPicker
"Variable nicht definiert"
mfg
sigrid
AW: Danke für die Hilfe ! Bleibt stehen...
21.05.2018 14:56:33
Sepp
Hallo Sigrid,
sorry, hatte deine xl-Version nicht beachtet.
Modul Modul1
Option Explicit 
 
Sub test() 
  Dim strPath As String 
 
  strPath = ShellBrowseForFolder("D:\") 
 
  If Len(strPath) Then 
    MsgBox countFiles(Directory:=strPath, SubFolders:=True) 
  End If 
 
End Sub 
 
Private Function ShellBrowseForFolder(Optional StartPath As String = "") As String 
  Dim objShell    As Object 
  Dim lngRoot     As Long 
  Dim objFolder   As Object 
     
  lngRoot = 17 
   
  Set objShell = CreateObject("Shell.Application") 
  Set objFolder = objShell.BrowseForFolder(0, "Ordner auswählen", 0, IIf(Len(StartPath), StartPath, lngRoot)) 
  If Not objFolder Is Nothing Then 
    ShellBrowseForFolder = objFolder.Self.Path 
  End If 
  Set objFolder = Nothing 
  Set objShell = Nothing 
End Function 
 
Private Function countFiles(ByVal Directory As String, Optional ByVal FileName As String = "", Optional ByVal SubFolders As Boolean = False) As Long 
  Dim objFSO As Object, objFolder As Object, objFile As Object, objSubF As Object 
  Dim lngCount As Long 
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  Set objFolder = objFSO.GetFolder(Directory) 
 
  If Len(FileName) Then 
    For Each objFile In objFolder.Files 
      If objFile.Name Like FileName Then lngCount = lngCount + 1 
    Next 
  Else 
    lngCount = objFolder.Files.Count 
  End If 
 
  If SubFolders Then 
    For Each objSubF In objFolder.SubFolders 
      lngCount = lngCount + countFiles(objSubF.Path, FileName, SubFolders) 
    Next 
  End If 
 
  countFiles = lngCount 
 
  Set objSubF = Nothing 
  Set objFile = Nothing 
  Set objFolder = Nothing 
  Set objFSO = Nothing 
End Function 

VBA/HTML-CodeConverter, AddIn für Office 2002-2016 - in VBA geschrieben von Lukas Mosimann. Projektbetreuung:RMH Software & Media

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0


 ABCDEF
1Gruß Sepp
2
3

Anzeige
Das klappt einwandfrei, wie ...
21.05.2018 15:11:49
sigrid
Hallo Sepp,
klappt ohne Probleme.
Vielleicht kannst Du mich noch unterstützen, ich möchte das Ergebnis in die
aktuelle Sheet B1 setzen und die nächste Auswahl in B2 setzen...
mfg sigrid
AW: Das klappt einwandfrei, wie ...
21.05.2018 15:16:35
Sepp
Hallo Sigrid,
der Rest bleibt unverändert!
Sub test()
  Dim strPath As String, lngRow As Long

  strPath = ShellBrowseForFolder("D:\")

  If Len(strPath) Then
    If Range("B1") = "" Then
      lngRow = 1
    Else
      lngRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
    End If
    Cells(lngRow, 2) = countFiles(Directory:=strPath, SubFolders:=True)
  End If

End Sub

VBA/HTML-CodeConverter, AddIn für Office 2002-2016 - in VBA geschrieben von Lukas Mosimann. Projektbetreuung:RMH Software & Media

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0


 ABCDEF
1Gruß Sepp
2
3

Anzeige
Geil, oh na ja einfach toll
21.05.2018 15:47:29
sigrid
Hallo Sepp einfach toll !
Wenn ich die Zahlen der Monate jetzt einsetzte, habe auf B5 geändert,
würde es Sinn machen der ausgewählten Namen in die Zelle ab A5 ebenfalls einzusetzen,
geht das ?
mfg
sigrid
AW: Geil, oh na ja einfach toll
21.05.2018 15:49:48
Sepp
Hallo Sigrid,
ob es Sinn macht, musst schon du entscheiden.
so?
Sub test()
  Dim strPath As String, lngRow As Long

  strPath = ShellBrowseForFolder("D:\")

  If Len(strPath) Then
    If Range("B5") = "" Then
      lngRow = 5
    Else
      lngRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
    End If
    Cells(lngRow, 1) = strPath
    Cells(lngRow, 2) = countFiles(Directory:=strPath, SubFolders:=True)
  End If

End Sub

VBA/HTML-CodeConverter, AddIn für Office 2002-2016 - in VBA geschrieben von Lukas Mosimann. Projektbetreuung:RMH Software & Media

Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0


 ABCDEF
1Gruß Sepp
2
3

Anzeige
Einwandfrei, danke Sepp für alles !!! -)
21.05.2018 15:56:34
sigrid
Eine kleine Nachfrage !
21.05.2018 16:21:04
sigrid
Hallo Sepp,
ich habe das Makro nochmal erstellt und in Test2 umbenannt, habe
die Zeilen für das Einsetzen richtig gemacht.
Das Problem, es wird nicht der Pfad angezeigt wie beim ersten Makro,
sondern die Gesamtauswahl vom PC.
Was muß ich ändern ?
gruß
sigrid
AW: Eine kleine Nachfrage !
21.05.2018 16:31:13
Sepp
Hallo Sigrid,
hier
strPath = ShellBrowseForFolder("D:\")
gibst du das Startverzeichnis an.
Sonst bitte deinen gesamten Code zeigen.
 ABCDEF
1Gruß Sepp
2
3

Anzeige
Erledigt Pfad war mit \ versehen... --))
21.05.2018 16:31:38
sigrid
AW: Dateien im Verzeichnis zählen
21.05.2018 14:38:35
snb

Sub M_snb()
MsgBox CreateObject("scripting.filesystemobject").getfolder("G:\OF").Files.Count
End Sub

AW: Dateien im Verzeichnis zählen
21.05.2018 15:13:42
sigrid
Hallo snb,
leider klappt das nicht. Anzeige 0
Sub M_snb()
MsgBox CreateObject("scripting.filesystemobject").getfolder("C:\Lager\Osram\Bestand\2017\"). _
Files.Count
End Sub
mfg
sigrid
AW: Dateien im Verzeichnis zählen
21.05.2018 16:43:43
snb
Check das Pfad.
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige