excel - How do I Count Cells in Columns of a non-rectangular range -
so have range selected user. 2 columns, not next each other. need count number of cells in each column of selected range determine if number of cells in each column equal. (if it's not need adjust range.)
for example, user may select b5:b10 , d6:d9. need code return 6 , 4 respectively.
i've tried:
set rng = selection rng.columns(1).count
this returns 1, isn't number need.
thanks in advance!
you can use areas
method of range
object areas of range. areas groups of contiguous ranges within non-contiguous range.
set rng = selection = 1 rng.areas.count debug.print rng.areas(i).cells.count next
there caveat here may need test for, , if user selects, example, a1:b10, 1 mouse drag. since contiguous range, have 1 area
, not 2 distinct numbers. if need test this, can below.
set rng = selection 'non-contiguous ranges return 1 column, if there mutiple columns both cell counts equal default if rng.columns.count = 1 = 1 rng.areas.count debug.print rng.areas(i).cells.count next end if
Comments
Post a Comment