Type MyValue first AsLong second AsLong third AsLong End Type
Type MyColor first As Variant second As Variant third As Variant End Type
Sub 選択列において上位3つの数値に網掛けする() Dim v As MyValue v.first = 0 v.second = 0 v.third = 0
Dim c As MyColor c.first = RGB(255, 150, 150) c.second = RGB(150, 255, 150) c.third = RGB(150, 150, 255)
Dim i, j AsLong j = Selection(1).Column
If Cells(1, j).SpecialCells(xlLastCell).Row < 2Then End EndIf
Dim x As Range
For i = 1To Cells(1, j).SpecialCells(xlLastCell).Row For j = Selection(1).Column To Selection(Selection.Count).Column Set x = Cells(i, j) If IsNumeric(x) Then Call UpdateValue(v, x) EndIf Next j Next i For i = 1To Cells(1, j).SpecialCells(xlLastCell).Row For j = Selection(1).Column To Selection(Selection.Count).Column Set x = Cells(i, j) If x = v.first Then x.Interior.Color = c.first ElseIf x = v.second Then x.Interior.Color = c.second ElseIf x = v.third Then x.Interior.Color = c.third EndIf Next j Next i MsgBox "マクロを実行しました." EndSub
PrivateSub UpdateValue(ByRef v As MyValue, ByVal x As Range)
If x > v.first Then v.third = v.second v.second = v.first v.first = x ElseIf x = v.first Then 'pass ElseIf x > v.second Then v.third = v.second v.second = x ElseIf x = v.second Then 'pass ElseIf x > v.third Then v.third = x EndIf