Nikhil Chilwant
Nikhil Chilwant

Reputation: 649

Press other button2 when button 1 is pressed EXCEL VBA

I have 3 toggle buttons as shown in Excel VBA. First I press continuous then it will be pressed and start running macro. During that if I need to press Emergency button I need to do :

step1: Switch off Continuous button

step2 : Switch on Emergency Button ( manual is irrelevent here) Forget about manual button

Currently when I try to do this, Emergency Stop cannot be pressed until macro of continuous ends.

Upvotes: 1

Views: 489

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149277

I use this most of the time when I trying to say, scrape data from the net.

Option Explicit

Dim boolStop As Boolean

'~~> Continuous button
Private Sub CommandButton1_Click()
    CommandButton1.Enabled = False

    boolStop = False

    For i = 1 To 1000000000
        For j = 1 To 1000000000
            Debug.Print i
            DoEvents

            If boolStop = True Then
                Msgbox "Operation Paused by the User"
                CommandButton1.Enabled = True
                Exit Sub
            End If
        Next j
    Next i

End Sub

'~~> Emergency button
Private Sub CommandButton3_Click()
    boolStop = True
End Sub

Upvotes: 2

Related Questions