我以双向方式链接多个单元格,这意味着如果我更改一个单元格,另一个将更新,反之亦然。 此代码适用于两个单元格
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$4" Then
'Application.EnableEvents = False
Sht_input.Range("E4").Value = Sht_input.Range("B4").Value
'Application.EnableEvents = True
End If
If Target.Address = "$E$4" Then
'Application.EnableEvents = False
Sht_input.Range("b4").Value = Sht_input.Range("e4").Value
'Application.EnableEvents = True
End If
End Sub
现在,当我尝试链接三个单元格时,代码进入不定式循环
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$4" Then
'Application.EnableEvents = False
Sht_input.Range("E4").Value = Sht_input.Range("B4").Value
Sht_input.Range("h4").Value = Sht_input.Range("B4").Value
'Application.EnableEvents = True
End If
If Target.Address = "$E$4" Then
'Application.EnableEvents = False
Sht_input.Range("b4").Value = Sht_input.Range("e4").Value
Sht_input.Range("h4").Value = Sht_input.Range("e4").Value
'Application.EnableEvents = True
End If
If Target.Address = "$H$4" Then
'Application.EnableEvents = False
Sht_input.Range("b4").Value = Sht_input.Range("H4").Value
Sht_input.Range("e4").Value = Sht_input.Range("H4").Value
'Application.EnableEvents = True
End If
End Sub
我试图调试,但我无法理解为什么它会回到“if”,即使目标单元格不再被选中。任何人都可以帮助我吗?
答案 0 :(得分:1)
您需要禁用这些事件。否则,它将始终进入无限循环,正在更改单元格值。它是否是相同的值无关紧要。
Application.EnableEvents = False
If Target.Address = "$B$4" Then
Sht_input.Range("E4").Value = Sht_input.Range("B4").Value
Sht_input.Range("h4").Value = Sht_input.Range("B4").Value
End If
If Target.Address = "$E$4" Then
Sht_input.Range("b4").Value = Sht_input.Range("e4").Value
Sht_input.Range("h4").Value = Sht_input.Range("e4").Value
End If
If Target.Address = "$H$4" Then
Sht_input.Range("b4").Value = Sht_input.Range("H4").Value
Sht_input.Range("e4").Value = Sht_input.Range("H4").Value
End If
Application.EnableEvents = True
请注意代码开头和结尾所做的更改。