Starting a new VBA procedure

This is the code framework l use when building a new procedure.
It includes error handling and timers.’ My approach is to include everything at the outset and strip out what l don’t want.
Author: michael@excelexperts.co.uk

Sub NEW_Procedure()

   	Preperation and error trapping
    Const PROC_NAME As String = "NEW_Procedure"
    If myerrors Then On Error GoTo myerrortrap
    bEXITALL = False
    startTime = Time

   	Declare variables:

   	Main code:
Call TurnOff	'<< Procedure controlling Excel environment
    
myexit:
   	Don't forget to release variables and arrays (ie Set myvariable = Nothing, Erase myarray)
    Call TurnOn	'<< Procedure controlling Excel environment
    If showtimer = True Then Debug.Print MOD_NAME &amp; " : " &amp; PROC_NAME &amp; " Elapsed time: " &amp; Format(Time - startTime, "hh:mm:ss")
    If bEXITALL = False Then
        mymsg = MsgBox("Success")
    Else
        mymsg = MsgBox("Failed")
    End If
    Exit Sub

myerrortrap:
   	If errortrap sub contains END &amp; TURNON please remove from sub errortrap
    Call errortrap(MOD_NAME, PROC_NAME, Erl)
    If myerrorlog = True Then Call CentralErrorHandler(PROC_NAME, "An error occurred when running the Example code.", Err.Number, Err.Description)
    bEXITALL = True
    Resume myexit

End Sub

Address

Brookfield,
55 Heath Drive
Brookwood, Surrey
GU24 0HQ England

Scroll to Top