是否存在工作表事件“BeforeRowDelete”?

时间:2013-08-07 18:41:40

标签: vba events

是否有工作表事件" BeforeRowDelete"? 只有"工作表更改"事件可用,但范围对象"目标"指向我删除的行的下一行。

是否有任何想法让行在实际发生之前被删除?

1 个答案:

答案 0 :(得分:0)

你可以使用我刚刚敲了一下这样的东西。在A列的最后一行输入RowID,如果删除移动此单元格的行/单元格,它将报告最后选择的范围和范围之前的行。

   Option Explicit

    Dim deletedRng() As Variant

    Private Sub Worksheet_Change(ByVal Target As Range)
    '***Enter string "RowID" in cell [A1048547]

    Dim rowID As Long
    Dim rowFind As Range

        With Target.Parent

            Set rowFind = .Range("A:A").Find(What:="RowID", LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

            Application.EnableEvents = False

            If rowFind Is Nothing Then
                .Range("A" & .Rows.Count).Value = "RowID"
            Else
                If rowFind.Row < .Rows.Count Then
                    MsgBox "Cell (1,1) of range deleted = " & deletedRng(Target.Cells(1, 1).Row, Target.Cells(1, 1).Column)
                    rowFind.ClearContents
                    .Range("A" & .Rows.Count).Value = "RowID"
                End If
            End If
            Application.EnableEvents = True

        End With
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

     If Target.Address = "$A$1" Then
        ReDim deletedRng(1, 1)
        deletedRng(1, 1) = Target.Value
     Else
        deletedRng = Target.Parent.Range("A1:" & Target.Address).Value
     End If
    End Sub