vba为每行中最小值单元格添加背景颜色

Sub laolao()
Dim rols, role, cols, cole '根据情况修改这四个参数
rols = 2 '数据开始行
role = 20 '数据结束行数
cols = 2 '数据开始列
cole = 20 '数据结束列数
   
    Cells.Select '选中全部并清除背景
    Selection.Interior.ColorIndex = xlNone
    Range("A1").Select


    Do While (rols < role) '当行数大于最大行时退出循环
        Dim colIndex, tempValue 'colindex记录列号,tempValue记录最小值
        colIndex = cols
        tempValue = 999999999 ' Cells(rols, cols).Value为防止第一列为空值不处理的问题,所以为初始值初始化为999999999
        tempCol = colIndex 'tempCol记录最小值的列号
       
        Do While (colIndex < cole) '当列数大于最列退出循环
            If (Cells(rols, colIndex).Value < tempValue And Cells(rols, colIndex).Value <> "") Then '当前单元格的值小于前面的最小值,且不等于空
           
                tempValue = Cells(rols, colIndex).Value '当前单元格的值小于前面的最小值,就把最小值设为当前列的值
                tempCol = colIndex '记录最小值的列号
           
            End If
               colIndex = colIndex + 1
        Loop
        If (Cells(rols, tempCol).Value <> "") Then '但最小值所在的单元格等于空,不加背景色
            Cells(rols, tempCol).Select '选中最小值的单元格
            With Selection.Interior
                     .Color = 65535 '着色
            End With
        End If
       
        colIndex = cols
        Do While (colIndex < cole) '本循环为全部等于最小值的单元格着色,当列数大于最列退出循环
            If (Cells(rols, colIndex).Value = tempValue And Cells(rols, colIndex).Value <> "") Then '当前单元格的值小于前面的最小值,且不等于空
           
                Cells(rols, colIndex).Select '选中最小值的单元格
                With Selection.Interior
                     .Color = 65535
                End With
           
            End If
               colIndex = colIndex + 1
        Loop
        rols = rols + 1
    Loop

End Sub

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值