Better way to find last used row

前端 未结 8 1178
情歌与酒
情歌与酒 2020-11-22 15:32

I am trying to make this way of finding the last row as I found the last column:

Sheets(\"Sheet2\").Cells(1,Sheets(\"Sheet2\").Columns.Count).End(xlToLeft).C         


        
8条回答
  •  再見小時候
    2020-11-22 16:11

    This gives you last used row in a specified column.

    Optionally you can specify worksheet, else it will takes active sheet.

    Function shtRowCount(colm As Integer, Optional ws As Worksheet) As Long
    
        If ws Is Nothing Then Set ws = ActiveSheet
    
        If ws.Cells(Rows.Count, colm) <> "" Then
            shtRowCount = ws.Cells(Rows.Count, colm).Row
            Exit Function
        End If
    
        shtRowCount = ws.Cells(Rows.Count, colm).Row
    
        If shtRowCount = 1 Then
            If ws.Cells(1, colm) = "" Then
                shtRowCount = 0
            Else
                shtRowCount = 1
            End If
        End If
    
    End Function
    
    Sub test()
    
        Dim lgLastRow As Long
        lgLastRow = shtRowCount(2) 'Column B
    
    End Sub
    

提交回复
热议问题