SOLVED

How to count how many times a form button that has a macro assigned to it has been clicked!?

Copper Contributor

Hello everyone,

 

I need your help with something, I was searching for a way to count how many times a form button has been clicked knowing that the form button has a macro assigned to it.

Also while searching I found this code and couldn't really understand it nor know how to use it for my request

Any ideas, please?

 

Dim clicked As Integer 'Declare global integer

Sub buttonClicked() clicked = clicked + 1 'Function to increase +1 End Sub

Sub Button1_Click() buttonClicked 'Here call the function on each button click

MsgBox clicked

End Sub

1 Reply
best response confirmed by EzzEldean (Copper Contributor)
Solution

@EzzEldean 

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
 
1 best response

Accepted Solutions
best response confirmed by EzzEldean (Copper Contributor)
Solution

@EzzEldean 

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
 

View solution in original post