How to Fix VBA Runtime Error 424 Object Required in a For Each Loop in Excel
Question details
The user is encountering runtime error 424 'Object required' when running a VBA function that iterates through a collection in a For Each loop.

- Product
- Microsoft Excel (VBA/Macros)
- Device & OS
- not provided
- Scenario
- Creating summary tables using a VBA macro from unique string values and numeric columns.
- Observed behavior
- The macro execution abruptly stops, throwing a 'Runtime error 424: Object required', specifically failing at the 'For Each cell In uniqueValues' line.
Before modifying your VBA script, ensure you have opened the VBA Editor (ALT + F11) and noted the exact line highlighted in yellow where the debugger stopped the code execution.
Change the Loop Variable to a Variant Type
Modify the variable type declared in your 'For Each' loop to match the primitive data type stored in your VBA collection.
When you add items to a collection using '.Add cell.Value', you are storing the actual data values (like text strings or numbers), not the Excel 'Range' objects themselves. If your loop variable is declared as a 'Range', VBA will throw Error 424 because it expects an object reference, but receives a primitive value from the collection.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications editor, then find your macro in the Project Explorer.
Find the line of code where you declared the loop variable, which currently looks like 'Dim cell As Range'.
Change the declaration from 'Range' to 'Variant' so it can handle primitive values. For example, change it to 'Dim item As Variant'.
Update your loop statement to use the newly declared Variant variable. Change 'For Each cell In uniqueValues' to 'For Each item In uniqueValues'.

Store Range Objects Instead of Values in the Collection
Adjust your collection insertion method to store actual Range objects if you specifically need to access cell properties like formatting or addresses later in the loop.
Experience Seamless Spreadsheet Management with WPS Office
Tired of dealing with complicated Excel VBA errors? WPS Office offers a lightweight, free alternative to Microsoft Office. With high compatibility for Excel files and intuitive data processing tools, you can manage your data easily without constantly relying on complex VBA scripts.
- 1. Download the Installer: Visit the official WPS Office website and click 'Download' to get the free installation package.
- 2. Install WPS Office: Run the downloaded installer and follow the quick on-screen instructions to set up WPS Office on your device.
- 3. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing Excel workbooks seamlessly to continue your data analysis.

Frequently Asked Questions
What does Runtime Error 424 'Object required' mean in VBA?
This error occurs when VBA expects an object reference (like a Worksheet, Range, or Workbook) but receives a non-object value (like a string, integer, or null), or when you attempt to assign an object to a variable without using the 'Set' keyword.
Can I use 'Set' to fix Error 424 in this specific scenario?
No, not in this specific case. While missing the 'Set' keyword is a common cause for Error 424, in a 'For Each' loop iterating over primitive string or numeric values stored in a collection, you cannot use 'Set'. You must change the variable type to Variant instead.
Why does adding 'CStr(cell.Value)' to a collection cause issues?
The 'CStr(cell.Value)' part is actually the key for the collection item, which is perfectly valid. The issue arises from the first argument (cell.Value), which inserts a primitive value instead of a Range object into the collection, creating a mismatch when you later try to loop through it using a Range variable.
Does WPS Office support VBA macros?
Yes, WPS Office provides support for VBA macros in its specific business versions or via an optional VBA module download, allowing you to run, edit, and debug macro-enabled workbooks (.xlsm) smoothly.




