File System Object Query

cameronb

Board Regular
Joined
Feb 13, 2009
Messages
146
I am using the below code to list files in folders and subfolders.

I only wish to list xls files, is there anyway that myfile can be limited to only xls files?

Can data be retrieved from the workbooks using the below without opening the files?

Cells(iRow, iCol).Value = myFile.Sheet1.Range("A1").Value
iRow = iRow + 1


Many thanks for any help

Code:
Dim iRow
Sub ListFiles()
    iRow = 11
    Call ListMyFiles(Range("C7"), Range("C8"))
End Sub
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Set MyObject = New Scripting.FileSystemObject
    Set mySource = MyObject.GetFolder(mySourcePath)
    On Error Resume Next
    For Each myFile In mySource.Files
        iCol = 2
        Cells(iRow, iCol).Value = myFile.Path
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Name
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.DateLastModified
        iRow = iRow + 1
        Cells(iRow, iCol).Value = myFile.Sheet1.Range("A1").Value
        iRow = iRow + 1
        Cells(iRow, iCol).Value = myFile.Sheet2.Range("A1").Value
        iRow = iRow + 1
        
        
        
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySubFolder.Path, True)
        Next
    End If
End Sub
[CODE/]
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Why not use Dir("c:\some path\*.xls") to limit the return file names? If you want to use FSO, then test if Right(myfile.name, 4) = ".xls"
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top