vba 使用VB创建宏以删除Excel中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3883458/
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 12:10:05  来源:igfitidea点击:

Creating a macro to delete rows in Excel using VB

excel-vbavbaexcel

提问by Edmond

I need a code that will delete the enitre row when a specific name is typed into column A. So, for each row that has "Surgery" in column A, it needs to be deleted. Thanks.

我需要一个代码,当在 A 列中输入特定名称时,该代码将删除整个行。因此,对于 A 列中包含“Surgery”的每一行,都需要将其删除。谢谢。

采纳答案by Michael

This should work. All you need to do is change the value of areaToSearch to fit your workbook. Also watch the case on the keyword, "Surgery" and "surgery" are not the same! I tested this and it worked on a sheet I made up.

这应该有效。您需要做的就是更改 areaToSearch 的值以适合您的工作簿。还要看关键词上的案例,“手术”和“手术”不一样!我测试了这个,它在我制作的一张纸上工作。

Option Explicit

Sub DeleteSurgery()

    Dim keyWord As String
    Dim cell As Range, areaToSearch As Range

    keyWord = "Surgery"
    Set areaToSearch = Sheet1.Range("A1:A10")

    For Each cell In areaToSearch
        If cell.Value = keyWord Then
            cell.EntireRow.Delete
        End If
    Next cell

End Sub