この列の最小値と最大値を強調表示する vba コードを取得するのを手伝ってもらえますか?
元:
コルA | B列 ------+--------- ストア 1 | 500 ストア 2 | 400 ストア 3 | 300 ========+========= 合計 | 1200
この列の最小値と最大値を強調表示する vba コードを取得するのを手伝ってもらえますか?
元:
コルA | B列 ------+--------- ストア 1 | 500 ストア 2 | 400 ストア 3 | 300 ========+========= 合計 | 1200
MINとMAXを使用した条件付き書式設定がスムーズに機能するという@Peter Lのコメントに完全に同意しますが、VBAコードが本当に必要な場合は、このアプローチで機能します
Sub HighlightMinMax()
Const column As Integer = 2
Dim row As Integer, minRow As Integer, maxRow As Integer
Dim minValue, maxValue
' initialize variables
row = 1
minRow = 1
maxRow = 1
minValue = Cells(row, column)
maxValue = Cells(row, column)
' loop until found "Total" in the first column
While Cells(row, 1) <> "Total"
Debug.Print Cells(row, 1) ' inspect the label
If (Cells(row, column) < minValue) Then
minRow = row
minValue = Cells(row, column)
End If
If (Cells(row, column) > maxValue) Then
maxRow = row
maxValue = Cells(row, column)
End If
row = row + 1
Wend
' select the cell with the min value and highlight it ...changing the background color to green
Cells(minRow, column).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
' select the cell with the max value and highlight it ...changing the background color to red
Cells(minRow, column).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Cells(maxRow, column).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub