+ Reply to Thread
Results 1 to 7 of 7

Finding the last row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Finding the last row

    Hello
    Good morning!
    I was trying to keep a history (in Col. D) of my data entered in a cell (A1)
    It is working ok, but started recording from 2nd Row (D2) but I need to start it from the 1st row (D1).
    Please help me to correct it. What i need is to keep the track from D1.
    Thanks
    Toms
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       If Not Intersect(Target, Range("A1")) Is Nothing Then
       confirm = MsgBox("Confirm ?", vbYesNo)
       If confirm = vbYes Then
        
        '    If Range("D65536").End(xlUp).Row = 1 Then
        '    lastrow = 1
        '    Else
     lastrow = Range("D65536").End(xlUp).Row + 1
        '     End If
         Cells(lastrow, 4).Value = Range("A1").Value
          
              End If
       Else
       End If
          
    End Sub
    Last edited by SMILE; 11-23-2011 at 05:40 AM.
    Appreciate clicking the * Add Reputation button on your left side, if you want say thanks to me

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Finding the last row

    try this way
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       If Not Intersect(Target, Range("a1")) Is Nothing Then
       confirm = MsgBox("Confirm ?", vbYesNo)
       If confirm = vbYes Then
        
        
        lastrow = Cells(Rows.Count, "D").End(xlUp).Row + 1
        
        Range("d" & lastrow) = Target
              
        End If
       Else
       End If
          
    End Sub
    Last edited by tom1977; 11-23-2011 at 04:45 AM.
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,102

    Re: Finding the last row

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    If Not Intersect(Target, Range("A1")) Is Nothing Then
         confirm = MsgBox("Confirm ?", vbYesNo)
         If confirm = vbYes Then
         
             If awf.CountA(Columns(4)) = 0 Then
                 lastrow = 1
             Else
                 lastrow = Range("D65536").End(xlUp).Row + 1
             End If
            Application.EnableEvents = False
            Cells(lastrow, 4).Value = Range("A1").Value
            Application.EnableEvents = True
        End If
    End If
          
    End Sub


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Re: Finding the last row

    Hi
    @TM Shucks
    It is working now. Thank you very much. But I couldn't figure it out what is wrong with my coding (considering the cancelled lines in my code). anyhow.. Thank you verymuch...
    @Tom1977
    Your code is same like mine in effect. It start recording from the 2nd Row. I wanted it to be from 1st row. TMShucks code is working perfect.

    Thanks everyone..

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,102

    Re: Finding the last row

    If you leave in the commented code, it will always log to cell D1, as the End(xlUp).Row will equal 1 whether the cell is empty or not. If you comment the code out, it goes up to the last row and then adds 1. So, first time through it records in cell D2.

    I've just checked to see if the column is empty and, if it is, set the next row to 1.

    Note that, as you are making more changes to the sheet, you need to disable and re-enable events.

    If this has answered your question, please mark your thread as solved.

    Regards

  6. #6
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Re: Finding the last row

    Thank u TM for the explanation!
    Great!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,102

    Re: Finding the last row

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1