ASKER
ASKER
Option Explicit
Sub scanIdentifyLinks()
Dim wkb As Workbook
Dim wks As Worksheet
Dim wkbIn As Workbook
Dim aLink As Variant
Dim strPath As String
Dim fName As Variant
Dim dialogFile As FileDialog
Dim iFile As Long
Dim aLinks As Variant
Dim iLink As Long
Dim fCol As New Collection
Application.ScreenUpdating = False
Set wkb = ThisWorkbook
Set wks = wkb.Worksheets("Output")
wks.Cells.Clear
wks.Range("A1:B1").Value = Split("Path\Filename,Links that exist ->", ",")
'prompt for path to explore
strPath = ThisWorkbook.Path & "\"
Set dialogFile = Application.FileDialog(msoFileDialogFolderPicker)
With dialogFile
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
.InitialFileName = strPath
.Title = "Select Folder for Link Analysis"
.Show
End With
If dialogFile.SelectedItems.Count > 0 Then
strPath = dialogFile.SelectedItems(1)
Application.EnableEvents = False
Call getFiles(strPath, "*.XLS*", fCol, True)
'fName = Dir(strPath & "\*.xls*") 'check all files in folder
On Error Resume Next
For Each fName In fCol 'Do While fName <> vbNullString
wks.Range("A2").Offset(iFile, 0).Value = fName
Application.StatusBar = "Processing file: " & fName
'open each file
Set wkbIn = Workbooks.Open(Filename:=fName, UpdateLinks:=False, ReadOnly:=True)
If Err.Number <> 0 Then
wks.Range("A2").Offset(iFile, 1).Value = "Unable to open file, moving on..."
wks.Range("A2").Offset(iFile, 2).Value = "Err: " & Err.Number & "-> " & Err.Description
Else
'get links
aLinks = wkbIn.LinkSources(xlExcelLinks)
'output links
If IsEmpty(aLinks) Then
wks.Range("A2").Offset(iFile, 1).Value = "No Links Found"
Else
For iLink = LBound(aLinks) To UBound(aLinks)
wks.Range("A2").Offset(iFile, iLink).Value = aLinks(iLink)
Next iLink
End If
End If
wkbIn.Close savechanges:=False
Err.Clear
iFile = iFile + 1
'fName = Dir()
Next fName
End If
MsgBox "Process Complete!"
gracefulExit:
Set dialogFile = Nothing
Set wkbIn = Nothing
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.StatusBar = False
End Sub
'below is code for changing links from old path to new path, and old workbook name to new workbook name
Function changeLink(myWkb As Workbook, oldPath As String, newPath As String, oldName As String, newName As String) As Boolean
Dim aLinks As Variant
Dim oldLink As String
Dim newLink As String
Dim i As Long
aLinks = myWkb.LinkSources(xlExcelLinks)
If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
oldLink = aLinks(i)
newLink = Replace(oldLink, oldPath & "\", newPath & "\")
newLink = Replace(newLink, oldName, newName)
If oldLink <> newLink Then
myWkb.changeLink Name:=oldLink, newName:=newLink
End If
Next i
End If
changeLink = True
End Function
Sub getFiles(strPath, strFilter As String, fCol As Collection, bSubFolders)
Dim FSO As Object
Dim fldr As Object
Dim subFldr As Object
Dim fName As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fldr = FSO.GetFolder(strPath)
For Each fName In fldr.Files
If UCase(getFileExt(fName)) Like strFilter Then
fCol.Add fName
End If
Next fName
If bSubFolders Then
For Each subFldr In fldr.subfolders
Call getFiles(subFldr, strFilter, fCol, True)
Next subFldr
End If
End Sub
Public Function getFileExt(fName As Variant) As String
Dim i As Integer
i = InStr(StrReverse(fName), ".")
getFileExt = StrReverse(Left(StrReverse(fName), i))
End Function
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
https://www.experts-exchange.com/questions/27721498/Identify-Links-in-Excel-Spreadsheets.html
My solution is the last post in that thread. Download the file in a trusted location (where Excel can run macros from) and give it a look-see. You'll be prompted for a folder, then it will identify links (references to other files) for every Excel file found in that folder.
Cheers,
Dave