简体   繁体   English

如何使用格式化文本查找和替换单元格的一部分

[英]How to find and replace part of a cell with formatted text

In an Excel 2007 spreadsheet I want to find-replace with highlighting part of the text in a cell.在 Excel 2007 电子表格中,我想通过突出显示单元格中的部分文本进行查找替换。 Using find-replace reformats the entire cell though.使用查找替换重新格式化整个单元格。

For example, if the cell contains:例如,如果单元格包含:

Pellentesque vel massa sit amet magna eleifend placerat. Pellentesque vel massa 坐在 amet magna eleifend placerat。 Pellentesque dictum, nibh vitae tincidunt placerat, elit libero tristique tellus, vel imperdiet nulla tortor id diam. Pellentesque dictum,nibh vitae tincidunt placerat,elit libero tristiquetellus,vel imperdiet nulla tortor id diam。 Mauris porta blandit vestibulum. Mauris porta blandit 前庭。

I want to find "Pellentesque" and replace it with Pellentesque .我想找到“Pellentesque”并将其替换为Pellentesque

Can this be done without VBE or formulas?这可以在没有 VBE 或公式的情况下完成吗?

Maybe this would suit (be sure to select no more cells than necessary before running or this could take a while): 也许这适合(确保在运行之前不再选择不必要的单元格,否则这可能需要一段时间):

Sub FormatSelection()

Dim cl As Range
Dim SearchText As String
Dim StartPos As Integer
Dim EndPos As Integer
Dim TestPos As Integer
Dim TotalLen As Integer

On Error Resume Next
Application.DisplayAlerts = False
SearchText = Application.InputBox _
(Prompt:="Enter string.", Title:="Which string to format?", Type:=2)
On Error GoTo 0
Application.DisplayAlerts = True
If SearchText = "" Then
Exit Sub
Else
For Each cl In Selection
  TotalLen = Len(SearchText)
  StartPos = InStr(cl, SearchText)
  TestPos = 0
  Do While StartPos > TestPos
    With cl.Characters(StartPos, TotalLen).Font
      .FontStyle = "Bold"
      .ColorIndex = 3
    End With
    EndPos = StartPos + TotalLen
    TestPos = TestPos + EndPos
    StartPos = InStr(TestPos, cl, SearchText, vbTextCompare)
  Loop
Next cl
End If
End Sub

Should embolden and colour Red. 应该加重红色。 Changes are not overwritten if macro is rerun. 如果重新运行宏,则不会覆盖更改。 Comment out .ColorIndex = 3 if not to change colour. 注释掉.ColorIndex = 3如果不改变颜色。

(Based on @Skip Intro's pointer to SO15438731 question with amendment and some code from SO10455366 answer.) (基于@Skip Intro指向SO15438731问题的修正案以及SO10455366的一些代码答案。)

This is what worked great for me: 这对我来说非常有用:

  1. Selected cells in Excel (I selected 1266 at a time--don't know if there's really any limit) Excel中选定的单元格(我一次选择1266个 - 不知道是否有任何限制)

  2. Clicked on COPY 单击COPY

  3. Opened a blank word file and clicked on PASTE 打开一个空白的word文件,然后单击PASTE

  4. Used CNTL+H to get Find & Replace 使用CNTL + H获取查找和替换

  5. Entered the characters to find and the characters to replace, adding any formatting desired and selected Replace All (the process took less than 2 seconds) 输入要查找的字符和要替换的字符,添加所需的任何格式并选择全部替换(该过程花费不到2秒)

  6. Selected Edit-Select-Select All in the Word file, then held the shift key and pressed the left arrow (I think not doing this would add a row to the next operation) 选择Edit-Select-Select All在Word文件中,然后按住shift键并按下左箭头(我认为不这样做会在下一个操作中添加一行)

  7. Went back into Excel and, in a spare area, selected Paste - Keep Source Formatting option 返回Excel并在备用区域中选择“粘贴 - 保留源格式”选项

That put exactly what I wanted into Excel. 这正是我想要的东西到Excel。 From there I could move it wherever I wanted it. 从那里我可以将它移动到我想要的任何地方。

The absolute EASIEST WAY to do this with the standard Find & Replace CTRL+F Command is to just "temporary" add-in the "Calibri Font" Character • aka. 使用标准的查找和替换CTRL + F命令执行此操作的绝对最简单方法是“临时”加载“Calibri字体”字符•aka。 "black bullet dot" “黑色子弹点”

like this: • sample • 像这样:•样品•

from the Insert Tab --> Symbols --> Calibri Font --> and at the very end) 从插入选项卡 - >符号 - > Calibri字体 - >并在最后)

Thus you can make it much easier to the eyes to read over many rows of excel data, 因此,您可以更轻松地阅读许多行的Excel数据,

and easily also later remove it back again to the original with simply again a Find & Replace reversed. 并且稍后再轻轻地将它再次移回原件,只需再次查找和替换。

Hope this helps someone, it sure helped my eyes to scan over quicker. 希望这有助于某人,它确实帮助我的眼睛更快地扫描。


OLD RESPONSE 旧的回应

the easy non-programming way to do this, is to: 这种简单的非编程方式是:

  1. paste the text into MS Word 将文本粘贴到MS Word中

  2. Then run the Find & Replace (CTRL+F) there (with the custom formatting changes at the bottom left button under "More > >" and then "Format" to your need customized) - which will then do all the changes perfectly... 然后在那里运行查找和替换(CTRL + F)(在“更多>>”下左下角的自定义格式更改,然后“格式化”到您需要的定制) - 然后将完美地完成所有更改。 。

  3. and then rather than doing a PASTE SPECIAL into Excel (via the Home Tab --> Paste --> Paste Special --> Paste as HTML (so to retain the formatting) (which can and is too memory intensive and too slow for anything above like 1000 rows) 然后而不是在Excel中进行PASTE SPECIAL(通过主页选项卡 - >粘贴 - >选择性粘贴 - >粘贴为HTML(以便保留格式)(这可能并且过于内存密集且对任何事情都太慢)以上1000行)

it is better to simply "Save as" from Word the document into a RTF-File, and then just re-import that file into Excel via the simple "Open File". 最好简单地从文档“另存为”将文档转换为RTF文件,然后通过简单的“打开文件”将该文件重新导入Excel。

Excel has a pretty good "repeating pattern cell structuring" recognition, but of course, this workaround may not work perfectly in reproducing the desired cell division every single time, but it should maintain the cell order nevertheless most of the time. Excel有一个非常好的“重复模式细胞结构”识别,但当然,这种解决方法可能无法在每次复制所需的细胞分裂时完美地工作,但它应该在大多数时间保持细胞顺序。

So much for the simple way to do it, until Microsoft stops limiting an option to "highlight/bold" a found-result-text WITHIN a cell, rather than always bolding the entire cell. 这么简单的方法就是这么做,直到微软停止限制一个选项,在一个单元格中“突出显示/加粗”一个找到结果文本,而不是总是粗加工整个单元格。

Greetings Marko 问候马可

To make the search item case agnostic you can use: 要使搜索项与案例无关,您可以使用:

Sub FormatSelection()

Dim cl As Range
Dim SearchText As String
Dim StartPos As Integer
Dim EndPos As Integer
Dim TestPos As Integer
Dim TotalLen As Integer

On Error Resume Next
Application.DisplayAlerts = False
SearchText = Application.InputBox _
(Prompt:="Enter string.", Title:="Which string to format?", Type:=2)
On Error GoTo 0
Application.DisplayAlerts = True
If SearchText = "" Then
Exit Sub
Else
For Each cl In Selection
  TotalLen = Len(SearchText)
  StartPos = InStr(Ucase(cl), Ucase(SearchText))
  TestPos = 0
  Do While StartPos > TestPos
    With cl.Characters(StartPos, TotalLen).Font
      .FontStyle = "Bold"
      .ColorIndex = 3
    End With
    EndPos = StartPos + TotalLen
    TestPos = TestPos + EndPos
    StartPos = InStr(TestPos, cl, SearchText, vbTextCompare)
  Loop
Next cl
End If
End Sub

The code of @pnuts has a small bug... The fixed version: @pnuts 的代码有个小bug... 修复版:

Sub FormatSelection()

Dim cl As Range
Dim SearchText As String
Dim StartPos As Integer
Dim EndPos As Integer
Dim TestPos As Integer
Dim TotalLen As Integer

On Error Resume Next
Application.DisplayAlerts = False
SearchText = Application.InputBox _
(Prompt:="Enter string.", Title:="Which string to format?", Type:=2)
On Error GoTo 0
Application.DisplayAlerts = True
If SearchText = "" Then
Exit Sub
Else
For Each cl In Selection
  TotalLen = Len(SearchText)
  StartPos = InStr(cl.Value2, SearchText)
  TestPos = 0
  Do While StartPos > TestPos
    With cl.Characters(StartPos, TotalLen).Font
      .FontStyle = "Bold"
      .ColorIndex = 3
    End With
    EndPos = StartPos + TotalLen
    TestPos = EndPos + 1
    StartPos = InStr(TestPos, cl.Value2, SearchText)
  Loop
Next cl
End If
End Sub

Practically the issue is with this line TestPos = EndPos + 1 - Previously TestPos = TestPos + EndPos实际上问题在于这条线TestPos = EndPos + 1 - 以前的TestPos = TestPos + EndPos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM