Excel宏根据关键字搜索文件,如果存在则返回true

时间:2013-09-07 12:54:55

标签: excel vba excel-vba

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

实施例

关键字| FolderPath |结果

1234 | E:\文件\ ABC

Apple | F:\

File2 | E:\ Documents \ Test \

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

提前致谢!

1 个答案:

答案 0 :(得分: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