0

I am trying to use the following code (copied from this site). But when I get to .FoundFiles.count, I get an error "Object variable or With block variable not set." I have searched for hours for a solution to this problem. I wonder if someone could explain. Thanks.

Sub RunCodeOnAllXLSFiles()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook


Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            'Change path to suit
            .LookIn = "C:\MyDocuments\TestResults"
            .FileType = msoFileTypeExcelWorkbooks
            'Optional filter with wildcard
            '.Filename = "Book*.xls"
                If .Execute > 0 Then 'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count 'Loop through all
                        'Open Workbook x and Set a Workbook variable to it
                        Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)

                        'DO YOUR CODE HERE
                         wbResults.Worksheets("Sheet1").Range("B7").NumberFormat = "YYYY.MM.DD"
                        wbResults.Close SaveChanges:=True
                    Next lCount
                End If
        End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
Community
  • 1
  • 1
user2200765
  • 21
  • 1
  • 1
  • 5

1 Answers1

1

The Application.FileSearch has apparently been deprecated in Excel 2007; a quick search yielded a few alternatives, such as this one. A bit more digging will turn up other solutions too.

Geoff
  • 8,551
  • 1
  • 43
  • 50