简体   繁体   English

如何使用条件if在Excel中突出显示单元格?

[英]How to highlight a cell in Excel using conditional if?

How can i conditionally assign color to cells ? 如何有条件地为单元格分配颜色?

I have the following function that is supposed to assign different color to cells depending on the if statement: 我有以下函数 ,应该根据if语句为单元格分配不同的颜色:

 Function .....
    ..........
    If (IsNumeric(x)  Then
    .Color = 65344                  // does not work
    Else
    ...........
    End If
    End Function

How to do this in a correct way? 如何以正确的方式做到这一点?

I'm not sure what color you are using here because 65344 isn't a hex value, but you can use RGB like this: 我不确定您在这里使用什么颜色,因为65344不是十六进制值,但是您可以像这样使用RGB:

Function .....
    ..........
    Dim c As Range
    Dim report As Worksheet

    Set report = Excel.ActiveSheet
    Set c = report.Cells(1, 1)

    If IsNumeric(c.Value) Then
       c.Interior.Color = RGB(110, 110, 100)
    End If
End Function

Here is a better example that may help. 这是一个更好的示例,可能会有所帮助。 (fyi this is free hand so double check it for syntax errors) (仅供参考,请仔细检查语法错误)

Sub changeColor()

Dim report as Worksheet

set report = Excel.ActiveSheet

dim i as integer

for i = 0 to 100
    if IsNumeric(report.cells(i,1).value) then
        report.cells(i,1).interior.color = rgb(220,230,241)
    else
        report.cells(i,1).interior.color = xlNone
    end if
next i
end sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM