Consulting

Results 1 to 8 of 8

Thread: Last Populated Row in Range

  1. #1

    Last Populated Row in Range

    Hi All,
    Does anybody know how to find the last populated row in a range of cells?:

    A B C D E F
    1 x x x x x x
    2 x x x x
    3 x x x x
    4 x x x x
    5 x x x
    6

    i.e. for the above, where x signifies a non empty cell:
    Function GetLastPopulatedRow( Range("A1:F6" ) = 5

    Many Thanks.
    BBM

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function GetLastPopulatedRow(ByRef rng As Range)
    GetLastPopulatedRow =rng.CurrentRegion.Rows.Count - rng.Row + 1
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I thought this would work for any location, but it fails in Excel 2000 (as does yours) Any thought why, Bob?

    [VBA]
    Function GetLastPopulatedRow(ByRef rng As Range)
    Dim r As Range
    Set r = rng.CurrentRegion
    MsgBox "Range - " & r.Address
    MsgBox "First row - " & r(1).Row
    MsgBox " Row Count - " & r.Rows.Count
    GetLastPopulatedRow = r(1).Row + r.Rows.Count
    End Function
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    In this, I find the last non-blank absolute last row or column in a range.
    [VBA]'=LastNBRow(A3:G10)
    Function LastNBRow(rng As Range) As Long
    Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Rows.
    LastRow = rng.Find(What:="*", After:=rng.Cells(rng.Rows.Count, rng.Columns.Count), _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    End If
    LastNBRow = LastRow
    End Function

    '=LastNBCol(A3:G10)
    Function LastNBCol(rng As Range) As Long
    Dim LastColumn As Integer
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Columns.
    LastColumn = rng.Find(What:="*", After:=rng.Cells(rng.Rows.Count, rng.Columns.Count), _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious).Column
    End If
    LastNBCol = LastColumn
    End Function[/VBA]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    I thought this would work for any location, but it fails in Excel 2000 (as does yours) Any thought why, Bob?

    [VBA]
    Function GetLastPopulatedRow(ByRef rng As Range)
    Dim r As Range
    Set r = rng.CurrentRegion
    MsgBox "Range - " & r.Address
    MsgBox "First row - " & r(1).Row
    MsgBox " Row Count - " & r.Rows.Count
    GetLastPopulatedRow = r(1).Row + r.Rows.Count
    End Function
    [/VBA]
    I think it is just logic errors Malcolm.

    You needed to subtract 1 else the first row gets double-counted, mine was adding where it should subtract and vice versa.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It's not that. With 1 or more cells selected in a block
    [VBA] GetLastPopulatedRow = rng.CurrentRegion.Rows.Count
    [/VBA]returns a value = selection.rows.count
    CurrentRegion is ignored.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not for me Malcolm, I just selected B4:C5, last data was in B6, and I ran

    ?GetLastPopulatedRow(range("A1:F10"))

    and it returned 6.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    With A1:E14 populated, I tried this.
    The sub returns 70 in both cells. =Test2(D6) returns 1

    [VBA]
    Sub Test()
    ActiveCell = ActiveCell.Offset(, -5).CurrentRegion.Cells.Count
    ActiveCell.Offset(1) = Test2(ActiveCell.Offset(, -5))
    End Sub

    Function Test2(ByRef rng As Range)
    Test2 = rng.CurrentRegion.Cells.Count
    End Function
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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