VBA Check if button has been clicked

余生长醉 提交于 2019-12-07 09:44:24

问题


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.

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

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

回答1:


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


来源:https://stackoverflow.com/questions/17883988/vba-check-if-button-has-been-clicked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!