如何在删除行时触发事件?

时间:2015-07-27 20:01:16

标签: excel vba excel-vba

由于某些原因,我使用的代码似乎不起作用。

Private Sub Worksheet_Change2(ByVal Target As Range)
If Target.Delete = Target.EntireRow.Delete Then
Range("BP9").Activate
ActiveCell.FormulaR1C1 = "=IF(RC[-65]='Trip Pad'!R1C2,1,0)"
Selection.AutoFill Destination:=Range("BP9:BP1071")
Range("BP9:BP1071").Select
Range("BQ9").Select
ActiveCell.FormulaR1C1 = "=RC[-1]+R[-1]C"
Selection.AutoFill Destination:=Range("BQ9:BQ1625")
Range("BQ9:BQ1625").Select
Range("BR9").Select
ActiveCell.FormulaR1C1 = "=RC[-2]*RC[-1]"
Range("BR9").Select
Selection.AutoFill Destination:=Range("BR9:BR118")
Range("BR9:BR118").Select
End If
End Sub

1 个答案:

答案 0 :(得分:0)

你可以这样做。每次更改工作表时,请查看行数。

Public lRowCount as Long

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws as excel.worksheet
Set ws = Application.ActiveSheet

if ws.UsedRange.Rows.Count < lRowCount then
    'We have less rows than the last time the worksheet changed.
    'Do something here
end if

'Record the number of rows in the usedrange for checking next time
lRowCount = ws.UsedRange.Rows.Count

End sub

并在工作簿上设置lRowCount

Private Sub Workbook_Open()
    lRowCount = ActiveSheet.UsedRange.Rows.Count
End Sub