首页 > 解决方案 > 尝试查看单元格值是否 > 10% 时代码未运行

问题描述

我的代码运行良好,我没有收到错误,但它似乎没有突出显示该列。

Sub highlight()
Dim Data As Range
Dim cell As Range
Dim currentsheet As Worksheet
Set currentsheet = ActiveWorkbook.Sheets("Watchlist")
Set Data = currentsheet.Range("B2:B50")
For Each cell In Data
If cell.Value > "10%" Then
   cell.Interior.ColorIndex = 5
End If
Next
End Sub

对于要突出显示值 >10% 的任何单元格。

标签: excelvba

解决方案


改变:

If cell.Value > "10%" Then
   cell.Interior.ColorIndex = 5
End If

进入:

If cell.Value > 0.1 Then
   cell.Interior.ColorIndex = 5
End If

像这样的比较是通过数学完成的,像你一样写“10%”是一个字符串而不是一个数字,这意味着你可以写得很好:“If cell.Value > "ThisFunWord" Then" <--- this显然毫无意义!


推荐阅读