jr13,
Give this a try. Run tgr, not GetFiles. GetFiles is called by tgr.
Sub tgr()
Const strFolderPath As String = "J:\COMPANYBLAH\Reinsurance\Claims"
Dim lCalc As Long
Dim wsDest As Worksheet
Dim wsFiles As Worksheet
Dim arrFiles As Variant
Dim rngPolicy As Range
Dim PolicyCell As Range
With Application
lCalc = .Calculation
.Calculation = xlCalculationManual
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
End With
Set wsDest = ActiveSheet
Set rngPolicy = wsDest.Range("A2", wsDest.Cells(Rows.Count, "A").End(xlUp))
For Each PolicyCell In rngPolicy.Cells
Set wsFiles = Sheets.Add
GetFiles strFolderPath, PolicyCell.Text & "*.txt", wsFiles
arrFiles = Application.Transpose(wsFiles.UsedRange.Value)
If IsArray(arrFiles) Then
PolicyCell.Offset(, 1).Resize(, UBound(arrFiles)).Value = arrFiles
Erase arrFiles
Else
PolicyCell.Offset(, 1).Value = arrFiles
End If
wsFiles.Delete
Next PolicyCell
With Application
.Calculation = lCalc
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub
Public Sub GetFiles(ByVal strFolderPath As String, ByVal strFileName As String, ByVal ws As Worksheet)
Dim FSO As Object
Dim oFile As Object
Dim oFolder As Object
Set FSO = CreateObject("Scripting.FileSystemObject").GetFolder(strFolderPath)
For Each oFile In FSO.Files
If LCase(oFile.Name) Like LCase(strFileName) Then
ws.Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = oFile.Path
End If
Next oFile
For Each oFolder In FSO.SubFolders
GetFiles oFolder.Path, strFileName, ws
Next oFolder
End Sub
Bookmarks