使用Font.Color作为If语句中的条件

问题描述 投票:1回答:2

我目前正在尝试使用字体颜色作为条件来检查if语句。这似乎非常容易,但VBA似乎无法做到这一点。

我无法显示我的实际代码,因为它有专有信息,但我甚至尝试过我的代码的简单版本,但无济于事。该代码如下所示。

Sub Testing()

Cells(1,1).Font.Color = -16776961
If Cells(1,1).Font.Color = -16776961 Then
     Cells(1,3) = "Worked!"
Else
     Cells(1,3) = "Didn't Work!"
End If

End Sub

第一行代码实际上将字体颜色或单元格A1更改为红色。但是条件语句由于某种原因不起作用。

excel vba
2个回答
3
投票

请改用RGB函数。 (不确定负值是从哪里来的?)

Sub Testing()

Cells(1, 1).Font.Color = RGB(255, 0, 0)
If Cells(1, 1).Font.Color = RGB(255, 0, 0) Then
     Cells(1, 3) = "Worked!"
Else
     Cells(1, 3) = "Didn't Work!"
End If

End Sub

3
投票

你应该像这样使用RGB

Cells(1,1).Font.Color = RGB(255, 0, 0)
If Cells(1,1).Font.Color = RGB(255, 0, 0) Then
     Cells(1,3) = "Worked!"
Else
     Cells(1,3) = "Didn't Work!"
End If

© www.soinside.com 2019 - 2024. All rights reserved.