[英]VBA - Highlighting a cell if it contains red text
I'm trying to write/run a VBA macro that highlights all cells in a workbook that contain red font (although, ALL characters within a string in given cell may not be red).我正在尝试编写/运行一个 VBA 宏,该宏突出显示工作簿中包含红色字体的所有单元格(尽管给定单元格中字符串中的所有字符可能不是红色的)。 The macro I've come up with falls short in that it only highlights cells that contain ONLY red font.
我提出的宏不足之处在于它仅突出显示仅包含红色字体的单元格。 I want it to highlight cells that may contain black text as well as red text.
我希望它突出显示可能包含黑色文本和红色文本的单元格。 Here's the macro I've come up with: '''
这是我想出的宏:'''
Sub HighlightCell()
Set ws = Sheets("MySheet")
For r = 1 To 104
For c = 1 To 36
If (ws.Cells(r, c).Font.Color = 255) Then
'set the desired color index
ws.Cells(r, c).Interior.ColorIndex = 34
End If
Next c
Next r
End Sub
Is there a better macro to do this?有没有更好的宏来做到这一点? Thank you so much.
太感谢了。
Like this:像这样:
Sub HighlightCell()
Dim ws As Worksheet, c As Range, i As Long, v
For Each ws In ActiveWorkbook.Worksheets
Debug.Print "Checking sheet '" & ws.Name & "' in workbook '" & _
ws.Parent.Name & "'"
For Each c In ws.Range("A1").Resize(104, 36).Cells
v = c.Value
If Not c.HasFormula And Not IsError(v) Then
If Len(v) > 0 Then 'if cell has any text
If c.Font.Color = 255 Then 'all text is red ?
c.Interior.ColorIndex = 34
ElseIf IsNull(c.Font.Color) Then 'mixed font color?
For i = 1 To Len(c.Value)
If c.Characters(i, 1).Font.Color = 255 Then
c.Interior.ColorIndex = 34
Exit For 'no need to check further
End If
Next i
End If
End If 'has any text
End If 'no formula, and does not contain an error value
Next c
Next ws
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.