我有一个包含以下代码模块的工作簿:
课程模块:clsComboBox
Option Explicit
Private WithEvents MyComboBox As MSForms.ComboBox
Public Property Set cmboControl(cmboBox As MSForms.ComboBox)
Set MyComboBox = cmboBox
End Property
Private Sub MyComboBox_Change()
If MyComboBox.Value = "2" Then
MsgBox "I will be so happy if this ever runs"
End If
End Sub
UserForm:UserForm1(包含一个按钮,CommandButton1)
Option Explicit
Private Sub CommandButton1_Click()
Dim ctlcombobox As ComboBox
Dim clsobject As clsComboBox
Set ctlcombobox = Me.Controls.Add("Forms.ComboBox.1", "ComboBox1", True)
Set clsobject = New clsComboBox
Set clsobject.cmboControl = ctlcombobox
With ctlcombobox
.Top = 100
.Left = 5
.Width = 200
.Height = 25
.Visible = True
.List = Array("1", "2")
End With
End Sub
我试图用我动态创建的每个新组合框覆盖combobox_change事件。但是,代码不起作用。在组合框中选择“2”时,没有任何反应。我做错了什么?
示例工作簿:
https://www.dropbox.com/s/ctr51lote86fbox/test%20combo%20box.xlsm?dl=0
修改的
谢谢,蒂姆,答案。我必须创建一个可变长度数组来保存我在内存中动态创建的所有组合框。 (最初的问题是关于单个组合框,但在我的实际工作表中,我在运行时创建了一个未知数字。)答案 0 :(得分:2)
Dim clsobject As clsComboBox
这需要是一个全局变量:在您当前的代码中,只要CommandButton1_Click
退出,它就会超出范围。如果你想捕获以后的事件,它需要持久存在。
答案 1 :(得分:1)
正如蒂姆所说 - 只需移动一行代码:
<!-- Must be set to pass instance vars to javascript -->
<%= javascript_tag "var js_marker_lat = #{@location.latitude.to_json};" %>
<%= javascript_tag "var js_marker_lng = #{@location.longitude.to_json};" %>