Hi Frank,
A bit on-the-fly, but I tested against a small folder as well:
Microsoft Office Excel Comma Separated Values File
Microsoft Excel Worksheet
Microsoft Office Excel 2007 Workbook
Text Document
Microsoft Office Excel 2007 Macro-Enabled Workbook
Microsoft Excel Worksheet
Microsoft Excel Worksheet
As you mentioned, no worries currently for testing the file type, but I am not yet 'clueing in' as to what workbooks would not open other than csv's. I rarely use them, only once in a while to rip data from a csv, but I thought that the one sheet is always named (tab name) the same as the filename. If I have that part right, I would not see why you would want to open any csv's in the same folder, as at least per my pea brain, there could only be one in the folder with the right name...
If you want csv's to open, I would think this should work.
If fsoFile.Type Like "Microsoft*Excel*" _
And Not fsoFile.Path = ThisWorkbook.FullName Then
Reference:
Set wb = Workbooks.Open(fsoFile.Path, False, True)
A bit of guessing, as I don't have access to 2007, and am a little under the weather to be thinking much, but maybe an added arg in newer ver, or maybe the shared workbooks caused? I even tested against a shared book, no issues, but regardless, certainly okay to ditch.
I don't really need that though, as I just filter the .xls files, then copy them to a new folder. - If you're into it though, I would enjoy seeing how you use that. And I have a couple ideas for how I could use that type of code for other taks.
Sorry, I'm sure its my foggy head, but not sure what you meant by "how you use that"? Presuming you mean checking if its an excel file before attempting to open, Like just uses simple patterns. If I'm way off, please say so...
I would change this:
On Error Resume Next
Set wksSource = wb.Worksheets("Out Side Purchase Order")
On Error GoTo 0
...and ditch the On Error GoTo 0 farther down. That was my fault, as you have it as I did. In short, there should be no errors, excepting if the worksheet doesn't exist in the source. Thus - if we were to have an error, we'd be flying by it and not knowing...which can lead to big ol' headaches trying to figure out why/where something is going kaboom! IMO, On Error Resume Next should be allowed only to allow and error, test for it, and handle.
I do not see any clumsy parts and it sounds as though it is working. I am guessing that the sheet error was because you had a different sheet name? The only thing that strikes me is the deleting a cell at a time part...
I would try .Find, or, as shown below, Application.Match to find the first cell in B6:B8 that has data. Please note that I tossed in a Debug.Print. Open the Immediate Window, and maybe you can see what workbooks it fails to open. I hope you'll be able to test (no rush) cuz its bugging me that the pattern fails...
Sub exa3()
Dim FSO As Object '<-- FileSystemObject
Dim fsoFol As Object '<-- Folder
Dim fsoFile As Object '<-- File
Dim wb As Workbook
Dim wksSource As Worksheet
Dim lStartRow As Long
'// Set references to FileSystemObject and the folder that this workbook //
'// resides in. //
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fsoFol = FSO.GetFolder(ThisWorkbook.Path & "\")
For Each fsoFile In fsoFol.Files
If fsoFile.Type Like "Microsoft*Excel*Work*" _
And Not fsoFile.Path = ThisWorkbook.FullName Then
Set wb = Workbooks.Open(fsoFile.Path)
Debug.Print wb.Name
Set wksSource = Nothing
'// Disallow fatal error in case we do not find the sheet //
On Error Resume Next
Set wksSource = wb.Worksheets("Out Side Purchase Order")
On Error GoTo 0
If Not wksSource Is Nothing Then
'// IF we find somethig in B6 or B7 or B8 //
If Not IsError(Application.Match("*", wksSource.Range("B6:B8"), 0)) Then
With ThisWorkbook.Worksheets("Sheet1")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 3).Value _
= Application.Transpose( _
wksSource.Range("B5").Offset( _
Application.Match("*", wksSource.Range("B6:B8"), 0)) _
.Resize(3).Value)
End With
End If
End If
'// Close wb//
wb.Close False
End If
Next
End Sub
Hope that helps,
Mark