从 excel sheet 动态读取数据时也会读取空行

Null rows is also read when reading data from excel sheet dynamically

endofrow  = excelWorksheet.get_Range("X3", "Y3").get_End(XlDirection.xlDown).Row.ToString();
//endofrow value must be 5 but it comes as 8 and null rows appear 

此函数必须只读取 2 行,因为只有两行数据存在,但它会读取额外的行,并且在插入过程中会出现空行。

我使用这段代码 return 最后一行。希望这对你有帮助

Sub GetLastRow()
Dim ws As Worksheet
Dim lRow As Long

Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        lRow = 1
    End If
End With

MsgBox "The last row in Sheet1 which has data is " & lRow
End Sub

您可以根据需要进行更改。