【发布时间】:2021-07-17 12:39:15
【问题描述】:
我以编程方式在 Multipage1 中添加一个新页面
Private Sub CommandButton3_Click()
Dim i As Integer
i = MultiPage1.Pages.Count
MultiPage1.Pages.Add.Caption = "Guarantee " & i
然后我希望我的新页面包含 3 个 ComboBoxes,ComboBox1 将在我的工作表名称“LeftTB”上列出表格中的项目,这是代码。
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编程时,我不知道检测它的变化过程。
谁能给我解决方案?
【问题讨论】:
-
请尝试我发布的解决方案并发送一些反馈。
-
感谢FaneDuru,它在新的添加页面上运行良好,但在前一页上不起作用。它只检测添加的最后一页。
-
我刚刚想通了!!!在多页更改程序中再次设置 ComboBox4-6,非常感谢 FaneDuru。
-
如果您尝试使用相同的对象,只有它们的最后一组会像您的代码那样设置它们......使用相关名称应该很好。第 1 页的
ComboBoxP1_1与第二页的ComboBoxP2_1之类的东西......很高兴你能解决它!您最好从自己可以找到解决方案的问题中学习。
标签: excel vba combobox multipage