[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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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