AW: Ein Teil eines Textes einfärben, geht das?
Firmus
Hi BugSimpson,
hier ein Beispiel, das ich vor einiger Zeit aus dem Forum gezogen habe.
Es sollte Teile eines Textes in einer Zelle einfärben.
An deine genaue Anforderung muss du es noch anpassen.
Sub FormatWorte()
Dim ws As Worksheet
Dim cell As Range
Dim word As String
Dim color As Long
' Definiere die Wörter und ihre Farben
Dim wordsAndColors As Object
Set wordsAndColors = CreateObject("Scripting.Dictionary")
wordsAndColors.Add "Water", RGB(0, 0, 255) ' Blau
wordsAndColors.Add "Energy", RGB(255, 255, 0) ' Gelb
' Füge hier weitere Wörter und Farben hinzu
' Durchlaufe alle Arbeitsblätter
For Each ws In ThisWorkbook.Worksheets
' Durchlaufe alle Zellen in jedem Arbeitsblatt
For Each cell In ws.UsedRange
If Not IsEmpty(cell) Then
Dim wordKey As Variant
For Each wordKey In wordsAndColors.Keys
word = wordKey
color = wordsAndColors(wordKey)
' Suche und formatiere das Wort
Call FormatWordInCell(cell, word, color)
Next wordKey
End If
Next cell
Next ws
End Sub
Sub FormatWordInCell(cell As Range, word As String, color As Long)
Dim startPos As Long
Dim wordLength As Long
startPos = InStr(1, cell.Value, word, vbTextCompare)
wordLength = Len(word)
While startPos > 0
cell.Characters(startPos, wordLength).Font.color = color
startPos = InStr(startPos + wordLength, cell.Value, word, vbTextCompare)
Wend
End Sub
Gruß,
Firmus
ps: bin nur sporadisch im Forum - Antworten könnten dauern.