Bottom line: Learn how to find the last row, column, or cell in a worksheet using three different VBA methods. The method used depends on the layout of your data, and if the sheet contains blank cells.Skill level: IntermediateVideo: 3 Part Series How to Find the Last Cell with VBAVideo best viewed in full screen HDDownload the file that contains the code:(79.6 KB)Jump to the Code Examples on this page:.Finding the Last Cell is All About the DataFinding the last used row, column, or cell is one very common task when writing macros and VBA applications. Like anything in Excel and VBA, there are many different ways to accomplish this.Choosing the right method mostly depends on what your data looks like.In this article I explain three different VBA methods of the Range object that we can use to find the last cell in a worksheet. Each of these methods has pros and cons, and some look scarier than others.
How to Number Rows in Excel. The best way to number the rows in Excel would depend on the kind of data set that you have. For example, you may have a continuous data set that starts from row 1, or a dataset that start from a different row.
П™‚But understanding how each method works will help you know when to use them, and why. #1 – The Range.End MethodThe Range.End method is very similar to pressing the Ctrl+Arrow Key keyboard shortcut. Hi Jon,Just thought I would share this with you and other readers (see sub below). I’ve used your Cells.Find method with great success, but as you mentioned, it is a lot to type out and set up. It dawned on me to collapse it into a single line for each by using the “,” argument method vs.
The “Argument:=” method. As you mentioned in your article, you can ignore the last two, as MatchByte is not relevant except for obscure language characters and unless you are finding cell or font formats, then SearchFormat can be ignored. At first, I thought I would need two comma placeholders at the end of the argument list (for MatchByte and SearchFormat), but I discovered it works without them.
Probably because they are in succession and at the end of the list. Defining the lRow and lCol in this way makes it pretty easy to write on the fly and does not vertically stack up your code too much. Once you write it out a few times, it is pretty each to remember and you need change only two items to go from row to column.
As a reminder, I commented the reference to the nine Arguments, just as a ticker to the names and the sequence in which they must appear if using only commas. Of course, there is nothing wrong with the more stacked “Argument:=” method.
Just more bulk and so more cumbersome and or intimidating to use. Putting it all in one line makes it not that much more cumbersome than typing say: lRow = Cells(Row.Count, 1).End(xlUp).Row, but you get the benefit of the returned lRow being for the entire worksheet vs. Just one column of the worksheet.
Anyway, I thought I would share this different perspective. I hope others find it helpful.
Thanks for the great tips and the inspiration to look at things in different ways. Hi James,You can use a variable to store the value of the last row, then use in the range reference. There are many ways to do this, but something like the following will work.
Range('B1:B' & lRow).FillDownThis assumes the lRow variable contains the number of the last row, which will be set in a line a code above this. It also assumes that your formula is in cell B1 and you want to copy it down.I have a bonus video at the bottom of that explains how to copy and paste below the last used row.
Some of the techniques in that video might help you as well. This function assumes a lot.It assumes that the header or data starts in row 1.It assumes that there is no gap in headers in row 1. (What happens if C1 is Blank, but F1 -Z1 are not?)Why not use the same method used for finding the last row to find the last column? (Because it would take just as long, or longer then the Range.Find method)If you have an understanding of the data that you are expecting, there is no need to check all the columns, just check the mandatory column(s) that you will be using to sort/filter the data.Either way, if you have Filtered data, none of the methods will work properly.