通过VBA下拉列表

时间:2016-04-04 08:57:13

标签: excel vba list drop-down-menu

enter image description here

我有三个下拉列表验证列表,我正在尝试编写一些代码来迭代所有"管理方法"可用的排列。

我可以找出迭代一个列表的第一步(例如Iterate through an Excel dropdown/validation list和其他列表),但我无法弄清楚如何通过其中三个列表。

理想情况下,即使您添加了更多选项,我希望以一种有效的方式编写。

为了达到这个目的,我想你想要一种计算每个列表中有多少选项然后从0-n迭代的方法。

非常感谢任何帮助。

Sub LoopThroughList()
Dim Dropdown1, Dropdown2, Dropdown3 As String
Dim Range1, Range2, Range3 As Range
Dim option1, option2, option3 As Range

' *** SET DROPDOWN LOCATIONS HERE ***
' ***********************************

    Dropdown1 = "C6"
    Dropdown2 = "D6"
    Dropdown3 = "E6"

' ***********************************
' ***********************************

Set Range1 = Evaluate(Range(Dropdown1).Validation.Formula1)
Set Range2 = Evaluate(Range(Dropdown2).Validation.Formula1)
Set Range3 = Evaluate(Range(Dropdown3).Validation.Formula1)

For Each option1 In Range1
    For Each option2 In Range2
        For Each option3 In Range3

            Worksheets("Sheet1").Range("C6:E6").Copy
            With Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
                .PasteSpecial Paste:=xlPasteColumnWidths
                .PasteSpecial Paste:=xlPasteValues
            End With


        Next option3
    Next option2
Next option1


End Sub

目前我得到了这个:

enter image description here

这是遍历作物类型下拉列表但返回相同的害虫和管理方法。我用来创建下拉列表的代码如下:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("C6")) Is Nothing Then
        Range("D6:E6").ClearContents
    End If

End Sub

1 个答案:

答案 0 :(得分:2)

执行遍历每个可能组合的循环的最简单方法是使用For Each循环。您可以找到有关他们的更多信息herehere以及herehere ....... !!!诸如此类....

这将遍历3个下拉列表的每个组合。您需要在我的代码中更改下拉列表的位置。

Sub LoopThroughList()
Dim Dropdown1, Dropdown2, Dropdown3 As String
Dim Range1, Range2, Range3 As Range
Dim option1, option2, option3 As Range

' *** SET DROPDOWN LOCATIONS HERE ***
' ***********************************

    Dropdown1 = "D8"
    Dropdown2 = "E8"
    Dropdown3 = "F8"

' ***********************************
' ***********************************

Set Range1 = Evaluate(Range(Dropdown1).Validation.Formula1)
Set Range2 = Evaluate(Range(Dropdown2).Validation.Formula1)
Set Range3 = Evaluate(Range(Dropdown3).Validation.Formula1)

For Each option1 In Range1
    For Each option2 In Range2
        For Each option3 In Range3

            ' *** PERFORM CODE HERE ***
            ' EXAMPLE
            ' Sheets(1).Cells(1, 1) = option1
            ' Sheets(1).Cells(2, 1) = option2
            ' etc...

        Next option3
    Next option2
Next option1


End Sub

编辑:

Sub LoopThroughList()
Dim Dropdown1, Dropdown2, Dropdown3 As String
Dim Range1, Range2, Range3 As Range
Dim option1, option2, option3 As Range

Dim Counter As Long

Counter = 1

' *** SET DROPDOWN LOCATIONS HERE ***
' ***********************************

    Dropdown1 = "C6"
    Dropdown2 = "D6"
    Dropdown3 = "E6"

' ***********************************
' ***********************************

Set Range1 = Evaluate(Range(Dropdown1).Validation.Formula1)
Set Range2 = Evaluate(Range(Dropdown2).Validation.Formula1)
Set Range3 = Evaluate(Range(Dropdown3).Validation.Formula1)

For Each option1 In Range1
    For Each option2 In Range2
        For Each option3 In Range3

            Sheets(2).Cells(Counter, 1) = option1
            Sheets(2).Cells(Counter, 2) = option2
            Sheets(2).Cells(Counter, 3) = option3
            Counter = Counter + 1

        Next option3
    Next option2
Next option1


End Sub