Object Required Error 424 in For Next Cell

JStellato

Board Regular
Joined
Nov 6, 2010
Messages
55
Hey Guys, I'm having a hard time with a simple problem. Here is my code:

Code:
    Dim rptRange As Variant
    rptRange = Range([B1], [B21])

    For Each Cell In rptRange
      If Cell.Value = 1 Then
      
        ' Do something
        End If
    Next

The For Each Cell in rptRange throws an "Object Required" Error 424, I didn't declare Cell as a type anywhere, what am I missing?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
When I changed your code to
Code:
Dim rptRange As Variant
rptRange = Range([B1], [B21])
For Each c In rptRange
   If c = 1 Then
  
   ' Do something
   End If
Next
It works, Cell may be a reserved word, for each c in range returns double or string not a cell
 
Upvote 0
rptRange is an array with the values from B1:B21.

When you loop through it Cell will be a value from that range.

So basically drop the .Value, as Cell isn't an object.
 
Upvote 0
When I changed your code to
Code:
Dim rptRange As Variant
rptRange = Range([B1], [B21])
For Each c In rptRange
   If c = 1 Then
  
   ' Do something
   End If
Next
It works, Cell may be a reserved word, for each c in range returns double or string not a cell

Thanks, I'm used to programming C# so I guess I assumed Cell would refer to the cell in the range, this worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top