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:
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.”![]()
Const LastRow = 20
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:”
So far so Good. The next step is to add the following code to find the last row:![]()
Private Sub UserForm_Initialize() GetData End Sub
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)![]()
Private Function FindLastRow() Dim r As Long r = 2 Do While r < 65536 And Len(Cells(r, 1).Text) > 0 r = r + 1 Loop FindLastRow = r End Function
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.
I take this to mean the following:![]()
LastRow = FindLastRow
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.![]()
Private Sub UserForm_Initialize() LastRow = FindLastRow GetData End Sub
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?
Bookmarks