更改单元格范围中特定字符的字体颜色

发布于 2024-12-10 01:36:04 字数 480 浏览 0 评论 0原文

我想更改一系列单元格内特定字符的字体颜色。我弄清楚了如何对单个单元格执行此操作(附有代码),但我无法弄清楚如何在整个单元格范围内执行此操作; 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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-12-17 01:36:04
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 并查看它是否如您所期望的那样工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文