Solved

Excel VBA File Type Filter

Posted on 2011-03-25
3
1,342 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now