This project has moved and is read-only. For the latest updates, please go here.

How to determine if two cells are merged together

May 14, 2014 at 2:18 PM

I'm trying to do some work with ranged cells and I'd like to know if there's a way to know which cells are merged together.

May 14, 2014 at 4:15 PM
Pick up the latest source code (just fixed a minor issue).

You're looking for:

range.IsMerged - Tells you is any cell in the range is part of a merge.
worksheet.MergedRanges - Tells you the specific ranges that are merged.

            var wb = new XLWorkbook();
            var ws = wb.AddWorksheet("Sheet1");


            Console.WriteLine(ws.Range("A1:A2").IsMerged()); // True
            Console.WriteLine(ws.Range("A2:A3").IsMerged()); // True
            Console.WriteLine(ws.Range("A1:A4").IsMerged()); // True
            Console.WriteLine(ws.Range("A4:A5").IsMerged()); // False

            Console.WriteLine(ws.MergedRanges.Contains(ws.Range("A2:A3"))); // True
            Console.WriteLine(ws.MergedRanges.Contains(ws.Range("A2:A4"))); // False
Marked as answer by jpmnteiro on 5/16/2014 at 3:46 AM
May 16, 2014 at 11:46 AM
Edited May 20, 2014 at 4:35 PM
Thanks, that did the trick. Nice job by the way, really enjoying it.

Jun 29, 2014 at 12:41 PM
Thanks! Your ClosedXML is great! And this fix is in proper time for me. You saved me from much pain.