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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

SteveO59L

Well-known Member
Joined
Apr 21, 2004
Messages
7,899
Set R = Range("AG2" & Lastrow)

should be

Set R = Range("AG2:ag" & Lastrow)
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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.
 

somnath_it2006

Well-known Member
Joined
Apr 11, 2009
Messages
574
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,728
Messages
5,833,328
Members
430,205
Latest member
chang2807

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