如何在Excel VBA中将纯色应用于数据栏?

我的数据栏在执行VBA命令时看起来很好(可靠)。 但是,保存文件并重新打开后,数据栏会自动更改为渐变。 我如何避免这种情况?

保存并重新打开文件之前:

在保存文件之前

保存并重新打开文件后:

保存文件后

这是我使用的代码:

Dim DB As Databar Set DB = Range("K2:K10").FormatConditions.AddDatabar With DB .BarFillType = xlDataBarSolid .BarBorder.Type = xlDataBarBorderSolid With .BarBorder.Color .Color = 15698432 End With With .BarColor .Color = 15698432 .TintAndShade = 0 End With End With With DB.BarColor .Color = 15698432 .TintAndShade = 0 End With With Range("K2:K10").FormatConditions(1) .MinPoint.Modify newtype:=xlConditionValueAutomaticMin .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax End With 

首先,你有两次; 任何一个都足够了。

 ... With .BarColor .Color = 15698432 .TintAndShade = 0 End With End With With DB.BarColor .Color = 15698432 .TintAndShade = 0 End With ... 

另外,这是非常重要的:根据我的经验,一旦你已经定义了一个Databar – 你完成填充,它不会改变。 如果需要,可以删除Databar并重新设置:

 With Range("K2:K10") For i = .FormatConditions.Count To 1 Step -1 .FormatConditions(i).Delete Next 'Create a DataBar object ' as you've been doing it already ... End With 

希望这对你有用。

我已经创build了testing小组,放置在Sheet1 VBA代码模块中,并在Excel 2010中运行它(请参阅下面的代码片段)。 一切正常,如预期。

 Sub FormatDatabar() Dim DB As Databar Set DB = Range("K2:K10").FormatConditions.AddDatabar With DB .BarFillType = xlDataBarSolid .BarBorder.Type = xlDataBarBorderSolid With .BarBorder.Color .Color = 15698432 End With With .BarColor .Color = 15698432 .TintAndShade = 0 End With End With 'this is redundant 'With DB.BarColor '.Color = 15698432 '.TintAndShade = 0 'End With With Range("K2:K10").FormatConditions(1) .MinPoint.Modify newtype:=xlConditionValueAutomaticMin .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax End With End Sub 

它也适用于hex颜色索引:

 Sub FormatDatabar() Dim DB As Databar Set DB = Range("K2:K10").FormatConditions.AddDatabar With DB .BarFillType = xlDataBarSolid .BarBorder.Type = xlDataBarBorderSolid With .BarBorder.Color 'Green color .Color = &HC0F0& End With With .BarColor .Color = &HC0F0& .TintAndShade = 0 End With End With With Range("K2:K10").FormatConditions(1) .MinPoint.Modify newtype:=xlConditionValueAutomaticMin .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax End With End Sub 

你可能应该检查你的机器上的设置。 亲切的问候,