我正在研究宏的增强功能之一,在这里我必须更改 基于值的单元格字体颜色: 如果大于5,则为“红色” 如果小于1,则为“琥珀色”
单元格包含以下值:
CB
0.30%
3.00%
3.00%
2.00%
2.00%
2.00%
3.00%
0.00%
0.00%
6.00%
0.00%
70.00%
70.00%
45.00%
代码:
Dim My_Range As Range
Set My_Range = ThisWorkbook.Sheets("Schemes").Range("CB15:CB100")
Dim BValue As String
'Dim AValue_1 As String
'Dim AValue_2 As Integer
'Dim AValue_3 As Integer
For Each Cell In My_Range
BValue = Cell.Value
'AValue_1 = CStr(Left(BValue, Len(BValue) - 1))
'AValue_2 = InStr(1, AValue_1, ".")
'AValue_3 = Left(AValue_1, AValue_2 - 1)
'AValue_3 = AValue_2(0)
If BValue > 5 Then
Cell.Font.ColorIndex = 0
Cell.Font.Bold = True
Else
Cell.Font.ColorIndex = xlNone
End If
If BValue < 1 Then
Cell.Font.ColorIndex = 44
Cell.Font.Bold = True
Else
Cell.Font.ColorIndex = xlNone
End If
Next
由于单元格包含百分比值,因此无法获得适当的结果。请您指导我。
答案 0 :(得分:0)
百分比值小于1:5%= 0.05和100%= 1-因此请尝试这样
Dim My_Range As Range
Set My_Range = ThisWorkbook.Sheets("Schemes").Range("CB15:CB100")
For Each Cell In My_Range
Select Case Cell.Value
Case Is > 0.05
Cell.Font.ColorIndex = 0 ' should be RED, but 0 is BLACK
Cell.Font.Bold = True
Case Is < 0.01
Cell.Font.ColorIndex = 44 ' AMBER
Cell.Font.Bold = True
Case Else
Cell.Font.ColorIndex = xlNone
End Select
Next