officefileapi-devexpress-dot-spreadsheet-dot-conditionalformattingcollection-dot-addaverageconditionalformatting-x28-cellrange-conditionalformattingaveragecondition-x29.md
Applies the "above or below average…" conditional formatting rule.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
AverageConditionalFormatting AddAverageConditionalFormatting(
CellRange range,
ConditionalFormattingAverageCondition condition
)
Function AddAverageConditionalFormatting(
range As CellRange,
condition As ConditionalFormattingAverageCondition
) As AverageConditionalFormatting
| Name | Type | Description |
|---|---|---|
| range | CellRange |
A CellRange object that specifies a range of cells to which the conditional formatting rule is applied.
| | condition | ConditionalFormattingAverageCondition |
One of the ConditionalFormattingAverageCondition enumeration values.
|
| Type | Description |
|---|---|
| AverageConditionalFormatting |
An AverageConditionalFormatting object that represents the specified conditional format.
|
This example demonstrates how to apply an "above or below average" conditional formatting rule to a range of cells.
To create a new conditional formatting rule represented by the AverageConditionalFormatting object, access the collection of conditional formats from the Worksheet.ConditionalFormattings property and call the ConditionalFormattingCollection.AddAverageConditionalFormatting method. Pass the following parameters:
Specify formatting options to be applied to cells if the condition is true using the ISupportsFormatting.Formatting property of the AverageConditionalFormatting object. Set the background color and specify the font attributes.
To remove the AverageConditionalFormatting object, use the ConditionalFormattingCollection.Remove, ConditionalFormattingCollection.RemoveAt or ConditionalFormattingCollection.Clear methods.
ConditionalFormattingCollection conditionalFormattings = worksheet.ConditionalFormattings;
// Create the rule highlighting values that are above the average in cells C2 through C15.
AverageConditionalFormatting cfRule1 = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range["$C$2:$C$15"], ConditionalFormattingAverageCondition.AboveOrEqual);
// Specify formatting options to be applied to cells if the condition is true.
// Set the background color to yellow.
cfRule1.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0xFA, 0xF7, 0xAA);
// Set the font color to red.
cfRule1.Formatting.Font.Color = Color.Red;
// Create the rule highlighting values that are one standard deviation below the mean in cells D2 through D15.
AverageConditionalFormatting cfRule2 = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range["$D$2:$D$15"], ConditionalFormattingAverageCondition.BelowOrEqual, 1);
// Specify formatting options to be applied to cells if the conditions is true.
// Set the background color to light-green.
cfRule2.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0x9F, 0xFB, 0x69);
// Set the font color to blue-violet.
cfRule2.Formatting.Font.Color = Color.BlueViolet;
Dim conditionalFormattings As ConditionalFormattingCollection = worksheet.ConditionalFormattings
' Create the rule highlighting values that are above the average in cells C2 through C15.
Dim cfRule1 As AverageConditionalFormatting = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range("$C$2:$C$15"), ConditionalFormattingAverageCondition.AboveOrEqual)
' Specify formatting options to be applied to cells if the condition is true.
' Set the background color to yellow.
cfRule1.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &HFA, &HF7, &HAA)
' Set the font color to red.
cfRule1.Formatting.Font.Color = Color.Red
' Create the rule highlighting values that are one standard deviation below the mean in cells D2 through D15.
Dim cfRule2 As AverageConditionalFormatting = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range("$D$2:$D$15"), ConditionalFormattingAverageCondition.BelowOrEqual, 1)
' Specify formatting options to be applied to cells if the conditions is true.
' Set the background color to light-green.
cfRule2.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &H9F, &HFB, &H69)
' Set the font color to blue-violet.
cfRule2.Formatting.Font.Color = Color.BlueViolet
The following code snippets (auto-collected from DevExpress Examples) contain references to the AddAverageConditionalFormatting(CellRange, ConditionalFormattingAverageCondition) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
// Create the rule highlighting values that are above the average in cells C2 through C15.
AverageConditionalFormatting cfRule1 = conditionalFormattings.AddAverageConditionalFormatting(worksheet["$C$2:$C$15"], ConditionalFormattingAverageCondition.AboveOrEqual);
// Specify formatting options to be applied to cells if the condition is true.
// Create the rule highlighting values that are above the average in cells C2 through C15.
AverageConditionalFormatting cfRule1 = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range["$C$2:$C$15"], ConditionalFormattingAverageCondition.AboveOrEqual);
// Specify formatting options to be applied to cells if the condition is true.
// Create the rule highlighting values that are above the average in cells C2 through C15.
AverageConditionalFormatting cfRule1 = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range["$C$2:$C$15"], ConditionalFormattingAverageCondition.AboveOrEqual);
// Specify formatting options to be applied to cells if the condition is true.
' Create the rule highlighting values that are above the average in cells C2 through C15.
Dim cfRule1 As AverageConditionalFormatting = conditionalFormattings.AddAverageConditionalFormatting(worksheet("$C$2:$C$15"), ConditionalFormattingAverageCondition.AboveOrEqual)
' Specify formatting options to be applied to cells if the condition is true.
' Create the rule highlighting values that are above the average in cells C2 through C15.
Dim cfRule1 As AverageConditionalFormatting = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range("$C$2:$C$15"), ConditionalFormattingAverageCondition.AboveOrEqual)
' Specify formatting options to be applied to cells if the condition is true.
' Create the rule highlighting values that are above the average in cells C2 through C15.
Dim cfRule1 As AverageConditionalFormatting = conditionalFormattings.AddAverageConditionalFormatting(worksheet.Range("$C$2:$C$15"), ConditionalFormattingAverageCondition.AboveOrEqual)
' Specify formatting options to be applied to cells if the condition is true.
See Also
ConditionalFormattingCollection Interface