• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Save Settings in Registry Using Excel

You are here: Home / Excel VBA / Save Settings in Registry Using Excel
save vba settings in registry
October 1, 2015 by Philip Treacy

If you want to save data from one Excel session to another, you can store this in the Windows registry. Using the registry is good for saving things like user preferences or program configuration data.

There are other means of saving this type of data like external files, hidden sheets and defined names, but I'm just looking at using the registry in this post.

This code works on a Macintosh too, with the information being saved in the application's initialization file.

What is the Registry?

The Windows registry is a database that stores configuration settings for Windows and the programs that run on Windows.

Data in the registry can be viewed and modified using the Windows programs regedt32.exe and regedit.exe, so bear that in mind if you are storing important information there.

Be very careful if you are not familiar with using either of these programs. If you modify registry settings incorrectly it can cause unpredictable results on your Windows computer.

Remember that these settings will only be recorded in the registry of your computer. If you open the workbook on another computer, you are accessing that computer’s registry so the data there will be different.

A trivial example

For the purpose of this example let's do something simple like count the number of times the workbook is opened.

To do this I will save a value to the registry, read it when the workbook is opened and display a message on screen showing how many times the workbook has been opened.

This value will then be incremented and saved back to the registry.

I'll also ask if you want to reset the count, and if you do I will delete the entry in the registry.

Message Box Asking to reset

You can of course get a bit more exciting with the data you save to the registry. You could use a userform to ask the user for information, or save data directly off a sheet, it’s up to you.

Saving settings for a single workbook

If you want to save some settings for a single workbook then you can store the VBA in the workbook itself, in the ThisWorkbook module.

ThisWorkbook VBA Module

Saving settings for all workbooks

If you want to save settings for all workbooks, then you need to create a PERSONAL.XLSB and store the VBA in its ThisWorkbook module.

ThisWorkbook VBA Module in PERSONAL.XLSB

Events

In order to get the VBA to run, I'm using events.

As I want to run code in a single workbook when it is opened I’ll use the Workbook_Open() event.

For code we want to run for every workbook, it can be a little more complicated.

To get PERSONAL.XLSB to run our code every time a workbook opens, we need to get the application (Excel) events handled by PERSONAL.XLSB. We can then use the App_WorkbookOpen() event to run code every time a workbook is opened.

To set all this up we use the following code


Private WithEvents App As Application

Private Sub Workbook_Open()
    
    Set App = Application
    
End Sub

What this is saying is:

Declare a variable called App as an application type object and handle events with it.


Private WithEvents App As Application

When a workbook is opened, set the App variable to be our application (Excel).


Private Sub Workbook_Open()
    
    Set App = Application
    
End Sub

Events in the application (Excel) are now handled by PERSONAL.XLSB and we can capture workbook opens by using App_WorkbookOpen().

The Functions That Do The Work

There are only three functions we need to write, read and delete data from the registry.

SaveSetting

This saves your data to the registry into HKEY_CURRENT_USER\Software\VB and VBA Program Settings. If this doesn’t exist already it’ll be created the first time SaveSetting is used.

vb and vba program settings registry key

The official Microsoft declaration for this is

SaveSetting appname, section, key, setting 

appname is any name you care to use. Confusingly, it does not mean Excel. I am using MOTH_Excel_Settings

An error occurs if the setting can't be saved for any reason so appropriate steps should be taken to allow for this. Sounds like I should write a blog post on error handling.

If the registry, or the parts you are accessing, are protected then the code may not be able to modify the registry.

The section can refer to what you are storing the data for, so in my case I am using WorkBook_Opens

They key is the name of the data. If I am storing a value for each workbook I open, I can use the name of the workbook itself.

The setting is the value of the data, which in my case is the number of times the workbook has been opened.

Once my macro has run the data in the registry looks like this:

Registry entry showing the number of times workbook opened

GetSetting

GetSetting appname , section, key [, default ]

GetSetting returns the data specified by appname , section, key

default is an optional parameter. If you supply it when calling GetSetting, then that default value is returned if GetSetting can't find the data at appname , section, key

This is useful for checking the existence of some data.

DeleteSetting

DeleteSetting appname [, section] [, key]

Deletes an entry from the registry, or on a Mac, deletes the data in the application's initialization file.

Both the section and key are optional parameters. But you can't specify a key without a section

An error occurs if you try to delete something that isn't in the registry.

By specifying a key, you can delete just that key. If you don’t specify the key, then the whole section is deleted. If you specify neither section nor key then everything under appname is deleted.

Sample Workbooks & Code

Enter your email address below to download the sample workbooks.

By submitting your email address you agree that we can email you our Excel newsletter.
Please enter a valid email address.

VBA for a Single Workbook

Remember that if you just want to record data for a single workbook, you can store the VBA in that workbook.

Download the example workbook.

VBA for All Workbooks

This is the code you use to record data for all workbooks you open. This is just a sample workbook with the code you need. You must create your own PERSONAL.XLSB and put the code in there for it to work on all workbooks.

Download the example code for all workbooks.

The sub SaveRegSettings that does the hard work is almost exactly the same as the code for recording the number of opens for a single workbook. The difference being that when deleting the registry data, the code deletes the key for the workbook, not all the data under AppName.

DeleteSetting AppName, Section, ActiveWorkbook.Name

save vba settings in registry

More Excel VBA Posts

Display All Matches from Search in Userform ListBox

Display All Matches from Search in Userform ListBox

Search a range for all partial and full matches of a string, and display matching records (entire rows) in a userform listbox. Sample code and userform.
animating excel charts

Animating Excel Charts

Use animation correctly to enhance the story your data is telling. Don't animate your chart just for some eye candy. Sample code and workbook to download.
dynamic data validation lists in userforms

Dynamic Data Validation Lists in Userforms

Data validation lists using the same source that are dynamically modified to prevent the same choice being made in each list.
show report filter pages for power pivot pivottables

Show Report Filter Pages for Power Pivot PivotTables

PivotTables created from Power Pivot can't use the 'Show Report Filter Pages' option. But this piece of VBA allows you to do just that.
charting real time data in excel

Charting Real Time Data in Excel

Receive data in real time and chart the data as it arrives. Can be used to chart things like stock prices or sensor readings. Sample code and workbook
select multiple items from drop down data validation list

Select Multiple Items from Drop Down (Data Validation) List

Choose multiple items from a data validation (drop down) list and store them all in the same cell. Sample workbook with working VBA.
Excel Calendar (Date Picker) to Use in Worksheets and Userforms

Multi-Language Excel Calendar (Date Picker) for Worksheets and Userforms

Easy to use, highly customizable and multi-language. This date picker is implemented as a userform that is simple to integrate into your workbook.
automating and emailing pivot table reports

Automating and Emailing Pivot Table Reports

Automate the creation and distribution of pivot table reports with some VBA. Send reports to multiple email recipients using Outlook.
search for data with userform

Searching for Data With a User Form

Search a list of records (like a table) using a user form, and then populate the fields of the search form when the record is found.
Checking values in range objects with vba

Checking Values in Range Objects With VBA

Use built in tools or your own code to examine the values contained within Range objects in VBA. Sample code to download.


Category: Excel VBA
Previous Post:Automatically Add Items to Data Validation ListAutomatically Add Items to Data Validation List
Next Post:Excel AGGREGATE FunctionExcel Aggregate function

Reader Interactions

Comments

  1. Ron Anderson

    June 23, 2017 at 4:41 am

    Excellent blog… Really easy to understand

    Reply
    • Philip Treacy

      June 23, 2017 at 10:19 am

      Thanks Ron

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

Featured Content

  • 10 Common Excel Mistakes to Avoid
  • Top Excel Functions for Data Analysts
  • Secrets to Building Excel Dashboards in Less Than 15 Minutes
  • Pro Excel Formula Writing Tips
  • Hidden Excel Double-Click Shortcuts
  • Top 10 Intermediate Excel Functions
  • 5 Pro Excel Dashboard Design Tips
  • 5 Excel SUM Function Tricks
  • 239 Excel Keyboard Shortcuts

100 Excel Tips and Tricks eBook

Download Free Tips & Tricks

Subscribe to Our Newsletter

Receive weekly tutorials on Excel, Power Query, Power Pivot, Power BI and More.

We respect your email privacy

Guides and Resources

  • Excel Keyboard Shortcuts
  • Excel Functions
  • Excel Formulas
  • Excel Custom Number Formatting
  • ALT Codes
  • Pivot Tables
  • VLOOKUP
  • VBA
  • Excel Userforms
  • Free Downloads

239 Excel Keyboard Shortcuts

Download Free PDF

Free Webinars

Excel Dashboards Webinar

Watch our free webinars and learn to create Interactive Dashboard Reports in Excel or Power BI

Click Here to Watch Now

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.