VBA - 突出显示包含红色文本的单元格
VBA - Highlighting a cell if it contains red text
我正在尝试 write/run 一个 VBA 宏,该宏突出显示工作簿中包含红色字体的所有单元格(尽管给定单元格中字符串中的所有字符可能不是红色)。我想出的宏不足之处在于它只突出显示仅包含红色字体的单元格。我希望它突出显示可能包含黑色文本和红色文本的单元格。这是我想出的宏:
'''
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
有没有更好的宏可以做到这一点?
非常感谢。
像这样:
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
我正在尝试 write/run 一个 VBA 宏,该宏突出显示工作簿中包含红色字体的所有单元格(尽管给定单元格中字符串中的所有字符可能不是红色)。我想出的宏不足之处在于它只突出显示仅包含红色字体的单元格。我希望它突出显示可能包含黑色文本和红色文本的单元格。这是我想出的宏: '''
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
有没有更好的宏可以做到这一点? 非常感谢。
像这样:
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