引用用户输入后刚刚创建的ComboBox
Refer to ComboBox that is just created after user input
我正在尝试让 VBA 创建一个用户表单,并根据您选择的选项填充更多选项。
以下代码是我的初始用户表单,我让它根据选择的内容添加组合框。
Private Sub UserForm_Initialize()
ComboBox1.AddItem "Selection1"
ComboBox1.AddItem "Selection2"
ComboBox1.AddItem "Selection3"
ComboBox1.FontSize = 13
End Sub
但是我要通过 comboboxchange 来做到这一点
Private Sub ComboBox1_Change()
'Here i have some Working Code That Adds another ComboBox
Dim MsgType As Control
Set MsgType = UserForm2.Controls.Add("Forms.ComboBox.1")
With MsgType
.Height = 25
.Width = 300
.Top = 75
.Left = 20
.FontSize = 13
.Name = "vmtype"
.AddItem "Selection1"
.AddItem "Selection2"
Debug.Print .Value
End With
EndSub
我现在需要像“Private Sub ComboBox1_Change()”一样引用此组合框的 private sub。这样我就可以根据该选择添加更多项目。
到目前为止我有以下内容。
Private Sub ComboBox2_Change()
Dim Notetype As String
Notetype = ComboBox1.Value
Debug.Print Notetype
End Sub
但它实际上并不是指新创建的组合框,不知道我该如何解决这个问题?
使用WithEvents
侦听动态添加的控件。
这是一个简单的例子,您只添加了一个控件:
Private WithEvents vmtype As MSForms.ComboBox
Private Sub vmtype_Change()
Debug.Print MsgType
End Sub
Private Sub ComboBox1_Change()
Set vmtype = UserForm2.Controls.Add("Forms.ComboBox.1")
With vmtype
.Height = 25
.Width = 300
.Top = 75
.Left = 20
.FontSize = 13
.Name = "vmtype"
.AddItem "Selection1"
.AddItem "Selection2"
End With
End Sub
添加多个控件时考虑使用自定义 class。看我的post:Responding to Events of Dynamically added Controls
我正在尝试让 VBA 创建一个用户表单,并根据您选择的选项填充更多选项。
以下代码是我的初始用户表单,我让它根据选择的内容添加组合框。
Private Sub UserForm_Initialize()
ComboBox1.AddItem "Selection1"
ComboBox1.AddItem "Selection2"
ComboBox1.AddItem "Selection3"
ComboBox1.FontSize = 13
End Sub
但是我要通过 comboboxchange 来做到这一点
Private Sub ComboBox1_Change()
'Here i have some Working Code That Adds another ComboBox
Dim MsgType As Control
Set MsgType = UserForm2.Controls.Add("Forms.ComboBox.1")
With MsgType
.Height = 25
.Width = 300
.Top = 75
.Left = 20
.FontSize = 13
.Name = "vmtype"
.AddItem "Selection1"
.AddItem "Selection2"
Debug.Print .Value
End With
EndSub
我现在需要像“Private Sub ComboBox1_Change()”一样引用此组合框的 private sub。这样我就可以根据该选择添加更多项目。
到目前为止我有以下内容。
Private Sub ComboBox2_Change()
Dim Notetype As String
Notetype = ComboBox1.Value
Debug.Print Notetype
End Sub
但它实际上并不是指新创建的组合框,不知道我该如何解决这个问题?
使用WithEvents
侦听动态添加的控件。
这是一个简单的例子,您只添加了一个控件:
Private WithEvents vmtype As MSForms.ComboBox
Private Sub vmtype_Change()
Debug.Print MsgType
End Sub
Private Sub ComboBox1_Change()
Set vmtype = UserForm2.Controls.Add("Forms.ComboBox.1")
With vmtype
.Height = 25
.Width = 300
.Top = 75
.Left = 20
.FontSize = 13
.Name = "vmtype"
.AddItem "Selection1"
.AddItem "Selection2"
End With
End Sub
添加多个控件时考虑使用自定义 class。看我的post:Responding to Events of Dynamically added Controls