【发布时间】:2019-05-09 17:36:21
【问题描述】:
找到了这段代码,它完成了我需要的“部分”。 我有多个条件 (20) 并希望根据查找设置字体、背景、图案颜色。
我需要: 在 sheet2 范围 A:A 上,如果值与颜色表上的 J:J 列匹配,则应用相应的填充/图案颜色/字体颜色。
我有: 在颜色表的“G”中填充颜色。 颜色表的“H”中的图案颜色。 颜色表“I”中的字体颜色。 颜色表“J”中的颜色代码。example
有人会这么好心并为我修改它以改变图案颜色,字体颜色就像改变背景一样吗?
尝试了几个小时,遗憾的是失败了。 我认为这与设置范围和 internal.pattern / colorindex 等有关。
除非你有比这更简单的方法? 希望我说得通。炒了一点,对不起。
代码:
Sub SetColors()
' DataCells: The cells that's going to be checked against the color values
Set DataCells = Range("A1:A15") ' Update this value according to your data cell range
' ColorValueCells: The cells that contain the values to be colored
Set ColorValueCells = Sheets("Colors").Range("j2:j41") ' Update this value according to your color value + index range
' Loop through data cells
For Each DataCell In DataCells
' Loop through color value cells
For Each ColorValueCell In ColorValueCells
' Search for a match
If DataCell.Value = ColorValueCell.Value Then
' If there is a match, find the color index
Set ColorIndexCell = Sheets("Colors").Range("g" & ColorValueCell.Row)
' Set data cell's background color with the color index
DataCell.Interior.ColorIndex = ColorIndexCell.Value
End If
Next
Next
End Sub
【问题讨论】:
标签: excel vba formatting conditional match