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

时间:2015-03-23 13:58:48

标签: excel-vba ssis vba excel

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行,因为只存在两行数据,但它会读取额外的行,并且在插入过程中会出现空行。

1 个答案:

答案 0 :(得分:0)

我使用此代码返回最后一行。希望这可以帮到你

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

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