windowsforms-18269-controls-and-libraries-spreadsheet-examples-conditional-formatting-how-to-apply-conditional-formatting-to-a-complex-range.md
This example demonstrates how to apply a conditional formatting rule to a complex range combination of two non-contiguous ranges.
// Create a union range to which the rule will be applied.
CellRange complexRange = worksheet.Range.Union(worksheet["G3:G6"], worksheet["G9:G12"]);
ConditionalFormattingCollection conditionalFormattings = worksheet.ConditionalFormattings;
// Specify the automatic minimum value for the shortest bar.
ConditionalFormattingValue lowBound = conditionalFormattings.CreateValue(ConditionalFormattingValueType.Auto);
// Specify the automatic maximum value for the longest bar.
ConditionalFormattingValue highBound = conditionalFormattings.CreateValue(ConditionalFormattingValueType.Auto);
// Create the rule to compare yearly total values for different states.
DataBarConditionalFormatting cfRule = conditionalFormattings.AddDataBarConditionalFormatting(complexRange, lowBound, highBound, Color.FromArgb(0x29, 0x3E, 0x6A));
' Create a union range to which the rule will be applied.
Dim complexRange As CellRange = worksheet.Range.Union(worksheet("G3:G6"), worksheet("G9:G12"))
Dim conditionalFormattings As ConditionalFormattingCollection = worksheet.ConditionalFormattings
' Specify the automatic minimum value for the shortest bar.
Dim lowBound As ConditionalFormattingValue = conditionalFormattings.CreateValue(ConditionalFormattingValueType.Auto)
' Specify the automatic maximum value for the longest bar.
Dim highBound As ConditionalFormattingValue = conditionalFormattings.CreateValue(ConditionalFormattingValueType.Auto)
' Create the rule to compare yearly total values for different states.
Dim cfRule As DataBarConditionalFormatting = conditionalFormattings.AddDataBarConditionalFormatting(complexRange, lowBound, highBound, Color.FromArgb(&H29, &H3E, &H6A))
The image below shows the result. The data bars allow you to compare values in the "Yearly Total" column for different states.
See Also