Excel VBA row and column statistics

From: https://www.cnblogs.com/acetaohai123/p/6505447.html

VBA get the number of rows and columns in EXCEL table

Please be careful not to use keywords such as Columus as variables, such as "Columus = ActiveSheet.UsedRange.Columns.Count"!

Children's shoes for beginner EXCEL macros always want to know the number of rows and columns in the table, especially when the number of rows and columns is uncertain. This can avoid many mistakes and improve efficiency. But every time I use it to search the Internet, I always give a lot of useless answers, and I often can't find the results I want. Every time I use it, temporary search is always a headache. I stumbled upon a blog, which recorded different methods in detail. The author tested several findings and it was really useful. In the spirit of long live sharing, share the blog content. I hope to be helpful.

Source: http://www.okexcel.com.cn/bbs/viewthread.php?tid=26

Note: In each method, the top is the number of Excel rows, and the bottom is the number of Excel columns.

method 1:

  ActiveSheet.UsedRange.Rows.Count

  ActiveSheet.UsedRange.Columns.Count

Disadvantages: Sometimes it may be larger than the actual number. The reason is that if you clear the last few rows (columns) of data (not the entire row or the entire column is deleted), this command still returns the value before it is not cleared. That is to say, although it is empty now, but you have used it before is counted as yours.

 

Method 2:

  ActiveSheet.Range("A65535").End(xlUp).Row

  ActiveSheet.Range("IV1").End(xlToLeft).Column

  Can be abbreviated as:

  ActiveSheet.[A65536].End(xlUp).Row

  ActiveSheet.[IV1].End(xlToLeft).Column

Disadvantages: You can only calculate the number of rows (columns) where the last cell in a column (row) is located. In this example, only the number of rows occupied by the last cell in column A is returned.

 

Method 3:

  ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row

  ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column

  Disadvantages: When the worksheet is deleted or cleared, it will become larger than the actual situation.

 

Method 4:

  ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

  ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column

  Disadvantages: When the worksheet is deleted or cleared, it will become larger than the actual situation.

 

Method 5:

  Application.CountA(ActiveSheet.Range("A:A"))

  Application.CountA(ActiveSheet.Range("1:1"))

  Only the actual usage of one column (row) can be counted, and the position of the last row (column) is not necessarily obtained. If the value of method 2 is larger than this method, it means that there is a blank unfilled cell between the data in column A.

 

Method 6:

  ActiveSheet.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

  ActiveSheet.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

  Same effect as method 2

 

Among the above methods, method 1 and method 2 are commonly used.

 

 

 

Published 10 original articles · Like 11 · Visits 20,000+

Guess you like

Origin blog.csdn.net/u013323018/article/details/83869552