Written by Allen Wyatt (last updated March 18, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Marius has a workbook that may be referenced (linked to) by other workbooks. He would like to delete this workbook, but cannot if it is linked to others. The problem is that he doesn't know how to figure out if this workbook is referenced by other workbooks or not.
Excel keeps track of outbound links for a workbook (just choose Edit Links from the Data tab of the ribbon to see what links there are within the current workbook), but for most versions of the program does not keep track of inbound links. The only versions that do allow you to track inbound links are some enterprise versions of Excel, as described at this Microsoft site:
https://support.microsoft.com/en-us/office/see-links-between-workbooks-4e12fb30-afb7-4588-89ce-df3ebb39e744
For the rest of us non-enterprise users, we are left to our own devices. This means that in order to see if there is a link targeting the current workbook, you actually need to open every other workbook on your system and check its links to see if any of them reference the current workbook.
This process is tedious at best but could best be classified as daunting. This makes it a perfect candidate for a macro. After all, a macro could open a bunch of other workbooks and check for a link to a specific workbook. Here's a simple macro that can check all the workbooks in a specific folder:
Sub FindWbkReferences() Dim folderp As String Dim fname As String Dim sMsg As String Dim wb As Workbook Dim ws As Worksheet Dim cel As Range folderp = "C:\Documents\Excel\" fname = Dir(folderp & "*.xlsx") sMsg = "" Do While fname <> "" Set wb = Workbooks.Open(folderp & fname) 'Open workbook For Each ws In wb.Worksheets 'Check each worksheet ' Loop through all cells in the worksheet For Each cel In ws.UsedRange.Cells ' Check if the cell contains a formula referencing ' the specified file If InStr(cel.Formula, "WbkToDelete") > 0 Then 'Save location of reference sMsg = sMsg & "Workbook: " & "'" & wb.Name & "'" sMsg = sMsg & " contains reference to this workbook in " sMsg = sMsg & " worksheet: " & ws.Name & ", " sMsg = sMsg & " cell: " & cel.Address & vbCr End If Next cel Next ws wb.Save wb.Close fname = Dir() Loop If sMsg = "" Then sMsg = "No inbound references located" MsgBox sMsg End Sub
The macro checks every formula in every used cell of every worksheet of every workbook in a given folder. The folder to check is specified in the folderp variable, and you'll note that it also specifies workbooks of a certain type. (In this case, it is XLSX workbooks.)
This process can take some time to accomplish, depending on how many folders there are. Plus, it becomes quickly apparent that this isn't a very robust approach. The problem is that you may need to check workbooks in multiple folders, or there could be links from objects other than formulas or even from macros. Or, there could be inbound links from other programs in the Office suite, such as Word or PowerPoint.
Could you adjust your macro to check for every possible source of an inbound link? Conceivably you could, but it would quickly become a task almost as arduous as opening and checking each potential source manually.
You could, if you desire, try a semi-manual way to look for inbound links. Let's say that you want to check for links to MyBook.xlsx. Close Excel and then open a Windows Explorer window. In the Search box (upper-right corner), enter the workbook name (MyBook.xlsx) and let Windows do its searching. Since Windows searches within each file for your text, you should soon see a list of any files that contain "MyBook.xlsx".
Perhaps the best approach, however, is to simply rename the workbook you are thinking of deleting. This would break any inbound links because the target of the link is no longer existent—you've renamed it. You can then continue to use your system as normal over the next few weeks or few months. If you get no notifications of broken links, then you can safely delete the workbook. A variation on this is to move the workbook instead of renaming it. Simply move it to an external device, such as a flash drive, and then go about your work. If you discover down the road you still need it, then you can always move it back, as needs dictate.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7639) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Excel has the capability to automatically open workbooks when you first start the program. You may not want to have one ...
Discover MoreExcel allows you to easily make changes to the ribbon, as well as to the Quick Access Toolbar, which is near the ribbon. ...
Discover MoreDo you work with a group of workbooks all the time in Excel? Windows and Excel both provide a plethora of ways you can ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-07-02 10:30:08
J. Woolley
For more on this subject, see https://excelribbon.tips.net/T007799
2023-03-26 15:31:03
J. Woolley
My Excel Toolbox now includes the LinksToMe macro to identify all workbooks with an external data link referencing the active workbook.
See https://sites.google.com/view/MyExcelToolbox
2023-03-18 11:37:56
J. Woolley
The Tip's macro "checks every formula in every used cell of every worksheet of every workbook in a given folder." To make it more efficient, replace the following statements
For Each cel In ws.UsedRange.Cells
...
Next cel
with these statements
On Error Resume Next
For Each cel In ws.UsedRange.SpecialCells(xlCellTypeFormulas)
...
Next cel
On Error GoTo 0
But to find all workbooks that reference MyBook.xlsx, you don't need to look at formulas for each cell of every worksheet of every workbook. You can look for workbooks that have an external link to MyBook.xlsx instead. Here is a modified version of the Tip's macro that should run faster:
Sub FindWbkReferences()
Const WorkbookToDelete = "MyBook.xlsx"
Const FolderPath = "C:\Documents\Excel\"
Dim fname As String, msg As String, wb As Workbook
Dim oldAS As Variant, oldCM As Variant, V As Variant, item As Variant
oldCM = Application.Calculation
Application.Calculation = xlCalculationManual
oldAS = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Application.ScreenUpdating = False
fname = Dir(FolderPath & "*.xls?")
On Error Resume Next
Do While fname <> ""
Set wb = Workbooks.Open(FolderPath & fname, _
UpdateLinks:=0, Password:="")
If Err Then
msg = msg & vbNewLine & "could not open " & fname
Err.Clear
Else
V = wb.LinkSources(xlExcelLinks)
If Not IsEmpty(V) Then
For Each item In V
If InStr(item, WorkbookToDelete) > 0 Then
msg = msg & vbNewLine & wb.Name
Exit For
End If
Next item
End If
wb.Close SaveChanges:=False
End If
fname = Dir()
Loop
On Error GoTo 0
Application.ScreenUpdating = True
Application.AutomationSecurity = oldAS
Application.Calculation = oldCM
If msg = "" Then
msg = "no files referencing " & WorkbookToDelete
Else
msg = "files referencing " & WorkbookToDelete & msg
End If
MsgBox FolderPath & vbNewLine & msg
End Sub
Replace Const WorkbookToDelete and Const FolderPath with appropriate values. Notice this version checks *.xlsm and *.xlsb as well as *.xlsx workbooks.
In each workbook that references MyBook.xlsx as an external link, you can use My Excel Toolbox's ListExLinks dynamic array function as follows:
=ListExLinks([SkipReference],[SkipHeader])
The result is similar to the Data > Queries & Connections > Edit Links dialog minus its Update column, but ListExLinks optionally adds a Reference column to identify cells that contain a formula referencing the link.
When using pre-2021 versions of Excel without support for dynamic arrays, consider UseSpillArray.pdf.
See https://sites.google.com/view/MyExcelToolbox
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments