在 Excel 工作簿中找不到链接

2024-05-27

我编写了一个宏来打开多个受密码保护的工作簿。这些工作簿彼此之间都有链接,因此为了方便起见,我设置了UpdateLinks:=0这样在其他书籍打开之前,我就不会收到所有链接更新的密码提示。

所有工作簿打开后,我尝试使用以下命令更新链接

Workbooks("Workbook1").UpdateLink Type:=1
Workbooks("Workbook2").UpdateLink Type:=1
Workbooks("Workbook3").UpdateLink Type:=1
Workbooks("Workbook4").UpdateLink Type:=1

这更方便,因为工作簿现在已打开,因此不需要密码提示。

这在两个工作簿上运行良好,但另外两个工作簿提示我找到不存在的链接的源。也就是说,工作簿中不存在实际的链接。

我花了几个小时试图弄清楚它是从哪里获取此链接的,但它根本不存在于任何地方。

为了使这一点更清楚,在工作簿 2 中,我有三个链接 A、B 和 C。这些链接在“数据”>“编辑链接”菜单中可见。但是,当我运行宏时,它要求我找到链接 E 的源...

我尝试了以下方法来查看是否存在由于某种原因不可见的链接

Workbooks("Workbook2").Activate

aLinks = ActiveWorkbook.LinkSources(1)
If Not IsEmpty(aLinks) Then
    For i = 1 To UBound(aLinks)
        MsgBox "Link " & i & ":" & Chr(13) & aLinks(i) 
    Next i
End If

这仅显示了我在编辑链接中可以看到的三个。

我在工作簿中搜索了它试图让我找到文件的链接名称,但什么也没找到。

有人以前见过这个或者有什么想法吗?这让我难住了,让原本简单的工作变得非常令人沮丧。


可以通过多种方式(有意或无意)创建工作簿之间的链接:

1. Within formulae 
2. Inside range names
3. Inside chart ranges

Excel 用户通常熟悉 (1),并搜索引用链接的文本,但这不会检测图表和范围名称中的链接。

比尔·曼维尔的findlink http://www.manville.org.uk/software/findlink.htm是查找和/或删除这些链接的出色解决方案。

下载插件,选择带有链接的文件,从 Excel 运行插件(Bill 页面上的说明),然后

  • 在下拉框中选择您要查找的参考
  • 我选择找到的选项,然后列出链接

各种链接类型的示例

样本输出

几年前,我尝试编写自己的链接查找器,下面的代码以防万一它有用

code

Option Explicit

' This code searches all sheets (worksheets and chart sheets) in the ActiveWorkbook for links
' and compiles a filtered CSV file to report on any:
' #1 Formula links (and validates them against linksources)
' #2 Range Name links
' #3 PivotTable links
' #4a Chart Series links (in both Chart Sheets and Charts on regular Worksheets)
' #4b Chart Title links (in both Chart Sheets and Charts on regular Worksheets)

' Download Bill Manville's FindLink at http://www.bmsltd.co.uk/MVP/Default.htm
' for a tool to manage (ie delete) links

' Notes
' 1) The Chart title method relies on activating the Chart.
'         ---> Protected sheets are skipped
'         ---> This method does not work in xl2007
' 2) I have deliberately left out error handling as I want to resolve any issues

Sub ListLinks()
    Dim objFSO As Object, objFSOfile As Object
    Dim wb As Workbook, sh
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rArea As Range
    Dim chr As ChartObject, chr1 As Chart
    Dim lSource, PivCh, chrSrs
    Dim FSOFileHeader As String, tmpStr As String, chrTitle As String, FirstAddress As String, ReportFile As String, ShProt As String
    Dim nameCnt As Long
    Dim FndRngLink As Boolean, FndChrLink As Boolean, FndNameLink As Boolean, FndPivLink As Boolean

    Application.ScreenUpdating = False
    'location of report file
    ReportFile = "c:\LinkReport.csv"
    FSOFileHeader = "Type,Object Level,Location,Linked Workbook,Full Linked File Path,Reference"

    Set objFSO = CreateObject("scripting.filesystemobject")
    On Error Resume Next
    'if report file is open then ask user to close it
    Set objFSOfile = objFSO.createtextfile(ReportFile)
    If Err.Number <> 0 Then
        MsgBox "Pls close " & vbNewLine & ReportFile & vbNewLine & "then re-run code"
        Exit Sub
    End If
    On Error GoTo 0

    'write report file headers
    With objFSOfile
        .writeline ActiveWorkbook.Path & "," & ActiveWorkbook.Name
        .writeline FSOFileHeader
    End With

    For Each sh In ActiveWorkbook.Sheets

        Select Case sh.Type
        Case xlWorksheet
            'look at formula cells in each worksheet
            Set rng1 = Nothing
            Set rng2 = Nothing
            Set rng3 = Nothing

            On Error Resume Next
            Set rng1 = sh.Cells.SpecialCells(xlCellTypeFormulas)
            On Error GoTo 0
            Application.StatusBar = "Searching formulas in sheet " & sh.Name
            If Not rng1 Is Nothing Then
                'look for *.xls
                With rng1
                    Set rng2 = .Find("*.xls", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False)
                    If Not rng2 Is Nothing Then
                        FirstAddress = rng2.Address
                        'validate that the *.xls is part of a linksource
                        For Each lSource In ActiveWorkbook.LinkSources
                            'look in open and closed workbooks
                            If InStr(Replace(rng2.Formula, "[", vbNullString), lSource) > 0 Or InStr(rng2.Formula, Right$(rng2.Formula, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                FndRngLink = True
                                'write to the report file
                                Set rng3 = rng2
                                Exit For
                            End If
                        Next
                        'repeat till code loops back to first formula cell containing "*.xls"
                        Do
                            Set rng2 = .FindNext(rng2)
                            If rng2.Address <> FirstAddress Then
                                For Each lSource In ActiveWorkbook.LinkSources
                                    If InStr(Replace(rng2.Formula, "[", vbNullString), lSource) > 0 Or InStr(rng2.Formula, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                        Set rng3 = Union(rng3, rng2)
                                        Exit For
                                    End If
                                Next
                            End If
                        Loop Until rng2.Address = FirstAddress
                    End If
                End With
            End If

            If Not rng3 Is Nothing Then
                For Each rArea In rng3.Areas
                    objFSOfile.writeline "Formula," & "Range" & "," & sh.Name & "!" & Replace(rArea.Address(0, 0), ",", ";") & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & rng3.Cells(1).Formula
                Next
            End If

            ' Charts
            For Each chr In sh.ChartObjects
                Application.StatusBar = "Searching charts in sheet " & sh.Name
                For Each chrSrs In chr.Chart.SeriesCollection
                    If InStr(chrSrs.Formula, ".xls") <> 0 Then
                        For Each lSource In ActiveWorkbook.LinkSources
                            'look in open and closed workbooks
                            If InStr(Replace(chrSrs.Formula, "[", vbNullString), lSource) > 0 Or InStr(chrSrs.Formula, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                FndChrLink = True
                                'write to the report file
                                objFSOfile.writeline "Chart Series," & chr.Name & "," & sh.Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & Replace(chrSrs.Formula, ",", ";")
                                Exit For
                            End If
                        Next
                    End If
                Next chrSrs

                If chr.Chart.HasTitle Then
                    If sh.ProtectContents = True Then
                        ShProt = ShProt & sh.Name & " - " & chr.Name & vbNewLine
                    Else
                        chr.Activate
                        chrTitle = CStr(ExecuteExcel4Macro("GET.FORMULA(""Title"")"))
                        If InStr(chrTitle, ".xls") <> 0 Then
                            For Each lSource In ActiveWorkbook.LinkSources
                                'look in open and closed workbooks
                                If InStr(Replace(chrTitle, "[", vbNullString), lSource) > 0 Or InStr(chrTitle, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                    FndChrLink = True
                                    'write to the report file
                                    objFSOfile.writeline "Chart Title," & chr.Name & "," & sh.Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & chrTitle
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                End If

            Next chr

            'Pivot Tables
            For Each PivCh In sh.PivotTables
                If InStr(PivCh.SourceData, ".xls") > 0 Then
                    For Each lSource In ActiveWorkbook.LinkSources
                        If InStr(Replace(PivCh.SourceData, "[", vbNullString), lSource) > 0 Or InStr(PivCh.SourceData, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                            objFSOfile.writeline "Pivot Table," & PivCh.Name & "," & sh.Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & PivCh.SourceData
                            FndPivLink = True
                            Exit For
                        End If
                    Next
                End If
            Next
        Case 3
            Set chr1 = Nothing
            On Error Resume Next
            Set chr1 = sh
            On Error GoTo 0
            If Not chr1 Is Nothing Then
                Application.StatusBar = "Searching charts in sheet " & sh.Name
                For Each chrSrs In chr1.SeriesCollection
                    If InStr(chrSrs.Formula, ".xls") <> 0 Then
                        For Each lSource In ActiveWorkbook.LinkSources
                            'look in open and closed workbooks
                            If InStr(Replace(chrSrs.Formula, "[", vbNullString), lSource) > 0 Or InStr(chrSrs.Formula, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                FndChrLink = True
                                'write to the report file
                                objFSOfile.writeline "Chart Series,Chart Sheet," & sh.Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & Replace(chrSrs.Formula, ",", ";")
                                Exit For
                            End If
                        Next
                    End If
                Next

                If chr1.HasTitle Then
                    chr1.Activate
                    chrTitle = CStr(ExecuteExcel4Macro("GET.FORMULA(""Title"")"))
                    If InStr(chrTitle, ".xls") <> 0 Then
                        For Each lSource In ActiveWorkbook.LinkSources
                            'look in open and closed workbooks
                            If InStr(Replace(chrTitle, "[", vbNullString), lSource) > 0 Or InStr(chrTitle, Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                                FndChrLink = True
                                'write to the report file
                                objFSOfile.writeline "Chart Title,Chart Sheet," & sh.Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & Replace(chrTitle, ",", ";")
                                Exit For
                            End If
                        Next
                    End If
                End If
            End If
        Case Else
        End Select
        'End If
    Next sh

    'Named ranges
    If ActiveWorkbook.Names.Count = 0 Then
    Else
        Application.StatusBar = "Searching range names"
        For nameCnt = 1 To ActiveWorkbook.Names.Count
            If InStr(ActiveWorkbook.Names(nameCnt), ".xls") <> 0 Then
                For Each lSource In ActiveWorkbook.LinkSources
                    If InStr(Replace(ActiveWorkbook.Names(nameCnt), "[", vbNullString), lSource) > 0 Or InStr(ActiveWorkbook.Names(nameCnt), Right$(lSource, Len(lSource) - InStrRev(lSource, "\"))) > 0 Then
                        FndNameLink = True
                        'write to the report file
                        objFSOfile.writeline "Range Name," & "Workbook level," & ActiveWorkbook.Names(nameCnt).Name & "," & Right$(lSource, Len(lSource) - InStrRev(lSource, "\")) & "," & lSource & ",'" & ActiveWorkbook.Names(nameCnt).RefersTo
                        Exit For
                    End If
                Next
                'Name link does not exist in "known" links
                If FndNameLink = False Then
                    FndNameLink = True
                    objFSOfile.writeline "Range Name," & "Workbook level," & ActiveWorkbook.Names(nameCnt).Name & "," & ActiveWorkbook.Names(nameCnt) & ",'" & Replace(ActiveWorkbook.Names(nameCnt).RefersTo, ",", ";")
                End If
            End If
        Next nameCnt
    End If

    'Close the report file
    objFSOfile.Close
    Set objFSO = Nothing

    'If at least one cell link was found then open report file
    If (FndChrLink = FndNameLink = FndRngLink = FndPivLink) And FndRngLink = False Then
        MsgBox "No formula links found", vbCritical
    Else
        Set wb = Workbooks.Open(ReportFile)
        With wb.Sheets(1)
            .Rows("1:2").Font.Bold = True
            .Columns("A:F").AutoFit
            .[A2].AutoFilter
        End With
    End If
    With Application
        .StatusBar = vbNullString
        .DisplayAlerts = True
    End With
    If ShProt <> vbNullString Then MsgBox "The following sheets were protected " & vbNewLine & "so these Chart titles could not be searched" & vbNewLine & ShProt, vbCritical
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Excel 工作簿中找不到链接 的相关文章

  • 如何模拟“焦点”和“打字”事件

    尝试模拟 onfocus 和打字事件 但它不起作用 Sub Login MyLogin MyPass Dim IEapp As InternetExplorer Dim IeDoc As Object Dim ieTable As Obje
  • xlwings: 删除一个列 | Excel 中的行

    如何删除 Excel 中的一行 wb xw Book Shipment xlsx wb sheets Page1 1 range 1 1 clear clear 用于删除内容 我想删除该行 我很惊讶 clear 函数有效 但 delete
  • 在 Excel 表格中选择多列的代码

    我是 Excel VBA 新手 我需要修改我的代码 以便我能够进一步进行 我想在 Excel 表格中选择多个表格列 这是我的代码 Dim ws As Worksheet Dim tbl As ListObject Set ws Sheets
  • excel 2010刷新BackgroundQuery中运行时错误1004

    我正在尝试用 vba 编写一个脚本 用于将多个文本文件导入 Excel 一张纸 然后将它们绘制在一张图表上 我面临一个问题刷新后台查询命令并出现 1004 运行时错误 我怎样才能解决它 谢谢 埃亚勒 这是我的代码 Sub fring1 Di
  • 如何让VLOOKUP在VBA中选择到最低行?

    希望自动在单元格中插入 VLOOKUP 公式 录制宏时 我指示它使用相同的公式填充下面的列 效果很好 但是 当 VLOOKUP 搜索的表发生变化 更多或更少的行 时 就会出现问题 在记录时 VLOOKUP 下降到表中的最后一行 273 但是
  • Excel FILTER() 对于空白单元格返回 0

    我怀疑以前有人问过这个问题 但我找不到 FILTER 即使指定了返回字符串 通常也会为空白行返回 0 Using filter 我经常收到空单元格的 0 返回值 假设 A 列中有 6 行数据 abc xyz abc xyz abc If I
  • 使用 ObjPtr(Me) 返回自定义类实例的名称?

    我明白那个ObjPtr http support microsoft com kb 199824将返回内存中对象的地址 并且它指向一个名为 IUNKNOWN 的结构 并且其中编码了某种接口定义以公开对象结构 但我不知道如何确定一个对象的接口
  • 将包含宏的工作簿复制到不带宏的工作簿

    我能够复制工作簿 复制到所需位置 其中在后台包含宏 该副本还包含相同的宏 我的问题是我不希望这个重复的工作簿包含宏 谁能告诉怎么做吗 先感谢您 将您的工作簿保存为无宏 即简单地保存为 Excel 工作簿 对于我的 Excel 2007 这是
  • 我可以获取VBA代码中的注释文本吗

    可以说我有以下内容 Public Sub Information TEST End Sub 有没有办法得到 TEST 结果 不知何故通过VBA 例如 在 PHP 中 有一个获取注释的好方法 这里有什么想法吗 编辑 应该有办法 因为像 MZ
  • 如何等到 Excel 计算公式后再继续 win32com

    我有一个 win32com Python 脚本 它将多个 Excel 文件合并到电子表格中并将其另存为 PDF 现在的工作原理是输出几乎都是 NAME 因为文件是在计算 Excel 文件内容之前输出的 这可能需要一分钟 如何强制工作簿计算值
  • VBA ByRef 参数类型不匹配

    最初在我的主代码部分中 我有一个丑陋的 if 语句 尽管它会运行丑陋 我决定将其设为我要调用的函数 这导致我收到错误 编译错误 ByRef 参数类型不匹配 我的假设是该函数需要正确引用 尽管我一直在阅读文档并且不明白为什么 gt 声明 Sh
  • 如何在Power Query中对N列求和

    我的数据每月都会更新 因此我尝试创建一个强大的查询表 该表将显示我创建的枢转 N 列的总和 但我似乎不知道如何在强大的查询中执行此操作 我目前有这个代码 旋转后 创建要求和的列的列表 添加索引列以限制每行 添加一列 该列对该行的列进行求和
  • 在Excel中,我可以使用超链接来运行vba宏吗?

    我有一个包含多行数据的电子表格 我希望能够单击一个单元格 该单元格将使用该行中的数据运行宏 由于行数总是在变化 我认为每行的超链接可能是最好的方法 ROW MeterID Lat Long ReadX ReadY ReadZ CoeffA
  • 我如何以更好的方式编码而不是像这样的VBA编码

    我正在 Excel 中创建一个仪表板 但是我想知道是否有比这更好的编码方式 我想对其进行模块化 而不是这样做以使其更加整洁 Private Sub Afford If af Value True Then af afr Value Shee
  • 使用 FindElementbyXpath() 获取 Selenium Basic 中可填充框的行和列名称

    我正在使用 Selenium Basic 将电子表格中的文本填充到网站中 网站的html代码是这样的 div table cellspacing 0 border 1 style width 99 tr th style font weig
  • Android Excel CSV 的 MIME 数据类型是什么?

    我尝试了 text csv 甚至 application vnd ms excel 但 Excel 不会显示在选择列表中 很多其他应用程序也可以 void shareCsv Uri uri Context context Intent in
  • 如何使用 VBA 添加 MS Outlook 提醒事件处理程序

    我想扩展 MS Outlook 以便当弹出日历提醒时 我可以运行一个可以运行外部程序 如批处理脚本 的 VBA 挂钩 就我而言 我想将提醒 转发 到 Linux 桌面 因为我在这两种环境中工作 并且 Windows 桌面并不总是可见 我看到
  • 在 Excel 中使用 VBA 设置图像透明度

    有没有办法使用 VBA 脚本对图像应用一些透明度 我录制了一个 宏 但似乎没有录制艺术效果 我已经找到了如何制作形状 但没有找到图像 这需要几个步骤 将自选图形 如矩形 放置在工作表上 使用以下方法将您的实际图片嵌入矩形中 ShapeRan
  • 使用 VBScript 在日期字段值上选择错误的数据

    我有一张包含以下数据的表 现在 Excel 共有 36 个任务 每个任务有 4 列 第一个任务 即 Task1 名称将始终从 L 列开始 144 列描述了 36 个任务 现在我们需要按行进行检查 并需要检查 TNStart 开始日期 你们能
  • 在 VBA 中捕获 shell 命令的输出值?

    发现这个功能http www cpearson com excel ShellAndWait aspx http www cpearson com excel ShellAndWait aspx 但我还需要捕获 shell 的输出 有什么代

随机推荐