在另一个组合框中选择值时如何触发一个组合框中的事件?

时间:2019-09-11 12:37:29

标签: vba events combobox

我有一个组合框“选择视图”,其中包含(周,月,季度和全部)。我想选择第一个ComboBox时,例如:“ WEEK”,第二个ComboBox“选择期间”将被填充,而没有单击按钮“ W1,W2,.... W52”,或者如果我们选择“ QUARTER”将会填充“ Q1,Q2,Q3,Q4”等。

Form View

我尝试了下面的代码,但效果不佳。

Private Sub ComboBox4_Change()

Dim index As Integer

With Sheets("SUMMARY")
index = .ComboBox3.ListIndex
    .ComboBox4.Clear

    With .ComboBox4
        Select Case index
            Case Is = 0
                .AddItem "W1"
                .AddItem "W2"
                .AddItem "W3"
                .AddItem "W4"
                .AddItem "W5"
                .AddItem "W6"
                .AddItem "W7"
                .AddItem "W8"
                .AddItem "W9"
                .AddItem "W10"
                .AddItem "W11"
                .AddItem "W12"
                .AddItem "W13"
                .AddItem "W14"
                .AddItem "W15"
                .AddItem "W16"
                .AddItem "W17"
                .AddItem "W18"
                .AddItem "W19"
                .AddItem "W20"
                .AddItem "W21"
                .AddItem "W22"
                .AddItem "W23"
                .AddItem "W24"
                .AddItem "W25"
                .AddItem "W26"
                .AddItem "W27"
                .AddItem "W28"
                .AddItem "W29"
                .AddItem "W30"
                .AddItem "W31"
                .AddItem "W32"
                .AddItem "W33"
                .AddItem "W34"
                .AddItem "W35"
                .AddItem "W36"
                .AddItem "W37"
                .AddItem "W38"
                .AddItem "W39"
                .AddItem "W40"
                .AddItem "W41"
                .AddItem "W42"
                .AddItem "W43"
                .AddItem "W44"
                .AddItem "W45"
                .AddItem "W46"
                .AddItem "W47"
                .AddItem "W48"
                .AddItem "W49"
                .AddItem "W50"
                .AddItem "W51"
                .AddItem "W52"

            Case Is = 1
                .AddItem "JANUARY"
                .AddItem "FEBRUARY"
                .AddItem "MARCH"
                .AddItem "APRIL"
                .AddItem "MAY"
                .AddItem "JUNE"
                .AddItem "JULY"
                .AddItem "AUGUST"
                .AddItem "SEPTEMBER"
                .AddItem "OCTOBER"
                .AddItem "NOVEMBER"
                .AddItem "DECEMBER"

            Case Is = 2
                .AddItem "Q1"
                .AddItem "Q2"
                .AddItem "Q3"
                .AddItem "Q4"
            Case Is = 3
                .AddItem "ALL"
        End Select
    End With

End With

End Sub

1 个答案:

答案 0 :(得分:0)

  1. 我将其分为两个功能,更易于维护。
  2. 您可以使用数组使代码更小
  3. 我的代码假定ComboBox3包含过滤器,并且combo4是需要填充的内容。我想您可能在上面混了这两个。
Private Sub ComboBox3_Change()

    with thisworkbook.worksheets("Summary")
        call PopulateCombo4(.ComboBox3.value)
    end with

end sub
Private Sub PopulateCombo4(val as string)

    thisworkbook.worksheets("Summary").combobox4.clear

    dim arr() as string
    if val = "QUARTERLY" then
        arr = split("Q1,Q2,Q3,Q4")
    Elseif val = "WEEKLY" then
        '....[same as above with W1,W2....]
    end if            

    dim s as string
    for each s in arr
        thisworkbook.worksheets("Summary").combobox4.additem s
    next

end sub

让我知道您是否有后续问题。去开会。