0

我不知道如何根据某个单元格值创建下拉列表。我有这两个功能,但不知道如何合并它们。

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("C14")) Is Nothing Then

    Select Case Range("C14")

        Case "Emergency": EmergencyError
        Range("C18") = "No"

        'Case "Basic":  ??

    'Stops Select Case statement
    End Select
End If
End Sub
Sub BasicList()

    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=DONNEES!$A$4:$A$10"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

要创建的下拉菜单位于另一个单元格上,唯一的单元格是相同的。

4

1 回答 1

0

像这样的东西应该工作:

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng As Range
    Set rng = Intersect(Target, Me.Range("C14"))
    
    On Error GoTo haveError
    
    If Not rng Is Nothing Then
        Application.EnableEvents = False 'don't re-trigger this event
        Select Case rng.Value
            Case "Emergency"
                EmergencyError
                Me.Range("C18") = "No"
            Case "Basic"
                BasicList rng.Offset(0, 1) 'puts a validation list in D14
        End Select
    End If

haveError:
    Application.EnableEvents = True 'make sure to re-enable events in case of an error
    
End Sub

'create a validation list in range "c"
Sub BasicList(c As Range)
    With c.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:="=DONNEES!$A$4:$A$10"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub
于 2020-11-13T01:41:55.913 回答