+ Reply to Thread
Results 1 to 5 of 5

VBA Count the Files in a Folder

Hybrid View

KColgrove VBA Count the Files in a... 05-30-2019, 03:19 PM
Leith Ross Re: VBA Count the Files in a... 05-30-2019, 04:23 PM
KColgrove Re: VBA Count the Files in a... 05-30-2019, 07:49 PM
Leith Ross Re: VBA Count the Files in a... 05-30-2019, 11:17 PM
KColgrove Re: VBA Count the Files in a... 05-31-2019, 09:08 AM
  1. #1
    Registered User
    Join Date
    04-12-2019
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    64

    VBA Count the Files in a Folder

    I am trying to pull the count on how many files are in a certain folder. I have a list of the folder names in column A and want the count in column B. I am having trouble with the code I have right now.

    Sub NumberFile()
    
        Dim FolderPath As String, path As String, count As Integer
    
        Dim c As Range
        For Each c In Range("A1", Cells(Rows.count, "A").End(xlUp))
           FolderPath = c.Offset(0, 1).Formula = "='" & "P:\Tools\14 Day\" & c.Value & "\"
        
        path = FolderPath & "\*.xls"
        fileName = Dir(path)
        Next c
        Do While fileName <> ""
           count = count + 1
            fileName = Dir()
        Loop
    End Sub
    I am new to VBA, so when I use this macro nothing happens.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Count the Files in a Folder

    Hello KColgrove,

    Here is one way to do it...

    Sub GetFileCount()
    
        Dim Cell    As Range
        Dim Files   As Object
        Dim Folder  As Variant
        
            With CreateObject("Shell.Application")
                For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
                    Set Folder = .Namespace(Cell.Value)
                    If Not Folder Is Nothing Then
                        Set Files = Folder.Items
                        Files.Filter 64, "*.xls"
                        Cell.Offset(0, 1).Value = Files.Count
                    Else
                        Cell.Offset(0, 1).Value = "Folder not found."
                    End If
                Next Cell
            End With
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-12-2019
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    64

    Re: VBA Count the Files in a Folder

    This worked for me. I know this wasn't the original question asked, but it there a way to hover over the number and it display what the files are?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Count the Files in a Folder

    Hello KColgrove,

    This version of the macro adds a Comment to each cell in column "B" to display the files when you mouse over the cell.

    Sub GetFilesAndCount()
    
        Dim Cell    As Range
        Dim File    As Variant
        Dim Files   As Object
        Dim Folder  As Variant
        Dim n       As Long
        Dim Note    As Comment
        Dim Text    As String
        
            With CreateObject("Shell.Application")
                For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
                    Set Folder = .Namespace(Cell.Value)
                    If Not Folder Is Nothing Then
                        Set Files = Folder.Items
                        Files.Filter 64, "*.xls"
                        Cell.Offset(0, 1).Value = Files.Count
                    Else
                        Cell.Offset(0, 1).Value = "Folder not found."
                    End If
                    
                    Text = ""
                    Set Note = Cell.Offset(0, 1).Comment
                        If Not Note Is Nothing Then Note.Delete
                    Set Note = Cell.Offset(0, 1).AddComment
                        Note.Shape.TextFrame.AutoSize = True
                        
                    For Each File In Files
                        Text = File & vbLf
                        n = Note.Shape.TextFrame.Characters.Count + 1
                        Note.Shape.TextFrame.Characters(n, Len(Text)).Insert Text
                        n = n + Len(Text)
                    Next File
                    
                Next Cell
            End With
            
    End Sub

  5. #5
    Registered User
    Join Date
    04-12-2019
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    64

    Re: VBA Count the Files in a Folder

    That works perfectly. Thank you so much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Count number of rows of all files in folder
    By resulgul in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-07-2023, 01:06 AM
  2. VBA loop through all the files in the folder and count the used rows
    By Andy308 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-14-2016, 10:04 AM
  3. Replies: 2
    Last Post: 06-01-2012, 07:40 AM
  4. count files in a folder automatically??
    By benjii19 in forum Excel General
    Replies: 13
    Last Post: 03-01-2011, 11:32 AM
  5. count the number of files in a specified folder
    By bambino86 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-04-2009, 12:55 PM
  6. Folder Inventory & Count for like named files
    By sdnicsm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2005, 08:23 PM
  7. [SOLVED] Count files in folder
    By gmunro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-18-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1