The DeleteSetting statement is one such tool that can come in handy when you need to remove specific settings from your VBA project. In this blog post, we’ll dive into the purpose, syntax, top 5 examples, important notes and remarks, and conclude with a request for feedback and views on the topic.
VBA DeleteSetting Statement
Purpose of DeleteSetting statement
The DeleteSetting statement in VBA is used to remove one or more key-value pairs from the Windows registry. The Windows registry is a database that stores configuration settings and options for the operating system and installed applications.
When a VBA application creates or modifies registry keys and values using the SaveSetting statement, the DeleteSetting statement can be used to remove those settings. This can be useful when you want to reset or clear certain settings in your application, or when you want your application to behave differently based on the presence or absence of certain settings.
Syntax of DeleteSetting statement
The general syntax for the DeleteSetting statement is as follows:
DeleteSetting [appname], [section], [key]
- appname is the name of the application whose settings you want to remove. This is an optional argument and can be omitted if the settings were saved using the current application’s name.
- section is the name of the section in the registry where the setting is stored. This is a required argument.
- key is the specific setting that you want to remove. This is also a required argument.
Examples of using VBA DeleteSetting statement
1. Removing all settings for the current application:
DeleteSetting , "MySection"
This example removes all settings that were saved under the “MySection” section for the current application.
2. Removing a specific key from the registry:
DeleteSetting "MyApp", "MySection", "MyKey"
This example removes the setting with the key “MyKey” from the “MySection” section in the registry for the application named “MyApp”.
3. Removing all settings from a specific application:
This example removes all settings that were saved using the application name “MyApp”. This can be useful when you want to reset all settings for a particular application.
4. Using a variable for appname:
Dim appName as String
appName = "MyApp"
DeleteSetting appName, "MySection"
By assigning the application name to a variable, you can easily change it and reuse the variable in multiple DeleteSetting statements.
5. Looping through a list of keys to delete:
Dim keyArray(3) as String
keyArray(0) = "Key1"
keyArray(1) = "Key2"
keyArray(2) = "Key3"
For i = LBound(keyArray) To UBound(keyArray)
DeleteSetting "MyApp", "MySection", keyArray(i)
This example shows how you can use a loop to delete multiple keys from the registry. The keys to be deleted are stored in an array, and the loop goes through each element of the array and deletes the corresponding key from the registry.
Important Notes & Remarks
- The DeleteSetting statement only works on keys and values that were created using the SaveSetting statement in VBA. It cannot be used to delete settings that were created manually in the Windows registry.
- When specifying the key argument, it’s important to note that it is case-sensitive. So make sure to use the exact same key that was used when saving the setting.
- The DeleteSetting statement can be used in any VBA project, including Microsoft Office applications such as Excel, Word, and PowerPoint.
- It’s good practice to check if a key exists before using the DeleteSetting statement to avoid any errors. This can be done using the GetSetting function.
- Deleting settings from the registry is a permanent action. So make sure to use the DeleteSetting statement carefully and only remove settings that you intend to.
In this blog post, we covered the purpose and syntax of the DeleteSetting statement in VBA. We also looked at 5 practical examples of using this statement to manage application settings. It’s important to note that the DeleteSetting statement is just one tool in your VBA toolbox, and can be used in conjunction with other statements and functions for even more powerful and efficient management of settings.
Do you have any other tips or examples for using the DeleteSetting statement in VBA? Or do you have any feedback or views on this topic? Let us know in the comments. Thank you for reading!