I'm using the following code to locate all target xls files, open them, and check to see the last used cell in column A.
Everything was working until I added the code to actually open the files and check the last used row; but now I'm getting a RTE 52 (bad file name or number), most often at the line marked with '*** below.
The weird thing is that when I select 'debug' at the error, then debug.print Fil or debug.print UCase(Left(Dir(Fil), 5)), it gives me a valid result- it has the next filename just fine, but something else is going on and I haven't been able to figure it out. I tried with and without setting a new excel application for opening new files, and neither made a difference.
I've also tried adding a 5 second wait where the DoEvents are, that didn't help. Is there a way to set timeout settings for a network drive, similar to what is shown here for http servers? http://msdn.microsoft.com/en-us/library/ms760403.aspx
Any help would be greatly appreciated!
Using XL2003 on WinXP
Keith
(apologies- at least in preview mode, all my code is left justified, even though in the text section I'm typing in, it is all indented)
Option Explicit
Sub SrchForFiles()
' Searches the selected folders and sub folders for files with the specified
'extension. .xls, .doc, .ppt, etc.
'A new worksheet is produced called "File Search Results". You can click on the link and go directly
'to the file you need.
Dim i As Long, z As Long, Rw As Long
Dim ws As Worksheet
Dim y As Variant
Dim fLdr As String, Fil As String, FPath As String
Dim LocName As String
Dim PString As String
Dim SummaryWB As Workbook
Dim SummaryWS As Worksheet
'grab current location for later reference, for where to paste final data
Set SummaryWB = Application.ActiveWorkbook
Set SummaryWS = Application.ActiveWorkbook.ActiveSheet
y = "xls"
'actual directory path masked for public posting
fLdr = "\\share.companyname.com\directory\subdirectory\"
'Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = fLdr
.SearchSubFolders = True
.Filename = y
Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
On Error GoTo 1
2: ws.Name = "FileSearch Results"
On Error GoTo 0
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Fil = .FoundFiles(i)
'screen for target file names ("Multi*.xls")
If UCase(Left(Dir(Fil), 5)) = "MULTI" Then '*** THIS IS WHERE IT BAILS
'Exclude the template file that has no unique file ID, and is therefore only 34 chars long
If Len(Dir(Fil)) > 34 Then
'Remove the standard naming convention (left 31 characters of filename)
LocName = Right(Dir(Fil), Len(Dir(Fil)) - 31)
'Remove the ".xls" from the end of the filename
'Remainder of the filename (LocName) is the unique file ID
LocName = Left(LocName, Len(LocName) - 4)
'Get file path from file name
FPath = Left(Fil, Len(Fil) - Len(Split(Fil, "\")(UBound(Split(Fil, "\")))) - 1)
'Get file information
If Left$(Fil, 1) = Left$(fLdr, 1) Then
If CBool(Len(Dir(Fil))) Then
z = z + 1
ws.Cells(z + 1, 1).Resize(, 5) = _
Array(Dir(Fil), _
LocName, _
FileLen(Fil) / 1000, _
FileDateTime(Fil), _
FPath)
ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
Address:=.FoundFiles(i)
End If
End If
'open the file and grab the data
'Dim oExcel As Excel.Application
'Dim oWB As Workbook
'Set oExcel = New Excel.Application
Workbooks.Open (Fil)
DoEvents
'Set oWB = oExcel.Workbooks.Open(Fil) 'Workbooks.Open(.FoundFiles(i1))
Dim LastRow As Long
LastRow = Sheets(1).Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRow = qexc_GetLastFilledCellInColumn 'Sheets(1).Range("A1").End(xlDown).Select
PString = PString & vbCrLf & LastRow
'Workbooks.Close 'Fil
Workbooks(Dir(Fil)).Close SaveChanges:=False
DoEvents
End If
End If
Next i
End If
End With
MsgBox PString
With ws
Rw = .Cells.Rows.Count
With .[A1:E1]
.Value = [{"Full Name","Location","Kilobytes","Last Modified", "Path"}]
.Font.Underline = xlUnderlineStyleSingle
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
End With
.[F1:IV1 ].EntireColumn.Hidden = True
On Error Resume Next
Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
End With
'Application.ScreenUpdating = True
Exit Sub
1: Application.DisplayAlerts = False
Worksheets("FileSearch Results").Delete
Application.DisplayAlerts = True
GoTo 2
End Sub
Function qexc_GetLastFilledCellInColumn() As Integer
Sheets(1).Cells(65536, 1).Select
Selection.End(xlUp).Select
qexc_GetLastFilledCellInColumn = Selection.Row
End Function
Bookmarks