Results 1 to 8 of 8

Thread: Find the last row in a range

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13

    Find the last row in a range

    Hi,

    Use the following code to identify the last row of the range.
    Can you recommend a better solution!

    Code:
        Dim Riga  As Long
        Riga = Application.WorksheetFunction.Max(Cells(Rows.Count, "A").End(xlUp).Row, _
        Cells(Rows.Count, 2).End(xlUp).Row, Cells(Rows.Count, "C").End(xlUp).Row)
        MsgBox Riga
    Thanks in advance

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    Use the following code to identify the last row of the range.
    Can you recommend a better solution!

    Code:
        Dim Riga  As Long
        Riga = Application.WorksheetFunction.Max(Cells(Rows.Count, "A").End(xlUp).Row, _
        Cells(Rows.Count, 2).End(xlUp).Row, Cells(Rows.Count, "C").End(xlUp).Row)
        MsgBox Riga
    Give this a try...
    Code:
    Riga = Columns("A:C").Find(What:="*", SearchOrder:=xlRows, _
           SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
    And if you do not mind maintaining empty arguments, we can shorten that code line down to this by eliminating the named arguments...
    Code:
    Riga = Columns("A:C").Find("*", , xlFormulas, , xlRows, xlPrevious).Row
    Last edited by Rick Rothstein; 03-21-2013 at 04:52 AM.

  3. #3
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,

    Thanks for the suggestions, I ask if there are alternatives in the event of non-adjacent columns.
    I tried to change the range of research, but I can not use the method ..

    Code:
    Riga = Columns("A:C", "G").Find

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Wink try this

    Code:
    riga = Range("A:C, G:G").find
    Last edited by ashu1990; 03-21-2013 at 05:17 PM. Reason: doesnt know to post in the code window

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    Thanks for the suggestions, I ask if there are alternatives in the event of non-adjacent columns.
    I tried to change the range of research, but I can not use the method ..

    Code:
    Riga = Columns("A:C", "G").Find
    ashu1990 gave you the correct syntax to use... Range("A:C,G:G") instead of Columns("A:C","G") because Columns cannot take non-contiguous ranges for its argument... however, you cannot use non-contiguous ranges with the Find function (well, you can, but it will end up finding a value in the last area of the non-contiguous range first and not look at the other areas making up the non-contiguous range, so you will not always get the correct answer doing so). I think you are back to your Max-method, but I would write it as a loop so that it is more flexible...
    Code:
    Dim Riga As Long, Ar As Range
    
    For Each Ar In Range("A:C,G:G")
      Riga = Application.Max(Riga, Ar(Ar.Count).End(xlUp).Row)
    Next
    
    MsgBox Riga

  6. #6
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,

    I think you are back to your Max-method, but I would write it as a loop so that it is more flexible...
    Thanks Rick Rothstein the suggestion, unfortunately if run with Excel 2003 takes 10 seconds!!

    I propose the following Sub

    Code:
    Sub MaxCol()
        Dim Coln As Long, Surface
        Surface = Array("B", "C", "G")
        For Coln = 0 To UBound(Surface)
            Surface(Coln) = Cells(Rows.Count, Surface(Coln)).End(xlUp).Row
        Next
        MsgBox Application.Max(Surface)
    End Sub

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by PcMax View Post
    Thanks Rick Rothstein the suggestion, unfortunately if run with Excel 2003 takes 10 seconds!!

    I propose the following Sub

    Code:
    Sub MaxCol()
        Dim Coln As Long, Surface
        Surface = Array("B", "C", "G")
        For Coln = 0 To UBound(Surface)
            Surface(Coln) = Cells(Rows.Count, Surface(Coln)).End(xlUp).Row
        Next
        MsgBox Application.Max(Surface)
    End Sub
    I did notice a slight hesitation on my system, but nowhere near 10 seconds, that's for sure. Okay, let's put that aside and try something completly different (it should be fast)...
    Code:
    Sub GetMaxRow()
      Dim Ar As Range, Max As Long, MaxRow As Long
      For Each Ar In Range("A:C,G:G").Areas
        Max = Evaluate("MAX(ROW(" & Ar.Resize(Ar.Rows.Count - 1).Address & _
              ")*(" & Ar.Resize(Ar.Rows.Count - 1).Address & "<>""""))")
        If Max > MaxRow Then MaxRow = Max
      Next
      MsgBox MaxRow
    End Sub

  8. #8
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,
    I did notice a slight hesitation on my system, but nowhere near 10 seconds, that's for sure. Okay, let's put that aside and try something completly different (it should be fast)...
    Code:
    For Each Ar In Range("A:C,G:G")
      Riga = Application.Max(Riga, Ar(Ar.Count).End(xlUp).Row)
    Next
    I tried the last time in a sheet with the cell K625:
    Excel 2003 7,9 sec while Excel 2013 4,2 sec Pc New!

    Remember that this code should work on a PC 800 mhz...

    Thanks for all the suggestions received

Similar Threads

  1. Lookup From Cell Range By Matching Row and Column
    By paul_pearson in forum Excel Help
    Replies: 2
    Last Post: 03-07-2013, 02:02 PM
  2. Find Last Used Row In A Column Using Long Variable
    By xander1981 in forum Excel Help
    Replies: 2
    Last Post: 01-27-2013, 08:53 PM
  3. Vba Code to find value and paste on certain row
    By jwitte in forum Excel Help
    Replies: 3
    Last Post: 11-28-2012, 08:52 PM
  4. Find Last Used Row In Column
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 2
    Last Post: 06-04-2012, 07:40 AM
  5. Find The Last Entry Row In A Column That Contains Only Numbers
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-09-2011, 05:19 AM

Posting Permissions

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