【问题标题】:VBA: Disable button in Excel after first clickVBA:第一次单击后禁用 Excel 中的按钮
【发布时间】:2017-12-05 23:23:09
【问题描述】:

一旦用户单击它,我试图禁用放置在我的 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)

提前致谢。

【问题讨论】:

  • 为什么不保留一个变量来记住它已被点击?
  • 谢谢@S Meaden 你能告诉我怎么做吗

标签: vba excel


【解决方案1】:

您需要以某种方式记录以不处理点击。您可以检查按钮的文本颜色,如果是灰色则忽略单击。或者,您可以将变量设置为全局变量、模块变量或静态局部变量。我在下面的代码中选择了静态局部变量,称为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

【讨论】:

    【解决方案2】:

    在暗线之后只写:

    if myshape.TextFrame.Characters.Font.ColorIndex = 15 then exit sub
    

    应该够了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      相关资源
      最近更新 更多