You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请教:使用VBA遍历文件夹中无规律命名的Excel文件

VBA遍历指定Excel文件的可行方案(无需提前重命名)

兄弟,完全不用费劲提前统一文件名!VBA本身就自带灵活的文件筛选能力,直接就能命中你要的.xls/.xlsx格式,同时捕捉包含Revised或revised的修订版文件。下面给你几套直接能用的方案,一步步讲清楚:

核心思路

不用额外修改文件名,通过两种方式实现精准筛选:

  • 利用通配符直接匹配文件名和后缀
  • 把文件名转成小写后,用字符串判断关键词和格式

方案1:轻量版(用Dir函数快速筛选)

适合只遍历当前文件夹的场景,代码简洁无需额外引用:

Sub TraverseTargetExcelFiles()
    Dim folderPath As String
    Dim fileName As String
    Dim fullFilePath As String
    
    ' 设置目标文件夹(这里用当前工作簿所在文件夹,可自行修改)
    folderPath = ThisWorkbook.Path & "\"
    
    ' 第一步:筛选包含Revised/revised的.xls文件
    fileName = Dir(folderPath & "*[Rr]evised*.xls")
    
    Do While fileName <> ""
        fullFilePath = folderPath & fileName
        ' 这里写你要执行的操作,比如打开文件、读取数据
        Debug.Print "找到目标文件:" & fullFilePath
        
        ' 继续查找下一个符合条件的文件
        fileName = Dir()
    Loop
    
    ' 第二步:筛选包含Revised/revised的.xlsx文件
    fileName = Dir(folderPath & "*[Rr]evised*.xlsx")
    
    Do While fileName <> ""
        fullFilePath = folderPath & fileName
        Debug.Print "找到目标文件:" & fullFilePath
        
        fileName = Dir()
    Loop
End Sub

关键说明:

  • *[Rr]evised*.xls里的[Rr]是通配符技巧,同时匹配大小写的R,不管是Revised还是revised都能命中
  • 分两次遍历是因为Dir函数无法同时匹配两种不同后缀,如果你觉得麻烦,可以看下面的方案2

方案2:灵活版(一次遍历全筛选)

把所有文件过一遍,用字符串判断实现多条件筛选,更灵活:

Sub TraverseExcelFilesWithFilter()
    Dim folderPath As String
    Dim fileName As String
    Dim fullFilePath As String
    Dim lowerFileName As String
    
    folderPath = ThisWorkbook.Path & "\"
    fileName = Dir(folderPath & "*.*") ' 遍历当前文件夹所有文件
    
    Do While fileName <> ""
        lowerFileName = LCase(fileName)
        ' 双条件筛选:是xls/xlsx格式,且文件名包含revised(不区分大小写)
        If (Right(lowerFileName, 4) = ".xls" Or Right(lowerFileName, 5) = ".xlsx") _
            And InStr(lowerFileName, "revised") > 0 Then
            
            fullFilePath = folderPath & fileName
            Debug.Print "匹配到目标文件:" & fullFilePath
            ' 示例:打开文件处理(记得处理完关闭)
            ' Dim wb As Workbook
            ' Set wb = Workbooks.Open(fullFilePath)
            ' ' 你的业务逻辑...
            ' wb.Close SaveChanges:=False
        End If
        
        fileName = Dir()
    Loop
End Sub

关键说明:

  • 把文件名转成小写后,用InStr判断是否包含revised,彻底解决大小写问题
  • 用Right函数判断后缀,同时覆盖.xls(4位)和.xlsx(5位)两种格式

方案3:递归遍历子文件夹

如果需要遍历当前文件夹下的所有子文件夹,用FileSystemObject实现递归:

Sub TraverseAllSubFolders()
    Dim fso As Object
    Dim mainFolder As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set mainFolder = fso.GetFolder(ThisWorkbook.Path)
    
    ' 开始遍历主文件夹和所有子文件夹
    TraverseSingleFolder mainFolder, fso
    
    ' 释放对象
    Set fso = Nothing
    Set mainFolder = Nothing
End Sub

Sub TraverseSingleFolder(currentFolder As Object, fso As Object)
    Dim fileObj As Object
    Dim lowerFileName As String
    
    ' 遍历当前文件夹的文件
    For Each fileObj In currentFolder.Files
        lowerFileName = LCase(fileObj.Name)
        If (Right(lowerFileName, 4) = ".xls" Or Right(lowerFileName, 5) = ".xlsx") _
            And InStr(lowerFileName, "revised") > 0 Then
            
            Debug.Print "找到文件:" & fileObj.Path
        End If
    Next fileObj
    
    ' 递归遍历子文件夹
    For Each subFolder In currentFolder.SubFolders
        TraverseSingleFolder subFolder, fso
    Next subFolder
End Sub

注意事项

  1. 操作文件后记得关闭:打开Excel文件处理完,一定要用wb.Close SaveChanges:=False(根据需求决定是否保存),避免占用资源
  2. 路径问题:如果你的目标文件夹不是当前工作簿所在路径,直接修改folderPath为绝对路径即可,比如folderPath = "C:\Your\Target\Folder\"
  3. 特殊文件名:这些方案都支持带特殊字符的文件名,不用担心遗漏

内容的提问来源于stack exchange,提问作者Kob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:29:12