运行时'13'错误types不匹配当试图以编程方式创build用户窗体button

Private Sub UserForm_Initialize() Dim cCont As Control Set cCont = Me.Controls.Add _ ("Forms.CommandButton.1", "CopyOf") With cCont .Caption = "Thanks for creating me" .AutoSize = True End With End Sub 

上面的代码给我一个运行时错误“13”types不匹配错误

但是,如果我这样做:

 Private Sub UserForm_Initialize() Me.Controls.Add _ "Forms.CommandButton.1", "CopyOf" end sub 

命令button被创build,没有错误。 我想创build几个命令button,因此,我希望上面的第一段代码能够工作。 我怎么能这样做?

这是在Excel 2010中,Windows 7 x64

看这个:

 Private Sub UserForm_Initialize() With Controls.Add("Forms.CommandButton.1", "CopyOf") .Caption = "Thanks for creating me" .AutoSize = True End With With Controls.Add("Forms.CommandButton.1", "cmd2nd") .Top = 25 .Caption = "Me too..." .AutoSize = True End With End Sub 

另外,如果你真的想使用对象variables,那么你会更好地使用特定的types:

 Dim cCont As MSForms.CommandButton