Bỏ qua nội dung chính

Làm cách nào để đồng bộ hóa danh sách thả xuống trong nhiều trang tính trong Excel?

Giả sử bạn có danh sách thả xuống trên một số trang tính trong sổ làm việc chứa chính xác các mục thả xuống giống nhau. Bây giờ bạn muốn đồng bộ hóa danh sách thả xuống trên các trang tính để sau khi bạn chọn một mục từ danh sách thả xuống trong một trang tính, danh sách thả xuống trong các trang tính khác sẽ tự động được đồng bộ hóa cùng một lựa chọn. Bài viết này cung cấp mã VBA để giúp bạn giải quyết vấn đề này.

Đồng bộ hóa danh sách thả xuống trong nhiều trang tính với mã VBA


Đồng bộ hóa danh sách thả xuống trong nhiều trang tính với mã VBA

Ví dụ: danh sách thả xuống nằm trong năm trang tính có tên Trang tính1, Trang tính2, ..., Trang tính 5, để đồng bộ hóa danh sách thả xuống trong các trang tính khác theo lựa chọn thả xuống trong Sheet1, vui lòng áp dụng mã VBA sau để hoàn tất.

1. Mở Sheet1, nhấp chuột phải vào tab trang tính và chọn Mã Chế độ xem từ menu chuột phải.

2. bên trong Microsoft Visual Basic cho các ứng dụng cửa sổ, dán mã VBA sau vào Sheet1 (Mã) cửa sổ.

Mã VBA: Đồng bộ hóa danh sách thả xuống trong nhiều trang tính

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20220815
    Dim tSheet1 As Worksheet
    Dim tRange As Range
    Dim xRangeStr As String
    On Error Resume Next
    If Target.Count > 1 Then Exit Sub
    
    xRangeStr = "A2:A11"

    Set tRange = Intersect(Target, Range(xRangeStr))
    If Not tRange Is Nothing Then
        xRangeStr = tRange.Address
        Application.EnableEvents = False
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet2")
        tSheet1.Range(xRangeStr).Value = Target.Value
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet3")
        tSheet1.Range(xRangeStr).Value = Target.Value
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet4")
        tSheet1.Range(xRangeStr).Value = Target.Value
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet5")
        tSheet1.Range(xRangeStr).Value = Target.Value
        Application.EnableEvents = True
    End If
    
End Sub

Ghi chú:

1) Trong mã, A2: A11 là phạm vi chứa danh sách thả xuống. Đảm bảo rằng tất cả danh sách thả xuống nằm trong cùng một phạm vi trên các trang tính khác nhau.
2) Trang tính2, Trang tính3, Trang tính4Sheet5 là các trang tính có chứa danh sách thả xuống mà bạn muốn đồng bộ hóa dựa trên danh sách thả xuống trong Sheet1;
3) Để thêm các trang tính khác trong mã, vui lòng thêm hai dòng sau trước dòng “Ứng dụng.EnableEvents = True”, Sau đó thay đổi tên trang tính“Sheet5”Đến tên bạn cần.
Đặt tSheet1 = ActiveWorkbook.Worksheets ("Sheet5")
tSheet1.Range (xRangeStr) .Value = Target.Value

3. Nhấn nút Khác + Q chìa khóa để đóng Microsoft Visual Basic cho các ứng dụng cửa sổ.

Từ bây giờ, khi bạn chọn một mục từ danh sách thả xuống trong Trang tính 1, danh sách thả xuống trong các trang tính được chỉ định sẽ được đồng bộ hóa tự động để có cùng lựa chọn. Xem bản trình diễn dưới đây.


Demo: Đồng bộ hóa danh sách thả xuống trong nhiều trang tính trong Excel

Công cụ năng suất văn phòng tốt nhất

🤖 Trợ lý AI của Kutools: Cách mạng hóa việc phân tích dữ liệu dựa trên: Thực thi thông minh   |  Tạo mã  |  Tạo công thức tùy chỉnh  |  Phân tích dữ liệu và tạo biểu đồ  |  Gọi các hàm Kutools...
Các tính năng phổ biến: Tìm, đánh dấu hoặc xác định các bản sao   |  Xóa hàng trống   |  Kết hợp các cột hoặc ô mà không làm mất dữ liệu   |   Vòng không có công thức hữu ích. Cảm ơn !
Siêu tra cứu: Nhiều tiêu chí VLookup    VLookup Nhiều Giá Trị  |   VLookup trên nhiều trang tính   |   Tra cứu mờ ....
Danh sách thả xuống nâng cao: Tạo nhanh danh sách thả xuống   |  Danh sách thả xuống phụ thuộc   |  Danh sách thả xuống nhiều lựa chọn ....
Trình quản lý cột: Thêm một số cột cụ thể  |  Di chuyển cột  |  Chuyển đổi trạng thái hiển thị của các cột ẩn  |  So sánh dãy và cột hữu ích. Cảm ơn !
Các tính năng nổi bật: Tiêu điểm lưới   |  Chế độ xem thiết kế   |   Thanh công thức lớn    Trình quản lý sổ làm việc & trang tính   |  Thư viện tài nguyên (Văn bản tự động)   |  Bảng chọn ngày   |  Kết hợp các bảng tính   |  Mã hóa/Giải mã ô    Gửi email theo danh sách   |  Siêu lọc   |   Bộ lọc đặc biệt (lọc in đậm/nghiêng/gạch ngang...) ...
15 bộ công cụ hàng đầu12 bản văn CÔNG CỤ (thêm văn bản, Xóa ký tự,...)   |   50 + Biểu đồ Các loại (Biểu đồ Gantt,...)   |   40+ Thực tế Công thức (Tính tuổi dựa trên ngày sinh,...)   |   19 chèn CÔNG CỤ (Chèn mã QR, Chèn ảnh từ đường dẫn,...)   |   12 Chuyển đổi CÔNG CỤ (Số thành từ, Chuyển đổi tiền tệ,...)   |   7 Hợp nhất & Tách CÔNG CỤ (Các hàng kết hợp nâng cao, Chia ô,...)   |   ... và nhiều hơn nữa

Nâng cao kỹ năng Excel của bạn với Kutools for Excel và trải nghiệm hiệu quả hơn bao giờ hết. Kutools for Excel cung cấp hơn 300 tính năng nâng cao để tăng năng suất và tiết kiệm thời gian.  Bấm vào đây để có được tính năng bạn cần nhất...

Mô tả


Tab Office mang lại giao diện Tab cho Office và giúp công việc của bạn trở nên dễ dàng hơn nhiều

  • Cho phép chỉnh sửa và đọc theo thẻ trong Word, Excel, PowerPoint, Publisher, Access, Visio và Project.
  • Mở và tạo nhiều tài liệu trong các tab mới của cùng một cửa sổ, thay vì trong các cửa sổ mới.
  • Tăng 50% năng suất của bạn và giảm hàng trăm cú nhấp chuột cho bạn mỗi ngày!
Comments (7)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi,

thanks for the code, it worked well. Is it possible to adapt the code so that i restructure all sheets as main sheet and can change all other drop down menus from sheet 1 and at the same time from sheet 2, sheet 3, ...? I don't know anything about VBA, but I could use your code successfully for sheet1 as main sheet. Now need the possibility to synchronize the drop down menus across all sheets.

Thanks!
This comment was minimized by the moderator on the site
Hi,

I appreciate your content a lot. I am struggling to find the code to write to have drop-downs synchronized in excel. My synchronization needs to be a lot more complex and I have been trying to use ChatGPT to help write it but a robot can only do so much. I figured I would reach out and see if you could provide some direction or advise if I am making progress since I am becoming more and more frustrated. I need drop downs in Sheet 3 to correspond with drop-downs on Sheet 1 and Sheet 2. The drop-downs in Sheet 3 are all in Column A and each cell in Column A corresponds to a different dropdown on either Sheet 1 or Sheet 2. Those dropdowns have conditional formatting to highlight certain cells when an item is selected from the list. The options are "Complete" (Highlights Green), "Not Satisfied" (Highlights Orange), and "Delinquent" (Highlights red). It is probably confusing to read but I use excel to track loan payments and I have very detailed instructions I am trying to code in excel. For example, I am trying to write the code to tell excel to do the following:

1. I need cell A7 on ACORE Cash Mgmt Sheet (Sheet 3) to align with the dropdown and the conditional formatting in cell C427 on the 8th payment sheet (Sheet 1)
2. I need A8 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C231 on the 8th payment sheet
3. I need A9 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C133 on the 8th payment sheet
4. I need A10 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C270 on the 8th payment sheet
5. I need A11 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C351 on the 8th payment sheet
6. I need A12 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C3 on the 8th payment sheet
7. I need A13 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C39 on the 8th payment sheet
8. I need A14 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C116 on the 8th payment sheet
9. I need A15 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C144 on the 8th payment sheet
10. I need A16 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C64 on the 8th payment sheet
11. I need A17 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C158 on the 8th payment sheet
12. I need A18 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C199 on the 8th payment sheet
13. I need A19 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C103 on the 8th payment sheet
14. I need A20 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C380 on the 8th payment sheet
15. I need A21 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C81 on the 8th payment sheet
16. I need A22 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C169 on the 8th payment sheet
17. I need A23 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C418 on the 8th payment sheet
18. I need A24 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C301 on the 8th payment sheet
19. I need A25 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C95 on the 8th payment sheet
20. I need A26 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C407 on the 8th payment sheet
21. I need A27 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C15 on the 8th payment sheet
22. I need A28 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C340 on the 8th payment sheet
23. I need A29 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C211 on the 8th payment sheet
24. I need A30 on ACORE Cash Mgmt Sheet to align with the dropdown and the conditional formatting in C52 on the 8th payment sheet


This is the code that got me the closet to do what I wanted...however, I could not get it to work exactly how I wanted and it would only be for the first instructions and not include everything else:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim SourceSheet As Worksheet
Dim DestSheet As Worksheet
Dim SourceCell As Range
Dim DestCell As Range
Dim FormattingRange As Range

' Set the source and destination sheets
Set SourceSheet = ThisWorkbook.Sheets("ACORE Cash Mgmt")
Set DestSheet = ThisWorkbook.Sheets("8th Payment")

' Set the source and destination cells
Set SourceCell = SourceSheet.Range("A7")
Set DestCell = DestSheet.Range("C427")

' Set the formatting range
Set FormattingRange = DestCell

' Check if the change was in the source cell
If Not Intersect(Target, SourceCell) Is Nothing Then
' Copy the value from the source cell to the destination cell
DestCell.Value = SourceCell.Value

' Apply conditional formatting based on the value
Select Case SourceCell.Value
Case "Complete"
FormattingRange.Interior.Color = RGB(146, 208, 80) ' Green
Case "Not Satisfied"
FormattingRange.Interior.Color = RGB(255, 192, 0) ' Orange
Case "Delinquent"
FormattingRange.Interior.Color = RGB(255, 0, 0) ' Red
Case Else
FormattingRange.Interior.ColorIndex = xlNone ' Clear formatting
End Select
End If
End Sub


Are you able to provide any expertise? I hope this all makes some sense considering it reads quite poorly.

Thank you in advance,

Sam
This comment was minimized by the moderator on the site
Crystal,

Thank you so much for your response, this worked! How could I modify the code to add another cell in the same sheet 6, B3 that also needed to be synchronized with B8 in sheet 7? I have attempted to modify it below, however it ends up putting the contents of B3 on sheet 6 in B7 on sheet 7 instead of B8.


Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221025
Dim tSheet1 As Worksheet
Dim tRange1 As Range
Dim tRange2 As Range
Dim xRangeStr1 As String
Dim xRangeStr2 As String
On Error Resume Next
If Target.Count > 1 Then Exit Sub

xRangeStr1 = "B2"
xRangeStr2 = "B3"

Set tRange1 = Range("B7")
If Not tRange1 Is Nothing Then
xRangeStr1 = tRange1.Address
Application.EnableEvents = False
Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
tSheet1.Range(xRangeStr1).Value = Target.Value
Application.EnableEvents = True
End If

Set tRange2 = Range("B8")
If Not tRange2 Is Nothing Then
xRangeStr2 = tRange2.Address
Application.EnableEvents = False
Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
tSheet1.Range(xRangeStr2).Value = Target.Value
Application.EnableEvents = True
End If

End Sub
This comment was minimized by the moderator on the site
Hi,

How can I do this if my dropdowns are in different ranges? To elaborate, I have one drop down in sheet 7 that is in cell B7 and the same dropdown on sheet 6 in cell B2.

Thank you,
Elaine
This comment was minimized by the moderator on the site
Hi E,
The following VBA code can help.
Here I take Sheet6 as the main worksheet, right click the sheet tab, select View Code from the right-click menu, then copy the following code in the Sheet6 (Code) window. When you select any item from the drop-down list in B2 of Sheet6, the drop-down list in B7 of Sheet7 will be cynchronized to have the same selected item.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221025
    Dim tSheet1 As Worksheet
    Dim tRange As Range
    Dim xRangeStr As String
    On Error Resume Next
    If Target.Count > 1 Then Exit Sub
    
    xRangeStr = "B2"
    
    Set tRange = Range("B7")
    If Not tRange Is Nothing Then
        xRangeStr = tRange.Address
        Application.EnableEvents = False
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
        tSheet1.Range(xRangeStr).Value = Target.Value
        Application.EnableEvents = True
    End If
    
End Sub
This comment was minimized by the moderator on the site
Hi Crystal,

Thank you so much for your response, your code worked! I have a cell right under b2 and b7, b3 and b8 respectively that need to have the same function. I tried to rewrite your code as shown below, however this did not work. It caused b7 instead of b8 to change when I changed b3. Might you be able to identify what I am doing wrong?

Thank you so much!

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221025
    Dim tSheet1 As Worksheet
    Dim tRange1 As Range
    Dime tRange2 As Range
    Dim xRangeStr1 As String
    Dim xRangeStr2 As String
    On Error Resume Next
    If Target.Count > 1 Then Exit Sub
    
    xRangeStr1 = "B2"
    xRangeStr2="B3"
    
    Set tRange1 = Range("B7")
    If Not tRange1 Is Nothing Then
        xRangeStr1 = tRange1.Address
        Application.EnableEvents = False
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
        tSheet1.Range(xRangeStr1).Value = Target.Value
        Application.EnableEvents = True
    End If
    
    Set tRange2 = Range("B8")
    If Not tRange2 Is Nothing Then
        xRangeStr2 = tRange2.Address
        Application.EnableEvents = False
        Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
        tSheet1.Range(xRangeStr2).Value = Target.Value
        Application.EnableEvents = True
    End If

End Sub
This comment was minimized by the moderator on the site
Hi E,
There is something wrong with the VBA code I replied to you above.
For the new question you mentioned, please try the following code.

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20221031
    
    Dim xBool1 As Boolean
    Dim xBool2 As Boolean
    Dim xRgStr As String
    Dim tRange As Range
    
    xRangeStr1 = "B2"
    xRangeStr2 = "B3"
    xRgStr = ""
    
    On Error Resume Next
    If Target.Count > 1 Then Exit Sub
    xBool1 = Intersect(Target, Range(xRangeStr1)) Is Nothing
    xBool2 = Intersect(Target, Range(xRangeStr2)) Is Nothing
    
    If xBool1 And xBool2 Then Exit Sub
    
    xRgStr = Target.Address(False, False, xlA1, False, False)
    
    If Target.Address(False, False, xlA1, False, False) = xRangeStr1 Then
        xRgStr = "b7"
    ElseIf Target.Address(False, False, xlA1, False, False) = xRangeStr2 Then
        xRgStr = "b8"
    End If
    If xRgStr = "" Then Exit Sub
    
    Application.EnableEvents = False
    Set tSheet1 = ActiveWorkbook.Worksheets("Sheet7")
    tSheet1.Range(xRgStr).Value = Target.Value
    Application.EnableEvents = True

End Sub
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations