【发布时间】:2021-04-11 23:36:06
【问题描述】:
我正在为 WORD 使用 VBA 宏,以便将所有“[”字符替换为“”。 该宏工作正常可以用某种颜色替换所有表格中的所有字符。
我如何添加 if 语句以使其仅在 A 列包含“测试”时才起作用
之前:
| Header 1 | [Text |
|---|---|
| Test: | [Text |
之后:
| Header 1 | [Text |
|---|---|
| Test: | Text |
到目前为止,我得到了这个并且它工作正常(但对于所有表行,而不是特定于“测试”行。
Sub FindChar2()
Dim oTbl As Table
Dim stT As Long, enT As Long
Dim stS As Long, enS As Long
With Selection.Find ' Replacement
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
For Each oTbl In ActiveDocument.Tables
If oTbl.Shading.BackgroundPatternColor = RGB(176, 255, 137) Then
oTbl.Columns(1).Select
Do While Selection.Find.Execute
stT = oTbl.Range.Start ' table range
enT = oTbl.Range.End
stS = Selection.Range.Start ' found text range
enS = Selection.Range.End
If stS < stT Or enS > enT Then Exit Do
Selection.Collapse wdCollapseStart
Selection.Find.Execute Replace:=wdReplaceOne
Loop
Selection.Collapse wdCollapseEnd
End If
Next
End Sub
【问题讨论】: