Macro for counting files by date range

DaveSaint1

New Member
Joined
Sep 19, 2019
Messages
4
Hi Everyone,

I need help.......

I am trying to create a macro which will count the number of .pdf files within a specified folder based on a a date range.

The dates are set in cells b5 and b6 (start date and end date) and the file location is in a cell named "Folder"

I have been watching VBA videos all day to try and figure this one out but i am no closer.

Thanks in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:

Code:
Sub counting_files_by_date()
  Dim wPath As String, wFile As Object, wFso As Object, n As Long
  wPath = Range("folder")
  If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
  If Dir(wPath, vbDirectory) = "" Then
    MsgBox "Folder does not exist"
    Exit Sub
  End If
  Set wFso = CreateObject("Scripting.FileSystemObject")
  For Each wFile In wFso.GetFolder(wPath).Files
    If LCase(Right(wFile, 3)) = "pdf" Then
      If wFile.Datecreated >= Range("B5") And wFile.Datecreated >= Range("B5") Then
        n = n + 1
      End If
    End If
  Next
  MsgBox "Number of PDF files: " & n, vbInformation, "COUNT PDF"
End Sub
 
Upvote 0
Try this:

Code:
Sub counting_files_by_date()
  Dim wPath As String, wFile As Object, wFso As Object, n As Long
  wPath = Range("folder")
  If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
  If Dir(wPath, vbDirectory) = "" Then
    MsgBox "Folder does not exist"
    Exit Sub
  End If
  Set wFso = CreateObject("Scripting.FileSystemObject")
  For Each wFile In wFso.GetFolder(wPath).Files
    If LCase(Right(wFile, 3)) = "pdf" Then
      If wFile.Datecreated >= Range("B5") And wFile.Datecreated >= Range("B5") Then
        n = n + 1
      End If
    End If
  Next
  MsgBox "Number of PDF files: " & n, vbInformation, "COUNT PDF"
End Sub



Thank you so much, that is great!! 1 minor tweak, would it be possible for that count figure to populate into cell b9??
 
Upvote 0
Thank you so much, that is great!! 1 minor tweak, would it be possible for that count figure to populate into cell b9??


Try:

Code:
Sub counting_files_by_date()
  Dim wPath As String, wFile As Object, wFso As Object, n As Long
  wPath = Range("folder")
  If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
  If Dir(wPath, vbDirectory) = "" Then
    MsgBox "Folder does not exist"
    Exit Sub
  End If
  Set wFso = CreateObject("Scripting.FileSystemObject")
  For Each wFile In wFso.GetFolder(wPath).Files
    If LCase(Right(wFile, 3)) = "pdf" Then
      If wFile.Datecreated >= Range("B5") And wFile.Datecreated >= Range("B5") Then
        n = n + 1
      End If
    End If
  Next
[COLOR=#0000ff]  Range("B9").value = n[/COLOR]
[COLOR=#0000ff]End Sub[/COLOR]
 
Upvote 0
Thanks again, it blows my mind that people can write this so quickly. It took me the best part of the day to make a date picker.

It seems to mostly work, however the count doesn't seem right when i double check within the folder itself?
 
Upvote 0
Thanks again, it blows my mind that people can write this so quickly. It took me the best part of the day to make a date picker.

It seems to mostly work, however the count doesn't seem right when i double check within the folder itself?


I'm sorry, I adjusted some details:

Code:
Sub counting_files_by_date()
  Dim wPath As String, wFile As Object, wFso As Object, n As Long
  wPath = Range("folder")
  If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
  If Dir(wPath, vbDirectory) = "" Then
    MsgBox "Folder does not exist"
    Exit Sub
  End If
  Set wFso = CreateObject("Scripting.FileSystemObject")
  For Each wFile In wFso.GetFolder(wPath).Files
    If LCase(Right(wFile, 3)) = "pdf" Then
      If wFile.Datecreated >= Range("B5") And wFile.Datecreated [B][COLOR=#ff0000]<[/COLOR][/B]= Range("B[B][COLOR=#ff0000]6[/COLOR][/B]") Then
        n = n + 1
      End If
    End If
  Next
  Range("B9").value = n
End Sub
 
Upvote 0
I'm sorry, I adjusted some details:

Code:
Sub counting_files_by_date()
  Dim wPath As String, wFile As Object, wFso As Object, n As Long
  wPath = Range("folder")
  If Right(wPath, 1) <> "\" Then wPath = wPath & "\"
  If Dir(wPath, vbDirectory) = "" Then
    MsgBox "Folder does not exist"
    Exit Sub
  End If
  Set wFso = CreateObject("Scripting.FileSystemObject")
  For Each wFile In wFso.GetFolder(wPath).Files
    If LCase(Right(wFile, 3)) = "pdf" Then
      If wFile.Datecreated >= Range("B5") And wFile.Datecreated [B][COLOR=#ff0000]<[/COLOR][/B]= Range("B[B][COLOR=#ff0000]6[/COLOR][/B]") Then
        n = n + 1
      End If
    End If
  Next
  Range("B9").value = n
End Sub
HI Can you please share with me the file path code. what should be used in the "folder"
 
Upvote 0

Forum statistics

Threads
1,224,899
Messages
6,181,629
Members
453,058
Latest member
rmd0725

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