|
自触发
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ARange As Range, DRange As Range
Dim LastA As Long, LastD As Long
Dim ACount As Long, DCount As Long
Dim Cell As Range
Set ARange = Range("A:A")
Set DRange = Range("D:D")
Application.EnableEvents = False
' Check if change is in column A or D
If Not Intersect(Target, ARange) Is Nothing Or Not Intersect(Target, DRange) Is Nothing Then
' Get the last non-empty row in column A and D
LastA = Cells(Rows.Count, "A").End(xlUp).Row
LastD = Cells(Rows.Count, "D").End(xlUp).Row
' Count the number of non-empty cells in column A and D
ACount = Application.WorksheetFunction.CountA(ARange)
DCount = Application.WorksheetFunction.CountA(DRange)
' If change is in column A
If Not Intersect(Target, ARange) Is Nothing Then
For Each Cell In Intersect(Target, ARange)
If Cell.Value <> "" Then
Cell.Offset(0, 1).Value = ACount + DCount
Else
Cell.Offset(0, 1).Value = ""
End If
Next Cell
End If
' If change is in column D
If Not Intersect(Target, DRange) Is Nothing Then
For Each Cell In Intersect(Target, DRange)
If Cell.Value <> "" Then
Cell.Offset(0, 1).Value = ACount + DCount
Else
Cell.Offset(0, 1).Value = ""
End If
Next Cell
End If
End If
Application.EnableEvents = True
End Sub
|
|