我认为我们这里有一个适当的错误。 S Meaden 发布的解决方案不起作用(至少在我的测试中不起作用)。以下是我一起测试的内容:
Private Sub CommandButton1_Click()
Dim w As Date
Me.CommandButton1.Enabled = False
w = Now + TimeSerial(0, 0, 2)
Debug.Print "point 1: " & Now
Application.Wait w
Debug.Print "point 2: " & Now
Me.CommandButton1.Enabled = True
End Sub
单击它会使其灰显(禁用时应如此)并运行例程。但是,单击两次会运行该例程两次。因为它打印时间,很明显例程是按顺序运行的,所以它接缝excel(在我的例子中是excel,没有用其他应用程序测试过)记住点击,当例程完成时(并且按钮被启用再次)调用例程。它也连续运行 3 或 4 次。
因此,实现 S Meaden 的答案,如下所示:
Dim clicked as Boolean
Private Sub CommandButton1_Click()
Dim w As Date
If Not clicked Then
clicked = True
Me.CommandButton1.Enabled = False
w = Now + TimeSerial(0, 0, 2)
Debug.Print Now
Application.Wait w
Debug.Print "punt 2 (" & Now & ")"
Me.CommandButton1.Enabled = True
clicked = False
End If
End Sub
也不行。
似乎如果在例程完成后启用按钮,则在例程执行期间放置的点击被丢弃。因此,作为一种解决方法,您可以使用:
Private Sub CommandButton1_Click()
Dim w As Date
Me.CommandButton1.Enabled = False
w = Now + TimeSerial(0, 0, 2)
Debug.Print "point 1: " & Now
Application.Wait w
Debug.Print "point 2: " & Now
Me.Button1_clicked = False
Application.OnTime (Now + 0.000001), "enable_commandbutton"
End Sub
“enable_commandbutton”是:
Public Sub enable_commandbutton()
Dim uf As Object
Debug.Print "check"
For Each uf In VBA.UserForms
If uf.Name = "UserForm1" Then
uf.CommandButton1.Enabled = True
End If
Next uf
End Sub
在一个普通的代码模块中。
它不漂亮,但它有效。