亲,是这个意思么?
打开你的Excel文件,按“Alt+F11”打开VBA编辑窗口,然后在左侧对应的Sheet上双击,右侧空白处粘贴下面的代码。关闭VBA窗口。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Selection.Count = 1 And (Target.Column = 1 Or Target.Column = 2) Then
Application.EnableEvents = False
If IsNumeric(Range("A" & Target.Row)) And IsNumeric(Range("B" & Target.Row)) And _
Range("A" & Target.Row) <> "" And Range("B" & Target.Row).Value <> "" Then _
Range("D" & Target.Row).Value = Range("B" & Target.Row).Value - Range("A" & Target.Row) _
.Value Else Range("D" & Target.Row).Value = ""
Application.EnableEvents = True
End If
End Sub