[英]Change columns value based on another cell
I want to update 2 columns values based on another columns value (if value change).我想根据另一个列值更新 2 列值(如果值更改)。 Suppose I have column A with a list (AA1, AA2, AA3), column B with a list (BB1, BB2), column C with a list (CC1, CC2).假设我的 A 列有一个列表(AA1、AA2、AA3),B 列有一个列表(BB1、BB2),C 有一个列表(CC1、CC2)。 If a choose a value "AA1" from column A then Column B value should change to BB2 et column C to CC1.如果从 A 列中选择值“AA1”,则 B 列值应更改为 BB2 和列 C 到 CC1。 But nothing should happen if the value chosen in column A is different from "AA1".但是,如果 A 列中选择的值与“AA1”不同,则不会发生任何事情。 The same process occurs also for value "BB1" in column B. I added a vba but it not working. B列中的值“BB1”也会发生相同的过程。我添加了一个vba,但它不起作用。 Also is there another way to do it without running a vba code?还有另一种方法可以在不运行 vba 代码的情况下做到这一点吗? Thanks谢谢
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changedCells As Range
Set changedCells = Range("A:C")
If Not Application.Intersect(changedCells, Range(Target.Address)) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 And LCase(Target.Value) = "aa1"Then
Cells(Target.Row, 2) = "BB2"
Cells(Target.Row, 3) = "CC1"
ElseIf Target.Column = 2 And LCase(Target.Value) = "bb1" Then
Cells(Target.Row, 1) = "AA3"
Cells(Target.Row, 3) = "CC2"
ElseIf Target.Column = 3 And LCase(Target.Value) = "cc2" Then
Cells(Target.Row, 1) = "AA2"
Cells(Target.Row, 2) = "BB2"
End If
End If
End Sub
Your code is broadly OK, except it will cause an Event Cascade (changing a cell triggers the Worksheet_Change
event, which changes a cell, which triggers Worksheet_Change
, which...)您的代码大致可以,除了它会导致事件级联(更改单元格会触发Worksheet_Change
事件,该事件会更改单元格,触发Worksheet_Change
,...)
You need to add Application.EnableEvents = False
to prevent this (add ... = True
at the end)您需要添加Application.EnableEvents = False
以防止这种情况(在末尾添加... = True
)
Here's your code refactored to address this, and a few other minor issues这是为解决此问题而重构的代码以及其他一些小问题
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changedCells As Range
On Error GoTo EH '~~ ensure EnableEvents is turned back on if an error occurs
Set changedCells = Me.Range("A:C") '~~ explicitly refer to the correct sheet
If Target.Count > 1 Then Exit Sub '~~ do this first, to speed things up
If Not Application.Intersect(changedCells, Target) Is Nothing Then '~~ Target is already a range
Application.EnableEvents = False '~~ prevent an event cascade
'~~ original If Then Else works fine. But can be simplified
Select Case LCase(Target.Value)
Case "aa1"
If Target.Column = 1 Then
Me.Cells(Target.Row, 2) = "BB2"
Me.Cells(Target.Row, 3) = "CC1"
End If
Case "bb1"
If Target.Column = 2 Then
Me.Cells(Target.Row, 1) = "AA3"
Me.Cells(Target.Row, 3) = "CC2"
End If
Case "cc2"
If Target.Column = 3 Then
Me.Cells(Target.Row, 1) = "AA2"
Me.Cells(Target.Row, 2) = "BB2"
End If
End Select
End If
'~~ Fall through to EnableEvents
EH:
Application.EnableEvents = True '~~ ensure EnableEvents is turned back on
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.