Gå til hovedindhold

Hvordan farves eller fremhæves bestemt ord i celler i Excel?

Hvis en celle indeholder flere ord, hvordan kan du kun fremhæve et bestemt ord i denne celle? Og hvad med at fremhæve dette bestemte ord i flere celler? Denne artikel vil give en metode til at opnå det.

Farve bestemt ord i en enkelt celle / flere celler med VBA-kode


Farve bestemt ord i en enkelt celle / flere celler med VBA-kode

Følgende VBA-kode kan hjælpe dig med at fremhæve det bestemte ord i et valg. Gør som følger.

1. Vælg området, der indeholder de celler, du vil fremhæve et bestemt ord indeni. Tryk derefter på andre + F11 taster samtidigt for at åbne Microsoft Visual Basic til applikationer vindue.

2. I åbningen Microsoft Visual Basic til applikationer vindue, skal du klikke på indsatte > Moduler. Kopier og indsæt derefter VBA-kode i kodevinduet.

VBA-kode: Farve bestemt ord i celler i Excel

Sub HighlightStrings()
    Dim xHStr As String, xStrTmp As String
    Dim xHStrLen As Long, xCount As Long, I As Long
    Dim xCell As Range
    Dim xArr
    On Error Resume Next
    xHStr = Application.InputBox("What is the string to highlight:", "KuTools For Excel", , , , , , 2)
    If TypeName(xHStr) <> "String" Then Exit Sub
    Application.ScreenUpdating = False
        xHStrLen = Len(xHStr)
        For Each xCell In Selection
            xArr = Split(xCell.Value, xHStr)
            xCount = UBound(xArr)
            If xCount > 0 Then
                xStrTmp = ""
                For I = 0 To xCount - 1
                    xStrTmp = xStrTmp & xArr(I)
                    xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3
                    xStrTmp = xStrTmp & xHStr
                Next
            End If
        Next
    Application.ScreenUpdating = True
End Sub

3. Tryk på F5 nøgle for at køre koden. Og i Kutools til Excel dialogboks, skal du indtaste det bestemte ord, du vil fremhæve i celler, og klik derefter på OK knap. Se skærmbillede:

Derefter kan du se alle de ord, du har angivet, er farvet med rødt i valgte celler med det samme som vist nedenstående skærmbillede.


Relaterede artikler:

Bedste kontorproduktivitetsværktøjer

🤖 Kutools AI Aide: Revolutionér dataanalyse baseret på: Intelligent udførelse   |  Generer kode  |  Opret brugerdefinerede formler  |  Analyser data og generer diagrammer  |  Aktiver Kutools funktioner...
Populære funktioner: Find, fremhæv eller identificer dubletter   |  Slet tomme rækker   |  Kombiner kolonner eller celler uden at miste data   |   Runde uden formel ...
Super opslag: VLookup med flere kriterier    Multiple Value VLookup  |   VOpslag på tværs af flere ark   |   Fuzzy Lookup ....
Avanceret rulleliste: Opret hurtigt rulleliste   |  Afhængig rulleliste   |  Multivælg rulleliste ....
Column Manager: Tilføj et bestemt antal kolonner  |  Flyt kolonner  |  Skift synlighedsstatus for skjulte kolonner  |  Sammenlign områder og kolonner ...
Fremhævede funktioner: Grid fokus   |  Designvisning   |   Stor Formel Bar    Arbejdsbog & Ark Manager   |  Ressourcebibliotek (Autotekst)   |  Datovælger   |  Kombiner regneark   |  Krypter/Dekrypter celler    Send e-mails efter liste   |  Superfilter   |   Specielt filter (filter fed/kursiv/gennemstreget...) ...
Top 15 værktøjssæt12 tekst Værktøjer (tilføje tekst, Fjern tegn, ...)   |   50 + Chart Typer (Gantt kort, ...)   |   40+ Praktisk formler (Beregn alder baseret på fødselsdag, ...)   |   19 Indsættelse Værktøjer (Indsæt QR-kode, Indsæt billede fra sti, ...)   |   12 Konvertering Værktøjer (Tal til ord, Valutaomregning, ...)   |   7 Flet og del Værktøjer (Avancerede kombinere rækker, Opdel celler, ...)   |   ... og mere

Overlad dine Excel-færdigheder med Kutools til Excel, og oplev effektivitet som aldrig før. Kutools til Excel tilbyder over 300 avancerede funktioner for at øge produktiviteten og spare tid.  Klik her for at få den funktion, du har mest brug for...

Beskrivelse


Fanen Office bringer en grænseflade til et kontor med Office, og gør dit arbejde meget lettere

  • Aktiver redigering og læsning af faner i Word, Excel, PowerPoint, Publisher, Access, Visio og Project.
  • Åbn og opret flere dokumenter i nye faner i det samme vindue snarere end i nye vinduer.
  • Øger din produktivitet med 50 % og reducerer hundredvis af museklik for dig hver dag!
Comments (24)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Is there any chance that I can do both coloring and make words Bond with that VBA code? Please help me :)
This comment was minimized by the moderator on the site
Bonjour Cristal,
Merci pour ce code. Est-il possible de l'adapter pour mettre en évidence plusieurs mots "Apple,Rose,Vert,Merci"
Merci
This comment was minimized by the moderator on the site
Hi Cous,

The following VBA code can help. After running the code, you will get a dialog box. Please type in the words you want to highlight and separate them by comma.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/highlight.png
Sub HighlightStrings()
'Updated by Extendoffice 20230130
    Dim xHStr As String, xStrTmp As String
    Dim xHStrLen As Long, xCount As Long, I As Long
    Dim xCell As Range
    Dim xArr
    Dim xArr2
    On Error Resume Next
    xHStr = Application.InputBox("What is the string to highlight:", "KuTools For Excel", , , , , , 2)
    If TypeName(xHStr) <> "String" Then Exit Sub
    Application.ScreenUpdating = False
    
    xArr2 = Split(xHStr, ",")
    For j = 0 To UBound(xArr2)
        xHStr = xArr2(j)
    
        xHStrLen = Len(xHStr)
        For Each xCell In Selection
            xArr = Split(xCell.Value, xHStr)
            xCount = UBound(xArr)
            If xCount > 0 Then
                xStrTmp = ""
                For I = 0 To xCount - 1
                    xStrTmp = xStrTmp & xArr(I)
                    xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3
                    xStrTmp = xStrTmp & xHStr
                Next
            End If
        Next
    Next
    
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Thank you. that was really helpful. Can someone please tell how to color the text instead of highlighting it?

Regards
This comment was minimized by the moderator on the site
Hi Shaik Faiaz hamad,

Excel does not allow coloring a part of a cell. I'm sorry I can't help you with this problem.
This comment was minimized by the moderator on the site
Thank you that is very useful. How can I Highlight a word instead of a font color?

Regards.
This comment was minimized by the moderator on the site
how could the script be altered to do the following?

increase the font by 1 size, and
highlight multiple words with one running of the script?

Thx!
This comment was minimized by the moderator on the site
Hi t.taln,

If you want to increae the font size by 1 and highlight multiple words at the same time, please add the following line after the line "xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3" in the VBA code.
Note: You need to know the current font size of the selected cell beforehand, and then enter a number one size larger than the original word. The number 12 in the line below is the font size I will assign to the matching words. And the original font size of the word is 11.
xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.Size = 12
This comment was minimized by the moderator on the site
That's very nice, thx! I'm wondering if anyone knows how to make it work on Mac? Many thanks
Rated 5 out of 5
This comment was minimized by the moderator on the site
Ciao,
a me servirebbe evidenziare tutti i numeri (comprensivi di due decimali) da -10,00 a 0 in rosso e da 0 a +10,00 in verde. come posso fare per non aggiungere singolarmente ogni dato senza aggiungerli tutti manualmente?

Grazie mille
This comment was minimized by the moderator on the site
Hi Ciao,
Are your numbers located in different cells in a range? If so, you can create two conditional formatting rules (between -10 and 0, between 0 and 10) to highlight these numbers. If not, can you upload a screenshot of your data?
This comment was minimized by the moderator on the site
Buna,

Coloreaza cuvantul doar daca e la inceput. Daca e la mijloc in aceeasi casuta de excel sau la sfarsit nu-l coloreaza.
Ce anume as putea schimba in cod pentru a-l colora indiferent unde se afla in casuta excel?

Multumesc!
This comment was minimized by the moderator on the site
Hi Andreea,
If you only want to highlight the word if it is at the beginning of the selected cells. The following VBA code can do you a favor. Please give it a try.
Sub HighlightStrings()
'Updated by Extendoffice 20220805
    Dim xHStr As String, xStrTmp As String
    Dim xHStrLen As Long, xCount As Long, I As Long
    Dim xCell As Range
    Dim xArr
    On Error Resume Next
    xHStr = Application.InputBox("What is the string to highlight:", "KuTools For Excel", , , , , , 2)
    If TypeName(xHStr) <> "String" Then Exit Sub
    Application.ScreenUpdating = False
        xHStrLen = Len(xHStr)
        For Each xCell In Selection
            If xHStrLen <= Len(xCell.Value) Then
                If xHStr = Left(xCell.Value, xHStrLen) Then
                    xCell.Characters(1, xHStrLen).Font.ColorIndex = 3
                End If
            End If
        Next
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Bonjour,
Est-il possible de supprimer la boite de dialogue et de mettre par défaut "apple" comme mot recherché ?
Merci
This comment was minimized by the moderator on the site
Hello PAUC,
The following code can do you a favor. Please give it a try.
Sub HighlightStrings()
'Updated by Extendoffice 20220721
    Dim xHStr As String, xStrTmp As String
    Dim xHStrLen As Long, xCount As Long, I As Long
    Dim xCell As Range
    Dim xArr
    On Error Resume Next
    xHStr = "apple"
    If TypeName(xHStr) <> "String" Then Exit Sub
    Application.ScreenUpdating = False
        xHStrLen = Len(xHStr)
        For Each xCell In Selection
            xArr = Split(xCell.Value, xHStr)
            xCount = UBound(xArr)
            If xCount > 0 Then
                xStrTmp = ""
                For I = 0 To xCount - 1
                    xStrTmp = xStrTmp & xArr(I)
                    xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3
                    xStrTmp = xStrTmp & xHStr
                Next
            End If
        Next
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Thanks... this was very helpful! Is there a way to adjust the macro so that it only highlights whole words instead of partials. For instance, I'm trying to highlight the word "design" but it highlights the "design" in the word "designate". I want it to skip over that word if it's not the whole word. Thanks!
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