Home » excel » excel – How to find the LastColumn including blanks

excel – How to find the LastColumn including blanks

Posted by: admin May 14, 2020 Leave a comment


I am trying to find the last column with data in it. Some of my columns may contain blank columns between them.

The problem with my code is it keeps reverting back to column 13 as the last used column, when in fact the last used column is 19. I want my code to start counting from L8.

How can I fix my code to include blank columns in between?

LastCol = ws.Cells.Find(What:="*", _
            After:=ws.Range("L8"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext, _

Screen Shot

enter image description here

How to&Answers:

The Range.Find method returns a Range object that represents the first cell where that information is found. So if the first non-empty cell is in L13, that is what would always be returned by this formula. You may choose to search backward (from L1), but there is an easier and much more straightforward way. Just use:

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


Start from A1 and search backwards:

Sub sjkdfhsgf()
    Set ws = ActiveSheet
    Set terminus = ws.Cells.Find(What:="*", _
                After:=ws.Range("A1"), _
                LookAt:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, _
                Lastcol = terminus.Column
    MsgBox Lastcol
End Sub