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
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

SteveO59L

Well-known Member
Joined
Apr 21, 2004
Messages
7,896
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,514
Messages
5,511,733
Members
408,864
Latest member
cmajewsk

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top