跳到主要内容

如何基于其他文本突出显示单元格中的特定文本?

在Excel中,我们可以很容易地根据特定文本突出显示单元格,但是在这里,我想突出显示单元格中的特定文本以使其突出而不是整个单元格。 这对于我们大多数人来说可能是一个麻烦。 在本文中,我将讨论在Excel中解决此工作的一些技巧。


使用VBA代码突出显示多个单元格中的一个或多个特定文本

例如,我有一系列文本字符串,现在,我要突出显示特定文本“天空在这些单元格中获取“”,结果如下所示:

要仅突出显示单元格中的部分文本,以下VBA代码可以为您提供帮助。

1. 选择要突出显示特定文本的单元格,然后按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:突出显示单元格中的一部分文本:

Sub HighlightStrings()
'Updateby Extendoffice
Application.ScreenUpdating = False
Dim Rng As Range
Dim cFnd As String
Dim xTmp As String
Dim x As Long
Dim m As Long
Dim y As Long
cFnd = InputBox("Enter the text string to highlight")
y = Len(cFnd)
For Each Rng In Selection
  With Rng
    m = UBound(Split(Rng.Value, cFnd))
    If m > 0 Then
      xTmp = ""
      For x = 0 To m - 1
        xTmp = xTmp & Split(Rng.Value, cFnd)(x)
        .Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
        xTmp = xTmp & cFnd
      Next
    End If
  End With
Next Rng
Application.ScreenUpdating = True
End Sub

3。 然后按 F5 键以运行此代码,然后会弹出一个提示框,提醒您输入仅要突出显示的文本,请参见屏幕截图:

4。 然后点击 OK 按钮,您指定的所有文本仅在单元格中突出显示,请参见屏幕截图:

Tips:如果您需要突出显示文本字符串中的多个关键字,请应用以下代码:
VBA代码:从文本字符串中突出显示多个关键字:
Sub HighlightStrings()
'Updateby Extendoffice
Application.ScreenUpdating = False
Dim Rng As Range
Dim cFnd As String
Dim xTmp As String
Dim x As Long
Dim m As Long
Dim y As Long
Dim xFNum As Integer
Dim xArrFnd As Variant
Dim xStr As String
cFnd = InputBox("Please enter the text, separate them by comma:")
If Len(cFnd) < 1 Then Exit Sub
xArrFnd = Split(cFnd, ",")
For Each Rng In Selection
With Rng
For xFNum = 0 To UBound(xArrFnd)
xStr = xArrFnd(xFNum)
y = Len(xStr)
m = UBound(Split(Rng.Value, xStr))
If m > 0 Then
xTmp = ""
For x = 0 To m - 1
xTmp = xTmp & Split(Rng.Value, xStr)(x)
.Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
xTmp = xTmp & xStr
Next
End If
Next xFNum
End With
Next Rng
Application.ScreenUpdating = True
End Sub

然后,在弹出的框中,输入您要突出显示的关键字(用逗号分隔单词),请参见屏幕截图:

然后,单击 OK 按钮,指定的单词立即突出显示,请参见屏幕截图:

备注:以上代码区分大小写。


以惊人的功能突出显示多个单元格中的一个或多个特定文本

如果您不熟悉Excel中的代码,在这里,我将介绍一个简单的工具- Kutools for Excel,其 标记关键字 功能,您可以一次突出显示单元格中的特定一个或多个关键字。

请注意:应用这些 标记关键字 功能,首先,您应该下载 Kutools for Excel,然后快速轻松地应用这些功能。

安装后 Kutools for Excel,请执行以下操作:

1。 点击 库工具 > 文本 > 标记关键字,请参见屏幕截图:

2。 在 标记关键字 对话框,请执行以下操作:

  • 从中选择要使用的数据范围 范围 文本框;
  • 选择包含要突出显示的关键字的单元格,也可以将关键字手动输入(以逗号分隔)到 关键字 文本框
  • 最后,您应指定一种字体颜色以通过选中来突出显示文本 标记关键字颜色 选项。 (要为整个包含关键字的单元格上色,请选择 标记单元格内容颜色 选项​​)

3。 然后,点击 Ok 按钮,所有指定的文本均已突出显示,如下图所示:

备注:此功能不区分大小写,如果要突出显示区分大小写的文本,请检查 区分大小写 ,在 标记关键字 对话框。


使用VBA代码根据其他文本突出显示单元格中的特定文本

这是另一种情况,我有两列,第一列包含文本字符串,第二列是特定文本,现在,我需要根据第二列中的特定文本分别突出显示第一列中的相对文本行。

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

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:根据其他文本突出显示单元格中的一部分文本:

Sub highlight()
'Updateby Extendoffice
    Dim xStr As String
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim I As Long
    Dim J As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
LInput:
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "not support multiple columns"
        GoTo LInput
    End If
    If xRg.Columns.Count <> 2 Then
        MsgBox "the selected range can only contain two columns "
        GoTo LInput
    End If
    For I = 0 To xRg.Rows.Count - 1
        xStr = xRg.Range("B1").Offset(I, 0).Value
        With xRg.Range("A1").Offset(I, 0)
            .Font.ColorIndex = 1
            For J = 1 To Len(.Text)
                If Mid(.Text, J, Len(xStr)) = xStr Then .Characters(J, Len(xStr)).Font.ColorIndex = 3
            Next
        End With
    Next I
End Sub

3。 粘贴代码后,按 F5 要运行它,请弹出一个提示框,提醒您选择包含文本字符串和要突出显示的特定文本的数据范围,请参见屏幕截图:

4。 然后点击 OK 按钮,基于第二列中的特定文本,第一列中的所有相应文本已被染成红色,如以下屏幕截图所示:


更多相关文章:

  • 在Excel中连接两列时的粗体文本
  • 在Excel工作表中,将两个单元格值与公式连接起来后,您可能会发现它不会在组合的公式单元格中加粗文本的一部分。 有时这可能很烦人,当在Excel中连接两列时如何加粗零件文本?
  • 连接单元格列并在Excel中保留文本颜色
  • 众所周知,将单元格列连接或合并为一列时,单元格格式(例如文本字体颜色,数字格式等)将丢失。 本文,我将介绍一些技巧,以将单元格列合并为一个,并在Excel中尽可能轻松地保持文本颜色。
  • 根据另一列中的值显示特定的文本
  • 假设我有一个数字列表,现在,我想根据此列号在另一列中显示一些特定的文本。 例如,如果单元格号在1-100之间,则我希望在相邻的单元格中显示文本“减少”,如果该数字在101-200之间,则显示文本“稳定”,并且该数字大于200 ,显示“ Increase”(增加)文本,如下图所示。 要解决Excel中的此任务,本文中的以下公式可能会对您有所帮助。
  • 在Excel中用文本和数字求和的单元格
  • 例如,我有一个包含数字和文本字符串的值列表,现在,我只想对基于相同文本的数字求和,请看以下屏幕截图。 通常,您不能直接使用文本字符串将列表中的值求和,在这里,我将向您介绍一些公式来处理此任务。

最佳办公生产力工具

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

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

产品描述


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

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (39)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Thank you for "Highlight A Specific Text Within Multiple Cells With VBA Code" It works great. Would you please explain:
I need remove "Highlight"

so what can I do

thanks
This comment was minimized by the moderator on the site
Hello, Mukesh
If you want to delete the specific text from multiple cells, you can apply the Find & Replace feature in Excel.
You just need to enter the specific text that you want to delete into the Find textbox, and leave the Replace box blank, at last, click Replace All to get your results.
Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
This is amazing! One question: Is there any way that an Undo (CTRL+Z) can be used after running this?
This comment was minimized by the moderator on the site
Hello, ChristineW,The vba codes can't support Undo, so when applying the code, you'd better copy and paste the original data to another sheet first.If you use Kutools for Excel, the utility support Undo.
This comment was minimized by the moderator on the site
JUST WANT TO SLAY THANK YOU AS THE VBA FORMULA WORKS FOR ME... IT AWESOME.
This comment was minimized by the moderator on the site
Wow! Thank you!
This comment was minimized by the moderator on the site
Awesome. thanks
This comment was minimized by the moderator on the site
This was very useful, thanks very much!
This comment was minimized by the moderator on the site
Hi,
Please any one help me. I want to highlight the specific number in same sentence. For ex : " 2 days leave scansion" want to highlight only "2" in sentence.
This comment was minimized by the moderator on the site
Hi, anyone help me this. i want to highlight the Specific number in Cell within the same sentence. for Ex : " 2 days leave scansion " in this sentence want to highlight number.
This comment was minimized by the moderator on the site
Hi,
could anyone help me with the following

my Cells in Column "G" contain the text from Column Z to AN, not compulsory that Column g contains all the text from Z to AN.

My work here is to Highlight the text in Column G if it does not available in any of Column Z - AN

For example : Cell G1 contains (Hello sir I am doing well) but The text "Sir" do not exist in Column "Z1" to "AN1"

So i need to highlight the text "Sir"
This comment was minimized by the moderator on the site
i get a run-time error '13', type mismatch when i run the script. any suggestions?
This comment was minimized by the moderator on the site
I had the same issue; I found that one of my collumns were formulas and it was looking in them which was what triggered the error 13. Selected a range wihtout formula containing the text to highlight and it worked.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations