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
I've found this works:
Rows(CStr(iVar1) & ":" & CStr(iVar2)).Select
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
It's perhaps a bit of a kludge, but the following code does what you seem to want to do:
Set rng = wks.Range(wks.Rows(iStartRow), wks.Rows(iEndRow)).Rows