I have a certain Range
, for example:
Range rng = activeWorksheet.Range["A1", "B50"];
I've been retrieving the maximum row and column number used in this Range
by looping through it and assigning a row number to my variable, but there must be a better way? I've been doing this:
int maxRow = 0;
foreach (Range row in rng.Rows)
maxRow = row.Row;
I had this on a previous project:
Using lastRow will give you the emptylines which in my case we not useful.
If you don't mind evaluating the string Address... by using ".EntireRow.Address(False, False)" we get a string that's easily parsed...
C#...
VB... ;
given range BB9:C11
maxRow is 11 from 9:11 ...if not parsed to int, then the Max() would be 9 due to string sorting
maxCol is "BB" from C:BB ...if not padded left, then the Max() would be "C" due to string sorting
and given range non-contiguous range: A1:A3,AE15:AE9,C4:C7
maxRow is 15 from 1:3,9:15,4:7 ...even with non-contiguous range, and referenced out of order= AE15:AE9
maxCol is "AE" from C:AE ...even with non-contiguous range
this DOES work with whole COLUMN selection "A:A"
this does NOT work with whole row selection "3:3"
I think I found the most elegant solution for this, something like this:
rng.Row
will retrieve the first used row number in the range,rng.Rows.Count - 1
will retrieve the total amount of rows used in this range and we also deduct1
to get the correct maximum row number.How about you search for the last used Row?
If you start later than row 1 you can just do some additional math on the returned row...