|
一、插入一个标准模块,声明一个公共数组变量
'撰写:老朽
'网址:http://Club.ExcelHome.net
'日期:2009-7-16 下午 12:34:52
Public Cmb(1 To 10) As New COMDs
二、插入一个类模块,加入如下代码
Option Explicit
Public WithEvents CB As MSForms.CommandButton
'撰写:老朽
'网址:http://Club.ExcelHome.net
'日期:2009-7-16 下午 12:35:07
Private Sub CB_Click()
[a1] = "你点击了: " & Replace(CB.Name, "CommandButton", "按钮") & " !"
With CB
.BackColor = IIf(.BackColor = -2147483633, RGB(255, 0, 40), -2147483633)
End With
End Sub
三、使用工作簿的Open事件,执行初始化
'撰写:老朽
'网址:http://Club.ExcelHome.net
'日期:2009-7-16 下午 12:35:18
Private Sub Workbook_Open()
Set Cmb(1).CB = Sheet1.CommandButton1
Set Cmb(2).CB = Sheet1.CommandButton2
Set Cmb(3).CB = Sheet1.CommandButton3
Set Cmb(4).CB = Sheet1.CommandButton4
Set Cmb(5).CB = Sheet1.CommandButton5
Set Cmb(6).CB = Sheet1.CommandButton6
Set Cmb(7).CB = Sheet1.CommandButton7
Set Cmb(8).CB = Sheet1.CommandButton8
Set Cmb(9).CB = Sheet1.CommandButton9
Set Cmb(10).CB = Sheet1.CommandButton10
End Sub
四、在工作表中,插入10个按钮
之后,退出设计模式,点击按钮试试! |
|