ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4522|回复: 14

[Word 应用与开发] [第4期] WORD VBA-自动标色(已总结)

[复制链接]

TA的精华主题

TA的得分主题

发表于 2005-10-3 07:06 | 显示全部楼层 |阅读模式

[第四期WORD] WORD VBA-自动标色

题目要求:

将附件中的8个段落中的指定文字“只”进行颜色处理,要求,偶数段落中,当“只”为单数时,为兰色,“只”为偶数时为红色;奇数段落中,当“只”为单数时,其字体颜色设置为红色,“只”为偶数时,其字体颜色为兰色。

说明:

段落编号为自然段落编号,即1357段落为奇数段落;2468为偶数段落;“只”中该段落中的计数,从本段落开始处计数,第一个出现的为单数,第二个出现的为偶数……。

完成后的代码,请在必要处加入注释,以运行时间短,结构精简,设计合理规范者为优胜,最高分为5分,完成基本处理要求者,可得基本分1分。

非使用VBA者不得分。

本题使用的示例文档和处理结果如下:

示例文档:

1. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

2. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

3. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

4. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

5. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸。

6. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

7. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

8. 那只敏捷的棕毛狐狸跃过那只懒狗。 那只敏捷的棕毛狐狸跃过那只懒狗。

完成结果见附件,将全选此结果设置字体为自动色,作为示例文档进行编程处理。

[此贴子已经被apolloh于2005-11-1 9:03:39编辑过]

TA的精华主题

TA的得分主题

发表于 2005-10-6 19:22 | 显示全部楼层

先贴我的第一个答案:

Sub 第四期() '解题思路是这样的: '第一段+第1次找到的"只"字=2时,"只"字为红色;第一段+第2次找到的"只"字=3时,"只"字为蓝色... '第二段+第1次找到的"只"字=3时,"只"字为蓝色;第二段+第2次找到的"只"字=4时,"只"字为红色,... '同理,我们可以推出,当段落数加上找到的次数等于偶数时,"只"字为红色,反之为蓝色. '现在,只要在段落中循环找,同时,在每一个段落循环开始前,次数归零.即可. '以下为程序代码部分: On Error Resume Next '忽略错误 Application.ScreenUpdating = False '屏幕不更新 Dim dan%, a%, myrange As Range For dan = 1 To ActiveDocument.Paragraphs.Count a = 0 '归零 Set myrange = ActiveDocument.Paragraphs(dan).Range myrange.Select With Selection.Find '查找 .Forward = True '向下搜索 .Text = "只" '搜索文本 Do While .Execute '如果能查到 a = a + 1 '计数 If (dan + a) Mod 2 = 0 Then '如果段落数加查到数为偶数则 Selection.Font.Color = wdColorRed '找到后将选定文本修改为红色 Else: Selection.Font.Color = wdColorBlue '不为偶数,即奇数时后将选定文本修改为蓝色 End If Loop End With Next dan Application.ScreenUpdating = True '屏幕更新 End Sub

正在努力想第二个答案。

'* +++++++++++++++++++++++++++++

'说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时86.45 Sub 第四期() Debug.Print Timer '19832.81 On Error Resume Next '忽略错误 Application.ScreenUpdating = False '屏幕不更新 Dim dan%, a%, myrange As Range For dan = 1 To ActiveDocument.Paragraphs.Count a = 0 '归零 Set myrange = ActiveDocument.Paragraphs(dan).Range myrange.Select With Selection.Find '查找 .Forward = True '向下搜索 .Text = "" '搜索文本 Do While .Execute '如果能查到 a = a + 1 '计数 If (dan + a) Mod 2 = 0 Then '如果段落数加查到数为偶数则 Selection.Font.Color = wdColorRed '找到后将选定文本修改为红色 Else: Selection.Font.Color = wdColorBlue '不为偶数,即奇数时后将选定文本修改为蓝色 End If Loop End With Next dan Application.ScreenUpdating = True '屏幕更新 Debug.Print Timer '19919.26 End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:33:34编辑过]

TA的精华主题

TA的得分主题

发表于 2005-10-8 19:49 | 显示全部楼层
支持守柔版主! Private Sub CommandButton1_Click() Dim i%, j% Dim CntPrg%, Cntchr%, Nochr% CntPrg = ThisDocument.Paragraphs.Count For i = 1 To CntPrg Nochr = 0 Cntchr = ThisDocument.Paragraphs(i).Range.Characters.Count For j = 1 To Cntchr If ThisDocument.Paragraphs(i).Range.Characters(j) = "只" Then Nochr = Nochr + 1 ThisDocument.Paragraphs(i).Range.Characters(j).Font.Color = IIf((i + Nochr) Mod 2, wdColorBlue, wdColorRed) End If Next j Next i End Sub

'* +++++++++++++++++++++++++++++ '说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时15.28 Private Sub CommandButton1_Click() Dim i%, j% Dim CntPrg%, Cntchr%, Nochr% Debug.Print Timer '19464.34 CntPrg = ThisDocument.Paragraphs.Count For i = 1 To CntPrg Nochr = 0 Cntchr = ThisDocument.Paragraphs(i).Range.Characters.Count For j = 1 To Cntchr If ThisDocument.Paragraphs(i).Range.Characters(j) = "" Then Nochr = Nochr + 1 ThisDocument.Paragraphs(i).Range.Characters(j).Font.Color = IIf((i + Nochr) Mod 2, wdColorBlue, wdColorRed) End If Next j Next i Debug.Print Timer '19479.62 End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:26:09编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

发表于 2005-10-9 16:42 | 显示全部楼层

'* +++++++++++++++++++++++++++++

'说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时7.24 Sub Macro3() ' ' Macro3 Macro ' 宏在 05-10-8 cai 录制 ' Debug.Print Timer '19247.81 For i = 1 To ActiveDocument.Paragraphs.Count Set myrange = ActiveDocument.Paragraphs(i).Range j = 0 k = 0 For Each mychar In myrange.Characters k = k + 1 If mychar = "" Then j = j + 1 If (i + j) / 2 = Int((i + j) / 2) Then myrange.Characters(k).Font.ColorIndex = wdRed Else myrange.Characters(k).Font.ColorIndex = wdBlue End If End If Next mychar Next i Debug.Print Timer '19255.05 End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:22:38编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

发表于 2005-10-11 13:43 | 显示全部楼层

'* +++++++++++++++++++++++++++++

'说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时35.58 Sub markColor() Dim aPara As Range Dim ParagrahpCounts As Integer, curPara As Integer Dim curWord As Integer Debug.Print Timer '18887.67 ParagrahpCounts = ActiveDocument.Paragraphs.Count For curPara = 1 To ParagrahpCounts Set aPara = ActiveDocument.Range( _ Start:=ActiveDocument.Paragraphs(curPara).Range.Start, _ End:=ActiveDocument.Paragraphs(curPara).Range.End) curWord = 0 For Each fWord In aPara.Words If fWord.Text = "" Then curWord = curWord + 1 If curPara Mod 2 = 1 Then If curWord Mod 2 = 1 Then fWord.Font.Color = wdColorRed Else fWord.Font.Color = wdColorBlue End If Else If curWord Mod 2 = 1 Then fWord.Font.Color = wdColorBlue Else fWord.Font.Color = wdColorRed End If End If End If Next fWord Next curPara Debug.Print Timer '18923.25 End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:17:36编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

发表于 2005-10-11 14:55 | 显示全部楼层

还是第一个读到字符串里处理来得快.

'* +++++++++++++++++++++++++++++ 对不起,我未看到任何过程。

[em06]

----------------------------------------------------------------------

更晕.头一次在word中编辑宏,竟然把它放在normal模块里.我都不知道两个不是一起的.刚看到你的回复时,我也挺奇怪的,我打开附件没问题啊,代码在的啊.却不知道它竟然是在normal模块里.呜呼.

Sub Faster() '方法一 Dim i%, j%, l%, k%, m As Long, tmpR As Range, tmpS$ i = ActiveDocument.Paragraphs.Count For j = 1 To i Set tmpR = ActiveDocument.Paragraphs(j).Range tmpS = tmpR.Text m = tmpR.Start l = 0 For k = 1 To Len(tmpS) If Mid(tmpS, k, 1) = "只" Then l = l + 1 tmpR.Start = m + k - 1: tmpR.End = tmpR.Start + 1 If (j + l) Mod 2 Then tmpR.Font.Color = wdColorBlue Else tmpR.Font.Color = wdColorRed End If End If Next Next End Sub

[em06]
[此贴子已经被作者于2005-10-24 10:11:19编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

发表于 2005-10-12 08:45 | 显示全部楼层

我的答案:

Sub dtest() Dim prgh As Paragraph Dim jiouD As Integer Dim jiouZ As Integer Dim i As Integer, s As Integer ' Dim Ts As Date, tend As Date jiouD = 0 ' Ts = Now For Each prgh In ActiveDocument.Paragraphs jiouZ = 0 jiouD = jiouD + 1 For i = 1 To prgh.Range.Sentences.Count For s = 1 To prgh.Range.Sentences(i).Words.Count If prgh.Range.Sentences(i).Words.Item(s).Text = "只" Then jiouZ = jiouZ + 1 If jiouD Mod 2 = 0 Then If jiouZ Mod 2 = 0 Then prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorRed Else prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorBlue

End If Else If jiouZ Mod 2 = 0 Then prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorBlue

Else prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorRed

End If

End If End If Next Next Next 'tend = Now 'MsgBox tend & vbLf & Ts End Sub

'* +++++++++++++++++++++++++++++ '说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时34.14 Sub dtest() Dim prgh As Paragraph Dim jiouD As Integer Dim jiouZ As Integer Dim i As Integer, s As Integer Debug.Print Timer '18491.75 jiouD = 0 For Each prgh In ActiveDocument.Paragraphs jiouZ = 0 jiouD = jiouD + 1 For i = 1 To prgh.Range.Sentences.Count For s = 1 To prgh.Range.Sentences(i).Words.Count If prgh.Range.Sentences(i).Words.Item(s).Text = "" Then jiouZ = jiouZ + 1 If jiouD Mod 2 = 0 Then If jiouZ Mod 2 = 0 Then prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorRed Else prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorBlue End If Else If jiouZ Mod 2 = 0 Then prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorBlue Else prgh.Range.Sentences(i).Words.Item(s).Font.Color = wdColorRed End If End If End If Next Next Next Debug.Print Timer '18525.89 End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:10:26编辑过]

TA的精华主题

TA的得分主题

发表于 2005-10-18 16:33 | 显示全部楼层

'* +++++++++++++++++++++++++++++

'说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP '本过程运行用时6.05 Private Sub CommandButton1_Click() Dim i As Range, x As Integer, zhinumb As Integer, Paragnumb As Integer Debug.Print Timer '(17199.35) Application.ScreenUpdating = False For Paragnumb = 1 To ActiveDocument.Paragraphs.Count zhinumb = 0 For Each i In ActiveDocument.Paragraphs(Paragnumb).Range.Characters If i Like "" Then zhinumb = zhinumb + 1 Next ActiveDocument.Paragraphs(Paragnumb).Range.Select Selection.Find.Text = "" For x = 1 To zhinumb Selection.Find.Execute If (x + Paragnumb) Mod 2 = 0 Then Selection.Font.Color = wdColorRed Else: Selection.Font.Color = wdColorBlue End If Next Next Selection.HomeKey Unit:=wdStory Application.ScreenUpdating = True Debug.Print Timer '(17205.4) End Sub '----------------------

[此贴子已经被守柔于2005-10-21 5:06:02编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?免费注册

x

TA的精华主题

TA的得分主题

 楼主| 发表于 2005-10-22 10:17 | 显示全部楼层

说明:

本次VBA竞赛题,考查的是大家对WORDRANGE对象的理解,从大家的代码情况来看,总体不错,字、词、句、段落、SELECTION对象等,基本上都出现了。

这个竞赛题,从代码的思路来说,有两种情况:

一是使用查找;二是遍历各字(词),显然,查找优于遍历;

从代码方法上说,也有几种情况

使用SELECTION对象和方法,也可以使用RANGE对象和方法,从我们完成的结果而言,此处,SELECTION对象快于RANGE对象,这是查找中SELECTION对象有其天然优势,WORD中设置SELECTION对象时,WORD会自动缩减范围(递减),而RANGE对象如果要递减则需要设置代码(目前无法找到理论依据,我的推论)

在处理段落中“只”数时,思路基本一致,对于段落数的处理,至少有三种方法,从1到段落总数的循环,使用RANGE对象获得当前段落数,使用END属性判断是否处于同一段落等等。

除了参赛选手提供的这些代码外,我另外写了一些代码,它们可根据不同的情况进行使用,供大家参考。

代码包括参赛选手代码运行时间,只是一个相对概念,机器配置和运行时间(同一电脑,今天与明天的运行)的不同,可能会有出入,仅供参考。

'* +++++++++++++++++++++++++++++ '* Created By I Love You_Word!@ExcelHome 2005-10-22 10:17:26 '仅测试于System: Windows NT Word: 10.0 Language: 2052 ' 00067^The Code CopyIn [ThisDocument-ThisDocument]^' '* -----------------------------

Option Explicit '显式变量声明 '说明:为了正确比较代码执行的效率,我把文档从8个段落扩大到了48个段落 '以利于各个代码运行时间的精确比较 '测试电脑的配置:CPU :Inter Celeron 534MHZ,RAM:264M,WIN2000,WORDXP Function myColor(P As Integer, N As Integer) As WdColor If P Mod 2 = 0 Then If N Mod 2 = 0 Then myColor = wdColorRed Else myColor = wdColorBlue Else If N Mod 2 = 0 Then myColor = wdColorBlue Else myColor = wdColorRed End If End Function '---------------------- Sub Example1() '本过程用时3.72S Dim intCount As Integer, ParCount As Integer, OldPostion As Long, EndPostion As Long Debug.Print Timer ' 33242.21 Application.ScreenUpdating = False '关闭屏幕更新 With Selection .HomeKey wdStory '移到文档首(强制从头开始搜索) With .Find .ClearFormatting '清除格式查找 .Text = "" '查找内容为"" Do While .Execute = True '每次成功查找后 '获得选定内容(被查找的项目)的段落标记所在位置 EndPostion = Selection.Paragraphs(1).Range.End '当变量OldPostion值小于EndPostion,将所选内容的段落标记位置赋值给OldPostion '也就是说,如果当前所选内容的段落标记位置与原位置(OldPostion)不等时,说明已进入下一个段落 '段落数(变量ParCount)进行累加,intCount归零,EndPostion(新位置)重新赋值给OldPostion If OldPostion < EndPostion Then ParCount = ParCount + 1: intCount = 0: OldPostion = EndPostion intCount = intCount + 1 '累加 '查找到的内容的颜色根据FUNCTION过程,设置其颜色 Selection.Font.Color = myColor(ParCount, intCount) Loop End With End With Application.ScreenUpdating = True '恢复屏幕更新 Debug.Print Timer ' 33245.78 End Sub '---------------------- Sub Example2() '本过程用时5.07 Dim ParRange As Range, intCount As Integer, ParCount As Integer Debug.Print Timer Application.ScreenUpdating = False '关闭屏幕更新 Selection.WholeStory '全选 With Selection.Find .ClearFormatting '清除查找格式 .Text = "" '设置查找内容 .Forward = True '向下查找 Do While .Execute = True '每次成功查找 '定义一个RANGE对象,为文档开始(0)位置到所查找的段落标记所在位置 Set ParRange = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End) '如果该RANGE对象的段落数>原有的段落数(也是说明查找已进行到下一个段落)则变量intCount=0 If ParRange.Paragraphs.Count > ParCount Then intCount = 0 '累加 intCount = intCount + 1 '将现在RANGE对象的段落数赋值给变量ParCount ParCount = ParRange.Paragraphs.Count '设置所选内容的字体颜色 Selection.Font.Color = myColor(ParCount, intCount) Loop End With Application.ScreenUpdating = True '恢复屏幕更新 Debug.Print Timer End Sub '---------------------- Sub Example3() '本过程运行用时10.74 Dim myRange As Range, OldPostion As Long, intCount As Integer, ParCount As Integer Debug.Print Timer Application.ScreenUpdating = False '关闭屏幕更新 Set myRange = ActiveDocument.Content '设置一个RANGE对象 '注意,每次成功查找后,WORD会自动重新定义该Range对象(即为查找内容所在的RANGE) '注意,在这个过程中,我们使用了格式查找,否则将进入死循环 With myRange.Find '查找 .ClearFormatting .Text = "" .Format = True .Font.Color = wdColorAutomatic '带格式(查找自动色()""的文本)查找 Do While .Execute = True '每次成功查找 '如果原变量位置小于现在myRange所在的段落标记位置时,变量ParCount累加(段落数):intCount则归零重新开始 If OldPostion < myRange.Paragraphs(1).Range.End Then ParCount = ParCount + 1: intCount = 0 intCount = intCount + 1 '累加 'myRange所在的段落结束位置赋值于OldPostion OldPostion = myRange.Paragraphs(1).Range.End 'myRange字体颜色根据FUNCTION过程值设置 myRange.Font.Color = myColor(ParCount, intCount) Loop End With Application.ScreenUpdating = True '恢复屏幕更新 Debug.Print Timer End Sub '---------------------- Sub Example4() '本过程运行用时12.64 Dim ParCount As Integer, i As Integer, myRange As Range, ZhiCount As Integer Debug.Print Timer ' 22026.89 Application.ScreenUpdating = False '关闭屏幕更新 With ActiveDocument ParCount = .Paragraphs.Count '获得文档段落总数 For i = 1 To ParCount '建立一个循环 ZhiCount = 0 '初始化变量值 GN: Set myRange = .Paragraphs(i).Range '定义一个RANGE对象为该段落所在的RANGE With myRange.Find '查找 .ClearFormatting '清除格式查找 .Text = "" .Format = True .Font.Color = wdColorAutomatic '也是带格式的查找 Do While .Execute '每次成功查找 ZhiCount = ZhiCount + 1 '累加 '根据FUNCTION过程值设置字体颜色 myRange.Font.Color = myColor(i, ZhiCount) GoTo GN '返回GN Loop End With Next End With Application.ScreenUpdating = True Debug.Print Timer '22039.53 End Sub '---------------------- Sub Example5() '本过程运行用时12.66 Dim myRange As Range, intTemp As Integer, intCount As Integer, ParCount As Integer Dim TempRange As Range, A As Integer Debug.Print Timer Application.ScreenUpdating = False '关闭屏幕更新 Set myRange = ActiveDocument.Content '定义一个RANGE对象 GN: With myRange.Find '查找 ParCount = myRange.Paragraphs.Count '取得指定RANGE对象的段落总数 .ClearFormatting '不限定格式查找 .Text = "" Do While .Execute = True '每次成功查找 '定义一个RANGE对象,为从myRange对象的结束位置开始,到文档的结束位置 Set TempRange = ActiveDocument.Range(myRange.End, ActiveDocument.Content.End) '取得TempRange对象的段落总数 intTemp = TempRange.Paragraphs.Count '如果两者不相等(说明上一次查找与本次查找不在同一段落上),intCount = 0: A = A + 1 If intTemp <> ParCount Then intCount = 0: A = A + 1 intCount = intCount + 1 '累加 '设置myRange字体的颜色 myRange.Font.Color = myColor(A, intCount) Set myRange = TempRange '重新定义该RANGE对象(递减了RANGE对象,下一次的查找从本次查找的结束位置到全文结束位置) GoTo GN '返回GN行号 Loop End With Application.ScreenUpdating = True '恢复屏幕更新 Debug.Print Timer End Sub '---------------------- Sub Example6() '本过程运行用时34.34 Dim i As Range, intCount As Integer, ParCount As Integer, ParRange As Range Debug.Print Timer Application.ScreenUpdating = False '关闭屏幕更新 For Each i In ActiveDocument.Words '遍历文档词组 If i = "" Then '如果为"" '定义一个RANGE对象,为从0开始到该词(字符)所在段落标记位置 '也可以同EXAMPLE1一样,直接使用END属性而不用段落数来判断,更快一些 Set ParRange = ActiveDocument.Range(0, i.Paragraphs(1).Range.End) '如果两者不等,则说明进入了下一个段落,intCount = 0 If ParRange.Paragraphs.Count > ParCount Then intCount = 0 intCount = intCount + 1 '累加 '取得指定对象段落数 ParCount = ParRange.Paragraphs.Count '设置字体颜色 i.Font.Color = myColor(ParCount, intCount) End If Next Application.ScreenUpdating = True '恢复屏幕更新 Debug.Print Timer End Sub '----------------------

[此贴子已经被作者于2005-10-22 10:20:54编辑过]

TA的精华主题

TA的得分主题

发表于 2005-10-22 11:15 | 显示全部楼层
守柔 版主测试速度为什么不使用这样的方式呢: sub xx() dim t1 t1=timer ..... msgbox "用时:" & timer-t1 end sub 我觉得这样是不是更方便些。 还有一点写下来供大家参考: 同样是遍历,for each ..in ... 比for i=1 to ...要快,我把3楼的代码改成for each后,速度提高了50%左右。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 01:28 , Processed in 0.041170 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表