简体   繁体   English

自动删除 Excel 中的特定行

[英]Automatically delete specific rows in Excel

I'm trying to make it so that any row in my spreadsheet would automatically disappear when the value in one of the columns is changed from to "No" to "Yes" (drop-down list in that column, only two options to choose from: "Yes" and "No").我正在尝试使电子表格中的任何行在其中一列中的值从“否”更改为“是”时自动消失(该列中的下拉列表,只有两个选项可供选择来自:“是”和“否”)。

Example of what I'm trying to achieve:我正在努力实现的示例:

  • Cell L6 = "No"单元格 L6 =“否”
  • I'm changing the value in L6 from "No" to "Yes" using the drop-down list我正在使用下拉列表将 L6 中的值从“否”更改为“是”
  • Row 6 is automatically deleted with no further steps第 6 行会自动删除,无需进一步操作

I'm under the impression this could be achieved with a macro (VBA code?) rather than using a formula, but my knowledge in macro is completely nonexistent - never used that before.我的印象是这可以通过宏(VBA 代码?)而不是使用公式来实现,但我对宏的了解完全不存在 - 以前从未使用过。 I've tried using codes or bits of codes found online on similar questions, but nothing seems to be working.我已经尝试使用在类似问题上在线找到的代码或代码位,但似乎没有任何效果。 I've tried amending them to match my spreadsheet and the results I'm trying to obtain but it always ends in an error (and having never coded before, my attempts to fix them are unsuccessful since I don't understand them).我尝试修改它们以匹配我的电子表格和我试图获得的结果,但它总是以错误结尾(并且以前从未编码,我尝试修复它们是不成功的,因为我不理解它们)。

I'm willing to learn new things though, so any help is welcome!我愿意学习新事物,所以欢迎任何帮助!

Thanks in advance.提前致谢。

In your code for the worksheet, you want something like the following:在您的工作表代码中,您需要以下内容:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Cell As Range
    'Make a list to store the rows to be deleted
    ReDim RowsToDelete(0) As Integer
    'Dummy item in the list prevents errors elsewhere in the code
    RowsToDelete(0) = 0

    'Check if any of the updated cells are in column L
    If Not Intersect(Target, Me.Range("L:L")) Is Nothing Then
        For Each Cell In Intersect(Target, Me.Range("L:L"))
           'If they are, for each such cell, see if the new value is "Yes"
            If Cell.Value = "Yes" Then
                'If it is, make our list one element longer
                ReDim Preserve RowsToDelete(UBound(RowsToDelete) + 1)
                'And add the row number as the last item
                RowsToDelete(UBound(RowsToDelete)) = Cell.Row
            End If
        Next Cell
    End If

    Dim intRow as Integer

    'Check if the list contains any row numbers for deletion
    If UBound(RowsToDelete) > 0 Then
        'Step through the rows from the last to the first
        'This prevents row numbers of rows still to be deleted changing
        For intRow = UBound(RowsToDelete) To 1 Step -1 'Loop excludes the 0th item as that's our dummy item
            Application.EnableEvents = False
            Me.Rows(RowsToDelete(intRow)).Delete Shift:=xlUp
            Application.EnableEvents = True
        Next intRow
    End If

End Sub

The Application.EnableEvents parts turn off (and back on) the firing of events. Application.EnableEvents部分关闭(并重新打开)事件的触发。 Without that, you'll find that each row deletion causes a further Worksheet Change event to be fired which creates looping weirdness.没有它,您会发现每行删除都会导致进一步的 Worksheet Change 事件被触发,从而产生循环怪异。

I've tweaked the code to make it more robust when updating multiple rows at the same time.我已经调整了代码,使其在同时更新多行时更加健壮。 Probably the same thing can be done more efficiently in other ways.也许同样的事情可以通过其他方式更有效地完成。

Given that you're changing the value of the cells in Col L by using a dropdown, you could probably get away with relatively simple code - you just need to know where to place it.鉴于您正在使用下拉菜单更改 Col L中单元格的值,您可能会使用相对简单的代码 - 您只需要知道将其放置在哪里。 See Here for an explanation as to where to place the suggested code.有关放置建议代码的位置的说明,请参见此处

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("L:L"), Target) Is Nothing Then
    If Target.Text = "Yes" Then
        Target.EntireRow.Delete
    End If
End If
End Sub

Note that @Steve Lovell's answer is better if there's a chance of you changing multiple cells at the same time.请注意,如果您有机会同时更改多个单元格,@Steve Lovell 的答案会更好。

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

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