Word VBA 使用 VBA 替代单词突出显示
您好,场景如下:我需要以红色和绿色交替的颜色突出显示单词,并且我有以下已经可以使用的代码。问题是,如何在不使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在计算机能够快速进行除法之前,有两种常见的方法来交替事物。
当整数递增时,其最低有效位将在 0 和 1 之间交替。您可以使用按位 AND 运算符来选出该位:
由于您实际上并未计算范围内的项目,因此您可以使
count
本身在 0 和 1 之间切换: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:
Since you're not actually counting the items in your range, you can just make
count
itself toggle between 0 and 1: