跳到主要内容

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

doc查找替换保留格式1
doc查找替换保留格式2
当我们需要查找特定值并将其替换为另一个值时,“查找和替换”功能功能强大且有用。 但是,如果存在一定范围的单元格,则某些单元格包含不同的格式值。 在这种情况下,通过“查找和替换”功能查找和替换值将破坏单元格中值的格式,如下图所示,如何在Excel中查找和替换时如何保留每个单元格中值的格式?

查找并用宏代码替换保留格式

箭头蓝色右气泡 查找并用宏代码替换保留格式

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

1。 按 Alt + F11键 键打开 Microsoft Visual Basic应用程序 窗口。

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

 

doc查找替换保留格式5

3。 按 F5 键,会弹出一个对话框供您选择要查找和替换的范围,请参见屏幕截图:
doc查找替换保留格式6

4.单击“确定”,所选单元格中的特定字符串将替换为其他字符串并保留格式。
doc查找替换保留格式4

Tips::在代码中,  KK 是您要查找的字符串,并且 库工具 是您要替换的字符串,您可以根据需要进行更改。

最佳办公生产力工具

热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

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

kte选项卡201905


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (6)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Thanks, it has worked wonders for me. I needed to replace a colon with a newline character, but all the text before the colon should stay bold caption, where the rest of it should be left in regular. It has worked and abridged my work today.
Though I didn't try with cells with more than 255 characters, as it seems to be an issue with other people commenting.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Can you clarify if there is a solution that works in cells with more than 255 characters, or is this a limitation of Excel? Does your add-in support cells with more than 255 characters that contain formatting?
This comment was minimized by the moderator on the site
Same Question as John Birk i need a workaround with Cells that have more then 255 signs.
This comment was minimized by the moderator on the site
The method only work while characters less than 255 digits.
This comment was minimized by the moderator on the site
I am getting syntax error in VBA
This comment was minimized by the moderator on the site
I tried your solution on a cell that is over 255 characters long and nothing changes. Is there a workaround for this case?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations