Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA File Type Filter

Posted on 2011-03-25
3
Medium Priority
?
1,613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
wobbled earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 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