application.onkey doesn't work when for...next... is running

jackni

Board Regular
Joined
Feb 10, 2011
Messages
73
Hello, Everyone. In my Test.xlsm Excel workbook.
thisworkbook code is :
sub workbook_open()
Application.OnKey "{ESC}", "a"​
end sub

the module code is :
sub a()
thisworkbook.close savechanges:=fasle​
end sub
sub b()
for i=1 to 1048576​
sheet1.cells(i,1)=i​
doevents​
next​
end sub

Now, here is my question,when sub b is not running, if you press {ESC}, this workbook will be closed normally.
but when sub b is running, if you press {ESC}, Excel doesn't responding.
WHY? Can someone here help me, Thank you !!!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
The loop seems to lock the Onkey event .

Try this and see if it works for you :

Code:
#If VBA7 Then
    Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Sub b()
    For i = 1 To 1048576
        Sheet1.Cells(i, 1) = i
        MyDoEvents
    Next
End Sub

Sub MyDoEvents()
    DoEvents: If GetAsyncKeyState(vbKeyEscape) Then Call a: End
End Sub
 
Upvote 0
The loop seems to lock the Onkey event .

Try this and see if it works for you :

Code:
#If VBA7 Then
    Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Sub b()
    For i = 1 To 1048576
        Sheet1.Cells(i, 1) = i
        MyDoEvents
    Next
End Sub

Sub MyDoEvents()
    DoEvents: If GetAsyncKeyState(vbKeyEscape) Then Call a: End
End Sub


Hi,Jaafar Tribak
IT WORKED!!!
THANK YOU VERY MUCH! The application.onkey finally works under any circumstances. Thank you. You are very kind.
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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