Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I stop a loop when the right mouse button is selected? 4

Status
Not open for further replies.

JensKKK

Technical User
May 8, 2007
119
GB
How can I interrupt the execution of a do while loop when the right mousebutton was selected.

Any help appreciated.
 
Using DoEvents with event driven breaking:
Code:
Public WithEvents xlWbk As Workbook

Private Sub xlWbk_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
If MsgBox("Do you want to stop(break) the code", vbYesNo + vbExclamation, "a code is running") = vbYes Then
    Set Me.xlWbk = Nothing
    End
    ' 'Stop' to break the code
    ' however, xlWbk object has to be handled differently
End If
End Sub
Code:
Sub EndlessLoop()
Set ThisWorkbook.xlWbk = ThisWorkbook
Do
    DoEvents
Loop
End Sub

combo
 
Cheers Combo.

That is what I really needed.

Thanks again.
 
Thanks, however my above code is rather brute. In practice I would rather set global value in the event procedure, that would be checked in the rest of procedure with loop - there may be need to clear variables, set output data etc. The samples were in other replies.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top