Word VBA 使用 VBA 替代单词突出显示

发布于 2024-10-21 08:46:27 字数 697 浏览 2 评论 0原文

您好,场景如下:我需要以红色和绿色交替的颜色突出显示单词,并且我有以下已经可以使用的代码。问题是,如何在不使用 Mod 或 Modulo 运算符的情况下完成此操作?它还应该使用范围。欢迎任何建议!谢谢你们!

调用函数的模块:

Sub Test()
'If to call the function
If (altHighlight(ActiveDocument.Range)) = True Then MsgBox "Alternate Highlighting Done!"

End Sub

用于交替突出显示的函数:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count Mod 2 = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = count + 1
    Next
    altHighlight = True
End Function

Hi heres the scenario: I need to highlight words in alternating colors of red and green and i have the following code that is already working. Question is, how can this be done without using the Mod or Modulo operator? It should also be using Range. Any suggestions are welcomed! Thanks guys!

Module to call the Function:

Sub Test()
'If to call the function
If (altHighlight(ActiveDocument.Range)) = True Then MsgBox "Alternate Highlighting Done!"

End Sub

Function for alternate highlighting:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count Mod 2 = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = count + 1
    Next
    altHighlight = True
End Function

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

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

发布评论

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

评论(1

紅太極 2024-10-28 08:46:27

在计算机能够快速进行除法之前,有两种常见的方法来交替事物。

当整数递增时,其最低有效位将在 0 和 1 之间交替。您可以使用按位 AND 运算符来选出该位:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count And 1 = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = count + 1
    Next
    altHighlight = True
End Function

由于您实际上并未计算范围内的项目,因此您可以使 count 本身在 0 和 1 之间切换:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = 1 - count
    Next
    altHighlight = True
End Function

Before computers could do division quickly, there were two common ways to alternate things.

The least significant bit of an integer will alternate between 0 and 1 as it is incremented. You can use the bitwise AND operator to single out that bit:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count And 1 = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = count + 1
    Next
    altHighlight = True
End Function

Since you're not actually counting the items in your range, you can just make count itself toggle between 0 and 1:

Function altHighlight(R As Range) As Boolean
    Dim eachWord As Range
    Dim count As Integer

    For Each eachWord In R.Words
        If count = 0 Then
            eachWord.HighlightColorIndex = wdRed
        Else
            eachWord.HighlightColorIndex = wdGreen
        End If
        count = 1 - count
    Next
    altHighlight = True
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文