Ejecutar Sub antes de que la macro termine como Bloque finalmente

Is there some VBA best practice to use something like 'before end' subroutines?

I am changing Excel's default configs when my macro starts, and before my macro reaches its 'end sub' line I am resetting the configs to its standards. But what if some error occurs? Am I supposed to define 'On Error' treatment inside all subs to reset the configs to the standard properties?

Just for example, I am changing configs such as:

ScreenUpdating
DisplayStatusBar
Calculation
DisplayAlerts

preguntado el 28 de mayo de 14 a las 14:05

I couldn't find a proper duplicate of this question, but you can see mi respuesta aqui. By resuming at the ExitSub: label, you can ensure whatever necessary clean up code is executed. -

This one looks like the duplicate you were looking for: ¿Necesitamos crear un controlador de errores para cada subrutina? -

I changed the Question tittle to better fit my purpose, and to better differentiate it from the one you suggested. -

2 Respuestas

I'm pretty sure there is no such mechanism that is called incondicionalmente before exiting a function or a subroutine. You may have though error handlers (but these are executed condicionalmente; see the comment of ckuhn203 para un ejemplo).

However, there is such a mechanism for instances of Class Modules (i.e. for objects). When an object is destroyed (this happens when is not referenced anymore by any variable/storage), its Class_Terminate subroutine is called no-matter-what. If you can wrap your task in such an object that you discard immediately after you create it, you could anular overwrite this subroutine to do the cleanup.

contestado el 23 de mayo de 17 a las 13:05

+1 I like this solution. As for discarding it immediately, you could use the With statement, E.g., With new UtilityClass .DoStuff() End With - LimaNocheHalcón

Knocking after 6 years, can you please add code to this post / share reference to a code that implements this? - Karthick Ganesan

If I understand your question correctly, yes, the best way is to define an On Error Goto line, in each method where it's needed, like this:

Public Sub DoSomething()

    On Error GoTo Finally ' Try
    Application.ScreenUpdating = False

    ' Do your stuff here

Finally:
    Application.ScreenUpdating = True


End Sub

This will ensure the things like ScreenUpdating get done even if there is an error. You can also add a catch block, like this:

Public Sub DoSomething()

    On Error GoTo Catch ' Try
    Application.ScreenUpdating = False

    ' Do normal stuff here

    GoTo Finally
Catch:

' Do only error stuff here

Finally:
    Application.ScreenUpdating = True
End Sub

Generalmente hablando, GoTo is a hated practice, but for error catching, VBA kind of forces your hand.

contestado el 28 de mayo de 14 a las 15:05

Im accepting your answer, in combination with @ckuhn203 comment and this link i found [link] So what im gonna do is use ckuhn's error handler, but restoring the default On Error treatment just after that. [link]: stackoverflow.com/questions/1038006/… - jSpeciale

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.