Written by Allen Wyatt (last updated December 30, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
John has data being entered in a worksheet. The real data begins in row 8. He would like, in a different cell, to use a formula to indicate the range of cells that contain data, such as A8:N112. The starting cell (A8) is static and the final column (N) is static. The biggest issue seems to be determining the number of rows.
There are several approaches you can take with your formula. As is often the case, the best solution depends on the way that your data is used within your worksheet.
The typical workhorse functions that people turn to in this situation are the COUNT and the COUNTA functions. If your data consists of numeric data, you can use COUNT; if it consists of alphanumeric data (or a mixture of numeric and alphanumeric), then you should use COUNTA. Here's the typical approach:
=ADDRESS(8,1) & ":" & ADDRESS(COUNTA(N:N)+7,14,3)
This will work just fine, with two huge caveats. First, it is critical to note that the actual count is performed on column N. If you have any blank cells in column N, then they won't be counted and, therefore, the formula won't return the correct last row in your data. If you have a different column that does have something in every cell, then you could use that one instead of column N.
The second caveat has to do with what may be in rows 1 through 7 of column N. If you have anything in those cells (including a column heading in row 7), then the formula won't give an accurate result. In that case, you'll need a more specific formula:
=ADDRESS(8,1) & ":" & ADDRESS(COUNTA(N8:N1048576)+7,14,3)
While this incarnation of the formula solves the second caveat, it doesn't solve the first one. If there are blank cells in column N, then your result will be incorrect.
If you prefer (and with the same caveats still in mind), you could shorten the formula quite a bit in this manner:
= "A8:N" & (COUNTA(N8:N1048576) + 7)
To get around both caveats, you need to use a different approach entirely in your formula. This formula doesn't care if there are blanks or not, nor does it care if there is anything in rows 1:7:
="A8:N" & MAX((A:N<>"")*(ROW(A:N)))
This formula works just fine in Excel for Microsoft 365, but if you are using an older version, you'll want to enter it as an array formula (Ctrl+Shift+Enter).
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8927) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
When compiling statistics on a collection of data points, you may want to know whether a particular value is the "highest ...
Discover MoreNormally you use the RAND and RANDBETWEEN functions to generate random numbers. What if you want to generate random ...
Discover MoreOperators are used in formulas to instruct Excel what to do to arrive at a result. Not all operators are evaluated in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-01-01 09:46:31
J. Woolley
Sorry. The LastNonBlankCellAddress UDF in my comment below should be named LastNonBlankRowColAddress because the cell might actually be blank.
2023-12-31 16:42:19
J. Woolley
The following UDF will return the absolute address of the last non-blank cell in a range of cells (Target):
Public Function LastNonBlankCellAddress(Target As Range)
Dim R As Range, LastRow As Long, LastCol As Long
Application.Volatile
Set R = LastNonBlankCellIn(Target, xlFormulas, xlByRows)
If R Is Nothing Then _
LastNonBlankCellAddress = CVErr(xlErrNA): Exit Function
LastRow = R.Row
LastCol = LastNonBlankCellIn(Target, xlFormulas, xlByColumns).Column
'dynamic array formula spill can result in cells
'that have a value without a formula or constant
Set R = LastNonBlankCellIn(Target, xlValues, xlByRows)
If Not (R Is Nothing) Then
LastRow = IIf(R.Row > LastRow, R.Row, LastRow)
Set R = LastNonBlankCellIn(Target, xlValues, xlByColumns)
LastCol = IIf(R.Column > LastCol, R.Column, LastCol)
End If
LastNonBlankCellAddress = Target.Parent.Cells(LastRow, LastCol).Address
End Function
Private Function LastNonBlankCellIn(Target As Range, _
LookIn As XlFindLookIn, SearchOrder As XlSearchOrder) As Range
Set LastNonBlankCellIn = Target.Find(What:="*", After:=Target(1), _
LookAt:=xlPart, LookIn:=LookIn, MatchCase:=False, _
SearchOrder:=SearchOrder, SearchDirection:=xlPrevious)
End Function
John could use the UDF in a cell formula like this:
= "$A$8:" & LastNonBlankCellAddress(A:N)
If a relative address is preferred, use this:
= "A8:" & SUBSTITUTE(LastNonBlankCellAddress(A:N), "$", "")
LastNonBlankCellAddress accounts for cells that are hidden or have a formula returning null text (""). The Tip's last formula ignores cells with null text.
2023-12-30 23:10:03
Tomek
The first two formulas work in specific circumstances, but with caveats as described. They work best if at least one column has no blank cells and the count is based on that column. The last formula given works without those caveats, but I think the worksheet can be designed in a way that may be easier to manage later, even though it is a bit more work to set it up.
My suggested approach is to first create a table for the area where the data will be entered. Such table can be created with some data already entered, including heading row with labels. After creating the table, if you wish you can turn off the auto-filter (on Data tab of the Ribbon there is an icon for that), as well as change/remove any shading and borders that are automatically imposed by Excel. The benefit of such approach is that if you enter new data in the row directly below the last row of the table, the table will expand automatically. If you enter the additional data further down the spreadsheet leaving some rows empty, the table will not expand automatically, but you can easily adjust the range of the table by dragging the small triangle at the bottom right of the table. This way you can also exclude some data from the table. And of course you can enter/update the data within the table. You can also insert or delete rows from the table except the header row, and the table range will adjust automatically.
Once you have created the table, Excel gives it a name, typically "TableN" where N is a counter of the tables created. You can find that name by clicking inside the table and opening the Table Design tab of the ribbon: the information is the the Properties group at the very left of the ribbon. Note that the table name refers to the range of the data in the table, excluding the header row.
Now, to use all that in the formula that John wanted, you can type in = "A8:N" & (ROWS(Table1) + 7). (adjust the table counter if necessary).
2023-12-30 09:39:39
J. Woolley
The Tip's last sentence says, "...if you are using an older version, you'll want to enter it as an array formula (Ctrl+Shift+Enter)." I don't think this is necessary because the formula does not return an array.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments