注册 登录
编程论坛 Excel/VBA论坛

求优化千分位

lxk1732942 发布于 2020-03-03 14:12, 1936 次点击
这个宏是给数字加千分位并补足小数点后两位,但是3位数一下的数不会被处理
希望大佬改一下,使之也可以给小于3位数的数字补足小数点后两位
程序代码:
Sub 加千位符()
 '加千位符
      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

1 回复
#2
神经蛙902020-03-20 22:32
我也是新手,我觉得可以逐步调试一下程序。
1