我有以下宏。它将x.x形式的数字更改为x,x。它被记录下来,我添加了IF语句以确保选择了文本,以防止用户在整个文档中使用它。
Sub fixComma()
'
' fixComma Macro
'
'
If (Selection.Start <> Selection.End) Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.LanguageID = wdEnglishUS
With Selection.Find
.Text = "([0-9]).([0-9])"
.Replacement.Text = "\1,\2"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Else
MsgBox "Nothing is selected, Macro terminated"
End If
End Sub
问题在于它正在更改整个文档,而不仅仅是更改选择。
最佳答案
改变中
Selection.Find.Execute Replace:=wdReplaceAll
至
Selection.Find.Execute Replace:=wdReplaceOne
将得到它,因此选择中x.x的第一个实例将更改为x,x而不是整个文档。
编辑:如果只想更改选定区域中的所有项目,则保持:
Selection.Find.Execute Replace:=wdReplaceAll
但是改变
.Wrap = wdFindAsk
至
.Wrap = wdFindStop
关于vba - 在选择中查找并替换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32007089/