Excel VBA 在另一个 ComboBox 更改时将项目添加到程序 ComboBox

Excel VBA add item to a programatic ComboBox when other ComboBox Change

我以编程方式在 Multipage1 中添加一个新页面

Private Sub CommandButton3_Click()
Dim i As Integer
    i = MultiPage1.Pages.Count
    MultiPage1.Pages.Add.Caption = "Guarantee " & i

然后我希望我的新页面包含 3 个 ComboBox,ComboBox1 将在我的工作表名称“LeftTB”的 table 中列出该项目,这是代码。

    For r = 1 To 3
            Set myCB = MultiPage1.Pages(i).Controls.Add("Forms.ComboBox.1", "ComboBox" & r, 1)
            With myCB
                .Width = 150
                .Height = 18
            Select Case r
                Case Is = 1
                        .Left = 54
                        .Top = 156
            'add item to combobox1
                    Dim rng, cl As Range
            'LeftTB is the name of Table contain Data
                    Set rng = Range("LeftTB") 
                    For Each cl In rng
                        .AddItem cl.Value
                    Next cl
                Case Is = 2
                        .Left = 252
                        .Top = 156
                Case Is = 3
                        .Left = 54
                        .Top = 180
                End Select
            End With
          Next r
    End Sub

通过这段代码在ComboBox1中添加值效果很好。对于ComboBox 2中的项目,它取决于ComboBox1的值,如下面的代码。

Private Sub ComboBox1_Change()
Dim rng, cl As Range

'The CenterTB is the table in my worksheet with two columns. The first column (SubD) contains the same data as table "LeftTB" and the next column is the item I would like to add to ComboBox2
Set rng = Range("CenterTB[SubD]")

For i = 1 to me.MultiPage1.Pages.Count
    me.MultiPage1.Pages(i).Controls("ComboBox2").Clear
    For Each cl In rng
        If cl.Value = me.MultiPage1.Pages(i).Controls("ComboBox1").Text Then
        me.MultiPage1.Pages(i).Controls("ComboBox2").AddItem cl.Offset(0, 1).Value
    End If
    Next cl
Next i
End Sub

但是,当 ComboBox1 是程序化的时,它不起作用。我不知道在程序化时检测 Combobox1 的更改过程。

有人可以提供解决方案吗?

动态创建控件时,VBA 不会自动创建它们的事件!

有两种方法可以做到这一点。要创建事件包装器 class,或者更简单,如果要添加已知数量的控件(您的情况)以预先以特定方式声明控件:

  1. 将下一个声明放在表单代码模块的顶部(在声明区域):
Option Explicit

Private WithEvents ComboBox1 As MSForms.ComboBox
Private WithEvents ComboBox2 As MSForms.ComboBox 'possible to use its events, too
Private WithEvents ComboBox3 As MSForms.ComboBox 'possible to use its events, too
  1. 然后您应该调整您的代码,以某种方式将上面声明的变量设置为您want/need它们的样子。因此,请按以下方式调整您的代码:
Private Sub btCreateCmb_Click()
  Dim myCB As MSForms.ComboBox, r As Long
   For r = 1 To 3
       Set myCB = MultiPage1.Pages(i).Controls.Add("Forms.ComboBox.1", "ComboBox" & r, 1)
       With myCB
          .Width = 150
          .Height = 18
          Select Case r
             Case Is = 1
                 Set ComboBox1 = myCB 'added to Set your first combo
                 .Left = 54
                 .Top = 156
                 'add item to combobox1
                 Dim rng, cl As Range
                 'LeftTB is the name of Table contain Data
                    Set rng = Range("LeftTB") 
                    For Each cl In rng
                        .AddItem cl.Value
                    Next cl
                Case Is = 2
                     Set ComboBox2 = myCB 'added to Set your second combo
                     .Left = 252
                     .Top = 156
                Case Is = 3
                     Set ComboBox3 = myCB 'added to Set your third combo
                     .Left = 54
                     .Top = 180
                End Select
            End With
          Next r
End Sub
  1. 只有现在,组合事件才会被触发(如果它们的代码存在于表单代码模块中)。请首先使用下一个简短的代码示例:
Private Sub ComboBox1_Change()
   MsgBox "Changed 1..."
End Sub

如果你完全按照上面的建议去做,应该一定会触发!

现在,您可以根据需要输入其事件代码...

请测试它并发送一些反馈。