1

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

例子

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

第1234章 E:\文档\ABC

苹果 | F:\

文件2 | E:\文档\测试\

我是擅长宏的新手。请帮帮我!

提前致谢!

4

1 回答 1

3

试试这个:

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
于 2013-09-07T14:25:34.420 回答