Background: I need to color an excele cell in red or green color. If cell is more zero that I need to color the cell in green (in right way from the middle of the cell), if the cell is less zero I need to color the cell in red (in left way from the middle of the cell).
I use "Microsoft.Office.Interop.Excel" library.
How shall I do this?
PS Cell color changing In Excel using C# is not a duplicate, because of i want to color only half of an excel cell, not full.
This (a) may be cheating (b) perhaps better as a Comment (but then there would be no image) and (c) possibly stretching the significance of the [excel] tag here, but may be of some interest to mention that CF can achieve something of the sort:
ColumnB (red Fill) being formatted with a formula rule of:
=$B1<0
and ColumnC (green Fill) with a formula rule of:
=$B1>0
The cheating part is that B:C have been reduced in width and formatted Center Across Selection.
Something very vaguely similar with Sparklines:
In a Comment (with link to an image) @BrakNicku has pointed out that Data Bars could be applied (and the image rather proves that it is possible to half fill an Excel cell with colour). A variation, also Data Bars, is to have the length proportional to the underlying value:
To solve this problem, I used the given scheme:
The given macro:
Sub CreateGistograms(r As String)
Range(r).Select
Selection.FormatConditions.AddDatabar
Selection.FormatConditions(Selection.FormatConditions.Count).ShowValue = True
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueAutomaticMin
.MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
End With
With Selection.FormatConditions(1).BarColor
.Color = 8700771
.TintAndShade = 0
End With
Selection.FormatConditions(1).BarFillType = xlDataBarFillGradient
Selection.FormatConditions(1).Direction = xlContext
Selection.FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor
Selection.FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid
Selection.FormatConditions(1).NegativeBarFormat.BorderColorType = _
xlDataBarColor
With Selection.FormatConditions(1).BarBorder.Color
.Color = 8700771
.TintAndShade = 0
End With
Selection.FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic
With Selection.FormatConditions(1).AxisColor
.Color = 0
.TintAndShade = 0
End With
With Selection.FormatConditions(1).NegativeBarFormat.Color
.Color = 255
.TintAndShade = 0
End With
With Selection.FormatConditions(1).NegativeBarFormat.BorderColor
.Color = 255
.TintAndShade = 0
End With
End Sub
How to save macro and run him from C# create macro at runtime in dotnet
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.