Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2:B4")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
If Application.Count(Range("B2:B4")) = 2 Then
If IsEmpty(Range("B2")) Then
Range("B2") = Range("B3").Value2 * Range("B4").Value2
ElseIf IsEmpty(Range("B3")) Then
Range("B3") = Range("B2").Value2 / Range("B4").Value2
Else
Range("B4") = Range("B2").Value2 / Range("B3").Value2
End If
Range("B2").NumberFormat = "0 \N"
Range("B3").NumberFormat = "0.0 \k\g"
Range("B4").NumberFormat = "0 \m\/\s\²"
End If
End If
safe_exit:
Application.EnableEvents = True
End Sub
2 回答
将其放在工作表的私有代码表中(右键单击工作表名称选项卡,查看代码) .
您可以使用if()语句执行此操作:
没有进行任何错误检查,例如检查任何两个单元格中的数字等...