vba 更改单元格范围中特定字符的字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7759962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font color for specific char in a cell range
提问by user994277
I would like to change the font color for a specific character within a range of cells. I figured out how to do this for a single cell (code attached), but I can't figure out how to do it over a full range of cells; B8: F12. Can anyone help?
我想更改单元格范围内特定字符的字体颜色。我想出了如何对单个单元格执行此操作(附上代码),但我无法弄清楚如何在整个单元格范围内执行此操作;B8:F12。任何人都可以帮忙吗?
thanks!
谢谢!
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
回答by shahkalpesh
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
Make a call to LoopAndChangeColorForThisRange
and see if it works, as you'd expect.
LoopAndChangeColorForThisRange
如您所料,拨打电话看看它是否有效。