How can I find last row that contains data in a specific column?

后端 未结 13 1594
南笙
南笙 2020-11-22 03:44

How can I find the last row that contains data in a specific column and on a specific sheet?

13条回答
  •  攒了一身酷
    2020-11-22 04:12

    Public Function GetLastRow(ByVal SheetName As String) As Integer
        Dim sht As Worksheet
        Dim FirstUsedRow As Integer     'the first row of UsedRange
        Dim UsedRows As Integer         ' number of rows used
    
        Set sht = Sheets(SheetName)
        ''UsedRange.Rows.Count for the empty sheet is 1
        UsedRows = sht.UsedRange.Rows.Count
        FirstUsedRow = sht.UsedRange.Row
        GetLastRow = FirstUsedRow + UsedRows - 1
    
        Set sht = Nothing
    End Function
    

    sheet.UsedRange.Rows.Count: retrurn number of rows used, not include empty row above the first row used

    if row 1 is empty, and the last used row is 10, UsedRange.Rows.Count will return 9, not 10.

    This function calculate the first row number of UsedRange plus number of UsedRange rows.

提交回复
热议问题