如何从右向左移动减号?

时间:2019-03-19 10:37:08

标签: ms-word word-vba digits

我需要一个MSWord宏来转换以下这些值:

568.63- 682.3- 12.78-

收件人

-568.63 -682.3 -12.78

1 个答案:

答案 0 :(得分:0)

您实际上不需要此宏。您需要做的是使用通配符查找/替换为:

Find = (<[0-9.]@)(-)
Replace = \2\1

作为宏,它将变为:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(<[0-9.]@)(-)"
    .Replacement.Text = "\2\1"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub

对于@Freeflow标识的80.9-100.1问题,我倾向于用普通的连字符(例如,不间断的连字符)替换那些连字符。在这种情况下,您可以使用如下编码的宏:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Text = "([0-9])-([0-9])"
    .Replacement.Text = "\1^~\2"
    .Execute Replace:=wdReplaceAll
    .Text = "(<[0-9.]@)(-)"
    .Replacement.Text = "\2\1"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub