我想更改单元格范围内特定字符的字体颜色。我想出了如何为单个单元格(附加代码)执行此操作,但我无法弄清楚如何在整个单元格中执行此操作; B8:F12。有人可以帮忙吗?
谢谢!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim FindChar As String
Dim SearchString As String
SearchString = Range("B8").Value
FindChar = Chr(182)
For i = 1 To Len(SearchString)
If Mid(SearchString, i, 1) = FindChar Then
Range("B8").Characters(i, 1).Font.Color = RGB(221, 221, 221)
End If
Next i
End Sub
答案 0 :(得分:3)
Sub ChangeColorIfMatchesCondition(byval cell as Range)
Dim i As Integer
Dim FindChar As String
Dim SearchString As String
SearchString = cell.Value
FindChar = Chr(182)
For i = 1 To Len(SearchString)
If Mid(SearchString, i, 1) = FindChar Then
cell.Characters(i, 1).Font.Color = RGB(221, 221, 221)
End If
Next i
End Sub
Sub LoopAndChangeColorForThisRange()
dim cell
dim targetRange as Range
set targetRange = me.Range("B8:F12")
for each cell in targetRange.Cells
ChangeColorIfMatchesCondition cell
next
End Sub
拨打LoopAndChangeColorForThisRange
,看看它是否有效,正如您所期望的那样。