Solved

Excel VBA File Type Filter

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy value from a certain cell 5 24
Excel VBA When using VLookup 6 26
Excel Automation VBA 19 25
Issues with DAX Calculated Columns 6 0
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

929 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

13 Experts available now in Live!

Get 1:1 Help Now