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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

sergioMabres

Well-known Member
Joined
Feb 24, 2013
Messages
946
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
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

JStellato

Board Regular
Joined
Nov 6, 2010
Messages
55
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,195,644
Messages
6,010,891
Members
441,571
Latest member
stolenweasel

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
Top