【问题标题】:Excel Macro to search for files based on a keyword and return true if presentExcel 宏根据关键字搜索文件,如果存在则返回 true
【发布时间】:2013-09-07 12:54:55
【问题描述】:

有人可以帮助我使用 excel VBA 宏来搜索 B 列中提供的各种目录中的文件,基于 A 列中给出的关键字,并在 C 列中返回“文件存在”/“文件不存在”。

例子

关键字 |文件夹路径 |结果

1234 | E:\Documents\ABC

苹果 | F:\

文件2 | E:\Documents\Test\

我是 excel 宏的新手。请帮帮我!

提前致谢!

【问题讨论】:

  • 文件名中是否有关键字??
  • 是的,很抱歉。关键字是文件名的一部分。比如说,如果关键字是“1234”,并且在目录“E:\Documents\ABC”中,文件名可以是1234_abcdefg或者abcde_1234_ghij,希望这样有意义

标签: vba excel


【解决方案1】:

试试这个:

Sub IsItThere()
    Dim KeyWd As String
    Dim Pathh As String, fName As String
    Dim N As Long, J As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    For J = 1 To N
        KeyWd = Cells(J, 1).Value
        Pathh = Cells(J, 2).Value
        If Right(Pathh, 1) = "\" Then
            Pathh = Mid(Pathh, 1, Len(Pathh) - 1)
        End If
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.Namespace((Pathh))

        For Each strFileName In objFolder.Items
            fName = objFolder.GetDetailsOf(strFileName, 0)
            If InStr(1, fName, KeyWd) > 0 Then
                Cells(J, 3).Value = "File Present"
                GoTo NextRecord
            End If
        Next
        Cells(J, 3).Value = "File Not Present"
NextRecord:
        Set objFolder = Nothing
        Set objShell = Nothing
    Next J
End Sub

【讨论】:

  • 这非常好用!谢谢!我只需要将For J = 1 To N 中的值更改为For J = 2 To N - 我只是想知道,如果文件夹位置在共享驱动器上,那么位置字符串就会变成这样的“\\Files\CheckData\”。在这种情况下,我应该将代码更改为其他内容吗?
  • 我不知道............我总是为网络文件夹分配一个逻辑驱动器指示器(如 Z:)
  • 好的!这个宏有没有办法检查子文件夹?
猜你喜欢
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多