[Excel] Error in finding last used cell in VBA
As to the correct way of finding the last used cell, one has first to decide what is considered used, and then select a suitable method. I conceive at least two meanings:
Used = non-blank, i.e., having data.
Used = "... in use, meaning the section that contains data or formatting." This is the criterion used by Excel at the time of saving. See also this. This criterion usually produces unexpected results, but it may also be intentionally exploited (less often, surely), e.g., to highlight or print specific regions, which may eventually have no data. And, of course, it is desirable as a criterion for the range to use when saving a workbook.
How to find the last used cell depends on what you want (your criterion).
For criterion 1, I suggest reading this answer.
UsedRange is cited as unreliable. I think that is misleading (i.e., "unfair" to
UsedRange is simply not meant to report the last cell containing data. So it should not be used in this case, as indicated in that answer. See also this comment.
For criterion 2,
UsedRange is the most reliable option, as compared to other options also designed for this use. It even makes it unnecessary to save a workbook to make sure that the last cell is updated.
Ctrl+End will go to a wrong cell prior to saving
(“The last cell is not reset until you save the worksheet”, from
It is an old reference, but in this respect valid).
There is yet another pitfall:
Criterion 2 does not account for Conditional Formatting. One may have formatted cells, based on formulas, which are not detected by
UsedRange or Ctrl+End.
In the figure, the last cell is B3, since formatting was applied explicitly to it. Cells B6:D7 have a format derived from a Conditional Formatting rule, and this is not detected even by
As to your specific question: What's the reason behind this?
Your code uses the first cell in your range E4:E48 as a trampoline, for jumping down with
The "erroneous" output will obtain if there are no non-blank cells in your range other than perhaps the first. Then, you are leaping in the dark, i.e., down the worksheet (you should note the difference between blank and empty string!).
If your range contains non-contiguous non-blank cells, then it will also give a wrong result.
If there is only one non-blank cell, but it is not the first one, your code will still give you the correct result.
When I want to find the last used cell value, I use:
Dim LastRow As Long LastRow = Range("E4:E48").End(xlDown).Row Debug.Print LastRow
I am getting the wrong output when I put a single element into a cell. But when I put more than one value into the cell, the output is correct. What's the reason behind this?
I would add to the answer given by Siddarth Rout to say that the CountA call can be skipped by having Find return a Range object, instead of a row number, and then test the returned Range object to see if it is Nothing (blank worksheet).
Also, I would have my version of any LastRow procedure return a zero for a blank worksheet, then I can know it is blank.
I wonder that nobody has mentioned this, But the easiest way of getting the last used cell is:
Function GetLastCell(sh as Worksheet) As Range GetLastCell = sh.Cells(1,1).SpecialCells(xlLastCell) End Function
This essentially returns the same cell that you get by Ctrl + End after selecting Cell
A word of caution: Excel keeps track of the most bottom-right cell that was ever used in a worksheet. So if for example you enter something in B3 and something else in H8 and then later on delete the contents of H8, pressing Ctrl + End will still take you to H8 cell. The above function will have the same behavior.
I was looking for a way to mimic the CTRL+Shift+End, so dotNET solution is great, except with my Excel 2010 I need to add a
set if I want to avoid an error:
Function GetLastCell(sh As Worksheet) As Range Set GetLastCell = sh.Cells(1, 1).SpecialCells(xlLastCell) End Function
and how to check this for yourself:
Sub test() Dim ws As Worksheet, r As Range Set ws = ActiveWorkbook.Sheets("Sheet1") Set r = GetLastCell(ws) MsgBox r.Column & "-" & r.Row End Sub
However this question is seeking to find the last row using VBA, I think it would be good to include an array formula for worksheet function as this gets visited frequently:
You need to enter the formula without brackets and then hit Shift + Ctrl + Enter to make it an array formula.
This will give you address of last used cell in the column D.