首页 > 解决方案 > 如果 Cell.Value >(大于)函数无法为单元格着色

问题描述

我当前使用的代码。

Dim iRowCq As Range
Set iRowCq = Range("B2:DJ26")

For Each Cell In iRowCq

    If Cell.Value > "0.3" or cell.value > 0.3 Then
        Cell.Interior.Color = RGB(0, 255, 0)
    End If

Next

我想为大于 0.3 绿色的单元格着色。然而,虽然 99% 的单元格有效,但有时有些单元格大于 0.3 并且没有着色。我什至尝试将数据四舍五入到小数点后两位,但在此之前不起作用。有人可以帮忙吗?

excel表格截图

标签: excelvba

解决方案


您不能像这样组合字符串和数字比较来进行大于/小于比较。字符串可能不会大于"0.3"字符串,但它的排序总是高于0.3数字。` 组合字符串和数字比较可能在严格等于的基础上工作。

取一个字符串或一个混合数字的数值并将其用于比较。

Dim iRowCq As Range Set iRowCq = Range("B2:DJ26")

For Each Cell In iRowCq

    If val(Cell.Value) > 0.3 Then
         Cell.Interior.Color = RGB(0, 255, 0)
    End If

Next

推荐阅读