0

您能帮我获取 vba 代码以突出显示该列的最小值和最大值吗?

前任:

   上校 | B栏
   --------+---------
   商店 1 | 500
   商店 2 | 400
   店铺 3 | 300
   ========+==========
   总计 | 1200
4

1 回答 1

0

我完全同意@Peter L 的评论,即使用 MIN 和 MAX 的条件格式可以顺利完成这项工作,但如果你真的想要 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
于 2013-02-17T17:05:58.403 回答