我试图禁用一个按钮放在我的sheet1上,一旦用户点击它。我经历了几次旧的Stackoverflow answers,但我的期望是大量的工作。
代码:
Sub Button2_Click()
Call FindADate
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = False '---> Disable the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
End Sub
它实际上是灰色的按钮,这让人觉得按钮是禁用的,但用户可以一次又一次地单击,并运行我的代码。
请让我知道一个解决方案,使按钮被禁用后,1点击,该按钮将再次活动,只有在我关闭和重新打开excel。(我正在使用MS Office professional Plus 2013)
提前谢谢。
发布于 2017-12-05 23:29:44
您需要以某种方式记录不处理单击。您可以检查按钮的文本颜色,如果是灰色,则忽略单击。或者,您可以设置一个变量--一个全局变量、一个模块变量或一个静态局部变量。我在下面的代码中选择了静态局部变量,称为vDisable
。
Option Explicit
Sub Button2_Click()
Static vDisable As Variant
If IsEmpty(vDisable) Then
Call FindADate
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = False '---> Disable the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
vDisable = True
End If
End Sub
发布于 2017-12-05 23:31:01
在Dim行之后,只需写:
if myshape.TextFrame.Characters.Font.ColorIndex = 15 then exit sub
那应该就够了
发布于 2022-06-16 14:43:20
我知道这是个老生常谈的问题,但我也遇到过同样的情况,我用以下方法解决了这个问题:
With myshape
.OnAction = "" '---> Removes any macro from the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
这消除了对全局/静态变量的需求。
https://stackoverflow.com/questions/47664316
复制相似问题