简体   繁体   English

如何自动删除Excel中包含“总计”的所有行?

[英]How to delete all row which contains “Grand Total” in Excel automatically?

In my active sheet called Report I have 2 column I & F. 在名为“报告”的活动工作表中,我有2列I&F。

It has repeating cells containing text "Grand Total" . 它具有包含文本“总计”的重复单元格。

I would like to delete whole row if it contains Grand Total automatically. 如果它自动包含总计,我想删除整行。

VBA code would be nice. VBA代码会很好。

在此处输入图片说明

With the following VBA code, you can quickly delete the rows with certain cell value, please do as the following steps: 使用以下VBA代码,您可以快速删除具有某些单元格值的行,请执行以下步骤:

  1. Select the range that you want to delete the specific row. 选择要删除特定行的范围。

  2. Click Developer>Visual Basic, a new Microsoft Visual Basic for applications window will be displayed, click Insert > Module, and input the following code into the Module: 单击“开发人员”>“ Visual Basic”,将显示一个新的Microsoft Visual Basic for Applications窗口,单击“插入”>“模块”,然后在“模块”中输入以下代码:

    VBA code to Remove entire rows based on cell value(ie Grand Total): VBA代码根据单元格值(即总计)删除整个行:

     Sub Delete_Rows() Dim rng As Range, cell As Range, del As Range Set rng = Intersect(Range("A1:D22"), ActiveSheet.UsedRange) For Each cell In rng If (cell.Value) = "Grand Total" _ Then If del Is Nothing Then Set del = cell Else: Set del = Union(del, cell) End If End If Next cell On Error Resume Next del.EntireRow.Delete End Sub 
  3. Then click "Play/Run" button to run the code, and the rows which have certain value have been removed. 然后单击“播放/运行”按钮运行代码,具有特定值的行已被删除。

(Note: If you can't see Developer Tab in Excel Do these Steps: 1)Click the Office Button, and then click Excel Options. (注意:如果在Excel中看不到“开发人员”选项卡,请执行以下步骤:1)单击“ Office按钮”,然后单击“ Excel选项”。 2)In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check box, and then click OK.) 2)在“热门”类别的“使用Excel的主要选项”下,选择“功能区”中的“显示开发人员”选项卡,然后单击“确定”。)

Using AutoFilter is very efficient. 使用自动AutoFilter非常有效。 This can also be done without VBA (ie manually) 也可以在没有VBA情况下完成(即手动)

Main Sub 主子

Sub Main()
Dim ws As Worksheet
Dim rng1 As Range
Dim StrIn As String
Dim rng2  As Range
Set ws = Sheets("Sheet1")
Application.ScreenUpdating = False
Set rng1 = ws.Range("F:F,I:I")
StrIn = "Grand Total"
For Each rng2 In rng1.Columns
Call FilterCull(rng2, StrIn)
Next
Application.ScreenUpdating = True
End Sub

Delete Sub 删除子

Sub FilterCull(ByVal rng2, ByVal StrIn)
With rng2
    .Parent.AutoFilterMode = False
    .AutoFilter Field:=1, Criteria1:=StrIn
   .EntireRow.Delete
    .Parent.AutoFilterMode = False
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM