打開VBE,添加入一個(或使用現有的)標準模塊,粘貼下面的這段代碼。
然后,按你說的“在一列中選定多個單元格(可以是“分散”的)”,
再按“ Alt + F8 ”,選定 FindMaxMin程序,執行它,看看效果。
Public Sub FindMaxMin()
Dim objCMax As Range
Dim objCMin As Range
Dim i&, u As Long
Dim dMax As Double
Dim dMin As Double
Dim dVal As Double
u = Selection.Cells.Count
If (u < 2&) Then
MsgBox "請選定多個單元格后執行!", 48
Exit Sub
End If
Set objCMax = Selection.Cells(1, 1)
Set objCMin = objCMax
dMax = Val(objCMax.Value)
dMin = dMax
For i = 2& To u
dVal = Val(Selection.Cells(i, 1).Value)
If (dVal > dMax) Then
dMax = dVal
Set objCMax = Selection.Cells(i, 1)
ElseIf (dVal < dMin) Then
dMin = dVal
Set objCMin = Selection.Cells(i, 1)
End If
Next
With ActiveSheet.Rows(objCMin.Row).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
End With
With ActiveSheet.Rows(objCMax.Row).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
End With
Set objCMax = Nothing
Set objCMin = Nothing
End Sub
uj5u.com熱心網友回復:
錄制宏,不是是萬能的。 有一些東西是“錄不下來的”!!!
uj5u.com熱心網友回復:
這個用條件格式即可(可以靜態/動態設定):
Option Explicit
Sub test()
'''測驗代碼
SetMaxAndMinHighLight Sheet1.Range("b1:b18")
End Sub
Sub SetMaxAndMinHighLight(Rng As Excel.Range)
''''這段代碼只需要執行一次即可
On Error Resume Next
With Rng.FormatConditions
.Delete
.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=MAX(" & Rng.Address & ")"
.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=MIN(" & Rng.Address & ")"
End With
With Rng.FormatConditions(1)
.Font.Bold = True ''''''''''粗體
.Font.Color = vbWhite ''''''白色
.Interior.Color = vbRed ''''背景色---紅色
End With
With Rng.FormatConditions(2)
.Font.Bold = True ''''''''''粗體
.Font.Color = vbWhite ''''''藍色
.Interior.Color = vbBlue '''背景色---藍色
End With
End Sub