title: how to detect merged cells in c# using MS interop excel
body: <p>I want to detect merged cells either in a row/entire sheet(preferable).Here is my code     </p>
  
  <pre><code>Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application(); 
  Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(source);
  //Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[sheetNumber];
  Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[objInMemory._sheetName];
  xl.ScreenUpdating = false;
  ws.Columns.ClearFormats();
  ws.Rows.ClearFormats();
  int colCount = ws.UsedRange.Columns.Count;
  int rowCount = ws.UsedRange.Rows.Count;
  int strtRow = ws.UsedRange.Rows[1].Row;
  int strtCol = ws.UsedRange.Columns[1].Column;
  
  
   Microsoft.Office.Interop.Excel.Range objRange = null;
  </code></pre>
  
  <p>Neither this piece of code</p>
  
  <pre><code>if (ws.Cells.MergeCells)
  {
  
  }
  </code></pre>
  
  <p>Nor this piece of code(only for row1)</p>
  
  <pre><code>for (int j = strtCol; j &lt; strtCol + colCount; j++)
  {
      objRange = ws.Cells[strtRow, j];
  
      if (ws.Cells[strtRow, j].MergeCells)
      {
          message = "The Sheet Contains Merged Cells";
          break;
      }  
  }
  </code></pre>
  
  <p>seem to work..Kindly let me know how to check if a sheet/specific range contains merged cells.</p>