InsertCopy Merge Cells

Hi,

I’ll try to explain my problem.
I have 2 cols A and B:

  • A1 and B1 are distinct
  • A2 and B2 are merged
  • A3 and B3 are merged
  • A4 and B4 are distinct

Each cells values are their location (A1 value is A1, A2 value is A2, …)
If I use this method

 _excelWorksheet.Columns.InsertCopy(targetIndex, 2, _excelWorksheet.Columns[0]);

This is the result:

  • A1 and B1 are distinct
  • A2 and B2 are merged
  • A3 and B3 are merged
  • A4 and B4 are distinct

All B cells values are equal to the A cells values.
How can I copy my range (col A and B) to another location with my merged cells?

Thanks

Hi,

That InsertCopy method will copy a single column multiple times. In other words, it’s copying just the column “A” two times on the targetIndex location.

Nevertheless, try using CellRange.CopyTo instead, like this:

_excelWorksheet.Columns.InsertEmpty(targetIndex, 2);
var copyRange = _excelWorksheet.Cells.GetSubrangeRelative(0, 0, 2, _excelWorksheet.Rows.Count);
copyRange.CopyTo(0, targetIndex);

Does this solve your issue?

Regards,
Mario

Thanks for your reply.

I tried to implement your code in my solution and i got this error :

Destination range can’t overlap with existing merged range.

What does it means exactly ?

Hi,

It means that the copyRange object is being copied over the cells that have merged cells or part of them.

I’m afraid that without reproducing your issue I cannot say what is wrong.
Can you send us a small repro VS project so that we can investigate it?

Regards,
Mario

Hi,

I found my problem. That’s what i have in my template

D1 E1 distinct
D2 E2 merged
D3 E3 merged
The entire row 4 is merged for another usage
D5 E5 distance

I tried to copy D1 to E5 in one instruction => Destination range can’t overlap with existing merged range.
But if i copy D1 to E3 and then copy D5 E5 it works.

Thanks for you help !