跳至主要内容

如何在Excel中查找和替换文本并保留格式?

Author: Sun Last Modified: 2025-05-07
A screenshot showing Excel cells with partial formatting changes after find and replace
A screenshot showing desired result where formatting is preserved in Excel cells
当我们需要查找特定值并将其替换为另一个值时,查找和替换功能非常强大且实用。但如果存在一个单元格区域,并且其中一些单元格包含不同格式的值,在这种情况下,使用查找和替换功能进行值的查找和替换会破坏单元格中值的格式,如下截图所示。那么,如何在Excel中查找和替换的同时保留每个单元格中值的格式呢?使用宏代码实现查找和替换并保留格式

arrow blue right bubble 使用宏代码实现查找和替换并保留格式

除了宏代码之外,没有其他方法可以帮助您查找和替换文本并保留格式。

1. 按 Alt + F11 键打开 Microsoft Visual Basic for Application 窗口。

2. 单击 插入 > 模块,并将以下代码粘贴到脚本中。

VBA:查找和替换并保留格式

Sub CharactersReplace(Rng As Range, FindText As String, ReplaceText As String, Optional MatchCase As Boolean = False)
  'UpdatebyExtendoffice20160711
    Dim I As Long
    Dim xLenFind As Long
    Dim xLenRep As Long
    Dim K As Long
    Dim xValue As String
    Dim M As Long
    Dim xCell As Range
    xLenFind = Len(FindText)
    xLenRep = Len(ReplaceText)
    If Not MatchCase Then M = 1
    For Each xCell In Rng
        If VarType(xCell) = vbString Then
            xValue = xCell.Value
            K = 0
            For I = 1 To Len(xValue)
              If StrComp(Mid$(xValue, I, xLenFind), FindText, M) = 0 Then
                xCell.Characters(I + K, xLenFind).Insert ReplaceText
                K = K + xLenRep - xLenFind
              End If
            Next
        End If
    Next
End Sub

Sub Test_CharactersReplace()
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("Select a range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Call CharactersReplace(xRg, "KK", "Kutools", True)
End Sub

A screenshot of the VBA module window with code to find and replace

提示:在代码中,KK 是您要查找的字符串,Kutools 是您要替换为的字符串,您可以根据需要更改它们。

3. 按 F5 键,弹出一个对话框供您选择要查找和替换的区域,见截图:
A screenshot showing the input box for selecting range

4. 单击确定,所选单元格中的特定字符串将被替换为其他内容并保持格式不变。
A screenshot of cells after find and replace, preserving formatting

最佳办公效率工具

🤖 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%,每天为您减少数百次鼠标点击!