我正在嘗試撰寫/運行一個 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
有沒有更好的宏來做到這一點?太感謝了。
uj5u.com熱心網友回復:
像這樣:
Sub HighlightCell()
Dim ws As Worksheet, c As Range, i As Long
For Each ws in ActiveWorkbook.Worksheets 'or ThisWorkbook.Worksheets
For Each c In ws.Range("A1").Resize(104, 36).Cells
If Len(c.Value) > 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
Next c
Next ws
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484133.html
