Mega Code Archive

 
Categories / VisualBasic Script / Excel
 

Returns a number that represents the last nonempty cell in the same row

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