0

I need an MSWord macro to convert these below values :

568.63- 682.3- 12.78-

To

-568.63 -682.3 -12.78

4

1 回答 1

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
于 2019-03-19T22:52:16.720 回答