Hi all, I am attempting to read the filenames of workbooks contained in a folder into a spreadsheet...I have coded this:
Sub WriteXLSFilenames()
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
.LookIn = "W:\Production\Work Orders\In Production\"
.FileType = msoFileTypeExcelWorkbooks
'.Filename = "Book*.xls"
Set Rng = wbCodeBook.Range("C3:E30")
aydata = Rng
If .Execute > 0 Then 'Workbooks in folder
For i = LBound(aydata) To UBound(aydata)
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)
aydata(i, 1) = "wbResults"
wbResults.Close SaveChanges:=True
Next lCount
Next i
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
When ran, nothing happens...what have I done wrong??
Sub WriteXLSFilenames()
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
.LookIn = "W:\Production\Work Orders\In Production\"
.FileType = msoFileTypeExcelWorkbooks
'.Filename = "Book*.xls"
Set Rng = wbCodeBook.Range("C3:E30")
aydata = Rng
If .Execute > 0 Then 'Workbooks in folder
For i = LBound(aydata) To UBound(aydata)
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)
aydata(i, 1) = "wbResults"
wbResults.Close SaveChanges:=True
Next lCount
Next i
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
When ran, nothing happens...what have I done wrong??