This error means your VBA macro is trying to assign an invalid value to an object property, usually because the object isn't initialized, isn't of the correct type, or a property doesn't exist for its class. For example, you might try setting a Range property on a Worksheet object that doesn't exist yet, or calling an invalid method like "Range.Resize" without proper parameters. The solution often involves checking object references, validating property names, and ensuring proper initialization.

What You'll Need

  • A working copy of Microsoft Excel with VBA enabled
  • Your macro code open in the VBA editor (Alt+F11)
  • Basic knowledge of Excel VBA syntax (optional but helpful)
  • A second Excel window open for testing (optional but recommended)
  • Debugger knowledge (optional) or step-by-step inspection

Step-by-Step

  1. Open the VBA editor (Alt+F11) and navigate to the module where your error occurs. Look for the line that triggers the error by pressing F8 to set a breakpoint or using the error message to find the line number.
  2. Identify the exact property being set by examining the error trace. Error 380 typically indicates something like: Object reference not set to an instance of an object or Type mismatch Check the line that includes this error. Common culprits:
    • Using an object reference that hasn't been initialized
    • Calling a property/method that doesn't exist for the object type
    • Trying to set a property after the object has closed
    • Using a variant value where a specific type is expected
  3. Check object initialization for the most common cases:
    1. For Worksheets: Ensure you're using Worksheets("SheetName") or ThisWorkbook.Sheets("SheetName"). Use: Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    2. For Workbooks: Always use: Dim wb As Workbook: Set wb = ThisWorkbook
    3. For Ranges: Use: Dim rng As Range: Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:B10")
  4. Validate property names and values:
    • Verify the exact property name exists for your object type. For example, Range.Resize only works with Range objects.
    • Check for typos in property names like Cells.Offset(1,0).Value vs Cells(1,0).Value
    • Use TypeName in your code to debug: Debug.Print TypeName(ws) should return "Worksheet" for a valid worksheet reference
  5. Handle optional parameters carefully:
    • For properties like Range.Font.Bold, if you set it to False but the object hasn't been initialized, you'll get this error.
    • Use optional parameters where available: Range.Font.Size = 12 instead of: Range.Font.Size = 12, Range.Font.Bold = True
  6. Add error handling where appropriate: On Error GoTo ErrorHandler ' Your code that might fail Exit Sub ErrorHandler: MsgBox "Error: " & Err.Description & vbCrLf & "Line: " & Err.Line Resume Next
  7. Test with minimal code to isolate the issue:
    1. Take a simple statement from your error line and test it in a new module:
    2. If setting a property works in the test module but not the original, the issue is with the object reference
    3. If setting the same property works but getting the value doesn't, the issue is with property access
  8. Check for unnecessary nested references that might cause initialization issues:
    • For example: Sheets("Sheet1").Range("A1").Font.Bold vs Sheets("Sheet1").Font.Bold
    • The first might fail if the Range doesn't exist or is empty
  9. Use the Debug.Print statement to verify values: Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") Debug.Print "Sheet name: " & ws.Name & vbCrLf & "Type: " & TypeName(ws)

If that did not work

If none of these steps resolve the error, follow these advanced troubleshooting steps:
  1. Check for cyclic references in your code that might cause objects to fail to initialize properly.
  2. Compare your code with similar working macros to identify differences.
  3. Try running the macro in a fresh workbook to rule out workbook-specific issues.
  4. Enable all VBA error messages in the Immediate Window:
    1. Press Alt+F11, click Tools > References
    2. Scroll down to Microsoft Visual Basic for Applications Extensibility and check it
    3. Close the References dialog and run your macro again
  5. Check for memory issues that might cause objects to fail initialization:
    1. Run your macro in Excel with Performance Mode enabled
    2. Close unused workbooks and windows
    3. Use Free statements to clean up objects
  6. Try rebuilding the project:
    1. Close Excel
    2. Open Excel's VBA editor
    3. Click Tools > Project Settings
    4. Select Build tab and click Rebuild
  7. Use the Object Browser to verify what properties/methods exist for your object:
    1. Press Ctrl+R in the VBA editor
    2. Select the object type (e.g., Worksheet) and browse
    3. Check for the exact property/method you're trying to use

Common Mistakes

  • Assuming an object exists when you haven't properly initialized it. For example, trying to access Sheets("Sheet1") when "Sheet1" doesn't exist.
  • Using partial object names like ws.Range when you should use ws.Range("A1:B10"). The Range property might not exist for the worksheet.
  • Ignoring optional parameters when a property requires them. For example, Cells(1).Font.Size = 10 works but Cells.Offset(1).Font.Size = 10 might fail.
  • Not checking for case sensitivity in property names. While Excel VBA is case-insensitive, it's good practice to verify.
  • Using generic variable names like r for ranges or w for workbooks that can't be debugged.
  • Forgetting that properties like Font might be read-only in some cases and require a Set statement for write operations.

Tips

  • Break your code into smaller functions to isolate where the error occurs.
  • Use the Immediate Window (Ctrl+G) to print intermediate values and debug step-by-step.
  • Record your macro manually and compare with your VBA code to spot differences.
  • Consider using Excel's built-in features instead of macros when possible to avoid these errors.
  • For complex objects like ActiveX controls, use GetObject or CreateObject instead of direct references.
  • Document your object references using comments like ' Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Data") to make debugging easier.

Troubleshooting

  1. Restart Excel and try again. Sometimes memory issues cause initialization problems.
  2. Check if the object exists in the current workbook: If Worksheets("MissingSheet") Then ' Code continues Else MsgBox "Sheet doesn't exist" End If
  3. Test with a minimal example using just the property that's causing the error on a fresh sheet.
  4. Check for dependencies where one object might be needed before another (like a worksheet before its range).
  5. Review the error stack trace carefully for any nested objects that might be failing to initialize.
  6. Try disabling macros temporarily to see if the error persists (though this won't help fix it).

Frequently Asked Questions

What causes VBA Error 380: Invalid Property Value?

This error occurs when your VBA code tries to set a property value for an object that isn't properly initialized, doesn't exist, or the property doesn't exist for the object type. Common causes include: - Using an object reference that hasn't been set - Trying to access properties on non-existent objects (like a worksheet that doesn't exist) - Calling invalid methods/properties for the object type - Setting properties after the object has been closed or released

How do I find out which property is causing the error?

To identify the exact property causing the error: 1. Open the Error window (Alt+F8 or Debug > Windows > Error) 2. Note the line number where the error occurs 3. Open the VBA editor and go to that line 4. Look for the specific property/method being set 5. Use Debug.Print statements before and after the problematic line to verify object values

What should I do if I can't find the error in my code?

If you can't locate the error in your code: 1. Enable all VBA error messages (Tools > References) 2. Test with a completely fresh workbook 3. Try running the macro in a different version of Excel 4. Check for memory issues (close unused workbooks, use Free statements) 5. Consider rebuilding your VBA project (Tools > Project Settings > Build > Rebuild)

Why does my code work in a new workbook but not in my existing one?

This typically happens because: - The workbook has custom properties, macros, or object references set - There might be dependencies between objects that aren't properly initialized - Some objects might exist in one workbook but not the other - The error might be hidden in a nested object reference chain

Can I prevent VBA errors like this in the future?

Yes, you can take these preventive measures: - Always properly initialize objects before using them - Use explicit object types (Dim ws As Worksheet) - Add error handling blocks - Test code in a fresh workbook first - Use the Object Browser to verify available properties - Break code into smaller functions for easier debugging

What's the difference between Error 380 and Error 3021?

Error 380 ("Invalid Property Value") occurs when you try to set an invalid value for a property, while Error 3021 ("Type mismatch") occurs when you try to assign incompatible types to a variable or property. Both are related to the value you're assigning, but Error 380 is more specific to the property/method being called.