【发布时间】:2019-01-31 05:20:25
【问题描述】:
我不熟悉 Word VBA,并且在使用 Selection.Find 时遇到了问题。
我想用小数点替换表格中一系列单元格中的逗号。但是,我只能让 Selection.Find 替换文档中的所有逗号或仅替换范围中的第一个逗号。
我想要的是 Excel 中的 Selection.Replace What:=".", Replacement:=",",但 Word 不支持。
非常感谢您的建议!
CJ
Sub Replace_Percent_Separator()
'Correct percent separator in row 6, table 2
Dim PcentCells As Range
Path = "C:\xxx\Word\"
file = Dir(Path & "*.docx")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Do While file <> ""
Documents.Open Filename:=Path & file
With ActiveDocument
Set PcentCells = .Range(Start:=.Tables(2).Cell(6, 2).Range.Start, _
End:=.Tables(2).Cell(6, 10).Range.End)
PcentCells.Select
With Selection.Find
.Text = ","
.Replacement.Text = "."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll '<-- replaces throughout document
'.Execute Replace:=wdReplaceOne '<-- replaces in first cell but no other cells
End With
End With
ActiveDocument.Save
ActiveDocument.Close
file = Dir()
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
看看这个答案中的解释是否有帮助:stackoverflow.com/questions/51925805/…
-
谢谢!我已经选择了 Macropod 的
.Wrap = wdFindStop解决方案。