Results 1 to 2 of 2

Thread: Count files in a folder

  1. #1

    Count files in a folder

    Register for a FREE account, and/
    or Log in to avoid these ads!

    I've been using the code from the knowledge base here and have had great success on my PC.

    But I've now switched to a mac and cannot figure out how to make it work on there. I've obviously changed the directory path, but the cell stays blank instead of having the number.

    This is the code I have now:

    Private Function CountFiles(strDirectory As String, Optional strExt As String = "*.*") As Double
    'Author : Ken Puls (website)
    'Function purpose: To count files in a directory. If a file extension is provided,
    ' then count only files of that type, otherwise return a count of all files.
    Dim objFso As Object
    Dim objFiles As Object
    Dim objFile As Object
    'Set Error Handling
    On Error GoTo EarlyExit
    'Create objects to get a count of files in the directory
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFiles = objFso.GetFolder(strDirectory).Files
    'Count files (that match the extension if provided)
    If strExt = "*.*" Then
    CountFiles = objFiles.Count
    For Each objFile In objFiles
    If UCase(Right(objFile.Path, (Len(objFile.Path) - InStrRev(objFile.Path, ".")))) = UCase(strExt) Then
    CountFiles = CountFiles + 1
    End If
    Next objFile
    End If
    'Clean up
    On Error Resume Next
    Set objFile = Nothing
    Set objFiles = Nothing
    Set objFso = Nothing
    On Error GoTo 0
    End Function
    and the cell content:
    Any help offered would be greatly appreciated.

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Excel Version
    Comment-out the On Error line, put a breakpoint on the first line of the function (the line that starts Private Function) then step through the code with F8, it will either report an error or just exit the function, but you'll have an idea which line causes the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts