locked
Clearing Cells RRS feed

  • Question

  • I need to clear 4 cells, I know I can do it like this, but is there a more efficient way of doing such?

    Excel.Range xlRange1;
    Excel.Range xlRange2;
    Excel.Range xlRange3;
    Excel.Range xlRange4;
    
    xlRange1 = oWS.get_Range("B3", "B3");
    xlRange1.Clear();
    xlRange2 = oWS.get_Range("D4", "D4");
    xlRange2.Clear();
    xlRange3 = oWS.get_Range("L15", "L15");
    xlRange3.Clear();
    xlRange4 = oWS.get_Rage("Z42", "Z42");
    xlRange4.Clear();

    Thursday, April 24, 2014 5:59 PM

Answers

  • Hi,

    Please try the code below:

    Excel.Range xlRange;
    xlRange = oWS.Range["B3,D4,L15,Z42"];
    xlRange.Clear();



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 25, 2014 7:18 AM
  • Hi,

    To clear the merged Cells, we could access to the merged Cell with MergeArea property of Range Object. Here is a sample for your reference. Range.MergeCells Property could be used to determine whether the range contains merged cells.

    Excel.Range xlRange;
    xlRange = oWS.Range["B3,D4,L15,Z42"];
    foreach (Excel.Range rng in xlRange)
    {
        if (rng.MergeCells)
        {
            rng.MergeArea.Clear();
        }
        else
        {
            rng.Clear();
        }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 28, 2014 3:28 AM

All replies

  • Hi,

    Please try the code below:

    Excel.Range xlRange;
    xlRange = oWS.Range["B3,D4,L15,Z42"];
    xlRange.Clear();



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 25, 2014 7:18 AM
  • Hi,

    Please try the code below:

    Excel.Range xlRange;
    xlRange = oWS.Range["B3,D4,L15,Z42"];
    xlRange.Clear();



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    I didn't realize this would be an issue, but one of the cells is a merged cell so I get an error of Unable to change part of a merged cell.  Is there a way to clear a merged cell in C# VSTO?
    Friday, April 25, 2014 1:01 PM
  • Hi,

    To clear the merged Cells, we could access to the merged Cell with MergeArea property of Range Object. Here is a sample for your reference. Range.MergeCells Property could be used to determine whether the range contains merged cells.

    Excel.Range xlRange;
    xlRange = oWS.Range["B3,D4,L15,Z42"];
    foreach (Excel.Range rng in xlRange)
    {
        if (rng.MergeCells)
        {
            rng.MergeArea.Clear();
        }
        else
        {
            rng.Clear();
        }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 28, 2014 3:28 AM