How to count the number of times a command button has been clicked

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
This post is somewhat similar to a previous post which I have not gotten a response yet. Since then I have figured out answers to my questions with the exception of this one. If I am wrong for doing this please let me know. Thank You
Brief explanation:That being said, I have a workbook that when loaded asks the user(through a messagebox) if this is a split lot, if the user clicks yes, an inputbox appears asking how many times. Then the userform loads for the first time. After they enter all the information on the form they click the submit button and the form reloads disabling certain textbox controls which the code is inside a sub called "disableCntrls". How can I count or if the button has been clicked so it can call the sub. Thank you
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You can do it in 2 ways.
The first is to start the form with a global variable, so each time you press the button you increase the variable by one.


Code:
Dim counter As Long

Private Sub CommandButton1_Click()
   counter = counter + 1
   MsgBox "Times : " & counter
End Sub

Private Sub UserForm_Initialize()
   counter = 0
End Sub

The second, you put the counter on a sheet in a cell, so each time you press the button you increase the cell by one.

Code:
Private Sub CommandButton1_Click()
   Sheets("SheetC").Range("A1").Value = Sheets("SheetC").Range("A1").Value + 1
   MsgBox "Times : " & Sheets("SheetC").Range("A1").Value
End Sub


Private Sub UserForm_Initialize()
   Sheets("SheetC").Range("A1").Value = 0
End Sub
 
Upvote 0
Or .. if you would like the counter directly on the button itself ... here is code with a button on the sheet.
You can modify it for use on the userform :

Code:
Option Explicit


Private Sub CommandButton1_Click()
    Dim arr
    Dim clks As String
    Dim wr As Long  'write row
    
arr = Split(CommandButton1.Caption, vbLf)
clks = arr(UBound(arr))
CommandButton1.Caption = "Number of clicks is" & vbLf & clks + 1


With Sheets("Sheet2")
    wr = .Cells(Rows.Count, "A").End(xlUp).Row + 1
    .Cells(wr, "A").Value = clks + 1
    .Cells(wr, "B").Value = Now
End With


End Sub
Private Sub CommandButton2_Click()
ResetClicks
End Sub


Sub ResetClicks()
Sheet1.CommandButton1.Caption = "Number of clicks is" & vbLf & "0"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top