我正在使用这个: 如何在 excel 中创建动态按钮
它创建一个按钮并将一个宏绑定到它并且运行良好。
我可以将它作为一个独立的宏运行,但如果我尝试调用它
Call CreateDynamicButton
什么都没有发生,是否可以调用它?
谢谢
Sub CreateDynamicButton()
Dim MyR As Range, MyB As OLEObject
Dim MyR_T As Long, MyR_L As Long
Set MyR = Range("C110") 'just an example - you get that from your own script
MyR_T = MyR.Top 'capture positions
MyR_L = MyR.Left '...
'create button
Set MyB = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)
'set main button properties
With MyB
.Name = "MyPrecodedButton" 'important - code must exist ... see below
.Object.Caption = "MyCaption"
.Top = MyR_T
.Left = MyR_L
.Width = 50
.Height = 18
.Placement = xlMoveAndSize
.PrintObject = True 'or false as per your taste
End With
End Sub
如果 - 提前 - 您在活动工作表中创建了以下例程
Private Sub MyPrecodedButton_Click()
MsgBox "Co-Cooo!"
End Sub