VBA 检查按钮是否被点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17883988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:16:11  来源:igfitidea点击:

VBA Check if button has been clicked

vbabuttonclick

提问by user2623046

I think this is an easy fix, but I've been stuck at this trouble spot for hours now.

我认为这是一个简单的解决方法,但我已经被困在这个麻烦点好几个小时了。

Basically in the excel worksheet I have two buttons assigned to macros: 1 and 2. If the user clicks 1 first and then clicks then 2, 2 runs off of some of the variables from 1. But I need 2 to be able to operate independently if 2 is clicked first. Therefore, I need some way in the 2 code to ask if 1 button has been clicked.

基本上在 Excel 工作表中,我有两个按钮分配给宏:1 和 2。如果用户先单击 1,然后单击 2,则 2 会从 1 中运行一些变量。但我需要 2 才能独立操作如果先点击 2。因此,我需要在 2 代码中以某种方式询问是否单击了 1 个按钮。

Both 1 and 2 are public subs. I think there is something that I am missing with the definitions, but I'm not sure.

1 和 2 都是公共潜艇。我认为我在定义中遗漏了一些东西,但我不确定。

Simple Sample:

简单示例:

Public Sub 1()
do this
End Sub

Public Sub 2()
If 1 clicked then
   process a
Else
   process b
End if
End Sub

回答by user2125348

Set a Public Boolean and use that

设置一个公共布尔值并使用它

For example

例如

Dim ButtonOneClick As Boolean 'Make sure this is before all subs

Sub Button1_Click()
ButtonOneClick = True
End Sub

Sub Button2_Click()
If ButtonOneClick Then
    MsgBox "Button 1 Was Clicked"
Else
    MsgBox "Button 1 Was NOT Clicked"
End If

ButtonOneClick = False
End Sub