如果满足条件,则显示下拉列表

时间:2019-06-27 07:05:29

标签: excel vba

我看过很少有关于可靠下拉列表的主题,但是没有一个主题能为我提供实现此目标的想法。我有“ C”列,其中信息是由另一个工作表中的VLOOKUP函数填充的,我需要一个选项来检查如果列A单元格中的值等于= 001377,那么它应该给我带有两个值的下拉列表,如果没有,则继续使用VLOOKUP函数,而无需任何列表。我该如何实现?

1 个答案:

答案 0 :(得分:0)

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long

    'Change sheet name if needed
    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 1 To LastRow

            If .Range("A" & i).Value = "001377" Then
                'Replace Formula1:="1,2" with the two values that you want to appear in the drop down
                With .Range("C" & i).Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="1,2"
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = True
                    .ShowError = True
                End With

            Else
                'Code for VLOOKUP
            End If

        Next i

    End With

End Sub