The "VBA Runtime Error 382: Property can't be set at runtime" occurs when a VBA module attempts to modify a property of an object (like a worksheet, workbook, or control) that’s locked or inaccessible during execution. This often happens in Excel, Word, or Access macros, where objects are in a read-only state or the underlying system prevents modification. The fix depends on whether the issue stems from a misconfigured property, a timing problem, or an external dependency. Below is a step-by-step guide to diagnose and resolve it, including common triggers, troubleshooting, and alternative workarounds.
What You'll Need
- A working Excel, Word, or Access application (v2013 or newer recommended)
- Basic familiarity with VBA editor (Alt+F11) and debugging tools
- Administrator or admin-level permissions for registry tweaks (rarely needed)
- Access to the VBA project file (.vbproj or .xlsm)
- A backup of your workbook/macro project before making changes
Step-by-Step
-
Identify the problematic module:
Open your VBA project (press
Alt+F11). Navigate to the module where the error occurs by selecting the sheet or object (e.g.,ThisWorkbook,UserForm1) in the Project Explorer. Right-click the module, then View Code. Add this debug line at the top:Debug.Print Err.Number & ": " & Err.Description. Run your macro again and observe the exact error code. Error 382 typically appears in these contexts:- Attempting to set a property on a frozen or invisible worksheet.
- Modifying a control’s value after the form is being closed.
- Calling a method that triggers the object being locked (e.g., after a save operation).
- Error 438: Use
Try/Catchblocks or check object availability. - Error 5: Invalid procedure call: Ensure you’re calling the correct method name.
-
Remove or replace the problematic property:
If the error is due to a direct object modification (e.g.,
ws.Visible = True), replace it with a conditional check. For example:If Not IsEmpty(Me) Then Me.Visible = True End IfFor Excel worksheets, try:If Not ws.IsLocked Then ws.Visible = True End IfAlternatively, use theEnableEventsflag to defer property changes. In your macro, add a delay or event handler:Application.EnableEvents = False ' Your code here Application.EnableEvents = True -
Check for external dependencies:
If the error occurs during file operations (e.g., saving, loading), ensure the file is unlocked. In VBA:
Dim fso As FileSystemObject Set fso = New FileSystemObject If Not fso.FileExists("C:\path\to\file.xlsm") Then MsgBox "File not found or locked!" Exit Sub End IfFor Office apps, check if another process (e.g., PowerPoint viewer) holds the file open. Use Task Manager to force-close it. -
Fix timing issues (for forms or UI events):
If the error occurs in a UserForm or button click handler, rearrange your code. For example:
- Move form initialization to a separate subroutine.
- Use timers or
DoEvents(caution:DoEventsis risky—only use it sparingly for UI delays). - Ensure controls exist before accessing them. Replace:
Me.Button1.Value = "Click"with:If Me.Controls.Item("Button1") Is Nothing Then CreateButton End If
-
Update VBA references (if applicable):
If the error persists after checking properties manually, ensure your VBA references match the object’s library. Go to
Tools > References(Alt+F11 > Tools > References) and check:- Excel Objects (for Excel macros).
- Microsoft Forms (for UserForms).
- Microsoft Office x.x Object Library (for Word/Access).
-
Recreate the macro from scratch:
If the error is structural (e.g., a recursive call or circular reference), rewrite the problematic section. Use an IDE like Visual Studio for VBA to avoid syntax mistakes.
If that did not work:
- Check for macros that conflict with your project (Windows Task Manager > Excel process).
- Run
Clean Up Code(Tools > Make Temporary Objects Visible) to identify hidden objects causing locks. - Enable debug logging by adding:
Private Sub Application_Open() LogFile "C:\Temp\VBAError.log" End Subto track runtime state.
Common Mistakes
-
Assuming an object is "unlocked" when it isn’t:
Worksheets can be locked via
Sheets("Sheet1").Unprotect Password "..."—always verify withws.Unprotectbefore modifying. -
Accessing controls before they’re loaded:
In UserForms, use
Me.InvalidateorMe.Refreshinstead of direct property access (e.g.,Me.Label1.Text). -
Ignoring Office event handlers:
Macros like
Application.WorkbookActivatemust not trigger properties on locked objects. UseApplication.EnableEvents = Falsetemporarily. -
Overlooking file locks in shared environments:
In multi-user setups, Excel may block changes during saves. Use
ThisWorkbook.SaveAsinstead ofThisWorkbook.Save.
Tips
-
Use conditional checks:
Replace direct property assignments with
If Not IsNull(ws.Name) Then ws.Visible = True. This prevents errors on null/empty objects. -
Avoid
DoEventsin production code: It can deadlock the UI. UseApplication.Wait Now + TimeValue("0:00:01")for delays instead. - Test in a minimal environment: Remove unrelated code to isolate the problematic section. If it works in a fresh module, the issue is likely a hidden dependency.
- Check for 32-bit/64-bit conflicts: If you’re mixing Excel 32-bit (old) and 64-bit (new), some properties may fail. Update all Office versions to the same architecture.
- Use a debug breakpoint: Place a breakpoint on the line where the error occurs. Step through the code to see which object or property is locked.
Troubleshooting
-
If the error appears randomly:
Check for pending updates. Run
Windows Updateand restart Excel. If the issue persists, try:- Repair Office via Control Panel > Programs > Microsoft Office > Change > Repair.
- Run Excel in Safe Mode:
Excel /safefrom Command Prompt.
-
If the error is tied to a specific object (e.g., a chart):
Right-click the chart >
Chart Design > Select Data.... Check if the data source is corrupted. Delete the chart and recreate it. -
If the macro fails during export/import:
Test the macro with a blank workbook first. Add this before exporting:
Dim data As Variant Set data = ThisWorkbook.Sheets("Sheet1").Range("A1:C10").Value ThisWorkbook.SaveAs "C:\Temp\Backup.xlsm"Then re-import the backup. -
If you’re on a shared PC and other users fail too:
Ask IT to check if macros are restricted via group policies (e.g.,
gpedit.msc > Computer Configuration > Administrative Templates > Windows Components > Microsoft Office > Security Settings). Disable macro enforcement if safe. -
If nothing works and the macro is critical:
Rewrite the problematic section using a
Try/Catchblock:On Error Resume Next ws.Visible = True If Err.Number <> 0 Then MsgBox "Failed to set visibility. Error: " & Err.Description End If On Error GoTo 0
Frequently Asked Questions
Why does my VBA macro crash with "Property can't be set at runtime" when running in Word?
The issue often stems from Word’s security settings or a locked document state. Ensure your macro references the correct Word object library (Microsoft Word x.x Object Library in Tools > References). Also, avoid modifying controls (e.g., TextBox1.Font) on a form that’s being closed. Use Application.EnableEvents = False before the problematic line and EnableEvents = True afterward.
How can I prevent this error from happening in Excel when saving files?
Use explicit file locking. Replace:
ThisWorkbook.Save
with:
If ThisWorkbook.Path <> "" Then
ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & ThisWorkbook.Name & ".tmp"
ThisWorkbook.Close SaveChanges:=False
My.Computer.FileSystem.RenameFile ThisWorkbook.Path & "\" & ThisWorkbook.Name & ".tmp", ThisWorkbook.Path & "\" & ThisWorkbook.Name
Else
ThisWorkbook.Save
End If
This avoids concurrent save conflicts.
Is there a way to debug this error without editing the VBA code?
Yes. Use the VBA debugger:
- Press
F8to step through the code line by line. - When the error occurs, inspect the
Err.NumberandErr.Descriptionin the Immediate Window (Ctrl+G). - Check the Watch window for object references (e.g.,
ws.Name). If it’sNothing, the object hasn’t loaded.
Why does my UserForm’s button click event fail with Error 382?
Likely causes include:
- The form isn’t loaded yet (access before
Me.Show). - A control’s parent is being destroyed (e.g., another form closes mid-event). Use
If Me.Parent Is Nothing Then Exit Sub. - The form is modal, and the button is disabled until interactive.
If Me.IsModal Then Exit Sub If Me.Visible = False Then Me.Show Me.Button1.Value = "Clicked"
Can I force a property change to work despite the error?
Only if you know the exact object and property involved. For example:
- Use reflection via VBA’s
TypeNameto dynamically check properties. - For Excel, try
ws.Cells(1, 1).Value = "Test"instead ofws.Range("A1").Valueif the range is locked.
What if the error is caused by a third-party add-in?
Disable all add-ins temporarily by enabling Safe Mode in Excel:
- Open Excel with
Excel /safefrom Command Prompt. - Test your macro. If it works, re-enable add-ins one by one.