How to handle a missing folder when parsing through a list of them?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
313
Office Version
  1. 365
Platform
  1. Windows
I have the following (excellent) routine mostly written by @RoryA (thanks, the code works perfectly!) but need a tweak
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
Range("B3").ClearContents
Range("A8:C500").ClearContents
For x = 1 To 49
   If x = 11 Then x = 20
QueueCount fso, "T:\V1Home\PDF_Poll\AP" & x & "\Test\", cell
' move output cell down four rows
Set cell = cell.Offset(4)
Next x
Range("B3").Value = "Done!"
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)
'strDir = FolderPath & "\" & folderList(n)
Dim objFiles As Object
Set objFiles = fso.GetFolder(strDir).Files
Dim thisFileCount As Long
thisFileCount = objFiles.Count
Dim strTemp As String
strTemp = Dir(strDir & "Thumbs.db", vbHidden + vbSystem)
If strTemp <> "" Then thisFileCount = thisFileCount - 1
lngFileCount = lngFileCount + thisFileCount
outputCell.Offset(n).Resize(, 3).Value = Array(strDir, thisFileCount, Now())
Next n
End Sub

So someone has deleted one of the folders and the code falls over with a "path not found" error.

How would I amend the above to handle that? In an idea world, I'd have a column named "present?" which returned yes or no depending on whether the path was found.

Thanks in advance as always for your kind help.
 
Untested, but 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
    Range("B3").ClearContents
    Range("A8:C500").ClearContents
    For x = 1 To 49
       If x = 11 Then x = 20
        QueueCount fso, "T:\V1Home\PDF_Poll\AP" & x & "\Test\", cell
        ' move output cell down four rows
        Set cell = cell.Offset(4)
    Next x
    Range("B3").Value = "Done!"
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)
        If fso.FolderExists(strDir) Then
            'strDir = FolderPath & "\" & folderList(n)
            Dim objFiles As Object
            Set objFiles = fso.GetFolder(strDir).Files
            Dim thisFileCount As Long
            thisFileCount = objFiles.Count
            Dim strTemp As String
            strTemp = Dir(strDir & "Thumbs.db", vbHidden + vbSystem)
            If strTemp <> "" Then thisFileCount = thisFileCount - 1
            lngFileCount = lngFileCount + thisFileCount
            outputCell.Offset(n).Resize(, 4).Value = Array(strDir, thisFileCount, Now(), "Folder present")
        Else
            outputCell.Offset(n).Resize(, 4).Value = Array(strDir, "", Now(), "Folder missing")
        End If
    Next n
End Sub

Note you don't actually appear to be doing anything with the lngFileCount variable so you could probably remove that.
 
Upvote 0
Solution

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