Question

I am trying to use VSTO to clear the contents of a merged cell. I get an error of 'Can Not Change Merged Cell'. Is there a way to clear the contents of a merged cell in VSTO? This is the code I am using

wbExcel = oXL.ActiveWorkbook;
Worksheet ws = (Worksheet)wbExcel.ActiveSheet;
sheetExcel.Range["A4, A43, C10, F4"].Clear();
Was it helpful?

Solution

If cells aren't part of a merged area, you can use the Range object like you did, otherwise clear the areas one-by-one:

sheetExcel.Range["A4"].MergeArea.Clear();
sheetExcel.Range["A43"].MergeArea.Clear();
sheetExcel.Range["C10"].MergeArea.Clear();
sheetExcel.Range["F4"].MergeArea.Clear();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top