Excel 在VBA中搜索特定文件

Excel 在VBA中搜索特定文件,excel,vba,Excel,Vba,下面的我的VBA代码搜索我的驱动器C:中的所有文件,并将它们列在工作表(1)中。现在,我的任务是只查找特定的.txt文件。我尝试过修改代码,但没有成功。我想这和OBJJ文件有关 Sub ListAllFiles() Dim ObjFSO As Scripting.FileSystemObject Dim objFolder As Scripting.Folder Set ObjFSO = CreateObject("Scripting.FileSystemObject") Set o

下面的我的VBA代码搜索我的驱动器C:中的所有文件,并将它们列在工作表(1)中。现在,我的任务是只查找特定的.txt文件。我尝试过修改代码,但没有成功。我想这和OBJJ文件有关

    Sub ListAllFiles()

Dim ObjFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = ObjFSO.GetFolder("C:\")


Call getfiledetails(objFolder)

End Sub

Function getfiledetails(objFolder As Scripting.Folder)

Dim objFile As Scripting.File
Dim nextRow As Long
Dim objSubFolder As Scripting.Folder

nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

For Each objFile In objFolder.Files
On Error Resume Next
    Cells(nextRow, 1) = objFile.Name
    Cells(nextRow, 2) = objFile.Path
    Cells(nextRow, 3) = objFile.Type
    Cells(nextRow, 4) = objFile.DateCreated
    Cells(nextRow, 5) = objFile.DateLastModified
    nextRow = nextRow + 1

Next objFile

For Each objSubFolder In objFolder.SubFolders
Call getfiledetails(objSubFolder)

Next

End Function

使用
DIR
可能会有轻微的性能优势,但如果您想使用现有代码,则对
getfiledetails
子项进行以下调整将为您提供所需的输出:

Function getfiledetails(objFolder As Scripting.Folder)

    Dim objFile As Scripting.File
    Dim nextRow As Long
    Dim objSubFolder As Scripting.Folder

    nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

    On Error Resume Next

    For Each objFile In objFolder.Files

        If objFile.Type = "Text Document" Then
            Cells(nextRow, 1) = objFile.Name
            Cells(nextRow, 2) = objFile.Path
            Cells(nextRow, 3) = objFile.Type
            Cells(nextRow, 4) = objFile.DateCreated
            Cells(nextRow, 5) = objFile.DateLastModified
            nextRow = nextRow + 1
        End If

    Next objFile

    For Each objSubFolder In objFolder.SubFolders
        Call getfiledetails(objSubFolder)
    Next

End Function
如果需要多个文档类型,可以将其添加到,例如

If objFile.Type = "Microsoft Word Document" Or objFile.Type = "Text Document" Then

使用
DIR
可能有轻微的性能优势,但如果您想使用现有代码,则对
getfiledetails
子项进行以下调整将获得所需的输出:

Function getfiledetails(objFolder As Scripting.Folder)

    Dim objFile As Scripting.File
    Dim nextRow As Long
    Dim objSubFolder As Scripting.Folder

    nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

    On Error Resume Next

    For Each objFile In objFolder.Files

        If objFile.Type = "Text Document" Then
            Cells(nextRow, 1) = objFile.Name
            Cells(nextRow, 2) = objFile.Path
            Cells(nextRow, 3) = objFile.Type
            Cells(nextRow, 4) = objFile.DateCreated
            Cells(nextRow, 5) = objFile.DateLastModified
            nextRow = nextRow + 1
        End If

    Next objFile

    For Each objSubFolder In objFolder.SubFolders
        Call getfiledetails(objSubFolder)
    Next

End Function
如果需要多个文档类型,可以将其添加到,例如

If objFile.Type = "Microsoft Word Document" Or objFile.Type = "Text Document" Then

使用如图所示的目录更改
StrFile=DIR(“c:\testfolder\*test*”)
以满足您的需要…使用如图所示的目录更改
StrFile=DIR(“c:\testfolder\*test*”)
以满足您的需要…先生,这就像一个符咒!非常感谢。我将检查DIR函数并遵循您的建议。再次感谢你!我认为
DIR
可以提高性能的原因是,我在那里调整的例程是将每个文件读入一个对象,然后检查
.Type
。使用
DIR
命令,例如
x=DIR(folder&“/*.txt”)
先为您进行筛选。这两种方法都有其用途。另外,@Siddharth通常知道他在说什么,所以如果他建议
DIR
,那么他会有一个很好的理由。很好的理由,CLR!!先生,这个很有魅力!非常感谢。我将检查DIR函数并遵循您的建议。再次感谢你!我认为
DIR
可以提高性能的原因是,我在那里调整的例程是将每个文件读入一个对象,然后检查
.Type
。使用
DIR
命令,例如
x=DIR(folder&“/*.txt”)
先为您进行筛选。这两种方法都有其用途。另外,@Siddharth通常知道他在说什么,所以如果他建议
DIR
,那么他会有一个很好的理由。很好的理由,CLR!!