返回列表 上一主題 發帖

[發問] 關於 UserForm1.Controls.Add "Forms.CommandButton.1" 問題請教

[發問] 關於 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
以上執行結果如下圖


請教各位先拜 如何按 各個 CommandButton 執行巨集呢?
是否要用到  物件特別模組  有勞大大囉   謝謝!!

回復 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
複製代碼
ss

TOP

本帖最後由 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
複製代碼
ss

TOP

回復 3# sunnyso


    感謝您的指導  我先研究一下

TOP

回復 3# sunnyso


    感謝您 已經可以使用

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題