+ Reply to Thread
Results 1 to 16 of 16

Is there any reliable way to get the last used row of a sheet?

  1. #1
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Is there any reliable way to get the last used row of a sheet?

    Is there any reliable way to get the last used row of a sheet?

    By used row I mean a row that contains either a value or a formula.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Is there any reliable way to get the last used row of a sheet?

    This returns the last used row number of the active sheet:

    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Re: Is there any reliable way to get the last used row of a sheet?

    Quote Originally Posted by Olly View Post
    This returns the last used row number of the active sheet:

    Please Login or Register  to view this content.
    Thank you for the attempt, but unfortunately that is not reliable.

    In this example sheet the VBA statement returns 8 when it should return 4.
    LastRow.xlsx

    My questions was:
    "Is there any reliable way to get the last used row of a sheet?
    By used row I mean a row that contains either a value or a formula."

    UsedRange also counts cells that have formatting and according to my experience cells that have had values at some point but don't anymore.

    Any more suggestions?

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Is there any reliable way to get the last used row of a sheet?

    option basing on the sample file:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Is there any reliable way to get the last used row of a sheet?

    Okay, then try:
    Please Login or Register  to view this content.
    Last edited by Olly; 09-22-2014 at 10:41 AM. Reason: Edited to display Row, not cell address

  6. #6
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Re: Is there any reliable way to get the last used row of a sheet?

    I guess it depends on what a person would define as "reliable".

    The suggested solutions give incorrect answers when filters are applied in Excel. You can test it on the sheet called suggestions 2 & 3 in the following file:
    LastRow2.xlsx

    Any more suggestions?

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Is there any reliable way to get the last used row of a sheet?

    Edit: snipped. Still not right.
    Last edited by Olly; 09-22-2014 at 11:30 AM.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Is there any reliable way to get the last used row of a sheet?

    Okay, so you want to return the last row containing either a constant, or a formula (regardless of what the formula returns), regardless of which column it is in, regardless of whether it is visible or filtered, regardless of whether the file has been saved since last change... is that right?

    Struggling to find a neater method than a loop:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is there any reliable way to get the last used row of a sheet?

    I'd loop, but I'd loop rows, not cells:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Is there any reliable way to get the last used row of a sheet?

    or maybe something like
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    05-30-2011
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    107

    Re: Is there any reliable way to get the last used row of a sheet?

    Quote Originally Posted by romperstomper View Post
    I'd loop, but I'd loop rows, not cells:
    Please Login or Register  to view this content.
    Looping from last row in used range and upwards is the best solution I have found as well.

    But I've often heard that used range is unreliable. Will the answer always be correct?

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is there any reliable way to get the last used row of a sheet?

    Usedrange is only unreliable in about 5% of cases and less than 1% of cases where you use code to reset the usedrange. It's also only unreliable in that it:
    1. Will overstate the range of used cells, not understate. (so looping back should be fine)
    2. Will not take into account shapes on the sheet (including embedded charts)

    For this purpose it should be fine.

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Is there any reliable way to get the last used row of a sheet?

    FWIW:

    Please Login or Register  to view this content.

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is there any reliable way to get the last used row of a sheet?

    I don't understand the logic of that. This part:
    Please Login or Register  to view this content.
    will always be 1 since you're just referring to one contiguous block of cells.

  15. #15
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Is there any reliable way to get the last used row of a sheet?

    re: statements in post#12

    would you like to confirm these are correct (using your code) for suggestion 1 in the downloaded file?

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is there any reliable way to get the last used row of a sheet?

    The statements are correct but there is an error in the code:
    Please Login or Register  to view this content.
    should read:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pasting data into filtered cells not reliable
    By Motox in forum Excel General
    Replies: 1
    Last Post: 07-31-2013, 01:32 AM
  2. VBA file saving reliability - how reliable is it?
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2013, 09:54 PM
  3. How do I get Countif to be reliable
    By RageWizard in forum Excel General
    Replies: 14
    Last Post: 03-05-2010, 04:33 PM
  4. Dir function to check for file not reliable
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-25-2006, 01:15 PM
  5. Is Excel reliable
    By ראובן in forum Excel General
    Replies: 18
    Last Post: 08-08-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1