The VBA Runtime Error 387, "Property Set Not Permitted," occurs when you attempt to modify a property that isn't writable by your code or is restricted by object model limitations. This typically happens with Excel objects (like Worksheets, Workbooks, or activeX controls) or when working with third-party controls where properties aren't exposed for direct setting. The fix requires understanding which properties can be modified and how to bypass internal restrictions.
What You'll Need
- Access to the Excel application running the VBA code
- Your VBA project window open in the immediate editor
- A secondary worksheet (to test property changes without affecting your main data)
- Excel's Developer tab enabled (enable it via File > Options > Customize Ribbon)
Step-by-Step
- Identify the problematic property
Temporarily replace the problematic property assignment with a debug print to identify the exact object and property being targeted:
Debug.Print Me.Name & " - Property: " & PropName
Run your code and examine the output. This will show you the object type and exact property name causing the error.
- Check property existence and type
Verify the property exists in the object's properties list by opening the Immediate Window (Ctrl+G) and typing:
? GetType(YourObject).GetProperty("PropertyName")If this throws an error, the property isn't accessible through standard methods.
- Use alternative property access methods
For Excel worksheet properties that often fail, try these workarounds:
- Use the code path that worked directly in Excel:
Sheets("Sheet1").Range("A1").Font.Bold = TrueTry setting the property this way instead of through VBA.
For ActiveX controls, use the control's specific property access methods or expose hidden properties via reflection:
Dim prop As Object Set prop = GetType(YourControl).InvokeMember("PropertyName", _ Reflection.BindingFlags.SetProperty, Nothing, YourControl, Array("ValueToSet")) - Use the code path that worked directly in Excel:
- Bypass internal restrictions
For properties that require elevated permissions, use this indirect approach:
Dim settings As Object Set settings = ThisWorkbook.Settings settings("PropertyName") = "DesiredValue"Use the workbook's Settings collection if the property requires special permissions.
- Check object locking
For Excel objects that might be protected, try this:
Sheets("Sheet1").Unprotect Password:="YourPassword"If the sheet is protected, unlock it first or modify the code to handle protected objects differently.
- Use the Application object
For global application settings that might be restricted:
Application.ScreenUpdating = False
Try setting the property through the Application object instead of directly on the worksheet.
- Implement change events
For properties that require validation, create a property that triggers when the value changes:
Private PropertyName As String Get Return Range("A1").Value End Get Set(ByVal Value) Range("A1").Value = Value If Value <> OldValue Then 'Do additional processing End If End Set
Common Mistakes
- Overwriting protected properties - Many Excel properties require user permissions that VBA can't override directly, especially those tied to worksheet protection or user interface elements.
- Using incorrect property names - The error often occurs when mistaking a method for a property (e.g., using a procedure name where a property name is expected).
- Ignoring object locking - When working with protected objects or during certain Excel states (like when saving), properties might become read-only.
- Assuming all properties are writable - Some third-party controls expose only read-only properties through their object model.
- Using absolute references incorrectly - Dynamic references (like "ThisWorkbook") might be losing their reference when the object becomes unavailable.
Tips
- Use the VBA Object Browser (View > Object Browser) to verify which properties are actually exposed for each object type.
- Create a test macro that just reads the property first to verify it exists before attempting to set it.
- For Excel properties, try setting values using the UI (click the cell) and then use Debug.Print to see what value Excel automatically assigns.
- If working with ActiveX controls, examine their object model documentation for alternative property access methods.
- Consider using the Excel Interop library for more reliable property access when dealing with complex Excel scenarios.
- Use the
TypeNamefunction in the Immediate Window to verify you're working with the correct object type before attempting property access.
Troubleshooting
If none of the above solutions work, try these additional steps:
- Verify the object exists
Before attempting to set a property, check if the object is properly instantiated:
If Not Worksheets("Sheet1") Is Nothing Then Worksheets("Sheet1").Range("A1").Font.Bold = True Else MsgBox "Sheet not found!" End If - Check for circular references
If your code references itself indirectly, property setting might fail due to circular reference errors:
Dim sheet As Worksheet Set sheet = ThisWorkbook.Sheets("Sheet1") ' Avoid this circular reference: ' sheet.Range("A1").Font = sheet.Range("A1").Font - Use reflection for complex objects
For objects with complex property structures, try reflection with carefully selected properties:
Dim propInfo As Reflection.PropertyInfo Set propInfo = GetType(Sheet1).GetProperty("PropertyName") If propInfo Is Nothing Then MsgBox "Property not found!" Else propInfo.SetValue(Sheet1, "DesiredValue", Nothing) End If - Consider using the PropertySheet class
For properties that require special handling, try using the PropertySheet class:
Dim propSheet As PropertySheet Set propSheet = New PropertySheet propSheet.Initialize ThisWorkbook propSheet.Property("PropertyName") = "Value" - Check for COM errors
If you're working with external COM objects, verify they're properly registered and have no COM errors:
- Check Event Viewer for COM errors
- Run regsvr32 on any required DLLs
- Restart the application and try again
- Use a different approach with UserForms
Create a UserForm that handles the UI interaction and use its properties instead:
With UserForm1 .Caption = "New Window" .Visible = True End With - If all else fails: use a workaround
For critical properties that remain inaccessible, consider:
- Modifying the VBA source code to expose the property
- Using a VBA library designed for the specific object model
- Creating a wrapper object that implements the desired properties
Frequently Asked Questions
Why does my worksheet property keep giving me error 387?
The most common cause is Excel's protection settings. Worksheets often have properties that become read-only when protected, or when certain Excel states are activated (like during a save operation). Always verify the worksheet has no protection before attempting to modify properties.
How can I tell if a property is read-only in VBA?
You can't directly check if a property is read-only through standard VBA, but you can try setting it anyway. If it throws error 387 or 438 (Object doesn't support this property), you know it's read-only. For a more reliable test, use reflection with try-catch blocks:
On Error Resume Next
propInfo.SetValue(YourObject, "TestValue", Nothing)
If Err.Number <> 0 Then
MsgBox "Property is Read-only"
Else
'Property is writable
End If
On Error GoTo 0
What are the most common properties that cause this error in Excel?
The most frequent culprits include:
- Sheet Protection properties (like
UnprotectorProtect) - UI-related properties (like
Fonton theActiveCellwhen certain UI states are active) - Application-level properties that require elevated permissions
- Third-party control properties that aren't properly documented
How can I temporarily debug which property is causing the error?
Use this technique to trace property access:
On Error Resume Next
PropertyName = "Font".SetValue(ActiveSheet.Range("A1"), "Arial", Nothing)
Debug.Print "Error: " & Err.Number & " - " & Err.Description
On Error GoTo 0
Modify the PropertyName variable to test different properties.
Is there a way to check if an object is properly connected before setting properties?
Yes, verify the object's existence and state:
If Not Worksheets("Sheet1") Is Nothing And _
Worksheets("Sheet1").UsedRange IsNot Nothing Then
'Safe to proceed
Else
MsgBox "Sheet or Range not available!"
End If
Always check for these conditions before attempting property access.
Why do some properties work in Excel but fail in VBA?
This occurs because VBA often has stricter object model restrictions than Excel's UI. Properties that appear writable in Excel might only appear as read-only in VBA's object model. The solution often involves using alternative property access paths or working through the application object.