[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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

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
17,252
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,474
Members
414,070
Latest member
DuncanLucas

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