Forum Discussion
How to count how many times a form button that has a macro assigned to it has been clicked!?
- Nov 19, 2022
I also replied to your PM.
The variable clicked is declared above and outside the macros, so it is "known" to all macros in the same module.
The macro buttonClicked takes the current value of the variable clicked, and adds 1 to it.
So the first time buttonClicked is called, the value of clicked increases from the default value 0 to 1; the second time it increases from 1 to 2, etc.
Sub buttonClicked() clicked = clicked + 1 'Function to increase +1 End Sub
The macro Button1_Click is assigned to the Forms command button Button1. It calls buttonClicked, increasing the value of clicked by 1, then displays the value of clicked in a message box.
Sub Button1_Click() buttonClicked 'Here call the function on each button click MsgBox clicked End Sub
I also replied to your PM.
The variable clicked is declared above and outside the macros, so it is "known" to all macros in the same module.
The macro buttonClicked takes the current value of the variable clicked, and adds 1 to it.
So the first time buttonClicked is called, the value of clicked increases from the default value 0 to 1; the second time it increases from 1 to 2, etc.
Sub buttonClicked()
clicked = clicked + 1 'Function to increase +1
End Sub
The macro Button1_Click is assigned to the Forms command button Button1. It calls buttonClicked, increasing the value of clicked by 1, then displays the value of clicked in a message box.
Sub Button1_Click()
buttonClicked 'Here call the function on each button click
MsgBox clicked
End Sub