|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
之前在EXCEL HOME 中看到的VBA千位分隔符转换,怎样才能实现,如:12456789 转成12,456,789.00 后面的.00,还有WORD 2007除了用VBA 实现,还有其它方法吗?谢谢!
'* +++++++++++++++++++++++++++++
'* Created By SHOUROU@ExcelHome 2008-1-24 17:40:17
'仅测试于System: Windows NT Word: 11.0 Language: 2052
'№ 0314^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------
'显式变量声明
Option Explicit
Sub StandardNumber()
'本代码旨在解决WORD中数据转化为千分位
'数据限定要求:-922,337,203,685,477.5808 到 922,337,203,685,477.5807
'转化结果1000以上数据以千分位计算,小数点右侧保留二位小数;1000以下数据不变
'"2008"不被转换,但如"2008."将被转换为"2,008","2008.0"将被转换为"2,008.0"
Dim myRange As Range, i As Byte, myValue As Currency, myString As String
On Error Resume Next '忽略错误
Application.ScreenUpdating = False '关闭屏幕更新
Selection.HomeKey wdStory
Do
With Selection.Find
.ClearFormatting '清除格式
.Text = "[0-9.]{4,15}" '4到15位数据
.MatchWildcards = True '使用通配符
.Wrap = wdFindStop
.Forward = True
If .Execute = False Then Exit Do
myString = Selection.Text
If VBA.IsNumeric(myString) Then
myValue = VBA.Val(myString)
If Len(myString) > 4 Then
'转为千分位格式
If myValue >= 1000 Then
myString = VBA.Format(myValue, "Standard")
Selection.Text = Replace(myString, ".00", "")
End If
End If
End If
Selection.MoveRight
End With
Loop
'恢复屏幕更新
Application.ScreenUpdating = True
'
End Sub |
|