2016-12-30 78 views
0

我正在嘗試編寫一個VBA腳本,它爲範圍添加了databar條件格式。我已經寫了下面的代碼,但是,我不知道如何更改databar的顏色,並提供了不同的界限。當我將minpoint設置爲0時,它可以工作,但我無法將maxpoint設置爲指定變量(設置爲鍵入integer)。我哪裏錯了?格式化數據欄對象

Set bar = rangeTest.FormatConditions.AddDatabar 

'assign max value + 1 for databar upper bound 
maxValue = Application.WorksheetFunction.Max(rangeTest) + 1 

With bar 
    .BarFillType = xlDataBarFillSolid 
    .BarColor = RGB(189, 215, 238) 'error thrown here (Object doesn't support this property) 
End With 

With rangeTest.FormatConditions(1) 
    .MinPoint.Modify newtype:=0 'xlConditionValueAutomaticMin 
    .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax 'error thrown here (invalid procedure call) 
End With 

回答

1

必須使用:

  • Color財產BarColor屬性返回FormatColor對象

  • xlConditionValueNumberConditionValue對象的Modify()方法newvalue論據MaxPoint財產

  • 返回

因此您的代碼將是:

With bar 
    .BarFillType = xlDataBarFillSolid 
    .BarColor.Color = RGB(189, 215, 238) 
End With 

With rangeTest.FormatConditions(1) 
    .MinPoint.Modify newtype:=0 'xlConditionValueAutomaticMin 
    .MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=maxValue  
End With