Consulting

Results 1 to 9 of 9

Thread: Solved: Last Row in Range (again)

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: Last Row in Range (again)

    hi,

    this recent thread http://www.vbaexpress.com/forum/showthread.php?t=22716 didn't help me with my question.

    i have data in multiple columns. i need to find the last row with data within a range of columns (column h to column m). i need to write data to the next blank row. the rows are not hidden & does not contain formulas.

    i have looked at other solutions but i couldn't find one that dealt with specific ranges.


    thanks
    zach

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Would i be correct in saying that was cell "i14" you want to identify/select?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi gerogiboy,

    i would need to know row 14, not necessary "i14"

    thanks
    zach

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's a Function to keep it flexible

    [vba]
    Option Explicit
    Sub LastRw()
    MsgBox LastRow("H", "M")
    End Sub

    Function LastRow(Strt As Variant, Endd As Variant) As Long
    Dim Rw As Long, R As Long, i As Long

    Strt = Columns(Strt).Column
    Endd = Columns(Endd).Column
    Rw = 0
    For i = Strt To Endd
    R = Cells(Rows.Count, i).End(xlUp).Row
    If R > Rw Then Rw = R
    Next
    LastRow = Rw
    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'

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub FindLastRow()
    Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Rows.
    LastRow = Range("H1:M" & Rows.Count).Find(What:="*", After:=[H1], _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    MsgBox LastRow+1
    End If
    End Sub[/VBA]

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    [VBA]Sub Last()
    Dim h, i, j, k, l, m As Integer

    h = WorksheetFunction.CountA(Range("H:H"))
    i = WorksheetFunction.CountA(Range("i:i"))
    j = WorksheetFunction.CountA(Range("j:j"))
    k = WorksheetFunction.CountA(Range("k:k"))
    l = WorksheetFunction.CountA(Range("l:l"))
    m = WorksheetFunction.CountA(Range("m:m"))

    'finds the column with the most data, then adds the ammount of empty rows above your data
    MsgBox WorksheetFunction.Max(h, i, j, k, l, m) + 9 '+ 8 is the ammount of empty rows above your data

    End Sub[/VBA]

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi georgiboy,
    That will only work with "solid" data, gaps will give misleading results
    Regards
    MD
    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'

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location

    Very true

    Thanks for the heads up
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  9. #9
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi malcolm, kenneth & georgiboy,

    thanks for the coding.

    zach

Posting Permissions

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