首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VBA >

在EXCEL的SHEET中动态增加一个BUTTON解决方案

2012-03-08 
在EXCEL的SHEET中动态增加一个BUTTON我想在EXCEL的SHEET中动态的增加一个BUTTON,ActiveSheet.OLEObjects.A

在EXCEL的SHEET中动态增加一个BUTTON
我想在EXCEL的SHEET中动态的增加一个BUTTON,
ActiveSheet.OLEObjects.Add方法只能增加COMMANDBUTTON1,我想增加一个名叫btnTest的BUTTON,Caption为 "测试 ",该如何实现!
哪位朋友指点一下,谢了!

[解决办法]
Dim sheet As Worksheet

Set sheet = ActiveSheet
Dim btn As CommandButton
Dim ole As OLEObject
Set ole = sheet.OLEObjects.Add(classtype:= "Forms.CommandButton.1 ", _
Link:=False, DisplayAsIcon:=False, Left:=40, Top:=40, _
Width:=80, Height:=20)
Set btn = ole.Object
btn.Name = "btnTest "
btn.Caption = "test "
[解决办法]
Private Sub CommandButton1_Click()
Dim btnTest As Control
Set btnTest = Worksheets(1).Controls.Add( "Forms.CommandButton.1 ", "btnTest ", True)
btnTest.Left = 18
btnTest.Top = 150
btnTest.Width = 175
btnTest.Height = 20
btnTest.Caption = "测试 "
End Sub

[解决办法]
向窗体上添加控件Forms.CommandButton.1为CommandButton的ProgID,如果想添加其它控件可以查询ProgID替换Forms.CommandButton.1。
Private Sub CommandButton1_Click()
Dim btnTest As Control
Set btnTest = Controls.Add( "Forms.CommandButton.1 ", "btnTest ", True)
btnTest.Left = 18
btnTest.Top = 150
btnTest.Width = 175
btnTest.Height = 20
btnTest.Caption = "测试 "
End Sub

热点排行