Merged VSTO cells

Is there a way in VSTO (/ JET OLEDB or other excel file reading methods) to find out if data is coming from a single cell or a merged range of cells and getting that range?

+2


source to share


2 answers


The shortest way here is to use the Boolean property Range.MergeCells

.

Assuming your cell reference has been named myCell

, you can use something like:

if (myCell.MergeCells)
{
    // The 'myCell' is part of a merged cell area.
}
Else
{
   // The 'myCell' is not part of any merged cell area.
} 

      

You can also check Cells.Count

in the range returned by the property Range.MergeArea

:



if (myCell.MergeArea.Cells.Count > 1) {...} 

      

or

if (myCell.MergeArea.Count > 1) {...}

      

The last example works because the Range.Count property always returns the same value as Range.Cells.Count by design.

0


source


Assuming you are using a method that can invoke and use the Excel Object Model, you check the cell's MergeArea property to see if it contains anything other than that cell. If so, then this cell is part of the MergeArea. This is how I did it in VBA:

IF CurrCell.MergeArea.Rows.Count > 1 Or CurrCell.MergeArea.Columns.Count > 1 Then
    'CurrCell is part of a MergeArea... '

      



The equivalent C # VSTO code should be fairly similar.

0


source







All Articles