VBA检查子文件夹

时间:2017-01-11 04:46:37

标签: excel vba excel-vba

我在VBA比较业余,并且在网上使用技术提供的代码。

我有一个包含B列文件名的Excel文档(并不总是一种文件类型),我试图确保我在指定的文件夹中有副本和正确的版本。

目前,代码适用于特定的文件夹位置,但Excel电子表格中引用的文件存在于各种其他文件夹中,因此我需要创建一个代码,可以搜索主文件夹并循环遍历各个子文件夹。

请参阅下面的当前代码以供参考。

Sub CheckIfFileExists()

    Dim LRow As Integer
    Dim LPath As String
    Dim LExtension As String
    Dim LContinue As Boolean

    'Initialize variables
    LContinue = True
    LRow = 8
    LPath = "K:\location\main folder\sub folder \sub folder"
    LExtension = ".pdf"

    'Loop through all column B values until a blank cell is found
    While LContinue

        'Found a blank cell, do not continue
        If Len(Range("B" & CStr(LRow)).Value) = 0 Then
            LContinue = True

        'Check if file exists for document title
        Else
            'Place "No" in column E if the file does NOT exist
            If Len(Dir(LPath & Range("B" & CStr(LRow)).Value & LExtension)) = 0 Then
                Range("E" & CStr(LRow)).Value = "No"
            'Place "Yes" in column E if the file does exist
            Else
                Range("E" & CStr(LRow)).Value = "Yes"
            End If


        End If

        LRow = LRow + 1

    Wend

End Sub

有超过1000个文档,所以简单的窗口搜索并不理想,我已经回顾了以前的几个问题,找不到有帮助的答案。

1 个答案:

答案 0 :(得分:2)

好的,我的答案将围绕您的问题提出两条评论。这只会作为您改进和适应您需要的基础。

N.B跳到我的答案底部查看完整的工作代码

第一条评论是:

  

我需要创建一个代码,可以搜索主文件夹并遍历各个子文件夹。

我将在下面解释的代码将采用MAIN FOLDER,您需要指定,然后它将遍历父Directoy的所有子文件夹。因此您无需担心特定的子文件夹。只要您知道要访问的文件名,代码就会找到它。

第二行是你的代码:

LPath = "K:\location\main folder\sub folder \sub folder"

这行代码将构成我将在下面显示的UDF(用户定义函数)的一部分。

第1步

将LPath重新标记为所谓的"主机文件夹"。这是主要的文件夹。 例如:Host Folder = "K:\User\My Documents\"(注意最后需要反斜杠)

第2步

在2个位置设置对Microsoft Scripting Runtime的引用:

i)在代码中

Set FileSystem = CreateObject("Scripting.FileSystemObject")

ii)在VBA编辑器中。 (关于如何在VBA编辑器中查找参考库的基本谷歌搜索)

第3步

这是主要元素,这是一个子程序,无论它在何处都可以找到该文件,提供文件名主机文件夹

Sub DoFolder(Folder)

    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    Dim File
    For Each File In Folder.Files
        If File.Name = "Specify Name.pdf" Then
            Workbooks.Open (Folder.path & "\" & File.Name), UpdateLinks:=False
            Workbooks(File.Name).Activate
            Exit Sub
        End If
    Next

End Sub

上面的代码只需在找到文件后立即打开。这只是我自己的具体用途;必要时适应。

主要代码

Option Explicit
Dim FileSystem As Object
Dim HostFolder As String

Sub FindFile()
HostFolder = "K:\User\My Documents\"

Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)

End Sub
Sub DoFolder(Folder)

    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    Dim File
    For Each File In Folder.Files
        If File.Name = "Specify Name.pdf" Then
            Workbooks.Open (Folder.path & "\" & File.Name), UpdateLinks:=False
            Workbooks(File.Name).Activate
            Exit Sub
        End If
    Next

End Sub

你可以按照你认为合适的方式进行修改,你可以把它扔进你的子CheckIfFileExists()或者单独使用它。

让我知道你是如何相处的,这样我可以帮助你进一步理解

相关问题