[VBA] Break loop when button is clicked

chungyin383

New Member
Joined
Dec 31, 2016
Messages
5
I am building a simple lucky draw Excel.

I have a user form with 2 buttons: start and stop. When "start" is clicked, a number is drawn and displayed (this part is done) and this is continued until the user clicks "stop" (This is the part I don't know how to implement).

Any ideas on how to make it?

What I have right now:
Code:
Private Sub start_button_Click()

Do While True
        label1.Caption = Application.WorksheetFunction.RandBetween(1, 100)
        form1.Repaint
Loop


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

chungyin383

New Member
Joined
Dec 31, 2016
Messages
5
OK I have found the answer:
Code:
DoEvenets

Simply add this to the loop to avoid the blocking of UI thread.

I am building a simple lucky draw Excel.

I have a user form with 2 buttons: start and stop. When "start" is clicked, a number is drawn and displayed (this part is done) and this is continued until the user clicks "stop" (This is the part I don't know how to implement).

Any ideas on how to make it?

What I have right now:
Code:
Private Sub start_button_Click()

Do While True
        label1.Caption = Application.WorksheetFunction.RandBetween(1, 100)
        form1.Repaint
Loop


End Sub
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
18,714
Office Version
  1. 2013
Platform
  1. Windows
I was looking at this but did not have an answer.

Show me what you mean.

Do you mean by adding DoEvenets to this script

Another script can run which would stop to other script.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,426
Messages
5,837,162
Members
430,480
Latest member
iangessey

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
Top