没机会参与前面各期正式竞赛。看了第四期竞赛题,学习之余,也编了一段VBA,呵呵,正式竞赛区回贴看不到,只好发到这里了。运行时间大致是最快的:
Sub test()
Dim myStr As String
Dim iColor As WdColor, cColor As WdColor
Dim myP As Paragraph, myR As Range
Dim t1 As Long
t1 = Timer
Application.ScreenUpdating = False
cColor = wdColorBlue
For Each myP In ActiveDocument.Paragraphs
iColor = cColor
For Each myR In myP.Range.Characters
If myR = "只" Then
myR.Select
Selection.Font.Color = iColor
iColor = IIf(iColor = wdColorRed, wdColorBlue, wdColorRed)
End If
Next myR
cColor = IIf(cColor = wdColorRed, wdColorBlue, wdColorRed)
Next myP
Application.ScreenUpdating = True
Debug.Print Timer - t1
End Sub
反复进行了多次测试,并使用=rand(200,51)虚拟文本,这里巧妙地使用了IIF函数切换颜色,回避对段落数、搜索到的字符进行计算判断。
[此贴子已经被作者于2005-12-14 11:39:24编辑过] |