简体   繁体   中英

Change font color for a row of text in cell which contains a certain value

I am writing a check in/out program in excel and have gotten te request that if a line contains "|0|" it should get a different font color.

I've tried with Instr and Cells().Characters but I cannot seem to figure out how to do it.

The cells can have a variety of rows of text. Which is easy enough to solve with splitting them on a return and having a for loop loop, but I cannot seem to figure out how to assign a different font color to a row of text that contains the required value.

Image for illustration of the data:

在此处输入图像描述

How do I best solve this?

Added information:

The goal of this is that on button press the whole line of text where the |O| is would be collored differently. Other lines of text that do not have this will remain the same color.

Like in this image as a concept

[ 在此处输入图像描述 ]

Try this. It works for me.

    Sub ChangeRowFontColour()
    
    Dim rng As Range
    Dim TextToFind As String
    Dim FirstFound As String
    
    TextToFind = "Specific Text"

        With ActiveSheet.UsedRange
            Set rng = .Cells.Find(TextToFind, LookIn:=xlValues)
            
            If Not rng Is Nothing Then
            FirstFound = rng.Address
                Do
                    rng.EntireRow.Font.ColorIndex = 3
                                            
                    For Each part In rng
                        lenOfPart = Len(part)
                        lenTextToFind = Len(TextToFind)
                        For i = 1 To lenOfPart
                            tempStr = Mid(part, i, lenTextToFind)
                            If tempStr = TextToFind Then
                                part.Characters(Start:=i, Length:=lenTextToFind).Font.ColorIndex = 0
                            End If
                        Next i
                    Next
                    
                    Set rng = .FindNext(rng)
                  
                Loop While Not rng Is Nothing And rng.Address <> FirstFound
            End If
        End With
            
End Sub

try this

Public Sub ExampleMainSub()
    Dim cell As Range
    For Each cell In Selection
        If HasMySymbols(cell.Value) Then
            WorkWithCellContent cell
        Else
            cell.Font.ColorIndex = xlAutomatic
            cell.Font.TintAndShade = 0
        End If
    Next cell
End Sub

Private Sub WorkWithCellContent(ByVal cell As Range)
    Dim arr As Variant
    arr = Split(cell.Value, Chr(10))
    
    Dim firstPosOfRow As Long
    firstPosOfRow = 1
    
    Dim subLine As Variant
    For Each subLine In arr
        If HasMySymbols(subLine) Then
            cell.Characters(start:=firstPosOfRow, Length:=Len(subLine)).Font.Color = vbRed
        Else
            cell.Characters(start:=firstPosOfRow, Length:=Len(subLine)).Font.ColorIndex = xlAutomatic
        End If
        firstPosOfRow = firstPosOfRow + Len(subLine) + 1 '+1 is needed
    Next subLine
    
End Sub

Private Function HasMySymbols(ByVal somestring As String) As Boolean
    HasMySymbols = InStr(1, somestring, "|0|") > 0
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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