简体   繁体   English

.interior.color \\ VBA EXCEL 2013中范围的值

[英]value from a range in .interior.color \ VBA EXCEL 2013

I have in column F numbers , each number refers to a color code, I'm trying to auto change each cell color in F based on its value , BUT ALL COMES ONLY IN BLACK , below is my code : 我在列F编号中,每个数字指的是一个颜色代码,我试图根据其值自动更改F中的每个单元格颜色,但是所有仅仅是黑色,下面是我的代码:

Sub Highlight() 子亮点()

 For Each C In Worksheets("Sheet3").Range("F3:F1000")


With Sheet3.Range("$F1:$F1000")
    .FormatConditions.Delete

    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=($F1<>"""")")
        .Interior.Color = C.Value
        .Font.Color = vbWhite
 End With

 End With
 Next C

End Sub 结束子

Help please & thank you in advance. 请帮助,并提前感谢您。

As you've already got a loop to repeat over the cells in F3:F1000, it would be enough to use that. 因为你已经有一个循环来重复F3:F1000中的单元格,所以使用它就足够了。 The main thing is changing .Interior.Color to .Interior.ColorIndex 最主要的是改变.Interior.Color到.Interior.ColorIndex

Sub Highlight()

For Each C In Worksheets("Sheet3").Range("F3:F1000")

If (C <> "") Then
    C.Interior.ColorIndex = C.Value
    C.Font.Color = vbWhite
End If

Next C
End Sub

The only issue is that if any of the numbers in your cells were greater than 56, you would get a subscript error in the ColorIndex values. 唯一的问题是,如果单元格中的任何数字大于56,您将在ColorIndex值中得到下标错误。 You could make it recycle the colours by putting 您可以通过放置来回收颜色

c.Interior.ColorIndex = (c.Value - 1) Mod 56 + 1

I would split your code to 2 Sub s: 我会将你的代码分成2 Sub

  1. Sub CondFormat - to apply conditional formatting (once). Sub CondFormat - 应用条件格式(一次)。
  2. Sub Highlight - to apply cell color based on it's value - more frequently. Sub Highlight - 根据它的值应用单元格颜色 - 更频繁。

Code

Option Explicit

Dim LastRow As Long

Sub CondFormat()

With Worksheets("Sheet3")
    LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

    With .Range("F1:F" & LastRow)
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:="=($F1<>" & Chr(34) & Chr(34) & ")"
    End With

End With

End Sub


Sub Highlight()

Dim Rng As Range

With Worksheets("Sheet3")
    LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
    For Each Rng In .Range("F3:F" & LastRow)
        Rng.Interior.Color = Rng.Value
        Rng.Font.Color = vbWhite
    Next Rng
End With

End Sub

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

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