VBA Help - List only File Names from Criteria and Specific File Type

johnny51981

Active Member
Joined
Jun 8, 2015
Messages
406
Hello:
I plagiarized the following VBA from a YouTube video that I found, and it gives me exactly what I am needing...however it is missing a piece that I hope can be solved here. I would appreciate any help, and please know...I am not very strong in VBA (yet).

I am needing to augment this code to only return file names that contain the words "WORK ORDER" and that are saved as a PDF file type.

VBA Code:
Option Explicit
Sub ListAllFiles()
    
    ThisWorkbook.Sheets("Files").Select
            
    Call ClearList
    
    Dim objFSO As Scripting.FileSystemObject
    Dim objFolder As Scripting.Folder
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Range("NTPFolder"))
    
    Call GetFileDetails(objFolder)
        
End Sub
Function GetFileDetails(objFolder As Scripting.Folder)

Dim objFile As Scripting.File
Dim nextRow As Long
Dim objSubFolder As Scripting.Folder

nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objFile In objFolder.Files
    Cells(nextRow, 1) = objFile.Name
    Cells(nextRow, 2) = objFile.Path
    Cells(nextRow, 3) = objFile.Size
    Cells(nextRow, 4) = objFile.Type
    Cells(nextRow, 5) = objFile.Attributes
    nextRow = nextRow + 1
Next

For Each objSubFolder In objFolder.SubFolders
    Call GetFileDetails(objSubFolder)
Next

End Function
Sub ClearList()
'
' ClearList Macro
'

'
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range("A2:E2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    Range("A2").Select
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Untested,but you can try this modification:
VBA Code:
For Each objFile In objFolder.Files
    If objFile.Name Like "*WORK ORDER*" & LCase(Right(objFile.Name, 4)) = ".pdf" Then
        Cells(nextRow, 1) = objFile.Name
        Cells(nextRow, 2) = objFile.Path
        Cells(nextRow, 3) = objFile.Size
        Cells(nextRow, 4) = objFile.Type
        Cells(nextRow, 5) = objFile.Attributes
        nextRow = nextRow + 1
    End If
Next
 
Upvote 0
Solution
Try this code. You only need to change the path to yours

VBA Code:
Dim ar() As Variant, x As Long

Public Sub file_search()
 getFile "C:\Users\xxx\xxx\xxx\"          'change this to your path
 Range("L2").Resize(x, 5) = Application.Transpose(ar)
 End
End Sub

Public Sub getFile(objFolderPath As String)
 Dim sFold, it, sf
 With CreateObject("scripting.filesystemobject")
    Set sFold = .GetFolder(objFolderPath)
    For Each it In sFold.Files
       If LCase(.GetExtensionName(it)) = "pdf" And InStr(it.Name, "WORK ORDER") Then
          ReDim Preserve ar(4, x)
          ar(0, x) = it.Name
          ar(1, x) = it.Path
          ar(2, x) = it.Size
          ar(3, x) = it.Type
          ar(4, x) = it.Attributes
          x = x + 1
       End If
    Next
    For Each sf In sFold.SubFolders
       getFile sf.Path
    Next
 End With
End Sub
 
Upvote 0
Untested,but you can try this modification:
VBA Code:
For Each objFile In objFolder.Files
    If objFile.Name Like "*WORK ORDER*" & LCase(Right(objFile.Name, 4)) = ".pdf" Then
        Cells(nextRow, 1) = objFile.Name
        Cells(nextRow, 2) = objFile.Path
        Cells(nextRow, 3) = objFile.Size
        Cells(nextRow, 4) = objFile.Type
        Cells(nextRow, 5) = objFile.Attributes
        nextRow = nextRow + 1
    End If
Next

This works with one tiny change, instead of using "&", I changed it to "And", which gave me the result I was looking for. Appreciate it!
 
Upvote 0
Untested,but you can try this modification:
VBA Code:
For Each objFile In objFolder.Files
    If objFile.Name Like "*WORK ORDER*" & LCase(Right(objFile.Name, 4)) = ".pdf" Then
        Cells(nextRow, 1) = objFile.Name
        Cells(nextRow, 2) = objFile.Path
        Cells(nextRow, 3) = objFile.Size
        Cells(nextRow, 4) = objFile.Type
        Cells(nextRow, 5) = objFile.Attributes
        nextRow = nextRow + 1
    End If
Next
Any idea on how to exclude a specific Sub Folder?
 
Upvote 0
Any idea on how to exclude a specific Sub Folder?
Again untested, but maybe something like this modification:
VBA Code:
For Each objSubFolder In objFolder.SubFolders
    If objSubFolder.Name <> "Specific SubFolder Name here" Then
        Call GetFileDetails(objSubFolder)
    End If
Next
 
Upvote 0
Again untested, but maybe something like this modification:
VBA Code:
For Each objSubFolder In objFolder.SubFolders
    If objSubFolder.Name <> "Specific SubFolder Name here" Then
        Call GetFileDetails(objSubFolder)
    End If
Next
This appears to be successful on my end. Thank you again!!!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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