In VB.NET I want to get the used rows so I wrote that:
Dim objWorksheet As Excel.Worksheet = workbook.Sheets(2)
Dim lastRow = objWorksheet.UsedRange.Rows.Coun
Try to avoid UsedRange
. It can be misleading. For instance, if you fill range A1:B5
and clear the contents of column B
, then UsedRange.Columns.Count
will return 2
- because Excel remembers cells with formatting and includes them into UsedRange.
UPDATE
To get real last column and row, use following code:
lRealLastRow = _
Cells.Find("*", Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
lRealLastColumn = _
Cells.Find("*", Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
UPDATE 2