Determine whether user is adding or deleting rows

前端 未结 7 2221

I have a VBA macro that validates user entered data (I didn\'t use data validation/conditional formatting on purpose).

I am using Worksheet_Change event

7条回答
  •  無奈伤痛
    2020-11-29 10:29

    You could define a range name such as RowMarker =$A$1000

    Then this code on your change event will store the position of this marker against it's prior position, and report any change (then stores the new position)

    Private Sub Worksheet_Change(ByVal Target As Range)
        Static lngRow As Long
        Dim rng1 As Range
        Set rng1 = ThisWorkbook.Names("RowMarker").RefersToRange
        If lngRow = 0 Then
        lngRow = rng1.Row
            Exit Sub
        End If
        If rng1.Row = lngRow Then Exit Sub
        If rng1.Row < lngRow Then
            MsgBox lngRow - rng1.Row & " rows removed"
        Else
            MsgBox rng1.Row - lngRow & " rows added"
        End If
        lngRow = rng1.Row
    End Sub
    

提交回复
热议问题