楼主 传递 |
Q:word如何让数字自动添加千位分隔符? 整篇文章里面有很多长数字 例如123456789,987654321等 如何能够自动转化为下面的形式 123,456,789 ? 以便不用再数个十百千万。
A: 有多种方法,但一般推荐用VBA ~查找替换会改变原数据
代码1如下
- Sub 设置千分位格式()
- Dim myRange As Range, myValue As Double
- Application.ScreenUpdating = False '关闭屏幕更新,加快程序运行
- Do '进入一个循环
- '定义myRange为活动文档的主文字部分
- Set myRange = ActiveDocument.Content
- With myRange.Find '查找设置
- .ClearFormatting '清除格式
- .Text = "[0-9]{4,}[!年]" '查找四个以上的数字且其后为不为年的内容
- .MatchWildcards = True '使用通配符
- If .Execute = False Then Exit Do '如果未找到则退出循环
- myValue = Val(myRange.Text) '取得数字
- myRange.SetRange myRange.Start, myRange.End - 1 '重新定义myRange对象
- myRange.Text = VBA.Format$(myValue, "#,##0") '写入千分位格式
- End With
- Loop
- Application.ScreenUpdating = True '恢复屏幕更新
- End Sub
代码二
- Sub 千元()
- '
- ' 千元 Macro
- ' 宏在 2011-09-03 由 传递 创建
- '
- If Selection.Start = Selection.End Then
- Selection.MoveLeft Unit:=wdCharacter, Count:=1
- Selection.MoveRight Unit:=wdCharacter, Count:=1
-
- Ch = Left(Selection.Text, 1)
- isDigit = (Asc(Ch) >= Asc("0") And Asc(Ch) <= Asc("9")) Or Ch = "," Or Ch = "."
-
- If isDigit Then
- Do
- isDigit = False
- If Selection.MoveRight = 1 Then
- Ch = Left(Selection.Text, 1)
- isDigit = (Asc(Ch) >= Asc("0") And Asc(Ch) <= Asc("9")) Or Ch = "," Or Ch = "."
- End If
- Loop While isDigit
-
- Do While Left(Selection.Text, 1) = "," Or Left(Selection.Text, 1) = "."
- Selection.MoveLeft
- Loop
- End If
-
- icount = Selection.End
-
- Do
- isDigit = False
- If Selection.MoveLeft = 1 Then
- Ch = Left(Selection.Text, 1)
- isDigit = (Asc(Ch) >= Asc("0") And Asc(Ch) <= Asc("9")) Or Ch = "," Or Ch = "."
- End If
- Loop While isDigit
-
- If Selection.Start > 0 Then Selection.MoveRight
- Do While Left(Selection.Text, 1) = "," Or Left(Selection.Text, 1) = "."
- Selection.MoveRight
- Loop
-
- Selection.End = icount
- End If
- If Selection.Start < Selection.End Then
- tmpstr = Format(Selection.Text, "")
- tmpnum = Val(tmpstr)
- If Abs(tmpstr - Format(tmpnum, "")) < 0.00000000001 Then Selection.Text = Format(tmpnum / 10000, "#,##0.00")
- 'Selection.Start = Selection.End
- End If
- End Sub
百度上的代码如下:- Sub CurrencyNumber()
- Dim i As Range, Acell As Cell, CR As Range
- On Error Resume Next
- Application.ScreenUpdating = False
- If Selection.Type = 2 Then
- For Each i In Selection.Words
- If i Like "####*" = True Then
- If i.Next Like "." = True And i.Next(wdWord, 2) Like "#*" = True Then
- i.SetRange Start:=i.Start, End:=i.Next(wdWord, 2).End
- i = Format(i, "Standard")
- Else
- i = Format(i, "Standard")
- End If
- End If
- Next i
- ElseIf Selection.Type = 5 Then
- For Each Acell In Selection.Cells
- Set CR = ActiveDocument.Range(Acell.Range.Start, Acell.Range.End - 1)
- MsgBox CR
- If CR Like "####*" = True Then
- If CR Like "####.#*" = True Then
- Yn = Format(CR, "Standard")
- CR.Text = Nn
- Else
- Yn = Format(CR, "Standard")
- CR.Text = Nn
- End If
- End If
- Next Acell
- Else
- MsgBox "您只能选定文本或者表格之一!", vbOK + vbInformation
- End If
- Application.ScreenUpdating = True
- End Sub
保存---运行(F5)--关闭 进入word2007---顶端的自定义工具栏---其他命令--从下列位置选择命令-宏--CurrencyNumber--添加至右边--修改(自己改个图标)及名称--确定 就完成了。
查找替换法 步骤一按<Ctrl+Home>组合键 步骤二按<Ctrl+H>组合键,打开"查找与替换",进行下图的设置
步骤四单击"全部替换"
如果有只个别需要设置,可以通过域进行设置,方法: 按<ctrl+f9>组合键,输入一对域括号,输入"=数字 \#"#,##0.00", 再按一下<F9>即可
|