【问题标题】:VBA - open all excel files from hard driveVBA - 从硬盘打开所有excel文件
【发布时间】:2021-06-12 20:15:48
【问题描述】:

我看到了许多从特定文件夹打开 excel 文件的代码。 你能帮我制作一个可以遍历我硬盘上每个文件夹的代码吗? 我知道这听起来很重要,但是你 - 这就是我需要的......

谢谢!

【问题讨论】:

    标签: excel file directory disk


    【解决方案1】:

    添加对 Microsoft Scripting Runtime 的引用(工具 -> 引用...)。然后你可以编写如下代码:

    Dim fso As New FileSystemObject
    
    Sub ParseFolder(fldr As Folder)
        Dim subfolder As Folder
        For Each subfolder in fldr.Subfolders
            ParseFolder subfolder
        Next
    
        Dim f As File
        For Each f In fldr.Files
            Select Case fso.GetExtensionName(file.path)
                Case "xls", "xlsb", "xlsm"
                    
                    ' Do something with Excel file here
    
            End Select
        Next
    End Sub
    
    Sub Main
        ' Run this macro to start the process running
        ParseFolder fso.GetFolder("C:\")
    End Sub
    

    根据磁盘上有多少 Excel 文件,您可能希望将代码仅定位到较小的 Excel 文件子集或较小的文件夹子集;或使用DoEvents,这样应用程序就不会冻结。


    参考文献

    脚本运行时

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多