GetLastUsedColumnNumberInWorksheet

Function GetLastUsedColumnNumberInWorksheet( _
    ws As Worksheet _
) As Long
    Dim _
        rng As Range, _
        rngResults As Range
    Set rng = _
        ws.Cells
    Set rngResults = _
        rng.Find( _
            what:="*", _
            After:=rng.Cells(1), _
            Lookat:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False _
        )
    If _
        rngResults Is Nothing _
        Then
        GetLastUsedColumnNumberInWorksheet = _
            -1
    Else
        GetLastUsedColumnNumberInWorksheet = _
            rngResults.Column
    End If
    Exit Function
ErrorHandler:
    GetLastUsedColumnNumberInWorksheet = _
        -1
End Function

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *