JustATheory
IS-IT--Management
Greetings,
This code loops through excel files in a directory and performs a process on each file. I wrote it in MS-Excel VBA 2003, works well. I tried it in 2007 and it halts. It halts on the FileSearch. Is this a known issue and can someone point me in the direction to fix this? Other code has migrated well, this is the only one that has been an issue with 2007. ('Loop Code Here, is proprietary and works in 2007, just a place holder)
Thanks in advance for your help,
Andy
Sub FileCount()
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:\Directory"
.FileType = msoFileTypeExcelWorkbooks
'.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)
‘Loop Code Here
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
This code loops through excel files in a directory and performs a process on each file. I wrote it in MS-Excel VBA 2003, works well. I tried it in 2007 and it halts. It halts on the FileSearch. Is this a known issue and can someone point me in the direction to fix this? Other code has migrated well, this is the only one that has been an issue with 2007. ('Loop Code Here, is proprietary and works in 2007, just a place holder)
Thanks in advance for your help,
Andy
Sub FileCount()
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:\Directory"
.FileType = msoFileTypeExcelWorkbooks
'.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)
‘Loop Code Here
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub