使用 VBA 替换 Microsoft Word 中 table 中多个单元格中的文本
Replace text in multiple cells in a table on Microsoft Word using VBA
我在 Microsoft Word 中有一个很大的 table,我想突出显示最后一列中的选定单元格并将其中的文本从“当前”更改为“已更改”。我知道您可以只使用复制和粘贴或使用 find/replace,但我一天要这样做多次,因此只需突出显示并单击按钮就非常有用。
我在 Excel 中有一些 VBA 在电子表格上工作正常但在 Word 中似乎不工作。
Sub WordEdit()
Dim x As Range
For Each x In Selection
If x.Value <> "" Then x.Value = "Changed"
Next
End Sub
我不是 VBA 方面的专家,因此非常感谢您的帮助。谢谢
以下会将所选内容中的“当前”一词更改为“已更改”。请注意,对于此宏,您必须将单词作为选择的一部分。它不需要在最后一列,也不需要在 table 中;它确实需要在选择中。
Sub ChangeSelectionCurrentToChanged()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
'
Set rng = Selection.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Set rng = Nothing
End Sub
如果文档中 any table 的最后一列中的“当前”一词,以下宏会将其更改为“已更改”。选择什么无所谓。
Sub ChangeCurrentToChanged()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
Dim iColumns As Long
Dim iRows As Long
Dim iCount As Long
Dim oTable As Table
'
For Each oTable In ActiveDocument.Tables
Let iColumns = oTable.Columns.Count
Let iRows = oTable.Rows.Count
For iCount = 1 To iRows
Set rng = oTable.Cell(iCount, iColumns).Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Next iCount
Next oTable
Set oTable = Nothing
Set rng = nothing
End Sub
以下宏将当前table(插入点所在的那一列)的最后一列中的“当前”更改为“已更改”。
Sub ChangeCurrentToChangedSelectedTable()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
Dim iColumns As Long
Dim iRows As Long
Dim iCount As Long
Dim oTable As Table
'
Set oTable = Selection.Tables(1)
Let iColumns = oTable.Columns.Count
Let iRows = oTable.Rows.Count
For iCount = 1 To iRows
Set rng = oTable.Cell(iCount, iColumns).Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Next iCount
Set oTable = Nothing
Set rng = Nothing
End Sub
其中之一能满足您的需求吗?
我在 Microsoft Word 中有一个很大的 table,我想突出显示最后一列中的选定单元格并将其中的文本从“当前”更改为“已更改”。我知道您可以只使用复制和粘贴或使用 find/replace,但我一天要这样做多次,因此只需突出显示并单击按钮就非常有用。
我在 Excel 中有一些 VBA 在电子表格上工作正常但在 Word 中似乎不工作。
Sub WordEdit()
Dim x As Range
For Each x In Selection
If x.Value <> "" Then x.Value = "Changed"
Next
End Sub
我不是 VBA 方面的专家,因此非常感谢您的帮助。谢谢
以下会将所选内容中的“当前”一词更改为“已更改”。请注意,对于此宏,您必须将单词作为选择的一部分。它不需要在最后一列,也不需要在 table 中;它确实需要在选择中。
Sub ChangeSelectionCurrentToChanged()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
'
Set rng = Selection.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Set rng = Nothing
End Sub
如果文档中 any table 的最后一列中的“当前”一词,以下宏会将其更改为“已更改”。选择什么无所谓。
Sub ChangeCurrentToChanged()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
Dim iColumns As Long
Dim iRows As Long
Dim iCount As Long
Dim oTable As Table
'
For Each oTable In ActiveDocument.Tables
Let iColumns = oTable.Columns.Count
Let iRows = oTable.Rows.Count
For iCount = 1 To iRows
Set rng = oTable.Cell(iCount, iColumns).Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Next iCount
Next oTable
Set oTable = Nothing
Set rng = nothing
End Sub
以下宏将当前table(插入点所在的那一列)的最后一列中的“当前”更改为“已更改”。
Sub ChangeCurrentToChangedSelectedTable()
' Charles Kenyon 13 Oct 2020
Dim rng As Range
Dim iColumns As Long
Dim iRows As Long
Dim iCount As Long
Dim oTable As Table
'
Set oTable = Selection.Tables(1)
Let iColumns = oTable.Columns.Count
Let iRows = oTable.Rows.Count
For iCount = 1 To iRows
Set rng = oTable.Cell(iCount, iColumns).Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Current"
.Replacement.Text = "Changed"
.Execute Replace:=wdReplaceAll
End With
Next iCount
Set oTable = Nothing
Set rng = Nothing
End Sub
其中之一能满足您的需求吗?