vba count # of files in folder

zrx1200

Well-known Member
Joined
Apr 14, 2010
Messages
622
Office Version
  1. 2019
Platform
  1. Windows
Hello,

I'm looking at being able to count the # of files in a folder that match a varible.

I only need the first portion of the filename. example filename is Apr 4 6 00 0.09 and I only need the Apr 4 portion which is the date.

I will then compare the current date (which is the varible) in code to the filename portion and count how many files are present.

The path for folder is constant.

Any thought folks?

Thanks,
Ralph
 
Re: SOLVED Re: vba count # of files in folder

There's more than one way to get a list but as an example if the criteria is in cells A2, A3, and A4 then you can add the counts in B2, B3, and B4:


Code:
Sub test()
    
    Dim fso As Object 'Scripting.FileSystemObject
    Dim fldr As Object 'Scripting.Folder
    Dim f As Object 'Scripting.File
    Dim lookFor As String
    Dim c As Range
    
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    With fso
        Set fldr = .GetFolder("C:\myTemp")
        For Each c In Range("A2:A4")
            lookFor = c.Value
            For Each f In fldr.Files
                If f.Name Like "*" & lookFor & "*" Then
                    c.Offset(0, 1).Value = c.Offset(0, 1).Value + 1
                End If
            Next f
        Next c
    End With
    
End Sub

Result:
----------------------------
| FileNameCriteria | Count |
----------------------------
|            Apr 4 |     2 |
|            Apr 5 |     2 |
|           Apr 10 |     3 |
----------------------------
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top