为什么要清空动态依赖项数据验证列表(VBA)?

时间:2018-08-23 08:53:05

标签: excel-vba validation dropdown

我有一个宏,它在 colNum + 1 列中的每个合并单元格上创建一个数据验证列表,然后在 colNum + 2列中的每个单元格上创建一个动态数据验证列表强>。这些动态验证列表取决于在 colNum + 1 列中选择了哪个选项。该代码成功运行,但是当我要从 colNum + 2 中选择时,我看到下拉列表为空。为什么以及如何做,我在数据验证下拉列表中看到“ -”,“ ”或“ ValidationListFormulaHyphen ”( colNum2 < / strong>)?

sub Validation()

Dim ValidationListFormula   As String
Dim ValidationListFormulaHyphen As String
Dim hyphen                  As String
Dim ws                      As Worksheet
Dim rowNum                  As Long
Dim colNum                  As Long
Dim j                       As Long
Dim i                       As Long
Dim k                       As Long

rowNum = Range("A1", Range("A1").End(xlDown)).Cells.Count
colNum = Range("A1", Range("A1").End(xlToRight)).Cells.Count

        j = 0
        Do
           j = j + 1
        Loop Until Cells(1, j).Value = "ID_COLS_VALUE"
        Range(Cells(2, j), Cells(rowNum, j)).Interior.Color = RGB(224, 224, 224)

        Cells(1, colNum + 1).Value = "Decision"
        Cells(1, colNum + 1).ColumnWidth = 50
        Cells(1, colNum + 2).Value = "Master Customer Number"
        Cells(1, colNum + 2).ColumnWidth = 25
        Cells(1, colNum + 3).Value = "Notes"
        Cells(1, colNum + 3).ColumnWidth = 50

        i = 2
        Do Until Cells(i, 1) = ""
            k = 0
            ValidationListFormula = Cells(i, j).Value
            Do While Cells(i, 1).Value = Cells(i + 1, 1).Value
                k = k + 1
                i = i + 1
                ValidationListFormula = ValidationListFormula & "," & Cells(i, j).Value
            Loop

            Range(Cells(i - k, colNum + 1), Cells(i, colNum + 1)).Merge
            Range(Cells(i - k, colNum + 3), Cells(i, colNum + 3)).Merge

            'validation column + 1'
            Dim MyList(2) As String
            MyList(0) = "Customers_in_this_group_are_not_the_same"
            MyList(1) = "All_the_customers_in_this_group_are_the_same"
            MyList(2) = "Part_of_the_customers_in_this_group_are_the_same"
            With Range(Cells(i - k, colNum + 1), Cells(i, colNum + 1)).validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                 Operator:=xlBetween, Formula1:=Join(MyList, ",")
        End With

        'validation column + 2'
            'convert column number to letter'
            Dim ColumnLetter As String
            ColumnLetter = Split(Cells(1, colNum + 1).Address, "$")(1)

            'validation for MyList(0)'
            ThisWorkbook.Names.Add _
            Name:="Customers_in_this_group_are_not_the_same", _
            RefersTo:="-"

            With Range(Cells(i - k, colNum + 2), Cells(i, colNum + 2)).validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="=IF(" & ColumnLetter & i - k & "="""",XER1,INDIRECT(" & ColumnLetter & i - k & "))"
            End With

            'validation for MyList(1)'
            Application.Sheets(1).Names.Add _
            Name:="All_the_customers_in_this_group_are_the_same", _
            RefersTo:=ValidationListFormula
            With Range(Cells(i - k, colNum + 2), Cells(i, colNum + 2)).validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="=IF(" & ColumnLetter & i - k & "="""",XER1,INDIRECT(" & ColumnLetter & i - k & "))"
            End With

            'validation for MyList(2)'
            ValidationListFormulaHyphen = ValidationListFormula & ",-"
            Application.Sheets(1).Names.Add _
            Name:="Part_of_the_customers_in_this_group_are_the_same", _
            RefersTo:=ValidationListFormulaHyphen
            With Range(Cells(i - k, colNum + 2), Cells(i, colNum + 2)).validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="=IF(" & ColumnLetter & i - k & "="""",XER1,INDIRECT(" & ColumnLetter & i - k & "))"
            End With

        i = i + 1
    Loop

End Sub

0 个答案:

没有答案