• Breaking News

    VBA Code: How to find last ROW and Last Column

    How to find the last row in the excel through VBA code

    To find the last row containing text, we search from the bottom of the worksheet using xlUp. It stops at the first cell that contains the text.

     

    VBA


    Code: 

     

    Private Sub CommandButton1_Click()

    DimlLastRow As Long

    Dimsh As Worksheet

    Setsh = ThisWorkbook.Worksheets("Sheet1")

    lLastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row

    MsgBox lLastRow

    End Sub


    How to find the last column in the excel through VBA code

    We get the last column with text using a similar method. In this case, we search from right to left so as to find the last column with text.

    VBA


    Private Sub CommandButton1_Click()

    Dim lLastCol As Long

    Dim sh As Worksheet

    Set sh = ThisWorkbook.Worksheets("Sheet1")

    lLastCol = sh.Cells(1, sh.Columns.Count).End(xlToLeft).Column

    MsgBox lLastCol

    End Sub


    No comments

    View My Stats