跳至主要内容

如何在 Excel 中隐藏特定的错误值?

Author: Amanda Li Last Modified: 2025-05-08

假设您的 Excel 工作表中有一些不需要修正但需要隐藏的错误值。为了解决这个问题,我们已经讨论过如何在 Excel 中隐藏所有错误值,那么如果您只想隐藏特定的错误值该怎么办呢?在本教程中,我们将向您展示以下三种方法来完成此任务。

A screenshot of specific error values being hidden


使用 VBA 将文本颜色变为白色以隐藏多个特定错误值

我们创建了两个 VBA 代码,帮助您通过将指定错误的字体颜色变为白色,快速隐藏选定区域或跨多个工作表中的多个特定错误值。请按照以下步骤操作,并根据需要运行代码。

1. 在您的 Excel 中,按“Alt”+“F11”键打开“Microsoft Visual Basic for Applications”窗口。

2. 点击“插入”>“模块”。然后将以下任一 VBA 代码复制到“模块”窗口中。
A screenshot of the VBA code in the module window in Excel

VBA 代码 1:在选定区域中隐藏多个特定错误值

Sub HideSpecificErrors_SelectedRange()
  'Updated by ExtendOffice 20220824
Dim xRg As Range
Dim xFindStr As String
Dim xFindRg As Range
Dim xARg As Range
Dim xURg As Range
Dim xFindRgs As Range
Dim xFAddress As String
Dim xBol As Boolean
Dim xJ

xArrFinStr = Array("#DIV/0!”, “#N/A”, “#NAME?") 'Enter the errors to hide, enclose each with double quotes and separate them with commas

On Error Resume Next
Set xRg = Application.InputBox("Please select the range that includes the errors to hide:", "Kutools for Excel", , Type:=8)
If xRg Is Nothing Then Exit Sub

xBol = False
For Each xARg In xRg.Areas
    Set xFindRg = Nothing
    Set xFindRgs = Nothing
    Set xURg = Application.Intersect(xARg, xARg.Worksheet.UsedRange)
    For Each xFindRg In xURg
        For xJ = LBound(xArrFinStr) To UBound(xArrFinStr)
            If xFindRg.Text = xArrFinStr(xJ) Then
                xBol = True
                If xFindRgs Is Nothing Then
                    Set xFindRgs = xFindRg
                Else
                    Set xFindRgs = Application.Union(xFindRgs, xFindRg)
                End If
            End If
        Next
    Next
    If Not xFindRgs Is Nothing Then
        xFindRgs.Font.ThemeColor = xlThemeColorDark1
        
    End If
Next
If xBol Then
    MsgBox "Successfully hidden."
Else
     MsgBox "No specified errors were found."
End If
End Sub

注意:在第 12 行的代码片段“xArrFinStr = Array("#DIV/0!", "#N/A", "#NAME?")”中,您应将“#DIV/0!", “#N/A”, “#NAME?”替换为您实际要隐藏的错误值,记住每个值都要用双引号括起来,并用逗号分隔。

VBA 代码 2:跨多个工作表隐藏多个特定错误值

Sub HideSpecificErrors_WorkSheets()
'Updated by ExtendOffice 20220824
Dim xRg As Range
Dim xFindStr As String
Dim xFindRg As Range
Dim xARg, xFindRgs As Range
Dim xWShs As Worksheets
Dim xWSh As Worksheet
Dim xWb As Workbook
Dim xURg As Range
Dim xFAddress As String
Dim xArr, xArrFinStr
Dim xI, xJ
Dim xBol As Boolean
xArr = Array("Sheet1", "Sheet2") 'Names of the sheets where to find and hide the errors. Enclose each with double quotes and separate them with commas
xArrFinStr = Array("#DIV/0!", "#N/A", "#NAME?") 'Enter the errors to hide, enclose each with double quotes and separate them with commas
'On Error Resume Next
Set xWb = Application.ActiveWorkbook
xBol = False
For xI = LBound(xArr) To UBound(xArr)
    Set xWSh = xWb.Worksheets(xArr(xI))
    Set xFindRg = Nothing
    xWSh.Activate
    Set xFindRgs = Nothing

    Set xURg = xWSh.UsedRange
    Set xFindRgs = Nothing
    For Each xFindRg In xURg
        For xJ = LBound(xArrFinStr) To UBound(xArrFinStr)
            If xFindRg.Text = xArrFinStr(xJ) Then
                xBol = True
                If xFindRgs Is Nothing Then
                    Set xFindRgs = xFindRg
                Else
                    Set xFindRgs = Application.Union(xFindRgs, xFindRg)
                End If
            End If
        Next
    Next
    If Not xFindRgs Is Nothing Then
        xFindRgs.Font.ThemeColor = xlThemeColorDark1
        
    End If
Next
If xBol Then
    MsgBox "Successfully hidden."
Else
     MsgBox "No specified errors were found."
End If
End Sub
注意事项:
  • 在第 15 行的代码片段“xArr = Array("Sheet1", "Sheet2")”中,您应将“Sheet1”, “Sheet2”替换为您实际要隐藏错误的工作表名称。记住每个工作表名称都要用双引号括起来,并用逗号分隔。
  • 在第 16 行的代码片段“xArrFinStr = Array("#DIV/0!", “#N/A”, “#NAME?")”中,您应将“#DIV/0!", “#N/A”, “#NAME?”替换为您实际要隐藏的错误值,记住每个错误值都要用双引号括起来,并用逗号分隔。

3. 按“F5”键运行 VBA 代码。

注意:如果您使用的是“VBA 代码 1”,会弹出一个对话框,要求您选择要查找和删除错误值的区域。您也可以点击工作表标签选择整个工作表。

4. 如下所示的对话框弹出,告诉您指定的错误值已被隐藏。点击“确定”关闭对话框。
A screenshot of the dialog box confirming that the specified error values were successfully hidden

5. 指定的错误值已立即被隐藏。
A screenshot of specific error values being hidden


使用“格式化显示错误信息向导”功能将特定错误值替换为其他值

如果您不熟悉 VBA 代码,Kutools for Excel 的“格式化显示错误信息向导”功能可以帮助您轻松找到所有错误值、所有 #N/A 错误或除 #N/A 之外的任何错误,并将其替换为您指定的其他值,请继续阅读以了解如何完成此任务。

Kutools for Excel 提供了超过 300 种高级功能,简化复杂任务,提升创造力与效率。 通过集成 AI 能力,Kutools 能够精准自动执行任务,让数据管理变得轻松简单。Kutools for Excel 的详细信息...         免费试用...

1. 在“Kutools”选项卡的“公式”组中,点击“更多”>“格式化显示错误信息向导”。
A screenshot of the Error Condition Wizard option on the Kutools tab in Excel

2. 在弹出的“格式化显示错误信息向导”对话框中,请执行以下操作:
  • 在“区域”框中,点击范围选择按钮 选择包含要隐藏错误的区域。
    注意:要搜索整个工作表,请点击工作表标签。
  • 在“错误类型”部分,指定要隐藏的错误值。
  • 在“错误信息显示为”部分,选择您希望如何替换这些错误的方式。
A
 screenshot of the Error Condition Wizard dialog box

3. 点击“确定”。指定的错误值将以您选择的选项显示。
A screenshot of the updated Excel sheet with error values replaced using Kutools' Error Condition Wizard

Kutools for Excel - 通过超过300个必备工具,让Excel功能大幅提升。永久免费享受AI功能!立即获取


使用公式将特定错误替换为其他值

要替换特定的错误值,Excel 的 IFIFNAERROR.TYPE 函数可以帮您忙。但首先,您应该知道每个错误值对应的数字代码。

# 错误 公式 返回
#NULL! =ERROR.TYPE(#NULL!) 1
#DIV/0! =ERROR.TYPE(#DIV/0!) 2
#VALUE! =ERROR.TYPE(#VALUE!) 3
#REF! =ERROR.TYPE(#REF!) 4
#NAME? =ERROR.TYPE(#NAME?) 5
#NUM! =ERROR.TYPE(#NUM!) 6
#N/A =ERROR.TYPE(#N/A) 7
#GETTING_DATA =ERROR.TYPE(#GETTING_DATA) 8
#SPILL! =ERROR.TYPE(#SPILL!) 9
#UNKNOWN! =ERROR.TYPE(#UNKNOWN!) 12
#FIELD! =ERROR.TYPE(#FIELD!) 13
#CALC! =ERROR.TYPE(#CALC!) 14
其他错误 =ERROR.TYPE(123) #N/A

A screenshot of a list with values and errors

例如,您有一个如上所示的表格。要将“#DIV/0!”错误替换为文本字符串“Divide By Zero Error”,您应首先找到该错误的代码,即“2”。然后在单元格“B2”中应用以下公式,并向下拖动填充柄以将公式应用于下方的单元格:

=IF(IFNA(ERROR.TYPE(A2),A2)=2,"Divide By Zero Error",A2)

A screenshot of the #DIV/0! error being replaced with 'Divide By Zero Error'

注意事项:
  • 在公式中,您可以将错误代码“2”替换为对应于其他错误值的代码。
  • 在公式中,您可以将文本字符串“Divide By Zero Error”替换为其他文本消息,或者如果想用空白单元格替换错误,则替换为“”。

相关文章

如何在 Excel 中隐藏所有错误值?

当您在 Excel 工作表中工作时,有时可能会发现一些错误值,例如 #DIV/0、#REF、#N/A 等,它们是由公式错误引起的。现在,您希望隐藏工作表中的所有这些错误值,如何在 Excel 中快速轻松地解决此任务?

如何在 Excel 中将 #DIV/0! 错误更改为可读的消息?

有时,当我们使用公式在 Excel 中计算时,会显示一些错误消息。例如,在这个公式 =A1/B1 中,如果 B1 为空或包含 0,公式将显示 #DIV/0 错误。有没有办法让这些错误消息清晰可读,或者如果您想用其他消息替换错误,该怎么办?

如何在 Excel 中删除行时避免 #Ref 错误?

当您引用另一个单元格时,如果引用的行已被删除,单元格将显示 #REF 错误,如下截图所示。现在我将讨论如何避免 #ref 错误并在删除行时自动引用下一个单元格。

如何在 Excel 中突出显示所有错误单元格?

如果您在工作表中创建公式,不可避免会出现一些错误值。您能否一次性突出显示工作表中包含错误值的所有单元格?Excel 中的条件格式功能可以帮助您解决此问题。

最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!