Results 1 to 9 of 9

Can't write code to find last data row in excel user form

Threaded View

  1. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,925

    Re: Can't write code to find last data row in excel user form

    I think you are confusing NextRow and LastRow. Your button for "last" is supposed to take you to the last record in your sheet. The nextrow will be the blank row just below it where the new record will be entered. RoyUK gave you code for that next row:

    Function NextRow(Optional sht As Worksheet) As Long
        If sht Is Nothing Then Set sht = ActiveSheet
        With sht
            NextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        End With
    End Function
    Notice that the line: NextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1, finds the last used row and adds 1 to it to move to the blank line below it to insert the new record.

    Your form Initializer:
    Private Sub UserForm_Initialize()
    	LastRow = FindLastRow
    	GetData
    End Sub
    is ok (notice that you are looking for the last used row/record in this case). Therefore you must modify the Roy's code to replace your your looping code:
    Function FindLastRow(Optional sht As Worksheet) As Long
        If sht Is Nothing Then Set sht = ActiveSheet
        With sht
            FindLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
    End Function
    Since this is the row number of the last record, you must remember to add 1 to that number when you want to insert a new record at the end of your database.
    Last edited by protonLeah; 02-24-2012 at 09:30 PM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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