Skip to content

Instantly share code, notes, and snippets.

@vascoferreira25
Last active March 3, 2019 20:38
Show Gist options
  • Save vascoferreira25/7e4a14670fe804c73cf88443eb2227b6 to your computer and use it in GitHub Desktop.
Save vascoferreira25/7e4a14670fe804c73cf88443eb2227b6 to your computer and use it in GitHub Desktop.
'******************************************************************************
' Author: TODO
' Description: TODO
' Version: 0.1
' Instructions: TODO
' Revisions: TODO
' - Date: yyyy/mm/dd
' - Author:
' - Description:
' @Folder("Folder.Subfolder")
Option Explicit
'******************************************************************************
Public Const SOME_MODIFIER As Integer = 42
'******************************************************************************
' Description: Increase performance by disabling the screen update
' and automatic calculations.
'******************************************************************************
Sub ToggleExcelUpdates(toggle As Boolean)
Application.ScreenUpdating = toggle
Application.DisplayStatusBar = toggle
Application.EnableEvents = toggle
If toggle Then
Application.Calculation = xlAutomatic
Application.Calculate
Else
Application.Calculation = xlManual
End If
End Sub
'******************************************************************************
' Description: Executes cleanup code at the end of each Sub, re-activates the
' current worksheet and shows the Sub runtime.
' It can be made public to handle cleanup code on other modules.
' Arguments: currentWorksheet
' startingTime - value of starting time
'******************************************************************************
Private Sub Cleanup(currentWorksheet As Worksheet, startingTime As Double)
' Re-enable Screen update and automatic calculations
ToggleExcelUpdates True
' Re-activate current worksheet
currentWorksheet.Activate
' Show executionRuntime
' MsgBox "Execution time: " & _
(Timer - startingTime) & " seconds.", _
vbOkOnly + vbInformation, "Procedure Execution Time"
Debug.Print "Execution time: " & (Timer - startingTime) & " seconds."
End Sub
'******************************************************************************
' Description: Handles all the errors and executes cleanup code afterwards
' It can be made public to handle cleanup code on other modules.
' Arguments: currentWorksheet
' startingTime - value of starting time
'******************************************************************************
Private Sub ErrHandler(currentWorksheet As Worksheet, startingTime As Double)
' Handle specific errors
Select Case Err.Number
Case 0
' No error
End Select
' Show the Error Handling form with the error number and message
'frmErrorHandling.DisplayErrorForm Err.Number, Err.Description
Debug.Print "+++ Error: " & Err.Number & ": " & Err.Description
Cleanup currentWorksheet, startingTime
End Sub
'******************************************************************************
' Description:
' Arguments:
' Returns:
'******************************************************************************
Sub ExampleSub()
' Turn off screen update and automatic calculations
ToggleExcelUpdates False
' Start Sub timer
Dim executionRuntime As Double
executionRuntime = Timer
Dim currentWorkbook As Workbook
Dim currentWorksheet As Worksheet
' `ThisWorkbook` won't work when an add-in tries to manipulate another
' workbook because `ThisWorkbook` will point to the add-in's workbook.
Set currentWorkbook = ActiveWorkbook
Set currentWorksheet = currentWorkbook.ActiveSheet
'**************************************************************************
' Variables Declaration
Dim newFolderPath As String
'**************************************************************************
' TODO: Main Code
On Error GoTo ErrorHandling
' BUG: Type mismatch
newFolderPath = "Folder/" + SOME_MODIFIER
Debug.Print newFolderPath
Cleanup currentWorksheet, executionRuntime
Exit Sub
ErrorHandling:
ErrHandler currentWorksheet, executionRuntime
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment