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

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

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