Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

vba - MS Word table -macro to find row containing specific text then move entire row to last row in the table

Looking to create a macro that will search down MS Word table, find a specific word, then move that entire row to the bottom of the table, then repeat for the next occurrence of that word.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The specs you've given @ vbaexpress are somewhat different to what you gave here. Assuming the vbaexpress specs are correct, try:

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

Note: the above code assumes you're processing just the first table in the document; if it's a different table, change the 1 in .Tables(1) to suit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...