2013-02-17 40 views
0

您能否幫我獲取vba代碼以突出顯示此列的最小值和最大值?如何使用此流程圖創建宏?

例:

 
    Col A | Col B 
    --------+--------- 
    Store 1 | 500 
    Store 2 | 400 
    Store 3 | 300 
    ========+========= 
    Total | 1200 
+4

難道你不介意與我們分享至少相關的一部分努力 - 只是爲了支持[你有什麼嘗試?](http://whathaveyoutried.com/)其實,你不需要VBA爲此 - 使用MIN&MAX進行條件格式化將會執行t他工作順利。 – 2013-02-17 07:59:17

回答

0

我完全同意,使用MIN & MAX將做的工作條件格式@Peter大號評論順利, 但是,如果你真的想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