|
之前写的功能类似的自触发
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
Application.EnableEvents = False
Static lastValue As Double
Static lastRecordedValue As Double
Dim inputValue As Variant
inputValue = Range("A1").Value
If inputValue = "reset" Then
Range("A1").Value = 0
lastValue = 0
lastRecordedValue = 0
Range("B:B").Clear
ElseIf inputValue = "return" Then
Dim lastRow As Long
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
If lastRow > 0 Then
Range("A1").Value = lastValue - lastRecordedValue
lastValue = lastValue - lastRecordedValue
Cells(lastRow, 2).Clear
End If
ElseIf IsNumeric(inputValue) Then
Dim currentValue As Double
currentValue = inputValue
Range("A1").Value = currentValue + lastValue
lastValue = Range("A1").Value
lastRecordedValue = currentValue
Dim nextRow As Long
nextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1
If nextRow = 2 And Cells(1, 2).Value = "" Then
nextRow = 1
End If
Cells(nextRow, 2).Value = currentValue
Cells(nextRow, 2).Font.Color = RGB(255, 0, 0)
Else
MsgBox "输入内容必须是数字型!", vbExclamation
Range("A1").Value = lastValue
End If
Application.EnableEvents = True
End If
End Sub |
|