Script picks wrong cell

realniceguy5000

Board Regular
Joined
Aug 19, 2008
Messages
148
Hi, I have an odd problem I have used this method in the past to loop though a range of cells however this time it is not working.

Here is the part that is causing the issue.

Code:
Sub Sortme()
Dim R As Range
Dim Cell As Object
Dim Lastrow As Long
Dim S As String
 
Lastrow = Range("AG65000").End(xlUp).Row
 
Set R = Range("AG2" & Lastrow)
For Each Cell In R
Cell.Select 'remove after
    S = Cell.Value
    S = Left(Cell, 20)
 
    Next Cell
 
End Sub

The code should select AG2 and work down to the lastrow with data. Last row picks the correct lastrow but Cell. select picks Cell AG2100 to start?

Any Ideas: Thank You, Mike
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Perhaps

Code:
Sub Sortme()
Dim R As Range
Dim Cell As Object
Dim Lastrow As Long
Dim S As String
 
Lastrow = Range("AG" & Rows.Count).End(xlUp).Row
 
Set R = Range("AG2:AG" & Lastrow)
For Each Cell In R
    S = Cell.Value
    S = Left(Cell, 20)

Next Cell
End Sub

Note that nothing is being done with S.
 
Upvote 0
I think like this:


Code:
Sub Sortme()

    Dim R As Range
    Dim Cell As Range
    Dim Lastrow As Long
    Dim S As String
 
    Lastrow = Range("AG65000").End(xlUp).Row
 
    Set R = Range([COLOR="Red"][B]"AG2:AG"[/B][/COLOR] & Lastrow)
    
    For Each Cell In R
        Cell.Select 'remove after
        S = Cell.Value
        S = Left(Cell, 20)
    Next Cell
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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