+ Reply to Thread
Results 1 to 9 of 9

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

  1. #1
    Registered User
    Join Date
    02-21-2012
    Location
    Sooke, Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

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

    I'm trying to build a user form for an Excel worksheet. I’m having trouble getting the user form to find the “last row” of data.

    I've been following a tutorial I found online. It seems pretty comprehensive and I’ve been following all the steps. For the purpose of the exercise, I’ve been told to
    "add the constant LastRow to the start of the user form module as follows:

    Please Login or Register  to view this content.
    while testing this routine” The lesson says that “Later, you'll see how to determine the real last row of data in the worksheet, and you'll convert this constant to a module level variable.”

    I followed all the other steps for setting up the code for the form command buttons “First, Next, and Previous”,and everything was working fine (all the data from the worksheet was displayed in the proper text boxes on the user form as I pressed the above command buttons)

    The problem came when I followed the directions for adding code to the “Last” command button. The lesson says:
    “To make the last row dynamic, a few changes need to be made to the program. First the LastRow constant needs to be switched to a variable like this:”
    Please Login or Register  to view this content.
    So far so Good. The next step is to add the following code to find the last row:

    Please Login or Register  to view this content.
    At this point I am still able to navigate through the data rows via the user form, but the “Last” command button still does not function (won’t take me to the last row)

    Here’s where the trouble is. The lesson then says “Then you can set the LastRow variable by adding the following line to the UserForm_Initialize event.

    Please Login or Register  to view this content.
    I take this to mean the following:

    Please Login or Register  to view this content.
    When I try this I get an error message “Compile error, Assignment to constant not permitted” I assume there is a conflict here with the temporary “Const LastRow = 20” that I was told to enter at the beginning.

    When I delete the “Const LastRow = 20” and run the user form I get an error message “Invalid row number” and no data is displayed in the form, and I am not able to navigate through the data.

    I need to resolve this in order to make data entry possible with this form.

    Am I missing something obvious, or is there a problem with the directions in the lesson?
    Last edited by stevegrobertson; 02-24-2012 at 10:18 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Find last data row in excel user form

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

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

    For starters, replace the line:
    Const LastRow = 20, with:
    Dim LastRow As Long
    Last edited by protonLeah; 02-23-2012 at 11:51 PM.
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    02-21-2012
    Location
    Sooke, Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

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

    Thanks,

    It now finds the last row, but the lesson claims it should find the first EMPTY row.
    I've read through and followed all the steps in the lesson carefully and still no luck.
    I need to find my way to the first empty row to add a new record.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

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

    I wouldn't use a Loop to find the next row

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    02-21-2012
    Location
    Sooke, Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

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

    Please forgive me, but I'm on a very steep learning curve. I'm not sure where to put this code. I already have code for the "UserForm_Initialize()" event as shown here
    Please Login or Register  to view this content.
    Do I need to delete this code and replace it with the code you suggested (and include the line "GetData")?

    Also, I am guessing I should delete the existing code for finding the last row.
    Please Login or Register  to view this content.
    Thanks for your patience. Rome wasn't built in a day"

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

    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:

    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    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.

  8. #8
    Registered User
    Join Date
    02-21-2012
    Location
    Sooke, Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

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

    Thanks, I will give this a careful look. It seems the tutorial I was following used "next" and "last" very loosely.

  9. #9
    Registered User
    Join Date
    02-21-2012
    Location
    Sooke, Canada
    MS-Off Ver
    Excel 2003
    Posts
    11

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

    I input the code with the changes you suggested and it is now working fine. I have added the following code to an "Add New" command button
    Please Login or Register  to view this content.
    and I am now on the first blank row ready to add a new record. As I said; I'm on a pretty steep learning curve, but thanks to the help I'm getting here, I'm making progress.
    Thanks Again

+ Reply to Thread

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