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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -