vba 在 Word 中,删除不包含某些文本的表格行或将这些行复制到新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6398359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:30:39  来源:igfitidea点击:

in Word, delete table rows that don't contain certain text or copy those rows to new file

vbams-officeword-vba

提问by sethwalt

I will apologize in advance for my limited knowledge of visual basic, but I'm trying to write a macro that will search through a rather large table for a certain string and then either delete the rows that DON'T contain that string or copy all the rows that DO contain it to a new Word document. Just a way to isolate the rows that do contain that string.

我会提前为我对visual basic的知识有限表示歉意,但我正在尝试编写一个宏,该宏将在相当大的表中搜索某个字符串,然后删除不包含该字符串的行或全部复制将包含它的行添加到新的 Word 文档中。只是一种隔离包含该字符串的行的方法。

Thanks!

谢谢!

回答by Ahmad

If you already know how to put the cursor in the top cell of the column you are iterating through, then here is the code you would want to put so that the entire row will be deleted if the cell text is blank.

如果您已经知道如何将光标放在您正在迭代的列的顶部单元格中,那么这里是您想要放置的代码,以便在单元格文本为空时删除整行。

Please make the necessary modification to the code as you see fit for your case

请根据您的情况对代码进行必要的修改

Selection.MoveEnd  'This ensures that you select the content of the cell to read
If InStr(Selection.Text, Chr(7)) Then   'if you select the whole cell it will contain
                                        'character 7 (TAB) so you will also select 
                                        '2 additional invisible characters as well 
    If Trim(Mid(Selection.Text, 1, Len(Selection.Text) - 2)) = "" Then
             Selection.Rows.Delete
    End If
Else
 ' you selected part of the text that does not contain invisible characters 
    If Trim(Mid(Selection.Text, 1, 1)) = "" Then
         Selection.Rows.Delete
    End If
End If

回答by sethwalt

Found a solution from another forum. Thought I'd post here in case it helps someone else:

从另一个论坛找到了解决方案。我想我会在这里发布以防它帮助其他人:

With ThisDocument.Tables(1)
    For r = .Rows.Count To 1 Step -1
        fnd = False
        For Each c In .Rows(r).Cells
            If InStr(c.Range.Text, "x") > 0 Then fnd = True
        Next
        If Not fnd Then .Rows(r).Delete
    Next
End With

where "x" is the text to be searched for.

其中“x”是要搜索的文本。

I also required a macro to search for a certain text string and delete that row, and that can be accomplished by removing the "not" from the above script. The script does take a while to run (on a table of about 250 rows), though, so at first I thought Word had locked, in case whoever runs this script notices similar behavior and gets worried. Also, I was searching for a text string that was all-caps, and the script didn't recognize it unless the search string ("x") was also in caps.

我还需要一个宏来搜索某个文本字符串并删除该行,这可以通过从上述脚本中删除“not”来完成。不过,脚本确实需要一段时间才能运行(在大约 250 行的表上),所以起初我认为 Word 已锁定,以防运行此脚本的人注意到类似的行为并感到担忧。此外,我正在搜索一个全大写的文本字符串,除非搜索字符串(“x”)也大写,否则脚本无法识别它。

Additionally, I've found that this macro doesn't work unless it's copied into the word document where it needs to run. Changing ThisDocument to ActiveDocument allows it to work if it's only in the Normal template.

此外,我发现这个宏不起作用,除非它被复制到需要运行的 word 文档中。如果仅在 Normal 模板中,将 ThisDocument 更改为 ActiveDocument 允许它工作。

Anyway, hope that helps someone!

无论如何,希望对某人有所帮助!