简体   繁体   中英

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

I add a new page in Multipage1 programmatically

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

Then I would like my new page containing 3 ComboBoxes and the ComboBox1 will list the item in the table on my worksheet name "LeftTB", here is the code.

    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

It works fine to add value in ComboBox1 by this code. For the item in ComboBox 2, it depends on the value of the ComboBox1 as the code below.

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

However, it does not work when ComboBox1 is programmatic. I have no idea to detect the change procedure of Combobox1 when it is programmatic.

Can someone provide me the solution?

When creating controls on the fly, VBA does not automatically create their Events!

There are two ways to do that. To create a events wrapper class, or simpler, in case of a known number of controls to be added (your case) to previously declare the controls in a specific way:

  1. Put the next declaration on top of the form code module (in the declarations area):
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. Then you should adapt your code, in a way to set the above declared variables as what you want/need them to be. So, please adapt your code in the next way:
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. Only now, the combo events will be triggered (if their code exists in the form code module). Please, firstly use the next short code example:
Private Sub ComboBox1_Change()
   MsgBox "Changed 1..."
End Sub

If you exactly followed the above suggestions, It should be surely triggered!

Now, you can put in its event code whatever you need...

Please, test it and send some feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM