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
- 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.
- 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 objectorType mismatchCheck 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
- Check object initialization for the most common cases:
- For
Worksheets: Ensure you're usingWorksheets("SheetName")orThisWorkbook.Sheets("SheetName"). Use:Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") - For
Workbooks: Always use:Dim wb As Workbook: Set wb = ThisWorkbook - For
Ranges: Use:Dim rng As Range: Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:B10")
- For
- Validate property names and values:
- Verify the exact property name exists for your object type. For example,
Range.Resizeonly works with Range objects. - Check for typos in property names like
Cells.Offset(1,0).ValuevsCells(1,0).Value - Use
TypeNamein your code to debug:Debug.Print TypeName(ws)should return "Worksheet" for a valid worksheet reference
- Verify the exact property name exists for your object type. For example,
- Handle optional parameters carefully:
- For properties like
Range.Font.Bold, if you set it toFalsebut the object hasn't been initialized, you'll get this error. - Use optional parameters where available:
Range.Font.Size = 12instead of:Range.Font.Size = 12, Range.Font.Bold = True
- For properties like
- 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 - Test with minimal code to isolate the issue:
- Take a simple statement from your error line and test it in a new module:
- If setting a property works in the test module but not the original, the issue is with the object reference
- If setting the same property works but getting the value doesn't, the issue is with property access
- Check for unnecessary nested references that might cause initialization issues:
- For example:
Sheets("Sheet1").Range("A1").Font.BoldvsSheets("Sheet1").Font.Bold - The first might fail if the Range doesn't exist or is empty
- For example:
- 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:- Check for cyclic references in your code that might cause objects to fail to initialize properly.
- Compare your code with similar working macros to identify differences.
- Try running the macro in a fresh workbook to rule out workbook-specific issues.
- Enable all VBA error messages in the Immediate Window:
- Press Alt+F11, click Tools > References
- Scroll down to Microsoft Visual Basic for Applications Extensibility and check it
- Close the References dialog and run your macro again
- Check for memory issues that might cause objects to fail initialization:
- Run your macro in Excel with Performance Mode enabled
- Close unused workbooks and windows
- Use Free statements to clean up objects
- Try rebuilding the project:
- Close Excel
- Open Excel's VBA editor
- Click Tools > Project Settings
- Select Build tab and click Rebuild
- Use the Object Browser to verify what properties/methods exist for your object:
- Press Ctrl+R in the VBA editor
- Select the object type (e.g., Worksheet) and browse
- 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.Rangewhen you should usews.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 = 10works butCells.Offset(1).Font.Size = 10might 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
rfor ranges orwfor 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
GetObjectorCreateObjectinstead of direct references. - Document your object references using comments like
' Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Data")to make debugging easier.
Troubleshooting
- Restart Excel and try again. Sometimes memory issues cause initialization problems.
- Check if the object exists in the current workbook:
If Worksheets("MissingSheet") Then ' Code continues Else MsgBox "Sheet doesn't exist" End If - Test with a minimal example using just the property that's causing the error on a fresh sheet.
- Check for dependencies where one object might be needed before another (like a worksheet before its range).
- Review the error stack trace carefully for any nested objects that might be failing to initialize.
- Try disabling macros temporarily to see if the error persists (though this won't help fix it).