简体   繁体   English

如何根据值更改单元格颜色

[英]How to make cell color change based on value

  Dim c As Range
 Application.ScreenUpdating = False
 For Each c In Range("G1", Range("G" & Rows.Count).End(xlUp))


 If Left(c, 1) = "X" Then c = Mid(c, 2, Len(c) - 1)
 If Left(c, 1) = "L" Then c = Mid(c, 2, Len(c) - 1)
 If Left(c, 1) = "C" Then c = Mid(c, 2, Len(c) - 1)


Next c
Application.ScreenUpdating = True

How can I get the cell change the interior color (let's say to light blue) after condition is met? 满足条件后,如何使单元格改变内部颜色(假设为浅蓝色)?

There's built-in support for conditional formatting already: https://support.office.com/en-us/article/Quick-start-Apply-conditional-formatting-6b6f7c2a-5d62-45a1-8f67-584a76776d67 已经对条件格式提供了内置支持: https : //support.office.com/en-us/article/Quick-start-Apply-conditional-formatting-6b6f7c2a-5d62-45a1-8f67-584a76776d67

No need to reinvent the wheel. 无需重新发明轮子。

Use: c.Interior.colorIndex = 8 使用: c.Interior.colorIndex = 8

The list of color codes can be found here . 颜色代码列表可以在此处找到。

EDIT: 编辑:

Following your comments the full code would be: 在您发表评论之后,完整的代码将是:

 Dim c As Range
 Application.ScreenUpdating = False
 For Each c In Range("G1", Range("G" & Rows.Count).End(xlUp))

     If Left(c, 1) = "X" Or Left(c, 1) = "L" Or Left(c, 1) = "C" Then
        c = Mid(c, 2, Len(c) - 1)
        c.Interior.ColorIndex = 8
     End If

Next c
Application.ScreenUpdating = True

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

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