Home » excel » excel – active cell find last column in a row vba

excel – active cell find last column in a row vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve got this code that finds a specific cell, and then then makes the active cell one to the right of this.

However, I want to make it so that it finds the cell with apples and bananas in it, and instead makes the active cell the cell in the last used column of the same row as the original found apples and bananas cell is in.

I hope this makes sense and I appreciate any help.

Thanks.

Cells.Find(What:="Apples and Banana", After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 1).Select
How to&Answers:

You can do it by finding the row of the cell with the help of find and .row and then checking the last column before activating the cell.

Option Explicit

    Sub test()

    Dim LastCol As Long
    Dim ABRow As Long
    Dim ABrange As Range

      Set ABrange = Cells.Find(What:="Apples and Banana", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)

    If Not ABrange Is Nothing Then
        ABRow = ABrange.Row
    End If

    LastCol = Cells(ABRow, Columns.Count).End(xlToLeft).Column

    Cells(ABRow, LastCol).Activate

End Sub