why i use Worksheet.UsedRange.Rows.Count got wrong result

让人想犯罪 __ 提交于 2019-12-17 20:34:38

问题


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.Count

But I don't know why the lastRow is number little than actual used rows, I searched the stackoverflow and someone suggest write like this:

Dim range As Excel.Range = objWorkSheet.UsedRange
Dim lastRow = range.Rows.Count

I tried and also didn't work, the num returns little than actual used rows is no rules, so I don't know how to deal with it, any idea is helpful, thanks very much

EDIT 1 the final answer is image:

With the help of answers i searched this question it is overall understanding the last used row and give the final answer

EDIT 2 Make a summery

The code UsedRange.Rows.Count means from the first rows that has data to the last rows that has data, which means you can have empty rows between the first non-empty rows to the last non-empty rows which not affect the result, but if the first row is empty, the result will be one less the actual non-empty row, and if the first two row is empty the result will be two less, so the link question said never use UsedRange.Rows.Count to get the last row


回答1:


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




回答2:


Sometimes it only appears too small. Say we have:

and we run:

Sub aRowsByAnyOtherName1()
    Dim N As Long
    N = ActiveSheet.UsedRange.Rows.Count
    MsgBox N
End Sub

We see:

The reason we get 3 rather than 4 is that the top row of the worksheet is not in UsedRange!

EDIT#1:

If the "top" of the worksheet needs to be included then use:

Sub aRowsByAnyOtherName2()
    Dim N As Long, rng As Range
    Set rng = ActiveSheet.UsedRange
    N = rng.Rows.Count + rng(1).Row - 1
    MsgBox N
End Sub



回答3:


This is a bit of a punt on what you mean by "used" rows. If you have any blank rows at the start of the sheet you will get the wrong last row number but you will get the used range row count.

Try the following with row 1 blank.

Option Explicit

Sub test()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    Debug.Print rng.Rows.Count                   '2
    Debug.Print ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row '3

End Sub

This yields 2 from UsedRange.Rows.Count and 3 from using xlCellTypeLastCell with data as below:




回答4:


I'm using Excel 2013, and as far as I can see the UsedRange and UsedRange.Count Properties work correctly

In previous versions I can remember noticing that they were unreliable, and I think this may be the reasons of some older posts, eg stackoverflow.com/questions/11169445...

Note that the UsedRange is a Single Rectangular Area bounded by the Top-, Right-, Bottom-, and Left-most Non-Blank Cells, so that the Last Used Row is thus

ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1  

Note also that UsedRange INCLUDES all Cells that have Any Content, for example a Border, Interior Coloring or a Comment, not just those with a Value or a Formula

Depending on what you are trying to achieve, using the SpecialCells and Find Methods may be preferable; for example the Last Used Row can also be found using

ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row


来源:https://stackoverflow.com/questions/50434444/why-i-use-worksheet-usedrange-rows-count-got-wrong-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!