+ Reply to Thread
Results 1 to 21 of 21

Find Last Used Row in a Range

  1. #1
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Find Last Used Row in a Range

    Hi,

    Been working this for a while.
    Can find VBA for entire column but not a range.
    Came up with the code below, after looking at excel formulas.
    Seems to work except if all cells in my range are empty.
    Any thoughts on how to fix this or alternate code?
    Thanks For your help, JV

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Find Last Used Row in a Range

    Please Login or Register  to view this content.
    That will find the last cell that has something in it. From there you can pull
    Please Login or Register  to view this content.

  3. #3
    Forum Expert wk9128's Avatar
    Join Date
    08-15-2020
    Location
    Australia
    MS-Off Ver
    365 V2403 and WPS V2022
    Posts
    3,498

    Re: Find Last Used Row in a Range

    Pls try this code

    Please Login or Register  to view this content.
    Last edited by wk9128; 04-13-2024 at 02:25 AM.

  4. #4
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Hi Thomglea,

    Need to read up on range find as never used it before.
    What:="*" means search for anything?
    How does fndrng.row find the last used row?
    Tested the code, works but not if range is blank.

    Thanks for your help, John

  5. #5
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: Find Last Used Row in a Range

    "*" is a wildcard. Means it will hit on anything. Only thing to think about there is if the cell has a formula, but the formula evaluates to a blank, it will return that cell. Easy to fix by adding LookIn:=XlValues
    fndrng.row does not find the last row, the .Find function finds the cell in the last row. fndrng.row is a method that returns the row number.

  6. #6
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Thank you.

    I saw this approach on entire column, but could not figure out how to apply to a range.
    I tested the code below, but seems that VBA does not accept this.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    I see, interesting.
    Thank you for explaining.

  8. #8
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,481

    Re: Find Last Used Row in a Range

    Another way:
    The range could be column or range

    PHP Code: 
    Option Explicit
    Sub findLastRow
    ()
    Dim i&, rng As Range
    Set rng 
    Range("F6:Z100")
    For 
    rng.Cells.Count To 1 Step -1
        
    If rng.Cells(i) <> "" Then
            MsgBox rng
    .Cells(i).Row
            
    'MsgBox rng.Cells(i).address ' return cell address
            
    Exit Sub
        End 
    If
    Next
    MsgBox 
    "The range is empty!"
    End Sub 
    Last edited by bebo021999; 04-13-2024 at 12:38 AM.
    Quang PT

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: Find Last Used Row in a Range

    Use of formula
    Please Login or Register  to view this content.
    Last edited by jindon; 04-13-2024 at 12:52 AM.

  10. #10
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Thank you bebe021999 and Jindon for your soutions.

    Your solutions are beyond my VBA understanding, sorry but much appreciated.

    I refined my original code and is now doing what I was looking for.

    Thank you all for your help.

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Hi Jindon,

    If i understand this correctly, this seems to be an excel formula translated to VBA code.
    I considered this idea, but my VBA skills are to insuficient.
    Thank you for taking the time to post this.
    Kindest regards john

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,596

    Re: Find Last Used Row in a Range

    You can check.

    Enter any cell other than F6:F16

    =MAX(MAX(IF(F6:F16<>"",ROW(F6:F16))),6)

  13. #13
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Hi Jindon,

    OK undrstood.
    I was using this excel formula =MAX(IF(ISBLANK(F6:F25),0,ROW(F6:F25)))
    Found it to complex to translateto VBA code.
    That now seems doable with your code example.
    Regards john

  14. #14
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Hi bebo021999,

    I almost understand this code, but still a bit foggy.
    Basically you start in the last row/column of the range and loop backwards from the total number of cells by -1.
    If the cell i not blank you provide the row number.
    I dont see where it gets the maximum row number, can you explain please?
    Also is this part code or text .....'MsgBox rng.Cells(i).address ' return cell address?

    Thanks for your help, John

  15. #15
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,444

    Re: Find Last Used Row in a Range

    Modify your code
    Please Login or Register  to view this content.

  16. #16
    Forum Expert wk9128's Avatar
    Join Date
    08-15-2020
    Location
    Australia
    MS-Off Ver
    365 V2403 and WPS V2022
    Posts
    3,498

    Re: Find Last Used Row in a Range

    Hi John Vieren
    POST # 3 code has been updated, please test it


    If you finally get a solution please mark your thread as SOLVED:
    - Click Thread Tools above your first post,you will see the word PREFIX on the upper left, press the button to select [SOLVED] select "Mark your thread as Solved".
    - you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help
    was given. By doing so you can add to the reputation(s) of those who helped.

  17. #17
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Hi wk9128,

    Updated code is working fine, thank you.
    Was not aware of the "add to reputation" and have done so for all who assisted me, thank you.
    The thread has previously been marked as solved.
    Kindest regards John

  18. #18
    Forum Expert wk9128's Avatar
    Join Date
    08-15-2020
    Location
    Australia
    MS-Off Ver
    365 V2403 and WPS V2022
    Posts
    3,498

    Re: Find Last Used Row in a Range

    @John Vieren You're Welcome. Glad to help . Thank You for the feedback and rep.

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    wk9128,

    I see, looping backards from bottom right hand corner of column F and first cell that does not have a blank is the last row.
    I did not expect such a massive response to my posting and learned quite alot of VBA as a result.
    Am very grateful and thank you all, JV

  20. #20
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,481

    Re: Find Last Used Row in a Range

    Quote Originally Posted by John Vieren View Post
    Hi bebo021999,
    Basically you start in the last row/column of the range and loop backwards from the total number of cells by -1.
    If the cell i not blank you provide the row number.
    I dont see where it gets the maximum row number, can you explain please?
    Also is this part code or text .....'MsgBox rng.Cells(i).address ' return cell address?
    The array rng (F6:Z100) contains hundreds of cells, with each cell represented as rng.cells(i), where i counts from 1 to the last cell.

    Looping from the last cell backward to 1, when encountering the first non-empty cell, display a MsgBox, then stop the program.

    Please Login or Register  to view this content.
    If the loop completes without encountering an empty cell, then the final message will be displayed:

    Please Login or Register  to view this content.
    Hope it is clear enough for you

  21. #21
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Hong Kong
    MS-Off Ver
    Office 365 PC Version
    Posts
    204

    Re: Find Last Used Row in a Range

    Very clear, thank you.

+ 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. Method FIND, to find a DATE in a hidden range
    By JCabral in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-13-2020, 12:43 PM
  2. [SOLVED] Use variable to find and copy range with find function
    By PaulM100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2019, 09:23 AM
  3. Find pairs of words in range, then find next pair
    By brent.fraser in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2014, 12:31 PM
  4. Code to find named range doesn't find the correct field range?
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-12-2012, 05:54 PM
  5. [SOLVED] Using range.find to find two specific values in two columns
    By mhrub in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2012, 01:02 PM
  6. Range.Find to find column and place value in next available cell in one line
    By davegugg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2011, 10:41 AM
  7. find, meet condition, sum range, deduce, find next
    By pinstripe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-27-2007, 09:30 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