vba为每行中最小值单元格添加背景颜色
2015-01-17 23:05
183 查看
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
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
相关文章推荐
- vba为每行中最小值单元格添加背景颜色
- PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- excel VBA对特定背景颜色的单元格进行赋值
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- EXCEL-VBA:根据单元格字体、背景颜色计数、求和
- VBA对指定单元格填充颜色并且赋值
- Qt学习笔记(四)ui界面通过样式表添加图片、背景、字体颜色等
- Gridview的RowDataBound事件(添加删除提示,改变背景颜色)
- JavaScript基础 为body节点下的第一个子节点p 添加style属性 添加背景颜色 firstChild setAttribute
- WPF DataGrid 改变单元格字体颜色或单元格背景颜色(一)
- exel 单元格数值变化,及时改变另一列显示背景颜色
- iOS cell添加点击时改变字体的颜色及背景