+ Reply to Thread
Results 1 to 10 of 10

What is the ideal, 100% full proof, way to find the last row or last column?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    What is the ideal, 100% full proof, way to find the last row or last column?

    I've been working with VBA for a few years now and over that time I've seen many ways to find the last row or last column in a spreadsheet. Any experts willing to put their reputation on the line and post the best way?

    Some examples I've used before.

        'Only works for one column.
        last_R = Range("A65536").End(xlUp).Row
    
        'Won't work if worksheet is empty.
        last_R = Cells.Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
    
        'Not sure how good this method is but only works on one column I think.
         last_R = Range("J" & Rows.Count).End(xlUp).Row
    Last edited by 111StepsAhead; 04-17-2013 at 08:37 AM.

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    I've always like just using rows.count to find the bottom, the doing and xlup

    lastR= someWorksheet.cells(rows.count, someColumnNumber).end(xlup).row
    For last column you have the similar columns.count

    lastC= someWorksheet.cells(someRowNumber, columns.count).end(xltoleft).column

    --edit--
    The 1st and 3rd example you posted are similar, but you can get around using the "range(Adress)" and just use Cells(row#,col#)

    Your second example fails because the find method will be null (is Nothing) and there for can't be referenced by .end(xlup).

    I would get in the habit of NOT using the "Range(J & Rows.count)" kind of statement. Basically it works just fine, but if someone edits the sheet you will have to edit the macro.

    Usually, it's handy to give the column headers a distinctive name, then used FIND to locate that column and pass in the column number. For example,
    lastR= someWorksheet.cells(rows.count, someWorksheet.cells.find("myHeaderName").column).end(xlup).row
    I like that last example because you can edit the sheet (and therefore move some columns around possibly) and not worry about having to track down hard-coded references (like your Range("J" & rows.count) example).

    AVOID HARD-CODED REFERENCES!
    Last edited by GeneralDisarray; 04-10-2013 at 11:27 AM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    I would get in the habit of NOT using the "Range(J & Rows.count)" kind of statement.-GeneralDisarray
    Yes, I did this when I first started and it came back to haunt me. I also like the header method you mentioned and will put that to use in the future.

    Thanks for your input.

  4. #4
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    last_R = someWorksheet.Cells(Rows.Count, someColumnNumber).End(xlUp).Row
    last_C = someWorksheet.Cells(someRowNumber, Columns.Count).End(xlToLeft).Column
    My issue with this code is I can only specify one row or one column to search. I'd like to have something that checks all used columns when looking for the last row and all used rows when looking for the last column.

  5. #5
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    Quote Originally Posted by 111StepsAhead View Post

    My issue with this code is I can only specify one row or one column to search. I'd like to have something that checks all used columns when looking for the last row and all used rows when looking for the last column.
    hmm... that never occurred to me. I've never needed to do that, usually i care about specific fields (columns).


    ---

    I was just trying to think of a reason you would need to do that, maybe need to add records where no column is guaranteed to be filled? Try the 'currentRegion' trick -- use this sometimes to fill blanks.
    The current region allows for some blanks in a table, but not complete gaps (missing entire rows). Again, I'm not sure what you are doing, but this is worth knowing about -- there's nothing wrong with the other method by AB33.

    To start with you need some kind of 'seed' cell or range. Like the upper left corner of a table or something.

    lastR = seedCell.row + seedCell.currentRegion.rows.count -1
    Look up currentRegion, it's a bit of a long explanation. Usually i use it to fill in gaps in a table. To do that i would do this:

    on error resume next
    seedCell.currentRegion.specialCells(xlCellTypeBlanks) = 0
    'if there are no blanks, will throw an error
    Again, if you are just looking for the last cell in a sheet for some reason try AB33's method, but if you would like to find the bottom of a rectangular range (and for some reason you can't guarantee there is a 'key' column that is always populated, try this
    Last edited by GeneralDisarray; 04-12-2013 at 05:42 AM.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    "Any experts willing to put their reputation on the line and post the best way?"
    I am not here for reputation and do not have any stake on it, but here are my ten cents

    last_R = Cells.Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
      last_C = Cells.Find("*", Searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column
    Last edited by AB33; 04-10-2013 at 11:26 AM.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    The code I posted works with any data format. It uses the "Find" function and would not bother about counts and columns. It does searches across rows.

  8. #8
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    Quote Originally Posted by AB33 View Post
    The code I posted works with any data format. It uses the "Find" function and would not bother about counts and columns. It does searches across rows.
    Yes, your code is great except if the worksheet is empty. I suppose, after thinking about it for a few days, I could just have it know the only error is an empty worksheet and deal with it accordingly.

    On Error GoTo DealWithError:
        last_R = Cells.Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
    On Error GoTo 0:
    
    DealWithError:
        Msgbox "Worksheet " & someWorksheet & " is void of all data!", vbokonly, "Missing Data"
        'take steps to cancel procedure
    Resume Next
    The reason for this exercise is I try to make code as robust as possible. Some times I have the user select a file to "process" and if an error is thrown the user will have to deal with it.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    Please do not reply with quote, just reply will do.
    Okay, I see your point, but you could add another line
    if LR< 1 then exit sub
    or
    if LR< 1 then msgbox: "Your data is blank"

  10. #10
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: What is the ideal, 100% full proof, way to find the last row or last column?

    wont happen.JPG

    This is what happens when you run that line of code on an empty worksheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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