OK, I am finishing up an add-on project for a legacy Excel-VBA application, and I have once again run up against the conundrum of the mysterious range.Rows
(?) and
Range.Rows
and Range.Columns
return essentially the same Range except for the fact that the new Range has a flag which indicates that it represents Rows or Columns. This is necessary for some Excel properties such as Range.Count and Range.Hidden and for some methods such as Range.AutoFit()
:
Range.Rows.Count
returns the number of rows in Range.Range.Columns.Count
returns the number of columns in Range.Range.Rows.AutoFit()
autofits the rows in Range.Range.Columns.AutoFit()
autofits the columns in Range.You might find that Range.EntireRow
and Range.EntireColumn
are useful, although they still are not exactly what you are looking for. They return all possible columns for EntireRow
and all possible rows for EntireColumn
for the represented range.
I know this because SpreadsheetGear for .NET comes with .NET APIs which are very similar to Excel's APIs. The SpreadsheetGear API comes with several strongly typed overloads to the IRange indexer including the one you probably wish Excel had:
IRange this[int row1, int column1, int row2, int column2];
Disclaimer: I own SpreadsheetGear LLC