在文件搜索中获取有关FINFiles.count的错误

时间:2013-04-22 18:07:34

标签: excel-vba vba excel

我正在尝试使用以下代码(从此站点复制)。但是当我到了.Files.count时,我得到一个错误“对象变量或没有设置块变量。”我已经搜索了几个小时来解决这个问题。我想知道是否有人可以解释。感谢。

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

1 个答案:

答案 0 :(得分:1)

Application.FileSearch显然已在Excel 2007中弃用;快速搜索产生了一些替代方案,例如this one。 多一点挖掘也会出现其他解决方案。