Command Button - Auto run

dcaraher

New Member
Joined
Jun 7, 2010
Messages
9
I have a command button with a click counter in the caption.

I am using it to recalculate a sheet which contains two random generated numbers between 0 and 36.

I would like to add code which runs the event on click until both the random generated numbers match. Statistically this has odds of over 1,300 -1 so I want to avoid having to click the button 1,300+ times.

Thanks for any advice.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This looks like it should be a loop until statement but not sure how to make it work.

My code so far is:

Code:
Private Sub CommandButton1_Click()
Sheet1.Calculate
Static cnt As Long
 cnt = cnt + 1
 Me.CommandButton1.Caption = "I have been clicked " & cnt & " times"
End Sub

Any help appreciated
 
Upvote 0
I think I have solved the initial problem. Not the most elegant but it works. I created a helper cell which used an if statement with the result if the two cells matched it said "STOP"

I then used the following code:
Code:
Private Sub CommandButton1_Click()
Do Until Range("D2").Value = "STOP"
Sheet1.Calculate
Static cnt As Long
 cnt = cnt + 1
 Me.CommandButton1.Caption = "I have been clicked " & cnt & " times"
 Loop
 End Sub

My new problem is how can I reset the counter without closing the workbook?
 
Upvote 0
Welcome to the Board!

My new problem is how can I reset the counter without closing the workbook?

Why not just add Range("D2").Value = "" at the beginning of the code?
 
Upvote 0
Thanks very much for the response.

I have tried adding at the beginning of the code and I end up with a continuous loop. It is obviously me doing something wrong.

What I'm trying to achieve is let the code run, get the number of "clicks" from the counter, then reset the counter to zero

Where should I place the code.

Thanks again
 
Upvote 0
The code will keep going until your Stop condition is met. Does it ever get to "Stop"?
 
Upvote 0
Yes when the two random generated numbers match, the IF statement shows "STOP" and the code stops and gives me the number of clicks.

I am unable to reset the counter without closing and saving the workbook.

Thanks
 
Upvote 0
If you add this:

Range("D2").Value = ""

What happens when you step through the code?
 
Upvote 0

Forum statistics

Threads
1,203,145
Messages
6,053,750
Members
444,681
Latest member
Nadzri Hassan

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