How to Change a Custom Object Property in an Excel VBA Dictionary
Question details
The user needs to update a specific property of a custom class object stored as a Dictionary item without replacing the entire key-item pair.

- Product
- Excel VBA
- Device & OS
- not provided
- Scenario
- Managing custom class objects using a Scripting.Dictionary in VBA and needing to dynamically modify the properties of an object already stored inside it.
- Observed behavior
- The goal is to cleanly update the custom object's property directly through the Dictionary reference, leaving the original Dictionary key perfectly intact.
Ensure you have enabled the 'Microsoft Scripting Runtime' reference in your VBA project via Tools > References if you are using early binding for the Dictionary object.
Update the Custom Object Property Directly via the Dictionary Key
You can directly access and modify the properties of an object stored in a Dictionary by referencing its key and chaining the property name.
When an object is added to a Dictionary, the Dictionary stores a reference to that object, not a static copy of its values. This means you do not need to extract the object, modify it, and write it back.
By simply calling the item by its key, you are directly interacting with the object in memory. You can update any public variable or property of that custom class instantly.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor in your workbook.
Navigate to the module or subroutine where your Dictionary (`Dict`) and your custom class objects are defined and populated.
Use the syntax `Dict.Item("YourKey").PropertyName = "New Value"`. For example, if your key is "a" and the property is "cfield2", write: `Dict.Item("a").cfield2 = "new1, new2, new3"`.
Press F5 or run your subroutine. The custom object's property is now successfully replaced while the Dictionary key remains entirely unchanged.

Write and Execute VBA Macros Seamlessly in WPS Spreadsheet
WPS Spreadsheet offers robust built-in support for VBA macros, including Scripting.Dictionaries and Custom Objects, allowing you to run, edit, and troubleshoot your existing Excel VBA scripts without modification.
- 1. Download and Install WPS Office: Get the latest version of WPS Office and ensure you download a version that includes built-in VBA support.
- 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing `.xlsm` or `.xlsb` file containing the Dictionary macro.
- 3. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'Visual Basic' to launch the familiar VBA Editor.
- 4. Edit Custom Object Properties: Edit your Dictionary and custom class object properties directly in the editor using standard VBA syntax.
- 5. Run Your Code: Run your macro from within the editor or trigger it via a worksheet button to see the property updates in action.

Frequently Asked Questions
Why do I get an 'Object required' error when updating a Dictionary item?
This usually happens if the item stored in the Dictionary is a primitive data type (like a string or integer) rather than an object, or if the specified key doesn't exist. Ensure you have instantiated your custom class using the 'New' keyword before adding it to the Dictionary.
Can I change the Dictionary key without losing the custom object?
To change a key, you must add a new key-item pair using the existing object reference, and then remove the old key. For example: `Dict.Add "newKey", Dict.Item("oldKey")` followed by `Dict.Remove("oldKey")`.
Does this direct property update method work for standard arrays stored in a Dictionary?
No. If you store a standard VBA array in a Dictionary, you cannot update an individual array element directly (e.g., `Dict.Item("Key")(1) = "Value"` will fail). You must extract the array to a temporary variable, update the element, and write the entire array back to the Dictionary item.




