Injecting pre-written Functions into Workbook Projects via VBA

Injecting pre-written Functions into Workbook Projects via VBA

During a recent interview I was asked to talk through a VBA Function that sets many of the application settings (Screen Updating, Enable Events, Display Alerts, etc) and a Function that returns them to their original values and it reminded me of something i started putting together a few years ago that injected some pre-defined functions within a Workbooks VBA Project via Userform.

Credit where it's due, the FindLast Function was Steve Southwick's (https://www.dhirubhai.net/in/steve-southwick-7b5187a0/). Remember putting these together Steve?

For anyone wanting to have a play feel free to download the add-in. I have left the Project unlocked should you want to mimic the functionality or Functions. Please note the Add-In only works if you check 'Trust Access to VBA Project' in Excel's Options. The add-in should show you how to do this when you open the tool via the Add-Ins menu.

https://drive.google.com/open?id=0B8-3mVn08WeSa1dtNEdnQVRxcFU

Should you just want the function mentioned above here you go...

Option Explicit

Public blnSavedScreenUpdating As Boolean
Public blnSavedEnableEvents As Boolean
Public blnSavedDisplayAlerts As Boolean
Public lngSavedCalculation As Long
Public blnSavedCancel As Boolean

Public Enum CalcTypes
    Manual = 0
    Automatic = 1
    AutomaticExceptTables = 2
End Enum

Function AppSettings(blnScreenUpdating As Boolean, _
                     blnEnableEvents As Boolean, _
                     blnDisplayAlerts As Boolean, _
                     lngCalculation As CalcTypes, _
                     blnCancel As Boolean, _
                     Optional blnSaveSettings As Boolean)
                     
    'From: NuFunc Excel Add-In
    'Author: Michael Blackman
    'Date Created: 27th July 2011
    'Supported Operating Systems: Windows XP, Vista, Win7
    'Compatible Versions: Excel 2000, 2003, 2007, 2010
                     
    With Application
        If blnSaveSettings = True Then
            blnSavedScreenUpdating = .ScreenUpdating
            blnSavedEnableEvents = .EnableEvents
            blnSavedDisplayAlerts = .DisplayAlerts
            lngSavedCalculation = .Calculation
            blnSavedCancel = .EnableCancelKey
        End If
        .ScreenUpdating = blnScreenUpdating
        .EnableEvents = blnEnableEvents
        .DisplayAlerts = blnDisplayAlerts
        .Calculation = lngCalculation
        .EnableCancelKey = blnCancel
    End With
                     
End Function

Function RestoreAppSettings()

    'From: NuFunc Excel Add-In
    'Author: Michael Blackman
    'Date Created: 27th July 2011
    'Supported Operating Systems: Windows XP, Vista, Win7
    'Compatible Versions: Excel 2000, 2003, 2007, 2010

    With Application
        .ScreenUpdating = blnSavedScreenUpdating
        .EnableEvents = blnSavedEnableEvents
        .DisplayAlerts = blnSavedDisplayAlerts
        .Calculation = lngSavedCalculation
        .EnableCancelKey = blnSavedCancel
    End With

End Function
 
  


Steve Southwick

Healthcare & Life Sciences Innovator | Director, Data & IT Ops with Full-Cycle Expertise (Dev, PM, Ops) | Strategic Leader Driving Value-Based Clinical Outcomes & Project Revitalization

8 年

Hey Mike, I do remember putting these together! In fact, is still use and have added quite a few that I found useful along the way. Thanks for posting this and remembering me, it was fun putting these together!

Michael Blackman

Data Analyst COG P&C at Chubb

8 年

:-)

回复

要查看或添加评论,请登录

Michael Blackman的更多文章

  • A folder is not a Database, probably!

    A folder is not a Database, probably!

    As many of you probably know by now I'm a technological dinosaur. Whilst many others are sharing information on new and…

    11 条评论
  • My Array Formula is Slow...

    My Array Formula is Slow...

    First things first. Yes I know Power BI exists.

    9 条评论
  • Using Names and Arrays to avoid nasty nested IF's

    Using Names and Arrays to avoid nasty nested IF's

    Hands up who remembers writing the longest nested IF statement in the world and thinking you've conquered the world? I…

    6 条评论
  • Saving User Settings locally in .ini files with VBA

    Saving User Settings locally in .ini files with VBA

    I’ve had a busy few months at Tax Automation and I’m delighted to say I’m learning new things again after stagnating to…

    2 条评论
  • ADO Function to Query most of the common Data Sources

    ADO Function to Query most of the common Data Sources

    I've lost count of the number of variants of this I've written on clients machines and have never remembered to keep a…

    3 条评论
  • Working with multiple Ranges without a Loop (Union)

    Working with multiple Ranges without a Loop (Union)

    Just wanted to quickly put something together to highlight a very powerful function built into the Excel Application…

    12 条评论
  • Function to Validate Headers

    Function to Validate Headers

    We've all been there, you've been downloading the same csv for the last 6 months every single damn day and blindly…

    10 条评论
  • VLOOKUP with a Binary Search - VLOOKUP(..., ..., ..., TRUE)

    VLOOKUP with a Binary Search - VLOOKUP(..., ..., ..., TRUE)

    As we all know VLOOKUP is a pretty useful function and many of us are also made aware by others that using TRUE in the…

    18 条评论
  • Dynamic Named Ranges & Index Match

    Dynamic Named Ranges & Index Match

    Back in the old days before Lists received its revamp to Tables we had another way to ensure our formula was efficient…

    9 条评论

社区洞察

其他会员也浏览了