VBA - Application.EnableCancelKey is acting somehow strange -
pretty question following - why in immediate window this:
application.enablecancelkey = 2 ?application.enablecancelkey 1
the last line should 2 or missing something? thanks!
edit:
the basis enum xlerrorhandler
states "the interrupt sent running procedure error, trappable error handler set on error goto statement.", need error handler use enum (xlerrorhandler
or 2
).
since statements not possible within immediate window, , on error goto
1 of these statements, cannot have error handler in immediate window , change value of enablecancelkey
2
. hence excel switches automaticaly 1
, giving value 1
when ask display value.
the solution use sub.
original reply:
ok, used code in documentation provided on msdn , edited few things test following code.
to explain shortly stopped execution pressing once "esc" button , preventing textbox exited. can watch in immediate window last enablecancelkey has changed in value normaly.
sub test1() debug.print " before execution result : " & application.enablecancelkey on error goto handlecancel application.enablecancelkey = 2 'xlerrorhandler debug.print " regular execution result : " & application.enablecancelkey x = 1 10000 ' 1,000,000 times (long!) debug.print "test" next x handlecancel: if err = 18 debug.print "aborted macro result : " & application.enablecancelkey msgbox "you cancelled" end if end sub
hope helps, got result expected.
you have write error handler 2 value, else code cannot catch error, that's why 1 every time executing macro.
since description of enum xlerrorhandler
states "the interrupt sent running procedure error, trappable error handler set on error goto statement.", need error handler use enum.
with should work:
sub test2() on error goto theend application.enablecancelkey = 2 debug.print application.enablecancelkey theend: end sub
Comments
Post a Comment