Can this be simplified?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
303
Office Version
  1. 365
Platform
  1. Windows
I have 40 queues to monitor with four sub-queues, so 160. I don't really want to write the below 160 times :-) This is for the first queue (AQ1), the folder structure runs upto AQ40.
VBA Code:
Sub CountFiles()

Dim strDir As String
Dim fso As Object
Dim objFiles As Object
Dim obj As Object
Dim lngFileCount As Long
       
strDir = "T:\X1Home\PDF_Poll\AQ1\Test\CREDIT\NONPOP\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = lngFileCount + objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("B8").Value = objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("A8").Value = strDir
ThisWorkbook.Worksheets("Sheet1").Range("C8").Value = Now()
    
strDir = "T:\X1Home\PDF_Poll\AQ1\Test\CREDIT\POP\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = lngFileCount + objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("B9").Value = objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("A9").Value = strDir
ThisWorkbook.Worksheets("Sheet1").Range("C9").Value = Now()

strDir = "T:\X1Home\PDF_Poll\AQ1\Test\INVOICE\NONPOP\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = lngFileCount + objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("B10").Value = objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("A10").Value = strDir
ThisWorkbook.Worksheets("Sheet1").Range("C10").Value = Now()
    
strDir = "T:\X1Home\PDF_Poll\AQ1\Test\INVOICE\POP\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFiles = fso.GetFolder(strDir).Files
lngFileCount = lngFileCount + objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("B11").Value = objFiles.Count
ThisWorkbook.Worksheets("Sheet1").Range("A11").Value = strDir
ThisWorkbook.Worksheets("Sheet1").Range("C11").Value = Now()
End Sub

Thanks in advance as always :-)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You could do something like this:

VBA Code:
Sub callit()
   Dim fso As Object
   Set fso = CreateObject("Scripting.FileSystemObject")
   
   Dim cell As Range
   Set cell = ThisWorkbook.Worksheets("Sheet1").Range("A8")
   
   Dim x As Long
   For x = 1 To 40
      QueueCount fso, "T:\X1Home\PDF_Poll\AQ" & x & "\Test\", cell
      ' move output cell down four rows
      Set cell = cell.Offset(4)
   Next x
End Sub
Sub QueueCount(fso As Object, FolderPath As String, outputCell As Range)
   Dim folderList
   folderList = Array("CREDIT\NONPOP\", "CREDIT\POP\", "INVOICE\NONPOP\", "INVOICE\POP\")
   Dim n As Long
   For n = LBound(folderList) To UBound(folderList)
      Dim strDir As String
      strDir = FolderPath & "\" & folderList(n)
      Dim objFiles As Object
      Set objFiles = fso.GetFolder(strDir).Files
      lngFileCount = lngFileCount + objFiles.Count
      outputCell.Offset(n).Resize(, 3).Value = Array(strDir, objFiles.Count, Now())
    Next n
End Sub
 
Upvote 1
That's genius thank you.

One further ask - it seems that a lot of these folders have a file called "thumbs.db" in? Can I exclude that particular file from the count?
 
Upvote 0
Try changing the second sub to this:

VBA Code:
Sub QueueCount(fso As Object, FolderPath As String, outputCell As Range)
   Dim folderList
   folderList = Array("CREDIT\NONPOP\", "CREDIT\POP\", "INVOICE\NONPOP\", "INVOICE\POP\")
   Dim n As Long
   For n = LBound(folderList) To UBound(folderList)
      Dim strDir As String
      strDir = FolderPath & "\" & folderList(n)
      Dim objFiles As Object
      Set objFiles = fso.GetFolder(strDir).Files
      Dim thisFileCount As Long
      thisFileCount = objFiles.Count
      If Len(Dir(strDir & "thumbs.db")) <> 0 Then thisFileCount = thisFileCount - 1
      lngFileCount = lngFileCount + thisFileCount
      outputCell.Offset(n).Resize(, 3).Value = Array(strDir, thisFileCount, Now())
    Next n
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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