我基本上是創建一個在運行時生成的項目列表。這些項目作爲標籤列在用戶窗體中(項目存儲在鏈接列表中)。對於每個項目,我想添加一個旋鈕,以便將項目上下移動。我的spinbuttons創建得很好,我編碼的事件不起作用?我不確定我做錯了什麼。可能是一些簡單的...VBA WithEvents不起作用
這就是類模塊保存的事件:cls_Spin_Btn
Private WithEvents Spin_Events As SpinButton
Private Sub Spin_Events_SpinUp()
Debug.Print "Hey. Spin button worked."
End Sub
Public Property Set SetNewSpinButtion(newSpinBtn As MSForms.SpinButton)
Set Spin_Events = newSpinBtn
End Property
這是代碼從一個模塊調用:
Function AddRunToForm(f As UserForm, r As ProductionRun, top As Integer) As Integer
Dim Run_SpinBtn As MSForms.SpinButton
Dim spinBtn As cls_Spin_Btn
Set Run_SpinBtn = f.Controls.Add("Forms.SpinButton.1", r.ProdID & "_SBtn", True)
Set spinBtn = New cls_Spin_Btn
With Run_SpinBtn
.top = ProdID_Lbl.top
.Left = 5
.height = 10
.Width = 12
.height = 18
.Visible = True
End With
Set spinBtn.SetNewSpinButtion = Run_SpinBtn
AddRunToForm = ProdID_Lbl.top + ProdID_Lbl.height
End Function
此代碼是從一個叫在同一個模塊中循環創建每個項目的標籤和旋鈕。我究竟做錯了什麼?任何幫助將非常感激。
你需要保持每'spinBtn'範圍(如)全局(模塊級)範圍。看到加里的答案在這裏:http://stackoverflow.com/questions/1083603/vba-using-withevents-on-userforms – 2012-08-07 16:11:44