Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 选择并删除文本的代码_Vba_Excel_Selection - Fatal编程技术网

Vba 选择并删除文本的代码

Vba 选择并删除文本的代码,vba,excel,selection,Vba,Excel,Selection,我试图创建一个宏来查找特定表达式并删除该内容。 例如,我需要删除从“通知”到“早上好”的所有内容(但保留“早上好,如果可能的话”) 我有一个删除一行的代码,但无法找出如何使用选择来执行,因为我每次的行数不同。可以是3行,也可以是9行,也可以是更多或更少 我的代码是这样的(我删除了代码中与此问题无关的部分): 有人能告诉我吗?这正是您想要的(据我所知)。只需将此函数粘贴到word文档的一个代码对象中并运行它(已在word上试用过,而不是Excel)。它主要是向您展示如何处理此类问题 Sub Lin

我试图创建一个宏来查找特定表达式并删除该内容。 例如,我需要删除从“通知”到“早上好”的所有内容(但保留“早上好,如果可能的话”)

我有一个删除一行的代码,但无法找出如何使用选择来执行,因为我每次的行数不同。可以是3行,也可以是9行,也可以是更多或更少

我的代码是这样的(我删除了代码中与此问题无关的部分):

有人能告诉我吗?

这正是您想要的(据我所知)。只需将此函数粘贴到word文档的一个代码对象中并运行它(已在word上试用过,而不是Excel)。它主要是向您展示如何处理此类问题

Sub LineRemover()

    Dim doc As Document
    Set doc = ActiveDocument

    Dim myParagraph As Paragraph
    Dim mySentences As Sentences
    Dim mySentence As Range

    Dim deleted As Boolean
    deleted = False

    For Each myParagraph In doc.Paragraphs

        Set mySentences = myParagraph.Range.Sentences
        For Each mySentence In mySentences

            If InStr(mySentence, "Good morning") <> 0 Then
                    '"Good morning" is present inside this line; exit the loop!

                    Exit For

            ElseIf deleted Then
                    '"NOTIFICATION" was already deleted, need to remove all subsequent lines!

                mySentence.Delete

            ElseIf InStr(mySentence, "NOTIFICATION") <> 0 Then
                    '"NOTIFICATION" is present inside this line; delete it!

                mySentence.Delete
                deleted = True 'Tell the program you've deleted it!

            End If

        Next

        If deleted Then
            Exit For
        End If

    Next

End Sub
Sub-LineRemover()
将文档变为文档
Set doc=ActiveDocument
将我的段落改为段落
模糊的神秘感如句子
模糊的神秘感如射程
作为布尔值删除
已删除=错误
对于文件段落中的每个段落
设置mycentenses=myparagration.Range.句子
为每一个我的名字在我的名字里
如果InStr(mycentence,“早上好”)0那么
“早上好”出现在该行中;退出循环!
退出
艾尔塞夫当时被删除了
““通知”已被删除,需要删除所有后续行!
删除
ElseIf InStr(mycentence,“通知”)0然后
““通知”在此行中存在;请删除它!
删除
deleted=True'告诉程序您已将其删除!
如果结束
下一个
如果删除,则
退出
如果结束
下一个
端接头
额外详细信息:
InStr(String1,String2)
将返回在
String1
中找到
String2
的位置

Sub LineRemover()

    Dim doc As Document
    Set doc = ActiveDocument

    Dim myParagraph As Paragraph
    Dim mySentences As Sentences
    Dim mySentence As Range

    Dim deleted As Boolean
    deleted = False

    For Each myParagraph In doc.Paragraphs

        Set mySentences = myParagraph.Range.Sentences
        For Each mySentence In mySentences

            If InStr(mySentence, "Good morning") <> 0 Then
                    '"Good morning" is present inside this line; exit the loop!

                    Exit For

            ElseIf deleted Then
                    '"NOTIFICATION" was already deleted, need to remove all subsequent lines!

                mySentence.Delete

            ElseIf InStr(mySentence, "NOTIFICATION") <> 0 Then
                    '"NOTIFICATION" is present inside this line; delete it!

                mySentence.Delete
                deleted = True 'Tell the program you've deleted it!

            End If

        Next

        If deleted Then
            Exit For
        End If

    Next

End Sub