Mega Code Archive

 
Categories / VisualBasic Script / Excel
 

Returns a range object that represents the last non-empty cell in the same row

Function GetLastCellInRow(rg As Range) As Range      Dim lMaxColumns As Long      lMaxColumns = ThisWorkbook.Worksheets(1).Columns.Count      If IsEmpty(rg.Parent.Cells(rg.Row, lMaxColumns)) Then          Set GetLastCellInRow = _              rg.Parent.Cells(rg.Row, lMaxColumns).End(xlToLeft)      Else          Set GetLastCellInRow = rg.Parent.Cells(rg.Row, lMaxColumns)      End If  End Function