2013-01-17 31 views
2

我試圖在844個不同的行上使用條件格式(綠色 - 黃色 - 紅色色標)來跟蹤過去六年(年爲列)的保費量。這是每個卷列之間棘手的部分是項目數量。我想格式化每行以獲得額外的音量,並保持項目數量不變。Excel 2010有條件格式化單個行

在這一點上,我選擇每個單獨的高級卷單元格,方法是按住Ctrl並選擇條件格式。

我試圖自動化這個,所以我不必爲844行和未來的電子表格繼續這個過程。

我附上了工作表的圖片供您參考。

任何幫助非常感謝!

感謝,

布拉德

enter image description here

回答

1

我通過運行宏錄製得到了條件格式的一些基本代碼。我更換了選擇的所有出現一個rng變量,並設置rng變量作爲參數傳遞給子程序,因此子可以在循環中調用:

Sub SetRangeCF(rng As Excel.Range) 

rng.FormatConditions.AddColorScale ColorScaleType:=3 
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority 
rng.FormatConditions(1).ColorScaleCriteria(1).Type = _ 
xlConditionValueLowestValue 
With rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor 
    .Color = 8109667 
    .TintAndShade = 0 
End With 
rng.FormatConditions(1).ColorScaleCriteria(2).Type = _ 
xlConditionValuePercentile 
rng.FormatConditions(1).ColorScaleCriteria(2).Value = 50 
With rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor 
    .Color = 8711167 
    .TintAndShade = 0 
End With 
rng.FormatConditions(1).ColorScaleCriteria(3).Type = _ 
xlConditionValueHighestValue 
With rng.FormatConditions(1).ColorScaleCriteria(3).FormatColor 
    .Color = 7039480 
    .TintAndShade = 0 
End With 
End Sub 

然後你在一個循環撥打以上子,在這種情況下,對於在列A中具有值的任何行,一次。這假設條件格式化從第2行開始,並且在列A中具有不間斷數據。如果不是,則必須調整該循環代碼:

Sub SetEachRow() 
Dim ws As Excel.Worksheet 
Dim LastRow As Long 
Dim cell As Excel.Range 

Set ws = ActiveSheet 'change as necessary 
With ws 
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row 
    For Each cell In .Range("A1:A" & LastRow)http://stackoverflow.com/questions/10245638/excel-changes-conditional-formatting-formula?rq=1 
     cell.EntireRow.FormatConditions.Delete 
     SetRangeCF cell.EntireRow 
    Next cell 
End With 
End Sub 

我不知道行的限制是什麼,這將工作,但1,000對我來說工作得很好。