2014-12-22 57 views
2

當執行VBA命令時,我的數據欄看起來很好(可靠)。但是,保存文件並重新打開後,數據欄會自動更改爲漸變。我如何避免這種情況?如何在Excel VBA中將純色應用於數據欄?

之前保存和重新打開文件:

Before saving the file

保存和重新打開文件後:

After saving the file

這是我使用的代碼:

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 
+0

如果vba代碼成功運行,我不認爲這是VBA的問題。我建議編輯添加'excel'和'bar-chart'標籤,以查看是否可以從更廣泛的excel用戶羣中獲得更多幫助。但是,它似乎並不是一個VBA問題。 – Chrismas007

回答

1

我創建測試小組,放置在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 

它還與十六進制顏色指標運行良好:

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 

你或許應該檢查你的機器上的設置。親切的問候,

2

首先,你有這兩次;任何一個都可以。

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

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

而且,這是非常重要的:在我的經驗,一旦你difined數據條 - 你用填充完成的,它不會改變。如果需要,您可以刪除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 

希望這適用於您。

相關問題