Excel - VBA:根据相邻单元格的文本颜色更改单元格的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19297905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:52:24  来源:igfitidea点击:

Excel - VBA: Change the text color of a cell based on the text color of an adjecent cell

vbacolorsfonts

提问by NewSpeaker

I am working on a macro that loops through the range dData and identifies which cells have a white font color. Then it changes the font color of any cell adjacent to dData white. The below code is what I have so far. It does not work yet but, am I on the right track?

我正在研究一个循环遍历范围 dData 并确定哪些单元格具有白色字体颜色的宏。然后它更改与 dData 白色相邻的任何单元格的字体颜色。下面的代码是我到目前为止所拥有的。它还不起作用,但是,我在正确的轨道上吗?

Thanks!

谢谢!

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim dData As Range
Dim Cell As Range

Set dData = Sheets("Sheet1").Range("l2:l10000")

For Each Cell In dData
    If Cell.Font.Color = 2 Then
        Cell.Offset(0, -1).Font.Color = 2
    End If
Next Cell
End Sub

回答by David Zemens

This appears to be working for me.

这似乎对我有用。

Sub Test()
Dim dData As Range
Dim Cell As Range

Set dData = Sheets("Sheet1").Range("l2:l10000")

For Each Cell In dData.Cells
        If Cell.Font.Color = 16777215 Then
            Cell.offset(,1).Font.Color = 16777215
        End If
Next
End Sub

Notealso the scope of dDatais limited to Sheet1

请注意,范围dData仅限于Sheet1

On my computer, "white" is a long value of 16777215which works for me in 2010 Excel, and I think should work in 2007. In Excel 2003 I am not sure.

在我的电脑上,“白色”是一个很长的值,16777215它在 2010 年 Excel 中对我有用,我认为应该在 2007 年工作。在 Excel 2003 中我不确定。

TRY THIS

尝试这个

Sub Sample()
    Dim dData As Range, aCell As Range

    Set dData = Sheets("Sheet1").Range("L2:L10000")

    For Each aCell In dData.Cells
        If aCell.Font.ColorIndex = 2 Then _
        aCell.Offset(, 1).Font.ColorIndex = 2
    Next
End Sub