locked
Dynamically change range of FormatCondition RRS feed

  • Question

  • I have added a Format Condition on a range with some formatting using the below code.

    Microsoft.Office.Interop.Excel.FormatCondition formatConditionObj = null;
    	
        formatConditionObj = (Microsoft.Office.Interop.Excel.FormatCondition)myRange.FormatConditions.Add(Excel.XlFormatConditionType.xlExpression, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        
        formatConditionObj.Interior.ColorIndex = 5;
    


    Now my range changes dynamically.I want that using the same FormatCondition object i just change the range where this formats are applied. For e.g at the first instance it might be just "$A$1" later it might be "$A$2,$D$5" or "$A$3:$A$20". 
    Thursday, August 2, 2012 7:37 AM

Answers

  • Hi Ronak,

    Thank you for posting in the MSDN Forum

    Based on your issue, if you want to set the same FormatCondition for every range, I would suggest you use a public method packaging your code. Please refer to this

    public void SetRangeFormatCondition(Excel.Range range)
    {
        Microsoft.Office.Interop.Excel.FormatCondition formatConditionObj = null;
        formatConditionObj = (Microsoft.Office.Interop.Excel.FormatCondition).range.FormatCondition.Add(Excel.XlFormatConditionType.xlExpression, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        formatConditionObj.Interior.ColorIndex = 5;
    }

    Then, you can call this method for evey range after they have been changed.

    Hope this can help you.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Leo_Gao Friday, August 3, 2012 5:58 AM
    • Proposed as answer by Leo_Gao Monday, August 6, 2012 8:48 AM
    • Marked as answer by Leo_Gao Thursday, August 9, 2012 3:13 AM
    Friday, August 3, 2012 5:54 AM