vba 根据包含在带有宏的行中的值删除整行?

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

Delete entire rows based on a value contained in the row with macro?

excelvbadelete-row

提问by Blazerunner1

I have a spreadsheet which contains a whole bunch of text and number values. I would like to have a macro search the sheet, select the rows containing the word "Total" and delete the entire row. I have been searching for hours and have not found any code that I can adapt to perform this task. I have a little experience with VBA but am out of practice and patience. The word is not case sensitive and may be contained in a string of text, for example: "Total for 12345 Jane Doe".

我有一个包含一大堆文本和数字值的电子表格。我想用宏搜索工作表,选择包含“总计”一词的行并删除整行。我已经搜索了几个小时,但没有找到任何可以适应执行此任务的代码。我对 VBA 有一点经验,但缺乏实践和耐心。该词不区分大小写,可能包含在文本字符串中,例如:“Total for 12345 Jane Doe”。

回答by sabhareesh

You can try this instead:

你可以试试这个:

Sub testing()
Dim pattern As String
pattern = "Total"
RowCount = ActiveSheet.UsedRange.Rows.Count
Dim i As Integer

For i = 1 To RowCount
    Dim j As Integer
    For j = 1 To 1
        If Cells(i, j) = pattern Then
           Cells(i, j).EntireRow.Delete
        End If
    Next j
Next i
End Sub

This may have more lines of code,but this will be helpful to explain your concept.

这可能有更多的代码行,但这将有助于解释您的概念。

To insert it do the following in excel:

要插入它,请在 excel 中执行以下操作:

  1. press Alt+F11
  2. Rightclick Sheet1
  3. ->Insert->Module and paste this code.
  1. 按 Alt+F11
  2. 右键单击 Sheet1
  3. ->插入->模块并粘贴此代码。

回答by Kevin McDowell

If you select the entire range, the following macro should delete every row that has a cell that contains "total", regardless of case.

如果您选择整个范围,以下宏应删除包含“总计”单元格的每一行,无论大小写。

Sub Test()
Dim cell As Range

For Each cell In Selection
    If InStr(1, cell, "total", vbTextCompare) > 0 Then
        cell.EntireRow.Delete
    End If
Next
End Sub

To insert it, do the following in Excel:

要插入它,请在 Excel 中执行以下操作:

  1. Press Alt+F11
  2. Click Insert>Module
  3. Copy and paste the above macro into the new module.
  1. 按 Alt+F11
  2. 单击插入>模块
  3. 将上述宏复制并粘贴到新模块中。