Last Row

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
20,551
Office Version
  1. 365
Platform
  1. Windows
Ok, I just discovered something new to me and I'm wondering if there's any reason why not to do it this way (1st line in example code)?

VBA Code:
lr = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
lr2 = Range("A" & Rows.Count).End(xlUp).Row

The 2nd one is a little shorter. Is that its advantage?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I prefer lr3, but this could start a heated debate with other users :cool::cool:
VBA Code:
lr3=cells(rows.count,"A").end(xlup).row
VBA Code:
lr2 = Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 1
As you delve deeper into the topic of searching for the last cell you will see many different ways. Each of them has some advantages, but also some disadvantages. That's why it's a good idea to learn not only these two mentioned ways, but a few more to choose the right method for your current data appearance.
VBA Code:
lr = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
If in other columns the data goes further than in column A, you will get the row of the farthest value. E.g. In column A, the last value is in A3, and in column C, for example, the last value is C10. The number returned will be 10, not 3. In addition, if, for example, in column D you have formatted cell D20 (and there is no value in it), the row number returned will be 20. This method can be used when the data in different columns end up in the same row AND the entire range of data has been inserted into a new sheet.
VBA Code:
lr2 = Range("A" & Rows.Count).End(xlUp).Row
If the data is filtered and the last few rows are hidden, the value of the last visible row will be returned, not the last non-empty cell in column A.

Artik
 
Upvote 0
Also, for variable column lengths.....
VBA Code:
lr4 = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
 
Upvote 0
Adding to @Artik's comment on the SpecialCells method
VBA Code:
lr = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
This mimics the behaviour of using Ctrl+End and finds the bottom right corner of the UsedRange.
An additional downside of this is that if you delete rows, it does not reset the bottom corner until either the workbook is saved or the UsedRange property is used in the code. (same for clearing content at the bottom that should be reducing where the last row falls)
 
Upvote 0
The find method is a impacted by a filter being applied but not rows that are manually hidden.
 
Upvote 0
As @Artik has stated .SpecialCells(xlCellTypeLastCell) detects formatting as well as values and so I avoid it

VBA Code:
Sub xxxx()
Dim lr As Long
lr = Range("A:A").SpecialCells(xlCellTypeLastCell).Row
MsgBox lr
End Sub


1715998275961.png
 
Upvote 0
As a VBA newbie, reading this thread has been very educational! Thanks guys.
 
Upvote 0
The topic is as long as a river. For example, a recent variation on the theme:

Artik
 
Upvote 0

Forum statistics

Threads
1,221,700
Messages
6,161,378
Members
451,700
Latest member
Eccymarge

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