Consulting

Results 1 to 5 of 5

Thread: Find the last item and select it

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Find the last item and select it

    I know how to find the next item in a workbook but how can I find the last one. ex:

    test
    test
    test
    test
    test
    test
    test
    report

    I would like to find the last test before report with the find command. Is this possible?

    Daniel

  2. #2
    VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Here is a Function that you can use to find the last populated row:

    [VBA] Function xlLastRow(Optional WorksheetName As String) As Long

    ' find the last populated row in a worksheet

    If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
    With Worksheets(WorksheetName)
    On Error Resume Next
    xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
    xlWhole, xlByRows, xlPrevious).Row
    If Err <> 0 Then xlLastRow = 0
    End With

    End Function [/VBA]

    This is a user defined function found in our KB. Don't think you can use the rergular "Find" to do it. HTH
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thank you but I am not looking for the last populated row. I am looking for the last of a particular text string.

  4. #4
    VBAX Regular
    Joined
    Jun 2006
    Location
    Melbourne
    Posts
    9
    Location
    You could use the Match command like this:

    [vba]Sub FindLastItem()
    Dim MatchFound As Integer
    On Error Resume Next
    MatchFound = Application.Match("report", Columns(1), 0)
    If MatchFound = 0 Then
    MsgBox "No match found"
    Else
    MsgBox "Match found on row " & MatchFound
    MsgBox "Selecting previous row..."
    Range("a1").Offset(MatchFound - 2, 0).Select
    End If
    End Sub
    [/vba]

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Two searches I think
    [VBA]
    Sub FindLastItem()
    Dim FirstReport As Range, LastNext As Range, ToFind As String
    ToFind = InputBox("Enter words to find", , "Test,Report")
    Set FirstReport = Columns(1).Find(what:=Split(ToFind, ",")(1), _
    after:=Cells(65536, 1), MatchCase:=False, searchdirection:=xlNext)
    Set LastNext = Columns(1).Find(what:=Split(ToFind, ",")(0), _
    after:=FirstReport, MatchCase:=False, searchdirection:=xlPrevious)

    LastNext.Activate

    End Sub

    [/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
  •