In Excel’s COM API:
Given an ExcelRange object, how would I determine which rows and columns are contained within it?
I do not want the contents of the range, just the “coordinates” of the range, preferably as integers.
I did notice that ExcelRange has both a Row and a Column property, however these only indicate the row and column of the upper left corner of the range.
Note: I am using Delphi, however this question could be relevant to any language using Excel though COM, so answering using Delphi is not necessary.
Assuming a simple rectangular range then you use the
RowsandColumnsproperties of theExcelRangeobject. The top-left of the selection is determined byRange.RowandRange.Column. The number of selected rows and columns is given byRange.Rows.CountandRange.Columns.Count.In complete generality an Excel range can be made up of multiple non-contiguous areas. In this case you use the
Areasproperty ofExcelRangeto iterate through the simple rectangular ranges that make up the complex range.To illustrate consider the following code:
I have not actually tested this code so I hope I have remembered correctly that indexing is 1-based.