是否有工作表事件“BeforeRowDelete”?只有“工作表更改”事件可用,但范围对象“目标”指向我删除的行的下一行。
有没有办法让行在实际发生之前被删除?
你可以使用我刚刚拼凑的类似的东西。在 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