excel 2003 vba setting last row in userform initialise

kazmdav

New Member
Joined
Oct 14, 2010
Messages
7
I am having trouble with my PREV & NEXT command buttons on userform where it thinks the last row used is row 3. Row 3 is the starting row with data but is not the last row with dat. I suspect the problem lies within my Userform_initailize procedure but I cannot figure out the answer after very many hours & driving me crazy with all sorts of attempts. Here's my current code & can someone please help me:

Private Sub UserForm_Initialize()
lCurrentRow = 3
Range("a3").Select
If IsNull(ActiveCell.Offset(1, 0)) Or ActiveCell.Offset(1, 0) = "" Then
lLastRow = 3
Else: Selection.End(xlDown).Select
lLastRow = ActiveCell.Row
End If
ActiveCell.Select
End Sub
_____________________________
Private Sub cmdNext_Click()
If lLastRow = lCurrentRow Then
MsgBox "You have reached the last row."
Else
lCurrentRow = lCurrentRow + 1
End If
LoadRow
End Sub
_____________________________
Private Sub cmdPrev_Click()
If lCurrentRow > 3 Then
lCurrentRow = lCurrentRow - 1
LoadRow
Else
MsgBox "The first row is displayed."
End If
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Perhaps it should be

Code:
Public lLastRow As Long, lCurrentRow As Long


Private Sub UserForm_Initialize()
lCurrentRow = 3
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & lLastRow).Select
End Sub
 
Upvote 0
That was quick as a flash response! Thank you. I tried your suggestion but it's still starting at row 3 (last row with data is row 9). Hmmm, maybe there's something more I delve into. Thus, I will try again!
 
Upvote 0
Then maybe

Code:
Public lLastRow As Long, lCurrentRow As Long


Private Sub UserForm_Initialize()
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & lLastRow).Select
lCurrentRow = ActiveCell.Row
End Sub
Note that you need to declare lLastRow and lCurrentRow at the top of the module (as I have done) to make their values available to the other subs.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top