跳到主要內容

如何基於其他文本突出顯示單元格中的特定文本?

在Excel中,我們可以很容易地基於特定文本突出顯示單元格,但是在這裡,我想突出顯示單元格中的特定文本以使其突出而不是整個單元格。 這對於我們大多數人來說可能是一個麻煩。 在本文中,我將討論在Excel中解決此工作的一些技巧。


使用VBA代碼突出顯示多個單元格中的一個或多個特定文本

例如,我有一系列文本字符串,現在,我要突出顯示特定文本“天空在這些單元格中獲取“”,結果如下所示:

要僅突出顯示單元格中的部分文本,以下VBA代碼可以為您提供幫助。

1. 選擇要突出顯示特定文本的單元格,然後按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

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 按鈕,您指定的所有文本僅在單元格中突出顯示,請參見屏幕截圖:

保養竅門:如果您需要突出顯示文本字符串中的多個關鍵字,請應用以下代碼:
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中的代碼,在這裡,我將介紹一個簡單的工具- Excel的Kutools,其 標記關鍵字 功能,您可以一次突出顯示單元格中的特定一個或多個關鍵字。

注意:應用這些 標記關鍵字 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用這些功能。

安裝後 Excel的Kutools,請執行以下操作:

1。 點擊 庫工具 > 文本 > 標記關鍵字,請參見屏幕截圖:

2。 在 標記關鍵字 對話框,請執行以下操作:

  • 從中選擇要使用的數據范圍 範圍 文本框;
  • 選擇包含要突出顯示的關鍵字的單元格,也可以將關鍵字手動輸入(以逗號分隔)到 關鍵詞 文本框
  • 最後,您應指定一種字體顏色以通過選中來突出顯示文本 標記關鍵字顏色 選項。 (要為整個包含關鍵字的單元格上色,請選擇 標記單元格內容顏色 選項)

3。 然後,點擊 Ok 按鈕,所有指定的文本均已突出顯示,如下圖所示:

備註:此功能不區分大小寫,如果要突出顯示區分大小寫的文本,請檢查 區分大小寫 ,在 標記關鍵字 對話框。


使用VBA代碼根據其他文本突出顯示單元格中的特定文本

這是另一種情況,我有兩列,第一列包含文本字符串,第二列是特定文本,現在,我需要根據第二列中的特定文本分別突出顯示第一列中的相對文本行。

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

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選項卡為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