VB.NET Find Last Row in an Excel Sheet / 엑셀 마지막 행 구하기

2016. 7. 21. 16:01IT/Visual Basic (.Net)

반응형

        Dim xls As New Excel.Application
        Dim xWorkbook As Excel.Workbook
        Dim xWorksheet As Excel.Worksheet
        Dim xRange As Excel.Range
        Dim xlotno As String
        Dim lRow As Long = 0

        xWorkbook = xls.Workbooks.Open("E:\1234512345.xls") 'File Location
        xWorksheet = xWorkbook.Sheets(1)

        With xWorksheet
            If xls.WorksheetFunction.CountA(.Cells) <> 0 Then
                lRow = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              LookAt:=Excel.XlLookAt.xlPart, _
                              LookIn:=Excel.XlFindLookIn.xlFormulas, _
                              SearchOrder:=Excel.XlSearchOrder.xlByRows, _
                              SearchDirection:=Excel.XlSearchDirection.xlPrevious, _
                              MatchCase:=False).Row
            Else
                lRow = 1
            End If
        End With

        MessageBox.Show(lRow) 'Find Last Row


        xWorkbook.Close()
        xls.Quit()

반응형