启动宏alt+f11,输入代码后需要手工按一下ALT+F8 (注明不是本人的,是去请教老师的) Sub 改变数字格式为千分位格式 () Dim strWithTh As String '用于保存处理后的数字 Dim intI As Integer '循环给定的数字 Dim intLen As Integer '求数字长度 Dim intCount As Integer '保存改变个数 With Selection .HomeKey Unit:=wdStory '回到文章开头 .Find.ClearFormatting Do .Find.Text = "^#^#^#^#" '查找4位数字 '如果没有找到,退出 If .Find.Execute = False Then MsgBox "千分位设置完毕!共改变 " & intCount & "个数字。", vbInformation Exit Sub End If intCount = intCount + 1 '计数器增加 .MoveLeft '移到整个数字的左边 .MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend '选择整个数字 intLen = Len(.Text) strWithTh = "" '清空 For intI = intLen To 1 Step -3 If intI > 3 Then strWithTh = "," & Mid(.Text, intI - 2, 3) & strWithTh Else '对于长度为3的倍数的数字特别处理 strWithTh = Left(.Text, IIf(intLen Mod 3 = 0, 3, intLen Mod 3)) & strWithTh End If Next .Text = strWithTh .MoveRight '右移一个来取消选择 Loop End With End Sub
[此贴子已经被作者于2010-9-8 14:34:38编辑过] |