VBA Check if button has been clicked

button, click, vba

Solution

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

Problem

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 ```

Original source