目的:将文档标为黄色的数值,自动以千分位形式展现。 下面是收集的两套代码,都有些问题,寻求帮助! Sub 正则4千分位() On Error Resume Next Application.ScreenUpdating = False '关闭屏幕更新,加快程序运行 Dim myRange As Range,mt As Match, oRang As Range, n%, m% If Selection.Start<> Selection.End Then Set myRange =Selection.Range Else '无选择区 Set myRange =ActiveDocument.Content '定义为主文档文字部分 End If WithCreateObject("vbscript.regexp") .Pattern ="[0-9.]{4,}" '正则表达式有些问题 .Global = True:.IgnoreCase = False: .MultiLine = True For Each mt In.Execute(myRange.Text) m =mt.FirstIndex: n = mt.length Set oRang =ActiveDocument.Range(myRange.Start + m, myRange.Start + m + n) oRang.Text =VBA.Format(oRang.Text, "Standard") '修改格式 Next End With End Sub Function GetRepeatedTimes(ByVal strParent As String, _ ByVal strChild AsString) As Long Dim lngLenOfChild AsLong lngLenOfChild =Len(strChild) If lngLenOfChild Then GetRepeatedTimes= (Len(strParent) - Len(Replace(strParent, strChild, vbNullString))) /lngLenOfChild End If End Function Sub 千分位() '78085842(凯文)&1838095599(春天)@QQ群257182022(Word& VBA) '有些问题,会把年份都更改了 On Error Resume Next Application.ScreenUpdating = False '关闭屏幕更新,加快程序运行 Dim Rng As Range,length%, End_selection& If Selection.Type =wdSelectionIP Then 'Selection.Start =Selection.End End_selection =ActiveDocument.Content.End Set Rng =ActiveDocument.Content '定义为主文档文字部分 Else '有选择区 End_selection =Selection.Range.End Set Rng =Selection.Range '定义为选中文字部分 End If With Rng.Find .ClearFormatting .Text ="[.0-9]{4,}" '查找数字 .MatchWildcards =True Do While .Execute With .Parent If .Start> End_selection Then Exit Do If GetRepeatedTimes(.Text,".") > 1 Then Exit Do '数值有一个以上的小数点,则跳过 length =Len(.Text) .Text =VBA.Format(VBA.Val(.Text), "Standard") '转为千分位格式 End_selection = End_selection + Len(.Text) - length .Start =.End End With Loop End With Application.ScreenUpdating = True '恢复屏幕更新 End Sub 现将2019年度财务状况汇报如下: 2018年7月1日后入司员工发放奖金的50%。一位数1个,俩位数12件,三位数234元,招待费788.12元,房租4935元,办公费674539.00 元,本年实现100087.50 元,甲产品1234600.05元,剩余9776500.0116 元,利润-8324.032元。资产总额95.84元。最后结余0元。4133.897
18376 | 14133.897552 | 1484 | -913 | 0.981 | 1. | .04842 | 0.1.2.3 |
|