VBA --- check boundary cases, using end (xlup) only a few cases easy to use

 

A nice situations - as large as possible can only try down the right position pegging back. "

Other good use is mainly due, range () will affect the border connecting end () judgment

end () will detect the boundary region is not empty / empty area

 

Sub test121()

'某列
Debug.Print "C列的上下限"
'查一列的上限比较少
Debug.Print Range("c1").End(xlEnd).Row  '这样不行,只会查到空区域
'查一列的下限是需求比较多
Debug.Print Range("c65536").End(xlUp).Row



'某单元格
Debug.Print "查某单元格的边界方法1"
Debug.Print Cells(16, 3).Row
Debug.Print Cells(16, 3).Column


Debug.Print "查某单元格的边界方法2--不好用"
Debug.Print Cells(16, 3).End(xlUp).Row
Debug.Print Cells(16, 3).End(xlDown).Row
Debug.Print Cells(16, 3).End(xlToLeft).Column
Debug.Print Cells(16, 3).End(xlToRight).Column




''某区域,一般也没查询区域边界的需要把?因为区域是已知的
Debug.Print "查某一区域的边界--不好用"
Debug.Print Range("c16:d18").End(xlUp).Row
Debug.Print Range("c16:d18").End(xlDown).Row
Debug.Print Range("c16:d18").End(xlToLeft).Column
Debug.Print Range("c16:d18").End(xlToRight).Column

Debug.Print "好用的几种情况--只能试往下往右尽量大的位置反查回来"
Debug.Print Range("c65536").End(xlUp).Row
Debug.Print Cells(5, 999).End(xlToLeft).Column



End Sub

 

Published 383 original articles · won praise 45 · Views 100,000 +

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/104060602