Mã VBA cho Sheet1:Gửi Anh/Chị,
Em đang tìm phương án cập nhật DataValidation 2 chiều giữa 2 sheet.
Nhờ Anh/Chị hỗ trợ.
Em cám ơn.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' Ki?m tra n?u ô thay d?i là DataValidation1
If Not Intersect(Target, Me.Range("E4")) Is Nothing Then
Application.EnableEvents = False
ws2.Range("D3").Value = Target.Value
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet1")
' Ki?m tra n?u ô thay d?i là DataValidation2
If Not Intersect(Target, Me.Range("D3")) Is Nothing Then
Application.EnableEvents = False
ws1.Range("E4").Value = Target.Value
Application.EnableEvents = True
End If
End Sub
Dạ, đúng cái em cần.Mã VBA cho Sheet1:
Mã:Private Sub Worksheet_Change(ByVal Target As Range) Dim ws2 As Worksheet Set ws2 = ThisWorkbook.Sheets("Sheet2") ' Ki?m tra n?u ô thay d?i là DataValidation1 If Not Intersect(Target, Me.Range("E4")) Is Nothing Then Application.EnableEvents = False ws2.Range("D3").Value = Target.Value Application.EnableEvents = True End If End Sub
Mã VBA cho Sheet2:
Mã:Private Sub Worksheet_Change(ByVal Target As Range) Dim ws1 As Worksheet Set ws1 = ThisWorkbook.Sheets("Sheet1") ' Ki?m tra n?u ô thay d?i là DataValidation2 If Not Intersect(Target, Me.Range("D3")) Is Nothing Then Application.EnableEvents = False ws1.Range("E4").Value = Target.Value Application.EnableEvents = True End If End Sub
VBA ở Sheet1:Dear Anh/Chị @hoangtuaotrang_hp_vn ,
Hiện tại thì với bài toán ban đầu là ok.
Tuy nhiên, em lại phát sinh có tạo thêm 1 DataValidation3 nằm cùng trên Sheet1.
Việc tự cập nhật đang ko được. Cụ thể sự thay đổi:
DataValidation1 thay đổi thì DataValidation2 và 3 thay đổi > ok
DataValidation2 thay đổi thì DataValidation1 và 3 thay đổi > ok
DataValidation3 thay đổi thì DataValidation1 ok, DataValidation2 KHÔNG thay đổi (not ok)
Anh/Chị xem giúp phần code và điều chỉnh logic cho đúng giúp em.
Cám ơn Anh/Chị.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' Kiểm tra nếu ô thay đổi là E4 hoặc M4
If Not Intersect(Target, Me.Range("E4, M4")) Is Nothing Then
Application.EnableEvents = False
' Cập nhật D3 trên Sheet2
ws2.Range("D3").Value = Target.Value
' Cập nhật ô còn lại trên Sheet1
If Target.Address = Me.Range("E4").Address Then
Me.Range("M4").Value = Target.Value
ElseIf Target.Address = Me.Range("M4").Address Then
Me.Range("E4").Value = Target.Value
End If
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet1")
' Kiểm tra nếu ô thay đổi là D3
If Not Intersect(Target, Me.Range("D3")) Is Nothing Then
Application.EnableEvents = False
' Cập nhật cả E4 và M4 trên Sheet1
ws1.Range("E4").Value = Target.Value
ws1.Range("M4").Value = Target.Value
Application.EnableEvents = True
End If
End Sub