java excel 颜色_EXCEL单元格颜色

日常记录一般EXCEL进行比较时,根据差异进行单元格颜色标

'进行数据比较分析

'nRow:开始行数

'nClm:比较数列

'nMonth:循环月份YTD

Sub Analysis_WS_by_Color(ByVal Sht$, ByVal iRow, ByVal nClm, ByVal nMonth)

Dim iMaxRow, iMaxClm

Dim m, n

Dim baseVal As Double

Dim targetVal As Double

Dim diff As Double

Dim ColorRange

With Sheets(Sht)

iMaxRow = .Cells(65536, 1).End(xlUp).Row

iMaxClm = 2 + nMonth

For m = iRow To iMaxRow

baseVal = .Cells(m, nClm) '比较基数

For n = 3 To iMaxClm

targetVal = Round(.Cells(m, n), 0) '目标数

If targetVal = 0 Then diff = 0 Else diff = Round(targetVal - baseVal, 0)

ColorRange = Round(Abs(targetVal / baseVal - 1), 2)

If ColorRange > 1 Then ColorRange = 1

'处理单元格颜色

Select Case diff

Case Is > 0

.Cells(m, n).Interior.ColorIndex = 6 'Yellow

.Cells(m, n).Interior.TintAndShade = 1 - ColorRange

Case Is = 0

.Cells(m, n).Interior.ColorIndex = 0

Case Is < 0

.Cells(m, n).Interior.ColorIndex = 14 'Green

.Cells(m, n).Interior.TintAndShade = 1 - ColorRange

End Select

Next n

Next m

End With

End Sub

效果如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值