Board logo

標題: [發問] 關於 UserForm1.Controls.Add "Forms.CommandButton.1" 問題請教 [打印本頁]

作者: pinklee    時間: 2013-5-31 16:24     標題: 關於 UserForm1.Controls.Add "Forms.CommandButton.1" 問題請教

ii = Sheets("sheet2").Range("m1").Value
For ii = 1 To ii
    UserForm1.Controls.Add "Forms.CommandButton.1", "CommandButton" & ii, True
Next
以上執行結果如下圖

[attach]15120[/attach]
請教各位先拜 如何按 各個 CommandButton 執行巨集呢?
是否要用到  物件特別模組  有勞大大囉   謝謝!!
作者: sunnyso    時間: 2013-5-31 23:08

回復 1# pinklee
  1. Private Sub UserForm_Initialize()
  2.     Dim fcb As MSForms.CommandButton
  3.     fcbWidth = 30
  4.     fcbNum = 5
  5.     For ii = 1 To fcbNum
  6.         Set fcb = UserForm1.Controls.Add("Forms.CommandButton.1", "CommandButton" & ii, True)
  7.         fcb.Width = fcbWidth
  8.         fcb.Left = (ii - 1) * fcbWidth
  9.         fcb.Caption = "C" & ii
  10.     Next
  11. End Sub
複製代碼

作者: sunnyso    時間: 2013-6-1 00:13

本帖最後由 sunnyso 於 2013-6-1 00:19 編輯

回復 1# pinklee
  1. Dim AllBtn() As New Class1

  2. Private Sub UserForm_Initialize()
  3.     Dim fcb As MSForms.CommandButton
  4.     Dim fcbNum As Integer
  5.     fcbWidth = 30
  6.     fcbNum = 5 'Sheets("總表").Range("A1").Value
  7.     ReDim AllBtn(1 To fcbNum)
  8.     For ii = 1 To fcbNum
  9.         Set fcb = UserForm1.Controls.Add("Forms.CommandButton.1", "CommandButton" & ii, True)
  10.         Set AllBtn(ii).Btn = fcb
  11.         fcb.Width = fcbWidth
  12.         fcb.Left = (ii - 1) * fcbWidth
  13.         fcb.Caption = "C" & ii
  14.     Next
  15. End Sub
複製代碼
物件類別模組
  1. Public WithEvents Btn As MSForms.CommandButton

  2. Private Sub Btn_Click()
  3.     'Dim ctrl As Control
  4.     'With UserForm1
  5.         'your code here
  6.         MsgBox Btn.Caption
  7.     'End With
  8. End Sub
複製代碼

作者: pinklee    時間: 2013-6-3 08:04

回復 3# sunnyso


    感謝您的指導  我先研究一下
作者: pinklee    時間: 2013-6-3 09:57

回復 3# sunnyso


    感謝您 已經可以使用




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)