像这样的东西怎么样,它在 VBA 中,但应该很容易翻译成 C#
Option Explicit
' We are monitoring cell B2...
Private initialB2Value As Variant ' holds the value for reinstatement when the user changes it
Private Sub Worksheet_Activate()
' record the value before the user makes any changes.
' Could be a constant value, or you could use .Formula to ensure a calculation is not lost
initialB2Value = Range("B2").Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Static alreadyChanging As Boolean
' when we reset the cell, Worksheet_Change will fire again, so we'll use a flag
' to tell us if we should care or not...
If alreadyChanging Then ' change is because of this code, no need to process
alreadyChanging = False
Exit Sub
End If
If IsEmpty(Intersect(Target, Range("B2"))) Then
' If the change is not happening to the range we are monitoring, ignore it
Exit Sub
End If
' Do something with the user's input here
Debug.Print "User input " & Range("B2").Value & " into B2"
' before we reset the value, flag that we are changing the value in code
alreadyChanging = True
' now apply the old value
Range("B2").Value = initialB2Value
End Sub