当用户在另一个下拉列表中进行更改时更改一个下拉列表的值

Changing Value of one drop down list when user makes a change in another drop down list

在 Excel 中,我希望当用户从另一个下拉列表中选择一个项目时,下拉列表的 displayed/selected 值会发生变化。 (我已经可以更改列表中的选项)

背景:

List1 是一个包含以下可用条目的下拉列表:

Product1                     
Product2                   

List2 是一个下拉列表,其中包含以下可用条目(这些是付款期):

6  (available for Product1 and Product2)
10 (available for Product1 and Product2)
3  (available for Product2)
16 (available for Product2)
20 (available for Product2)

到目前为止,当 List1 中的所选项目发生变化时,我已经设法更新了 List2 中的可用下拉条目。但只有下拉条目发生了变化; List2 的实际当前可见内容没有改变。

问题:

这还不是我想要的:一旦 List1 中的所选项目发生变化,List2 应该立即显示 "Please Select"(除了 List2's可用的下拉条目正在更新),因此用户知道必须在 List2.

中进行选择

利用 Worksheet_Change 事件并使用 Intersect 仅在特定单元格上触发。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("List1")) Is Nothing Then
        Application.EnableEvents = False 'prevent triggering another change event
        Range("List2").Value = "Please Select …"
        Application.EnableEvents = True
    End If
End Sub

注意 Range("List1") 应更改为引用 List1 下拉列表的单元格。相应地 Range("List2")

此代码将根据您在 List1 中选择的值更新您的下拉列表

    Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False

Dim MyCell As Range

Set MyCell = Range("A1") 'Change A1 by cell where you have List1 dropwdown list

If Target.Address <> MyCell.Address Then
    'we do nothing
    Set MyCell = Nothing
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Else
    'we modify List2 dropdown list
    Dim MyOption As String
    MyOption = MyCell.Value
    Set MyCell = Nothing
    Set MyCell = Range("B1") 'Change B2 by Cell where you have List2 dropwdown list

    'now 'We use Select Case because you can add all cases you want, just in case in Future you have, for example, Product3
    Select Case MyOption
        Case "Product1"
            With MyCell.Validation
                .Delete
                MyCell.Value = "Please Select"
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="6,10"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
        Case "Product2"
            With MyCell.Validation
                .Delete
                MyCell.Value = "Please Select"
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="6,10,3,16,20"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
            Case Else
                MsgBox "Update code with new option"
    End Select
End If

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

对我有用:

更新代码:添加了 "Please Select" 部分,问题已解决。