ML with circles

Mearns Learns

Common VBA

Some common boilerplate VBA procedures.

StartPosition determines the start position before running a procedure.
ReturntoStartPosition returns to the position stored by StartPosition.
StartSettings turns off some of the UI to speed up execution.
EndSettings turns the UI back on again.

Option Explicit

Public CurrentWorkbook As String
Public CurrentSheet As String
Public CurrentCell As String


Sub StartPosition()
    
    CurrentWorkbook = ActiveWorkbook.Name
    CurrentSheet = ActiveWorkbook.ActiveSheet.Name
    CurrentCell = ActiveCell.Address
'Determine starting position

End Sub

Sub ReturnToStartPosition()
    
    If IsWkBkOpen(CurrentWorkbook) = False Then
     CurrentWorkbook = Empty
     CurrentSheet = Empty
     CurrentCell = Empty
     Exit Sub
    End If
    
    Windows(CurrentWorkbook).Activate
    ActiveWorkbook.Sheets(CurrentSheet).Select
    ActiveSheet.Range(CurrentCell).Select
'Used stored variables to select original workbook/sheet/cell

    CurrentWorkbook = Empty
    CurrentSheet = Empty
    CurrentCell = Empty
'Clear variables

End Sub

Sub StartSettings()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.StatusBar = "Working . . ."
'Sets screen updating and display alerts to false

End Sub

Sub EndSettings()

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.StatusBar = False
'Sets screen updating and display alerts to false

End Sub

A more robust approach to the Settings is to store the existing setting in a variable.
Set the settings that you want and at the end of the code restore the previous settings.
Just like is done for the Start Position.