乔尔指出,当他关闭一个包含数千个公式的工作簿时,他最近收到此消息:“此工作表中的公式包含一个或多个无效引用。”乔尔想知道如何知道该工作簿中的七个工作表中的哪个。如何找到错误的公式?我在报告信息的显示中没有发现任何问题。

跟踪无效引用可能会令人沮丧。您可以开始寻找几个地方。第一个是在工作表上的公式中。 (是的,您需要对工作簿中的每个工作表执行这些步骤。)使用“转到特殊”对话框(按F5并选择“特殊”)可以选择仅转到包含错误的单元格。然后,您可以使用Tab键在Excel选择的任何单元格之间移动。

您也可以使用“查找”工具查找可能的错误。只需按Ctrl + F即可显示“查找和替换”对话框的“查找”选项卡,然后搜索#字符。确保告诉Excel在“公式”中进行搜索。检查找到的任何东西以查看是否为错误。

您还应该查看工作簿中定义的任何命名范围。在“名称管理器”对话框中(“公式”选项卡,单击“名称管理器”工具)查看每个名称,确保“引用到”列中的内容不包含任何错误指示。

并不是所有可能出错的地方; Excel确实擅长让错误存在于许多地方。如果您需要经常搜索错误,则可以尝试使用一个宏,该宏通过您的公式查找是否有任何潜在的错误。

Sub CheckReferences()

' Check for possible missing or erroneous links in ' formulas and list possible errors in a summary sheet

Dim iSh As Integer   Dim sShName As String   Dim sht As Worksheet   Dim c, sChar As String   Dim rng As Range   Dim i As Integer, j As Integer   Dim wks As Worksheet   Dim sChr As String, addr As String   Dim sFormula As String, scVal As String   Dim lNewRow As Long   Dim vHeaders

vHeaders = Array("Sheet Name", "Cell", "Cell Value", "Formula")

'check if 'Summary' worksheet is in workbook   'and if so, delete it   With Application     .ScreenUpdating = False     .DisplayAlerts = False     .Calculation = xlCalculationManual   End With

For i = 1 To Worksheets.Count     If Worksheets(i).Name = "Summary" Then       Worksheets(i).Delete     End If   Next i

iSh = Worksheets.Count

'create a new summary sheet     Sheets.Add After:=Sheets(iSh)

Sheets(Sheets.Count).Name = "Summary"

With Sheets("Summary")

Range("A1:D1") = vHeaders   End With   lNewRow = 2

' this will not work if the sheet is protected,   ' assume that sheet should not be changed; so ignore it   On Error Resume Next

For i = 1 To iSh     sShName = Worksheets(i).Name     Application.Goto Sheets(sShName).Cells(1, 1)

Set rng = Cells.SpecialCells(xlCellTypeFormulas, 23)



For Each c In rng       addr = c.Address       sFormula = c.Formula       scVal = c.Text

For j = 1 To Len(c.Formula)

sChr = Mid(c.Formula, j, 1)



If sChr = "[" Or sChr = "!" Or _           IsError(c) Then           'write values to summary sheet           With Sheets("Summary")

.Cells(lNewRow, 1) = sShName             .Cells(lNewRow, 2) = addr             .Cells(lNewRow, 3) = scVal             .Cells(lNewRow, 4) = "'" & sFormula           End With           lNewRow = lNewRow + 1           Exit For         End If       Next j     Next c   Next i

' housekeeping   With Application     .ScreenUpdating = True     .DisplayAlerts = True     .Calculation = xlCalculationAutomatic   End With

' tidy up   Sheets("Summary").Select   Columns("A:D").EntireColumn.AutoFit   Range("A1:D1").Font.Bold = True   Range("A2").Select End Sub

此宏创建一个名为“摘要”的工作表,该工作表用于列出有关在工作表链接中检测到的任何错误的信息。

您还可以使用Excel MVP Bill Manville的FindLink程序,该程序在链接中查找信息的工作非常出色。您可以使用外接程序在所有链接中搜索#字符,这将有助于您找到错误。目前尚不清楚该加载项是否将与Excel 2013一起使用,因为该文件的最新更新(于撰写本文时)是2011年。有关FindLink的更多信息,请参见:

http://www.manville.org.uk/software/findlink.htm

注意:

如果您想知道如何使用此页面(或_ExcelTips_网站上的任何其他页面)中描述的宏,我准备了一个特殊页面,其中包含有用的信息。

_ExcelTips_是您进行经济高效的Microsoft Excel培训的来源。

本技巧(8664)适用于Microsoft Excel 2007、2010和2013。您可以在此处为Excel的较早菜单界面找到此技巧的版本: