run time error 5 , can't figure out

anmac1789

New Member
Joined
Dec 6, 2020
Messages
43
Office Version
  1. 365
Platform
  1. Windows
For some reason this code is generating a runtime error 5 invalid procedure call or argument. When I click debug it takes me to i = Application.WorksheetFunction.Match(UCase(p.Name), aryFoldersToExclude, 0) in isFolderExcluded sub function. The full code is below:

VBA Code:
Option Explicit

Const colPath As Long = 1
Const colParent As Long = 2
Const colName As Long = 3
Const colFileFolder As Long = 4
Const colCreated As Long = 5
Const colModified As Long = 6
Const colSize As Long = 7
Const colType As Long = 8

Dim aryPathsToInclude() As String, aryFoldersToExclude() As String, aryFilenamesToExclude() As String, arySpecificFilesToExclude() As String

Dim oFSO As Object

Const incFilesFolders As Long = 100
Dim aryFilesFolders() As Object
Dim cntFilesFolders As Long


Sub Main()
    Dim i As Long
    
    If Not InitOk Then
        Call MsgBox("No top level path specified", vbCritical, "Look for Files & Folders")
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    
    ReDim aryFilesFolders(1 To incFilesFolders)
    For i = LBound(aryPathsToInclude) To UBound(aryPathsToInclude)
        Call getFiles(oFSO.GetFolder(aryPathsToInclude(i)))
    Next
    ReDim Preserve aryFilesFolders(1 To cntFilesFolders)
    
    listData
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"
End Sub

Private Function InitOk() As Boolean
    Dim i As Long, n As Long
    Dim rPaths As Range, rExcludes As Range, rFiles As Range, rSpecificFiles As Range
    
    
    Erase aryPathsToInclude
    Erase aryFoldersToExclude
    Erase aryFilenamesToExclude
    Erase arySpecificFilesToExclude
    
    InitOk = True
    
    'build paths list
    Set rPaths = Worksheets("FoldersToDo").Cells(1, 1).CurrentRegion
    
    If Len(rPaths.Cells(1, 1).Value) > 0 Then '   there are paths
        
        InitOk = True
        
        ReDim aryPathsToInclude(1 To rPaths.Cells.Count)
        
        For i = 1 To rPaths.Cells.Count
            aryPathsToInclude(i) = Trim(rPaths.Cells(i))
        Next i
    
    Else
        InitOk = False
        Exit Function
    End If
    
    'build excluded folders list
    Set rExcludes = Worksheets("FoldersToDo").Cells(1, 3).CurrentRegion
    
    If Len(rExcludes.Cells(1, 1).Value) > 0 Then '   there are excluded folders
        ReDim aryFoldersToExclude(1 To rExcludes.Cells.Count)
        
        For i = 1 To rExcludes.Cells.Count
            aryFoldersToExclude(i) = Trim(UCase(rExcludes.Cells(i)))
        Next i
    End If


    'build excluded files list
    Set rFiles = Worksheets("FoldersToDo").Cells(1, 5).CurrentRegion
    
    If Len(rFiles.Cells(1, 1).Value) > 0 Then '   there are excluded folders
        ReDim aryFilenamesToExclude(1 To rFiles.Cells.Count)
        
        For i = 1 To rFiles.Cells.Count
            aryFilenamesToExclude(i) = Trim(UCase(rFiles.Cells(i)))
        Next i
    End If

    'build excluded specific files list
    Set rSpecificFiles = Worksheets("FoldersToDo").Cells(1, 7).CurrentRegion
    
    If Len(rSpecificFiles.Cells(1, 1).Value) > 0 Then '   there are excluded folders
        ReDim arySpecificFilesToExclude(1 To rSpecificFiles.Cells.Count)
        
        For i = 1 To rSpecificFiles.Cells.Count
            arySpecificFilesToExclude(i) = UCase(rSpecificFiles.Cells(i))
        Next i
    End If


    'create File System Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    cntFilesFolders = 0
    
End Function
    

Sub getFiles(oPath As Object)
    Dim oFolder As Object, oSubFolder As Object, oFile As Object

    If isFolderExcluded(oPath) Then Exit Sub  '   stops recursion
        
    Call addFileFolder(oPath)
    
    For Each oFile In oPath.Files
        If Not isFileExcluded(oFile) Then
            If Not isSpecificFileExcluded(oFile) Then Call addFileFolder(oFile)
        
        Else
            If Not isSpecificFileExcluded(oFile) Then Call addFileFolder(oFile)
        End If
    Next
    
    For Each oSubFolder In oPath.SubFolders
        Call getFiles(oSubFolder)
    Next
End Sub


'   IFolder object
'       Attributes, DateCreated, DateLastAccessed, DateLastModified, Drive,
'       Files, IsRootFolder, Name, ParentFolder (IFolder), Path,
'       ShortName, ShortPath, Size, SubFolders, Type

'   iFile object
'       Attributes, DateCreated, DateLastAccessed, DateLastModified, Drive (IDrive),
'       Name, ParentFolder (IFolder), Path, ShortName, ShortPath, Size, Type
'       Attributes
Private Sub listData()
    Dim rowOut As Long, i As Long, j As Long
    Dim wsOut As Worksheet
    
    rowOut = 1
    
    Set wsOut = Worksheets("Files")
    
    wsOut.Cells(1, 1).CurrentRegion.Clear

    wsOut.Cells(rowOut, colPath).Value = "FILE/FOLDER PATH"
    wsOut.Cells(rowOut, colParent).Value = "PARENT FOLDER"
    wsOut.Cells(rowOut, colName).Value = "FILE/FOLDER NAME"
    wsOut.Cells(rowOut, colFileFolder).Value = "FILE or FOLDER"
    wsOut.Cells(rowOut, colCreated).Value = "DATE CREATED"
    wsOut.Cells(rowOut, colModified).Value = "DATE MODIFIED"
    wsOut.Cells(rowOut, colSize).Value = "SIZE"
    
    rowOut = rowOut + 1

    For i = LBound(aryFilesFolders) To UBound(aryFilesFolders)
        With aryFilesFolders(i)
            wsOut.Cells(rowOut, colPath).Value = .Path
            wsOut.Cells(rowOut, colParent).Value = oFSO.GetParentFolderName(.Path)  '   <<<<<<<<<<
            wsOut.Cells(rowOut, colName).Value = .Name
            wsOut.Cells(rowOut, colFileFolder).Value = TypeName(aryFilesFolders(i))
            
            If TypeName(aryFilesFolders(i)) = "Folder" Then
                wsOut.Cells(rowOut, colFileFolder).Value = "Folder"
                For j = LBound(aryPathsToInclude) To UBound(aryPathsToInclude)
                    If UCase(.Path) = aryPathsToInclude(j) Then
                        wsOut.Cells(rowOut, colFileFolder).Value = "Parent Folder"
                        Exit For
                    End If
                Next j
            End If
            wsOut.Cells(rowOut, colCreated).Value = .DateCreated
            wsOut.Cells(rowOut, colModified).Value = .DateLastModified
            wsOut.Cells(rowOut, colSize).Value = .Size
        End With
    
        rowOut = rowOut + 1
    Next i
    
    
    
    'remove duplicates
    wsOut.Cells(1, 1).CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlYes
    
    'final format
    wsOut.Columns(colName).HorizontalAlignment = xlLeft
    wsOut.Columns(colCreated).NumberFormat = "m/dd/yyyy"
    wsOut.Columns(colModified).NumberFormat = "m/dd/yyyy"
    wsOut.Columns(colSize).NumberFormat = "#,##0"
    
    wsOut.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
End Sub

Private Function isFolderExcluded(p As Object) As Boolean
    Dim i As Long
    
    i = -1
    On Error Resume Next
    i = Application.WorksheetFunction.Match(UCase(p.Name), aryFoldersToExclude, 0)
    On Error GoTo 0

    isFolderExcluded = (i <> -1)
End Function

Private Function isFileExcluded(p As Object) As Boolean
    Dim i As Long
    
    i = -1
    On Error Resume Next
    i = Application.WorksheetFunction.Match(UCase(p.Name), aryFilenamesToExclude, 0)
    On Error GoTo 0

    isFileExcluded = (i <> -1)

End Function

Private Function isSpecificFileExcluded(p As Object) As Boolean
    Dim i As Long
    
    i = -1
    On Error Resume Next
    i = Application.WorksheetFunction.Match(UCase(p.Name), arySpecificFilesToExclude, 0)
    On Error GoTo 0

    isSpecificFileExcluded = (i <> -1)

End Function

Private Function RemovePrefix(s As String) As String
    RemovePrefix = IIf(Left(s, 4) = "\\?\", Right(s, Len(s) - 4), s)
End Function


Private Sub addFileFolder(o As Object)
    cntFilesFolders = cntFilesFolders + 1
    
    If cntFilesFolders > UBound(aryFilesFolders) Then
        ReDim Preserve aryFilesFolders(1 To UBound(aryFilesFolders) + incFilesFolders)
    End If

    Set aryFilesFolders(cntFilesFolders) = o
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
aryFoldersToExclude is probably the issue, I'm guessing.
 
Upvote 0
oops, nevermind, you appear to be using one dimensional array.

Maybe, match returns a double data type and you are storing it into a Long data type? CInt() ?
 
Upvote 0
oops, nevermind, you appear to be using one dimensional array.

Maybe, match returns a double data type and you are storing it into a Long data type? CInt() ?
How can I check what data type it returns?
 
Upvote 0
Like I said, I am just guessing, I am far from the best Excel member here, but without any other smarter suggestions to assist you ...

Try adding:

VBA Code:
msgbox Application.WorksheetFunction.Match(UCase(p.Name), aryFoldersToExclude, 0)

Add that /\ /\ prior to the line:

VBA Code:
i = Application.WorksheetFunction.Match(UCase(p.Name), aryFoldersToExclude, 0)
and post here what the message box says.
 
Last edited:
Upvote 0
There is no message from the msgbox which is a big strange...Do you get any message?
 
Upvote 0
When you get the error, press Debug and look in the Locals window to see if your array actually has anything in it.
 
Upvote 0
Even if the array didn't have any data and I just wanted to search a parent folder path then the code should still work but im not sure why it isn't

screenshot1.png
 
Upvote 0
You can’t use match on an uninitialised array, which would give you an error.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,795
Members
449,048
Latest member
greyangel23

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