突出显示EXCEL选定范围内最大值

开发工具---VB编辑器---插入模块,输入:

Sub HighlightMaxValue()
Dim selectedRange As Range
Dim maxValue As Double
Dim cell As Range
Dim hasData As Boolean

' 检查是否已选择范围
If TypeName(Selection) <> "Range" Then
    MsgBox "请先选择一个数据范围!", vbExclamation
    Exit Sub
End If

Set selectedRange = Selection

' 检查范围内是否有数值数据
hasData = False
For Each cell In selectedRange
    If IsNumeric(cell.Value) And cell.Value <> "" Then
        hasData = True
        Exit For
    End If
Next cell

If Not hasData Then
    MsgBox "选定的范围内没有找到数值数据!", vbExclamation
    Exit Sub
End If

' 查找最大值
maxValue = Application.WorksheetFunction.Max(selectedRange)

' 清除原有格式
selectedRange.Interior.ColorIndex = xlNone
selectedRange.Font.Bold = False

' 突出显示最大值
For Each cell In selectedRange
    If IsNumeric(cell.Value) And cell.Value = maxValue Then
        With cell.Interior
            .Color = RGB(255, 255, 0) ' 黄色背景
            .Pattern = xlSolid
        End With
        cell.Font.Bold = True
        cell.Font.Color = RGB(255, 0, 0) ' 红色字体
    End If
Next cell

' 显示结果信息
MsgBox "已突出显示最大值: " & maxValue, vbInformation

End Sub
开发工具---宏---找到刚才添加的---选项,添加快捷键

评论已关闭