0
我需要創建將在運行時創建的代碼CommandButton
。這個命令按鈕是動態的,因爲它基於用戶數據。在運行時創建的命令按鈕代碼不起作用[Excel VBA]
User_From代碼
Private Sub UserForm_Activate()
Dim ctlTXT As Control
For RevNo = 1 To RevCounter
Set ctlTXT = Me.Controls.Add("Forms.CommandButton.1")
ctlTXT.name = RevNo
ctlTXT.Caption = Sheet4.Range("D" & RevNo + 4).value
ctlTXT.Left = 18
ctlTXT.Height = 18: ctlTXT.Width = 72
ctlTXT.Top = 15 + ((RevNo - 1) * 25)
Next
Me.Height = (RevNo * 17) + 50
ReDim Preserve cmdArray(1 To RevNo)
Set cmdArray(RevNo).CmdEvents = ctlTXT
Set ctlTXT = Nothing
End Sub
類模塊代碼
Private Sub CmdEvents_Click()
Dim i As Integer
i = CmdEvents.name
RevisionFormPrevious.LblResponsible.Caption = Sheet4.Range("C" & i +4).value
RevisionFormPrevious.LblEdition.Caption = Sheet4.Range("D" & i + 4).value
RevisionFormPrevious.LblTelNo.Caption = Sheet4.Range("E" & i + 4).value
RevisionFormPrevious.LblFeatures.Caption = Sheet4.Range("D" & i + 4).value
RevisionFormPrevious.Features.value = Sheet4.Range("F" & i + 4).value
Load RevisionFormPrevious
RevisionFormPrevious.Show
End Sub
的問題是,如果有創建了多個按鈕,該代碼只能用於創建最後一個按鈕當第一個和第二個按鈕被點擊時,什麼都沒有發生。
RevCounter在哪裏聲明,填充和調用時它的值是什麼? –
你的班級的名字是什麼?在你的'Class'代碼中,你需要在頂部添加'Public WithEvents CmdEvents As MSForms.CommandButton',然後,在你的'Sub'中,你需要使用'Dim ctlTXT As MSForms.CommandButton'來定義它。 –
@MarkFitzgerald RevCounter is在另一個模塊中聲明爲公共。調用時的值是可用的修訂版本總數 –