为Excel依赖搜索驱动器[关闭](Search drive for excel dependenc

2019-09-18 11:12发布

我目前简化公司的文件结构。 这是一个总的混乱。 目前,我做财务部门,已配发Excel文件之间的依赖关系。 这些文件我不能迁移到新的结构,因为位置的变化和依赖丢失。

因此,我在搜索扫描的文件夹和Excel依赖其子文件夹的工具的。 我想列出这些,说:嗨,那这些文件?

有任何想法吗?

Answer 1:

下面的代码

  • 打开的每个,在或通过指定的目录下面坐在文件strStartFolder (即,“C:\温度”)在这个例子中使用递归Dir
  • 会在每个文件中的任何文件链接
  • 使用一个数组来保存然后填充最后的结果

请改变你的路径strStartFolder ,以适应

此代码是以前出版的另一个论坛上的一篇文章

Option Explicit

Public StrArray()
Public lngCnt As Long

Public Sub Main()
Dim objFSO As Object
Dim objFolder As Object
Dim WB As Workbook
Dim ws As Worksheet
Dim strStartFolder As String

'Setup Application for the user
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

'reset public variables
lngCnt = 0
ReDim StrArray(1 To 4, 1 To 1000)

strStartFolder = "c:\temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Format output sheet
Set WB = Workbooks.Add(1)
Set ws = WB.Worksheets(1)
ws.[a1] = Now()
ws.[a2] = strStartFolder
ws.[a1:a3].HorizontalAlignment = xlLeft

ws.[A4:D4].Value = Array("Folder", "File", "Linked File", "Linked File Path")
ws.Range([a1], [c4]).Font.Bold = True
ws.Rows(5).Select
ActiveWindow.FreezePanes = True


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strStartFolder)

' Start the code to gather the files
ShowSubFolders objFolder, True
ShowSubFolders objFolder, False

If lngCnt > 0 Then
    ' Finalise output
    With ws.Range(ws.[a5], ws.Cells(5 + lngCnt - 1, 4))
        .Value2 = Application.Transpose(StrArray)
        .Offset(-1, 0).Resize(Rows.Count - 3, 4).AutoFilter
        .Offset(-4, 0).Resize(Rows.Count, 4).Columns.AutoFit
    End With
    ws.[a1].Activate
Else
    MsgBox "No files found!", vbCritical
    WB.Close False
End If

' tidy up

Set objFSO = Nothing

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .StatusBar = vbNullString
End With
End Sub


Sub ShowSubFolders(ByVal objFolder, bRootFolder As Boolean)

Dim colFolders As Object
Dim objSubfolder As Object
Dim WB As Workbook
Dim lnkSources
Dim lnk

'strName must be a variant, as ParseName does not work with a string argument
Dim strFname
Set colFolders = objFolder.SubFolders
Application.StatusBar = "Processing " & objFolder.Path

If bRootFolder Then
    Set objSubfolder = objFolder
    GoTo OneTimeRoot
End If

For Each objSubfolder In colFolders
    'check to see if root directory files are to be processed
OneTimeRoot:
    strFname = Dir(objSubfolder.Path & "\*.xls*")
    Do While Len(strFname) > 0
        Set WB = Workbooks.Open(objSubfolder.Path & "\" & strFname, False)
        lnkSources = WB.LinkSources
        If Not IsEmpty(lnkSources) Then
            For Each lnk In lnkSources
                lngCnt = lngCnt + 1
                If lngCnt Mod 1000 = 0 Then ReDim Preserve StrArray(1 To 4, 1 To (lngCnt + 1000))
                StrArray(1, lngCnt) = WB.Path
                StrArray(2, lngCnt) = WB.Name
                StrArray(3, lngCnt) = Left$(lnk, InStrRev(lnk, "\"))
                StrArray(4, lngCnt) = Right$(lnk, Len(lnk) - InStrRev(lnk, "\"))
            Next

        End If
        WB.Close False
        strFname = Dir
    Loop
    If bRootFolder Then
        bRootFolder = False
        Exit Sub
    End If
    ShowSubFolders objSubfolder, False
Next
End Sub


文章来源: Search drive for excel dependencies [closed]