求VBA代码改变按钮属性

2024-12-29 00:18:25
推荐回答(4个)
回答1:

首先几个相关对象为:
sheet1 表中有两个按钮:CommandButton1 、 CommandButton2

前面两个问题,将以下代码复制入sheet1表的代码窗中,实现两个按钮状态切换:
--------
Private Sub CommandButton1_Click()
CommandButton2.Enabled = True
CommandButton1.Enabled = False
End Sub

Private Sub CommandButton2_Click()
CommandButton1.Enabled = True
CommandButton2.Enabled = False
End Sub
-----------
问题3,麻烦一些,需要增加一模块,模块中定义两个全局变量:

Public com1, com2

然后将下面的代码复制入thisWorkbook 的代码窗中:
--------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet1.CommandButton1.Enabled = com1
Sheet1.CommandButton2.Enabled = com2
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
com1 = Sheet1.CommandButton1.Enabled
com2 = Sheet1.CommandButton2.Enabled
End Sub

Private Sub Workbook_Open()
com1 = Sheet1.CommandButton1.Enabled
com2 = Sheet1.CommandButton2.Enabled
End Sub
----------
当打开excel时,或点保存(不关闭excel)时,记录两个按钮的状态
当关闭excel,设置按钮为前面记录的状态

回答2:

思路:
移动焦点在第3个控件是写按钮:A.Enable = False:按钮A.Enable = False
在Deactivate事件将按钮状态写在单元格中,打开表读按钮状态。

回答3:

Private Sub CommandButton1_Click()
CommandButton1.Enabled = False
CommandButton2.Enabled = True
End Sub
Private Sub CommandButton2_Click()
CommandButton1.Enabled = True
CommandButton2.Enabled = False
End Sub

回答4:

你说的很清楚了啊,想都想这么明白了,去写啊。