Format Top 3 and Bottom 3 Values for each row

纵然是瞬间 提交于 2019-12-12 01:22:19

问题


I think I may need a VBA macro to solve this. I have a data set of approx 10,000 rows, with 15 columns of values, and what I want to do is, for each row, highlight by conditional formatting the top three and bottom three values.

I have set the rule up by using the conditional formatting tool within xl2010 for row 1, but when I copy-paste special-formats over the remaining 9,999 rows, the result is to format only the top three and bottom three values that are contained in the 9,999 rows.

I want to see this shading the top 3 and bottom 3 for each row, and not the whole data set, ideally without copying paste special 9,999 times!


回答1:


The macro record feature is good for these types of issues, especially for beginners (I'm not all that savvy in VBA myself).

This will highlight the top 3 values red, and the bottom 3 blue. Note I have i from 1 to 1000, change as needed (same for column section).

EDIT: Changed for your ranges, I didn't read them the first time.

Sub Conditions()
Dim myrange As Range


For i = 1 To 10000

Set myrange = Range("A" & i & ":" & "O" & i)
myrange.FormatConditions.AddTop10
myrange.FormatConditions(myrange.FormatConditions.Count).SetFirstPriority

With myrange.FormatConditions(1)
    .TopBottom = xlTop10Top
    .Rank = 3
    .Percent = False
End With
With myrange.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
End With

myrange.FormatConditions(1).StopIfTrue = False
myrange.FormatConditions.AddTop10
myrange.FormatConditions(myrange.FormatConditions.Count).SetFirstPriority

With myrange.FormatConditions(1)
    .TopBottom = xlTop10Bottom
    .Rank = 3
    .Percent = False
End With
With myrange.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 15773696
    .TintAndShade = 0
End With

myrange.FormatConditions(1).StopIfTrue = False

Next

End Sub


来源:https://stackoverflow.com/questions/18750053/format-top-3-and-bottom-3-values-for-each-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!