VBA查找多个文件

2023-12-22

我有这段代码,它根据搜索字符串查找文件名(以及文件路径)。这段代码在查找单个文件时效果很好。我希望这个宏能够查找多个文件并使用逗号分隔显示它们的名称。

Function FindFiles(path As String, SearchStr As String)

          Dim FileName As String   ' Walking filename variable.
          Dim DirName As String    ' SubDirectory Name.
          Dim dirNames() As String ' Buffer for directory name entries.
          Dim nDir As Integer      ' Number of directories in this path.
          Dim i As Integer         ' For-loop counter.
          Dim Name As String
          Dim Annex As String

          On Error GoTo sysFileERR
          If Right(path, 1) <> "\" Then path = path & "\"
          ' Search for subdirectories.
          nDir = 0
          ReDim dirNames(nDir)
          DirName = Dir(path, vbDirectory Or vbHidden Or vbArchive Or vbReadOnly _
    Or vbSystem)  ' Even if hidden, and so on.
          Do While Len(DirName) > 0
             ' Ignore the current and encompassing directories.
             If (DirName <> ".") And (DirName <> "..") Then
                ' Check for directory with bitwise comparison.
                If GetAttr(path & DirName) And vbDirectory Then
                   dirNames(nDir) = DirName
                   DirCount = DirCount + 1
                   nDir = nDir + 1
                   ReDim Preserve dirNames(nDir)
                   'List2.AddItem path & DirName ' Uncomment to list
                End If                           ' directories.
    sysFileERRCont:
             End If
             DirName = Dir()  ' Get next subdirectory.
          Loop

          ' Search through this directory and sum file sizes.
          FileName = Dir(path & SearchStr, vbNormal Or vbHidden Or vbSystem _
          Or vbReadOnly Or vbArchive)
          'Sheet1.Range("C1").Value2 = path & "\" & FileName
          While Len(FileName) <> 0
             FindFiles = path & "\" & FileName
             FileCount = FileCount + 1
             ' Load List box
            ' Sheet1.Range("A1").Value2 = path & FileName & vbTab & _
                FileDateTime(path & FileName)   ' Include Modified Date
             FileName = Dir()  ' Get next file.
          Wend

          ' If there are sub-directories..
          If nDir > 0 Then
             ' Recursively walk into them
             For i = 0 To nDir - 1
               FindFiles = path & "\" & FileName
             Next i
          End If

    AbortFunction:
          Exit Function
    sysFileERR:
          If Right(DirName, 4) = ".sys" Then
            Resume sysFileERRCont ' Known issue with pagefile.sys
          Else
            MsgBox "Error: " & Err.Number & " - " & Err.Description, , _
             "Unexpected Error"
            Resume AbortFunction
          End If
          End Function



          Sub Find_Files()
          Dim SearchPath As String, FindStr As String, SearchPath1 As String
          Dim FileSize As Long
          Dim NumFiles As Integer, NumDirs As Integer
          Dim Filenames As String, Filenames1 As String
          Dim r As Range
          'Screen.MousePointer = vbHourglass
          'List2.Clear

          For Each cell In Range("SS")
          SearchPath = Sheet3.Range("B2").Value2
          SearchPath1 = Sheet3.Range("B3").Value2

          FindStr = Cells(cell.Row, "H").Value
          Filenames = FindFiles(SearchPath, FindStr)
          Filenames1 = FindFiles(SearchPath1, FindStr)
          'Sheet1.Range("B1").Value2 = NumFiles & " Files found in " & NumDirs + 1 & _
           " Directories"
          Cells(cell.Row, "F").Value = Filenames
          Cells(cell.Row, "G").Value = Filenames1

          'Format(FileSize, "#,###,###,##0") & " Bytes"
          'Screen.MousePointer = vbDefault
          Next cell

          End Sub

任何想法都将受到高度赞赏。


我意识到这个问题很老了,但没有答案。这是查找多个文件及其路径的快速方法。 VBA的DIR功能不是很方便,但是 CMD 的DIR功能 http://ss64.com/nt/dir.html进行了很好的优化,并具有大量的命令行开关,使其仅返回符合您条件的文件(甚至只是文件夹)。诀窍是call DIR从 WScript shell https://blogs.technet.microsoft.com/heyscriptingguy/2004/08/10/how-can-i-call-the-dir-command/以便VBA可以解析输出。

例如,这段代码将找到系统上以config.

Dim oShell As Object 'New WshShell if you want early binding
Dim cmd As Object 'WshExec if you want early binding
Dim x As Integer
Const WshRunning = 0

Set oShell = CreateObject("Wscript.Shell")
Set cmd = oShell.Exec("cmd /c ""Dir c:\config* /a:-d /b /d /s""")

Do While cmd.Status = WshRunning
    DoEvents
Loop

Debug.Print cmd.StdOut.ReadAll
Set oShell = Nothing
Set cmd = Nothing
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VBA查找多个文件 的相关文章

  • Excel VBA 更改命令按钮的颜色

    我在更改颜色时遇到问题CommandButton 在电子表格中 我添加设计按钮作为表单或 ActiveX 然后在 VBA 中我尝试 Activesheet shapes CommandButton1 visible false 这个效果很好
  • VBA中如何四舍五入到小数点后两位?

    在单元格 B2 中 在进行计算之前 我的变量值为 297 123 在 VBA 中 我想将其四舍五入为 297 12 请参阅下面的代码了解我的尝试 两者都将燃油评估为 297 我做错了什么 Dim fuel As Integer Dim li
  • 过滤所有独特的项目,例如 Google 文档

    是否有一种快速 简单的方法来过滤 Excel 2013 列中的所有唯一项目 类似于 Google Docs 唯一 功能 这不是一个漂亮的答案 但它有效 将其作为数组公式粘贴到单元格中B2 LOOKUP 2 1 COUNTIF B 1 B1
  • 如何下载到 Excel?

    我想为我的 Coldfusion 网站不同部分上的几组不同数据提供 下载到 Excel 功能 我正在使用 Coldfusion 并且希望使用免费的自定义标签 库来帮助我完成此任务 而不是自己从头开始编码 我被指出cflib org http
  • 如何在 to_excel() 和 read_excel() 之间保留 pandas 多重索引?

    根据 pandas 文档读取Excel http pandas pydata org pandas docs dev generated pandas io excel read excel html 我可以将索引列名称放在单独的行上 然后
  • Interop.Excel 和 Tools.Excel 之间的区别?

    我目前正在开发 Microsoft Excel 的插件 但我对某些事情有点困惑 两者有什么区别Interop Excel and Tools Excel 例如 之间Interop Excel Workbook and Tools Excel
  • 延迟宏以允许事件完成

    在尝试从宏内访问外部 API 函数集时 我发现有必要添加延迟 以便外部 API 有时间处理选择 实现这一点会带来一些困难 因为使用 Application Wait 或 Application Sleep 不起作用 在线搜索让我尝试使用 G
  • 使用 Python 将 Excel 中的图表导出为图像

    我一直在尝试将 Excel 中的图表导出为 Python 中的图像文件 JPG 或 ING 我正在查看 WIn32com 这是我到目前为止所拥有的 import win32com client as win32 excel win32 ge
  • 更改列标签?例如:将“A”列更改为“名称”列

    谁能告诉我如何更改列标签 例如 我想将列 A 更改为列 名称 Excel Excel 的版本是什么 一般来说 您无法更改列字母 它们是 Excel 系统的一部分 您可以使用工作表中的一行来输入您正在使用的表格的标题 表标题可以是描述性列名称
  • VBA 激活 Internet Explorer 窗口

    我正在制作一个宏 用于打开 Internet Explorer 导航并登录网站 一切正常 但我需要将 IE 窗口放在前面并激活它 这样我就可以使用SendKeys在上面 我发现网站和视频在名为的命令上有不同的方法AppActivate我已经
  • 如何高效打开巨大的excel文件

    我有一个 150MB 的单页 Excel 文件 在一台非常强大的机器上使用以下命令打开大约需要 7 分钟 using python import xlrd wb xlrd open workbook file sh wb sheet by
  • Countif 不适用于小时和/或日期

    您好 我有 3 列内的数据 A 目的地 例如洛杉矶 B 承运人 例如 Ups C 发货时间 预计 4 00 使用的时间是24小时时间 不含Am Pm 我需要进行计数才能知道在特定时间我们有多少批货物 尝试过 COUNTIF A1 A100
  • Perforce Excel 文件差异

    我有一堆 excel 文件放在 perforce 上 我想对这些 excel 文件进行 diff 类似于我在 perforce 中对其他文件 如 java cs 等 所做的操作 我尝试寻找一个插件 发现 P4OFC 只适合对 word 文件
  • 使用 VBA 的下拉菜单

    我需要使用 VBA 从下拉菜单中选择特定选项 我怎样才能做到这一点 链接到我们试图从中提取的网页 IE document getElementsByName down count click 我尝试过的代码 Full Module Priv
  • 在 Excel 表格中选择多列的代码

    我是 Excel VBA 新手 我需要修改我的代码 以便我能够进一步进行 我想在 Excel 表格中选择多个表格列 这是我的代码 Dim ws As Worksheet Dim tbl As ListObject Set ws Sheets
  • 如何在VBA编辑器中跳转到行号?

    我在 Office 2010 中使用 VBA 在顶部 有一个带有行号和列号的框 例如 Ln 1480 Col 17 有没有办法在代码编辑中 而不是在执行中 直接跳转到另一个行号 就像我使用的那样Ctrl G在记事本中 这个MSDN答案 ht
  • excel 2010刷新BackgroundQuery中运行时错误1004

    我正在尝试用 vba 编写一个脚本 用于将多个文本文件导入 Excel 一张纸 然后将它们绘制在一张图表上 我面临一个问题刷新后台查询命令并出现 1004 运行时错误 我怎样才能解决它 谢谢 埃亚勒 这是我的代码 Sub fring1 Di
  • VBA Shell 并等待退出代码

    我正在打包一个办公应用程序 VBA 它调用 C 控制台应用程序来执行应用程序 大型模拟程序 的一些繁重工作 我希望能够让 VBA 应用程序等待控制台应用程序完成并从控制台应用程序检索退出代码 我已经能够做到前者 但尚未能够从应用程序中检索退
  • 合并和颜色样式不适用于 Apache POI excel 2003 格式

    在 Apache POI 中 我为某些单元格应用了一些样式并合并了这些单元格 当我在 2010 年或 2007 年打开时 它工作正常 但在 2003 年 格式样式消失了 每次保存 2003 Excel 文件之前都会弹出兼容性检查对话框 请参
  • 导出到excel时如何显示前导零?

    我正在通过更改内容类型来创建 Excel 报告 Response ContentType application vnd ms excel 我有包含前导零的值 问题是导出到 Excel 时缺少前导零 e g 000123 gt 123 我知

随机推荐