Solved

Excel VBA File Type Filter

Posted on 2011-03-25
3
1,432 Views
Last Modified: 2012-06-27
Experts,

I currently use a code which lists all the files in a folder/sub folder. Is there a way to modify the code to filter and show only ".txt" or "xls" files? I know currently I can use Autofilter but it is slow because the code first has to retrieve everything. I am hoping that applying the filter will speed up the code.

Thanks,
Fez
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
    Application.ScreenUpdating = False
    
    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.Type
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.DateLastModified
        iRow = iRow + 1
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySubFolder.Path, True)
        Next
    End If
    Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:bose3
3 Comments
 
LVL 17

Accepted Solution

by:
wobbled earned 125 total points
ID: 35216038
Can't you just put an IF statement in to check the file type

For Each myFile In mysource.Files
        If myFile.Type = "txt" Or myFile.Type = "xls" Then
    ......



Or use the GetExtensionName(path) filesytem object to again to a check

If GetExtensionName(myFile.Path) = "txt" or GetExtensionName(myFile.Path) = xls Then
........
0
 
LVL 2

Expert Comment

by:kenyayvette
ID: 35216194
I have code that modifies all of the excel files only in a given directory.  In order to do this, I used

Fname=Dir(Fpath & "*.xls")
Do while Fname <> ""
.......
0
 

Author Closing Comment

by:bose3
ID: 35216328
FYI - Excel didn't recognize file type as "txt" or ".txt". i have to change it to "Text Document".

Thanks for the quick help
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question