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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:03:18  来源:igfitidea点击:

Change font color for specific char in a cell range

excelvbacharacter

提问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 LoopAndChangeColorForThisRangeand see if it works, as you'd expect.

LoopAndChangeColorForThisRange如您所料,拨打电话看看它是否有效。