VBA:第一次点击后在Excel中禁用button
我试图禁用放在我的sheet1上的button,一旦用户点击它。 我经历了几个老的Stackoverflow的答案,但剂量工作符合我的期望。
代码:
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
它实际上是灰色的button,让人觉得该button被禁用,但用户可以反复点击,它运行我的代码。
请让我知道一个解决scheme让button被禁用后,只有一次closures并重新打开Excel的button将被激活。 (我正在使用MS Office专业Plus 2013)
提前致谢。
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
Dim line后面只写:
if myshape.TextFrame.Characters.Font.ColorIndex = 15 then exit sub
这应该够了