MS Word表格-宏以查找包含特定文本的行,然后将整行移至表格中的最后一行

时间:2018-08-01 00:19:51

标签: vba ms-word

试图创建一个宏,它将在MS Word表中进行搜索,找到一个特定的词,然后将整行移到表的底部,然后对该词的下一个重复进行重复。

1 个答案:

答案 0 :(得分:0)

@vbaexpress提供的规范与您在此处给出的规范有些不同。假设vbaexpress规格正确,请尝试:

Sub Demo()
Application.ScreenUpdating = False
Dim TblRng As Range, TmpRng As Range
With ActiveDocument.Tables(1)
  Set TblRng = .Range: Set TmpRng = .Range
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "DENIED"
      .Replacement.Text = ""
      .Forward = True
      .Format = False
      .Wrap = wdFindStop
      .MatchCase = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found
      If .InRange(TblRng) Then
        TmpRng.Collapse wdCollapseEnd
        TmpRng.FormattedText = .Rows(1).Range.FormattedText
        .Rows(1).Delete
      End If
      .Find.Execute
    Loop
  End With
  If .Rows.Count > TblRng.Rows.Count Then
    .Split .Rows(TblRng.Rows.Count + 1)
  End If
End With
Application.ScreenUpdating = True
End Sub

注意:以上代码假定您仅在处理文档中的第一个表;如果它是另一个表,请更改.Tables(1)中的1以适合。